Add support for per-channel mutes

This commit is contained in:
Draycia committed 2021-09-30 20:14:10 -07:00
1 parent befce62375
commit a57cd5c103
13 files changed
+137 -48

No files matched your search

@@ -1,6 +1,8 @@
package net.draycia.carbon.api.events;
import java.util.List;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.kyori.adventure.audience.Audience;
@@ -22,6 +24,7 @@ public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Resu
private final CarbonPlayer sender;
private final Component originalMessage;
private final List<? extends Audience> recipients;
private final ChatChannel chatChannel;
private Component message;
private Result result = Result.ALLOWED;
@@ -32,19 +35,22 @@ public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Resu
* @param originalMessage the original message that was sent
* @param recipients the recipients of the message
* @param renderers the renderers of the message
* @param chatChannel the channel the message was sent in
* @since 2.0.0
*/
public CarbonChatEvent(
final CarbonPlayer sender,
final Component originalMessage,
final List<? extends Audience> recipients,
final List<KeyedRenderer> renderers
) {
final List<KeyedRenderer> renderers,
final ChatChannel chatChannel
) {
this.sender = sender;
this.originalMessage = originalMessage;
this.message = originalMessage;
this.recipients = recipients;
this.renderers = renderers;
this.chatChannel = chatChannel;
}
/**
@@ -97,6 +103,16 @@ public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Resu
this.message = message;
}
/**
* The chat channel the message was sent in.
*
* @return the chat channel
* @since 2.0.0
*/
public ChatChannel chatChannel() {
return this.chatChannel;
}
/**
* The recipients of the message.
* List is mutable and elements may be added/removed.
@@ -4,6 +4,7 @@ import java.util.List;
import java.util.Locale;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.punishments.MuteEntry;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.text.Component;
@@ -144,19 +145,31 @@ public interface CarbonPlayer extends Audience, Identified {
List<String> groups();
/**
* Returns if the player is muted and unable to speak in chat.
* The mute entries the player has.
*
* @return if the player is muted
* @return the player's mute entries
* @since 2.0.0
*/
boolean muted();
List<MuteEntry> muteEntries();
/**
* Mutes and unmutes the player.
* Returns if the player is muted for the channel and unable to speak in it.
*
* @param chatChannel the chat channel
* @return if the player is muted in the channel
* @since 2.0.0
*/
void muted(final boolean muted);
boolean muted(final ChatChannel chatChannel);
/**
* Mutes and unmutes the player for the specified channel.
*
* @param chatChannel the channel to mute/unmute for
* @param muted if the player should be muted for the channel
* @param cause the UUID of the cause of the mute, typically a player UUID
* @since 2.0.0
*/
void muted(final ChatChannel chatChannel, final boolean muted, final @Nullable UUID cause);
/**
* Returns if the player is deafened and unable to read messages.
@@ -0,0 +1,13 @@
package net.draycia.carbon.api.users.punishments;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import java.util.UUID;
@DefaultQualifier(NonNull.class)
public record MuteEntry(long muteEpoch, @Nullable UUID muteCause, long expirationEpoch, @Nullable Key channel) {
}
@@ -93,14 +93,14 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
}
// Listeners
injector.getInstance(DeafenHandler.class);
injector.getInstance(MuteHandler.class);
this.injector.getInstance(DeafenHandler.class);
this.injector.getInstance(MuteHandler.class);
// Commands
injector.getInstance(ContinueCommand.class);
injector.getInstance(NicknameCommand.class);
injector.getInstance(ReplyCommand.class);
injector.getInstance(WhisperCommand.class);
this.injector.getInstance(ContinueCommand.class);
this.injector.getInstance(NicknameCommand.class);
this.injector.getInstance(ReplyCommand.class);
this.injector.getInstance(WhisperCommand.class);
final long saveDelay = 5 * 60 * 20;
@@ -127,7 +127,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
});
saveResult.exceptionally(exception -> {
exception.getCause().printStackTrace();
exception.getCause().printStackTrace();
return null;
});
}
@@ -162,7 +162,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
}
@Override
public final @NonNull CarbonEventHandler eventHandler() {
public @NonNull CarbonEventHandler eventHandler() {
return this.eventHandler;
}
@@ -48,9 +48,9 @@ public final class BukkitChatListener implements Listener {
var channel = requireNonNullElse(sender.selectedChannel(),
this.registry.defaultValue());
var originalMessage = PlainTextComponentSerializer.plainText().serialize(event.originalMessage());
final var originalMessage = PlainTextComponentSerializer.plainText().serialize(event.originalMessage());
for (var chatChannel : this.registry) {
for (final var chatChannel : this.registry) {
if (chatChannel.quickPrefix() == null) {
continue;
}
@@ -69,7 +69,7 @@ public final class BukkitChatListener implements Listener {
final var renderers = new ArrayList<KeyedRenderer>();
renderers.add(keyedRenderer(key("carbon", "default"), channel));
final var chatEvent = new CarbonChatEvent(sender, event.message(), recipients, renderers);
final var chatEvent = new CarbonChatEvent(sender, event.message(), recipients, renderers, channel);
final var result = this.carbonChat.eventHandler().emit(chatEvent);
if (!result.wasSuccessful()) {
@@ -8,7 +8,10 @@ import net.draycia.carbon.common.config.PrimaryConfig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class BukkitPlayerDeathListener implements Listener {
private final CarbonChat carbonChat;
@@ -30,7 +33,7 @@ public class BukkitPlayerDeathListener implements Listener {
return;
}
final ComponentPlayerResult result = this.carbonChat.server().player(event.getEntity().getUniqueId()).join();
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(event.getEntity().getUniqueId()).join();
if (result.player() == null) {
return;
@@ -38,7 +41,7 @@ public class BukkitPlayerDeathListener implements Listener {
final CarbonPlayer player = result.player();
if (player.muted()) {
if (!player.muteEntries().isEmpty()) {
event.deathMessage(null);
}
}
@@ -39,7 +39,7 @@ public class BukkitPlayerJoinListener implements Listener {
}
// Don't show join messages when muted
if (this.primaryConfig.hideMutedJoinLeaveQuit() && player.muted()) {
if (this.primaryConfig.hideMutedJoinLeaveQuit() && !player.muteEntries().isEmpty()) {
event.joinMessage(null);
}
}
@@ -60,7 +60,7 @@ public class BukkitPlayerJoinListener implements Listener {
final CarbonPlayer player = result.player();
// Don't show quit messages when muted
if (player.muted()) {
if (!player.muteEntries().isEmpty()) {
event.quitMessage(null);
}
}
@@ -167,7 +167,7 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
}
@Override
public void sendMessageAsPlayer(String message) {
public void sendMessageAsPlayer(final String message) {
Bukkit.getScheduler().runTask(Bukkit.getPluginManager().getPlugin("CarbonChat"), () -> {
Objects.requireNonNull(this.player()).chat(message);
});
@@ -31,12 +31,15 @@ public class MuteHandler {
this.messageService = messageService;
carbonChat.eventHandler().subscribe(CarbonChatEvent.class, 100, false, event -> {
if (!event.sender().muted()) {
return;
for (final var muteEntry : event.sender().muteEntries()) {
if (muteEntry.channel() != null && event.chatChannel().key().equals(muteEntry.channel())) {
return;
}
}
event.renderers().add(this.renderer);
// TODO: ShadowMuteHandler? Include that logic in here?
event.recipients().removeIf(entry -> entry instanceof CarbonPlayer carbonPlayer &&
!carbonPlayer.spying());
});
@@ -1,10 +1,12 @@
package net.draycia.carbon.common.users;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.punishments.MuteEntry;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
@@ -19,18 +21,27 @@ import org.jetbrains.annotations.NotNull;
public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Single {
protected boolean deafened = false;
protected @Nullable Component displayName = null;
protected @Nullable UUID lastWhisperTarget = null;
protected boolean muted = false;
protected @Nullable ChatChannel selectedChannel = null;
protected boolean spying = false;
protected @Nullable Component temporaryDisplayName = null;
protected long temporaryDisplayNameExpiration = -1;
protected @Nullable UUID whisperReplyTarget = null;
// All players have these
protected @MonotonicNonNull String username;
protected @MonotonicNonNull UUID uuid;
// Display information
protected @Nullable Component displayName = null;
protected @Nullable Component temporaryDisplayName = null;
protected long temporaryDisplayNameExpiration = -1;
// Whispers
protected @Nullable UUID lastWhisperTarget = null;
protected @Nullable UUID whisperReplyTarget = null;
// Administrative
protected boolean spying = false;
// Punishments
protected List<MuteEntry> muteEntries = new ArrayList<>();
public CarbonPlayerCommon(
final String username,
final UUID uuid
@@ -108,13 +119,37 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
}
@Override
public boolean muted() {
return this.muted;
public List<MuteEntry> muteEntries() {
return this.muteEntries;
}
@Override
public void muted(final boolean muted) {
this.muted = muted;
public boolean muted(final ChatChannel chatChannel) {
for (final var muteEntry : this.muteEntries) {
if (muteEntry.channel() != null && chatChannel.key().equals(muteEntry.channel())) {
return true;
}
}
return false;
}
@Override
public void muted(final ChatChannel chatChannel, final boolean muted, final @Nullable UUID cause) {
final var iterator = this.muteEntries.iterator();
MuteEntry muteEntry;
while (iterator.hasNext()) {
muteEntry = iterator.next();
if (muteEntry.channel() != null && chatChannel.key().equals(muteEntry.channel())) {
iterator.remove();
}
}
if (muted) {
this.muteEntries.add(new MuteEntry(System.currentTimeMillis(), cause, -1, chatChannel.key()));
}
}
@Override
@@ -2,6 +2,7 @@ package net.draycia.carbon.common.users;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.punishments.MuteEntry;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -95,13 +96,18 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
}
@Override
public boolean muted() {
return this.carbonPlayerCommon().muted();
public List<MuteEntry> muteEntries() {
return this.carbonPlayerCommon().muteEntries();
}
@Override
public void muted(boolean muted) {
this.carbonPlayerCommon().muted(muted);
public boolean muted(final ChatChannel chatChannel) {
return this.carbonPlayerCommon().muted(chatChannel);
}
@Override
public void muted(final ChatChannel chatChannel, final boolean muted, final @Nullable UUID cause) {
this.carbonPlayerCommon().muted(chatChannel, muted, cause);
}
@Override
@@ -135,13 +135,13 @@ public final class CarbonPlayerSponge implements CarbonPlayer, ForwardingAudienc
}
@Override
public boolean muted() {
return this.carbonPlayer.muted();
public boolean globallyMuted() {
return this.carbonPlayer.globallyMuted();
}
@Override
public void muted(final boolean muted) {
this.carbonPlayer.muted(muted);
public void globallyMuted(final boolean muted) {
this.carbonPlayer.globallyMuted(muted);
}
@Override
@@ -64,13 +64,13 @@ public final class CarbonPlayerVelocity extends CarbonPlayerCommon {
}
@Override
public boolean muted() {
return this.carbonPlayer.muted();
public boolean globallyMuted() {
return this.carbonPlayer.globallyMuted();
}
@Override
public void muted(final boolean muted) {
this.carbonPlayer.muted(muted);
public void globallyMuted(final boolean muted) {
this.carbonPlayer.globallyMuted(muted);
}
@Override