Add player-toggled optional chat filter (#587)

This commit is contained in:
Josua Parks authored and GitHub committed 2025-01-08 20:58:55 -08:00
1 parent 2ddb01132b
commit 620bd57c46
18 files changed
+253 -7

No files matched your search

@@ -47,6 +47,7 @@ import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.command.commands.ClearChatCommand;
import net.draycia.carbon.common.command.commands.ContinueCommand;
import net.draycia.carbon.common.command.commands.DebugCommand;
import net.draycia.carbon.common.command.commands.FilterCommand;
import net.draycia.carbon.common.command.commands.HelpCommand;
import net.draycia.carbon.common.command.commands.IgnoreCommand;
import net.draycia.carbon.common.command.commands.IgnoreListCommand;
@@ -68,6 +69,7 @@ import net.draycia.carbon.common.config.ConfigManager;
import net.draycia.carbon.common.config.DatabaseSettings;
import net.draycia.carbon.common.event.CarbonEventHandlerImpl;
import net.draycia.carbon.common.listeners.DeafenHandler;
import net.draycia.carbon.common.listeners.FilterHandler;
import net.draycia.carbon.common.listeners.HyperlinkHandler;
import net.draycia.carbon.common.listeners.IgnoreHandler;
import net.draycia.carbon.common.listeners.ItemLinkHandler;
@@ -219,6 +221,7 @@ public final class CarbonCommonModule extends AbstractModule {
private void configureListeners() {
final Multibinder<Listener> listeners = Multibinder.newSetBinder(this.binder(), Listener.class);
listeners.addBinding().to(DeafenHandler.class);
listeners.addBinding().to(FilterHandler.class);
listeners.addBinding().to(HyperlinkHandler.class);
listeners.addBinding().to(IgnoreHandler.class);
listeners.addBinding().to(ItemLinkHandler.class);
@@ -236,6 +239,7 @@ public final class CarbonCommonModule extends AbstractModule {
commands.addBinding().to(ClearChatCommand.class).in(Scopes.SINGLETON);
commands.addBinding().to(ContinueCommand.class).in(Scopes.SINGLETON);
commands.addBinding().to(DebugCommand.class).in(Scopes.SINGLETON);
commands.addBinding().to(FilterCommand.class).in(Scopes.SINGLETON);
commands.addBinding().to(HelpCommand.class).in(Scopes.SINGLETON);
commands.addBinding().to(IgnoreCommand.class).in(Scopes.SINGLETON);
commands.addBinding().to(MuteCommand.class).in(Scopes.SINGLETON);
@@ -0,0 +1,90 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.command.commands;
import com.google.inject.Inject;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
import net.draycia.carbon.common.command.CommandSettings;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;
import static org.incendo.cloud.parser.standard.BooleanParser.booleanParser;
@DefaultQualifier(NonNull.class)
public final class FilterCommand extends CarbonCommand {
private final CommandManager<Commander> commandManager;
private final CarbonMessages carbonMessages;
@Inject
public FilterCommand(
final CommandManager<Commander> commandManager,
final CarbonMessages carbonMessages
) {
this.commandManager = commandManager;
this.carbonMessages = carbonMessages;
}
@Override
public CommandSettings defaultCommandSettings() {
return new CommandSettings("filter");
}
@Override
public Key key() {
return Key.key("carbon", "filter");
}
@Override
public void init() {
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
.optional("enabled", booleanParser())
.permission("carbon.filter")
.senderType(PlayerCommander.class)
.commandDescription(richDescription(this.carbonMessages.commandOptionalFilterDescription()))
.handler(handler -> {
final CarbonPlayer sender = handler.sender().carbonPlayer();
boolean enabled = !sender.spying();
if (handler.contains("enabled")) {
enabled = handler.get("enabled");
}
sender.applyOptionalChatFilters(enabled);
if (enabled) {
this.carbonMessages.commandOptionalFilterEnabled(sender);
} else {
this.carbonMessages.commandOptionalFilterDisabled(sender);
}
})
.build();
this.commandManager.command(command);
}
}
@@ -25,6 +25,8 @@ import java.util.Map;
import net.draycia.carbon.common.util.Exceptions;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -83,6 +85,9 @@ public class PrimaryConfig {
@Comment("Basic regex based chat filter.")
private Map<String, String> chatFilter = Map.of();
@Comment("Player toggled chat filter. Useful for more mild profanity.")
private Map<String, String> optionalChatFilter = Map.of();
@Comment("Various settings related to pinging players in channels.")
private PingSettings pingSettings = new PingSettings();
@@ -165,6 +170,17 @@ public class PrimaryConfig {
return filteredMessage;
}
public Component applyOptionalChatFilters(final Component message) {
Component filteredMessage = message;
for (final Map.Entry<String, String> entry : this.optionalChatFilter.entrySet()) {
filteredMessage = filteredMessage.replaceText(TextReplacementConfig.builder()
.match(entry.getKey()).replacement(entry.getValue()).build());
}
return filteredMessage;
}
public PingSettings pings() {
return this.pingSettings;
}
@@ -91,7 +91,6 @@ public abstract class ChatListenerInternal {
}
String content = this.configManager.primaryConfig().applyChatPlaceholders(messageContent);
content = this.configManager.primaryConfig().applyChatFilters(content);
final CarbonEarlyChatEvent earlyChatEvent = new CarbonEarlyChatEvent(sender, content);
this.carbonEventHandler.emit(earlyChatEvent);
@@ -0,0 +1,59 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.listeners;
import com.google.inject.Inject;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.common.config.ConfigManager;
import net.draycia.carbon.common.event.events.CarbonEarlyChatEvent;
import net.kyori.adventure.key.Key;
public class FilterHandler implements Listener {
private final ConfigManager configManager;
@Inject
public FilterHandler(
final CarbonEventHandler events,
final ConfigManager configManager
) {
this.configManager = configManager;
events.subscribe(CarbonEarlyChatEvent.class, 0, false, event -> {
event.message(this.configManager.primaryConfig().applyChatFilters(event.message()));
});
events.subscribe(CarbonChatEvent.class, 0, false, event -> {
event.renderers().add(KeyedRenderer.keyedRenderer(Key.key("carbon", "filter"), ($, recipient, message, $$$) -> {
if (recipient instanceof CarbonPlayer carbonPlayer) {
if (carbonPlayer.applyOptionalChatFilters()) {
return this.configManager.primaryConfig().applyOptionalChatFilters(message);
}
}
return message;
}));
});
}
}
@@ -300,6 +300,21 @@ public interface CarbonMessages {
@Message("command.spy.description")
Component commandSpyDescription();
/*
* =============================================================
* ========================== Filters ==========================
* =============================================================
*/
@Message("command.filter.optional.enabled")
void commandOptionalFilterEnabled(final Audience audience);
@Message("command.filter.optional.disabled")
void commandOptionalFilterDisabled(final Audience audience);
@Message("command.filter.optional.description")
Component commandOptionalFilterDescription();
/*
* =============================================================
* ====================== Cloud Messages =======================
@@ -79,6 +79,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
// Administrative
protected final PersistentUserProperty<Boolean> spying;
protected final PersistentUserProperty<Boolean> applyOptionalChatFilters;
// Punishments
protected final PersistentUserProperty<Set<UUID>> ignoredPlayers;
@@ -98,7 +99,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
final @Nullable UUID whisperReplyTarget,
final boolean spying,
final boolean ignoreDirectMessages,
final @Nullable UUID party
final @Nullable UUID party,
final boolean applyOptionalChatFilters
) {
this.muted = PersistentUserProperty.of(muted);
this.deafened = PersistentUserProperty.of(deafened);
@@ -113,6 +115,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.leftChannels = PersistentUserProperty.of(Collections.emptySet());
this.ignoringDirectMessages = PersistentUserProperty.of(ignoreDirectMessages);
this.party = PersistentUserProperty.of(party);
this.applyOptionalChatFilters = PersistentUserProperty.of(applyOptionalChatFilters);
}
public CarbonPlayerCommon(
@@ -130,6 +133,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.uuid = uuid;
this.ignoringDirectMessages = PersistentUserProperty.of(false);
this.party = PersistentUserProperty.empty();
this.applyOptionalChatFilters = PersistentUserProperty.of(false);
}
public CarbonPlayerCommon() {
@@ -138,6 +142,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.selectedChannel = PersistentUserProperty.empty();
this.displayName = PersistentUserProperty.empty();
this.spying = PersistentUserProperty.of(false);
this.applyOptionalChatFilters = PersistentUserProperty.of(false);
this.ignoredPlayers = PersistentUserProperty.of(Collections.emptySet());
this.leftChannels = PersistentUserProperty.of(Collections.emptySet());
this.ignoringDirectMessages = PersistentUserProperty.of(false);
@@ -155,6 +160,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.selectedChannel,
this.displayName,
this.spying,
this.applyOptionalChatFilters,
this.ignoredPlayers,
this.leftChannels,
this.ignoringDirectMessages,
@@ -503,4 +509,14 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.party.set(party == null ? null : party.id());
}
@Override
public boolean applyOptionalChatFilters() {
return this.applyOptionalChatFilters.get();
}
@Override
public void applyOptionalChatFilters(final boolean applyOptionalChatFilters) {
this.applyOptionalChatFilters.set(applyOptionalChatFilters);
}
}
@@ -253,6 +253,16 @@ public class ConsoleCarbonPlayer implements CarbonPlayer, ForwardingAudience.Sin
return CompletableFuture.completedFuture(null);
}
@Override
public boolean applyOptionalChatFilters() {
return false;
}
@Override
public void applyOptionalChatFilters(final boolean applyOptionalChatFilters) {
}
@Override
public @NotNull Identity identity() {
return Identity.nil();
@@ -386,4 +386,14 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
this.carbonPlayerCommon.party(party);
}
@Override
public boolean applyOptionalChatFilters() {
return this.carbonPlayerCommon.applyOptionalChatFilters();
}
@Override
public void applyOptionalChatFilters(final boolean applyOptionalChatFilters) {
this.carbonPlayerCommon.applyOptionalChatFilters(applyOptionalChatFilters);
}
}
@@ -48,7 +48,8 @@ public final class PlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
uuid.map(rs, "whisperreplytarget", ctx),
rs.getBoolean("spying"),
rs.getBoolean("ignoringdms"),
uuid.map(rs, "party", ctx)
uuid.map(rs, "party", ctx),
rs.getBoolean("applycustomfilters")
);
}