From b7f2023977300809770a320b2d755213e49a66fb Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 3 Jan 2025 17:48:01 +0700 Subject: [PATCH] feat: filter reason fix: check for duplicate entries (filter) refactor: some other refactorings in the command --- build-number.txt | 2 +- .../chomens_bot/commands/FilterCommand.java | 63 ++++++++++++++++--- .../chomens_bot/data/FilteredPlayer.java | 4 ++ .../chomens_bot/plugins/FilterPlugin.java | 25 ++++---- 4 files changed, 73 insertions(+), 21 deletions(-) diff --git a/build-number.txt b/build-number.txt index 1d0ae911..740f5aec 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1417 \ No newline at end of file +1419 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java index b6291d38..c25fa12b 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java @@ -59,16 +59,40 @@ public class FilterCommand extends Command { switch (action) { case "add" -> { - final String player = context.getString(true, true); + final String player = context.getString(false, true); + final String reason = context.getString(false, false); + + if ( + FilterPlugin.localList.stream() + .map(filteredPlayer -> filteredPlayer.playerName) + .toList() + .contains(player) + ) { + throw new CommandException( + Component.translatable( + "The player %s is already in the filters", + Component.text(player) + ) + ); + } final boolean finalRegex = regex; final boolean finalIgnoreCase = ignoreCase; - DatabasePlugin.executorService.submit(() -> bot.filter.add(player, finalRegex, finalIgnoreCase)); - return Component.translatable( - "Added %s to the filters", - Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) - ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + DatabasePlugin.executorService.submit(() -> bot.filter.add(player, reason, finalRegex, finalIgnoreCase)); + + if (reason.isEmpty()) { + return Component.translatable( + "Added %s to the filters", + Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } else { + return Component.translatable( + "Added %s to the filters with reason %s", + Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)), + Component.text(reason).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } } case "remove" -> { context.checkOverloadArgs(2); @@ -107,9 +131,30 @@ public class FilterCommand extends Command { if (player.ignoreCase) args.add(Component.text("ignore case")); if (player.regex) args.add(Component.text("regex")); - options = options.append(Component.text("(")); - options = options.append(Component.join(JoinConfiguration.commas(true), args).color(ColorUtilities.getColorByString(bot.config.colorPalette.string))); - options = options.append(Component.text(")")); + options = options + .append(Component.text("(")) + .append( + Component + .join( + JoinConfiguration.commas(true), + args + ) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) + ) + .append(Component.text(")")) + .append(Component.space()); + } + + if (!player.reason.isEmpty()) { + options = options + .append(Component.text("(")) + .append(Component.text("reason: ").color(NamedTextColor.GRAY)) + .append( + Component + .text(player.reason) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) + ) + .append(Component.text(")")); } filtersComponents.add( diff --git a/src/main/java/me/chayapak1/chomens_bot/data/FilteredPlayer.java b/src/main/java/me/chayapak1/chomens_bot/data/FilteredPlayer.java index 1695abff..d600e80b 100644 --- a/src/main/java/me/chayapak1/chomens_bot/data/FilteredPlayer.java +++ b/src/main/java/me/chayapak1/chomens_bot/data/FilteredPlayer.java @@ -5,16 +5,19 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class FilteredPlayer { public final String playerName; + public final String reason; public final boolean regex; public final boolean ignoreCase; @JsonCreator public FilteredPlayer ( @JsonProperty("playerName") String playerName, + @JsonProperty("reason") String reason, @JsonProperty("regex") boolean regex, @JsonProperty("ignoreCase") boolean ignoreCase ) { this.playerName = playerName; + this.reason = reason; this.regex = regex; this.ignoreCase = ignoreCase; } @@ -23,6 +26,7 @@ public class FilteredPlayer { public String toString() { return "FilteredPlayer{" + "playerName='" + playerName + '\'' + + ", reason='" + reason + '\'' + ", regex=" + regex + ", ignoreCase=" + ignoreCase + '}'; diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java index f08b0895..bb0c5969 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/FilterPlugin.java @@ -20,9 +20,9 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; public class FilterPlugin extends PlayersPlugin.Listener { - private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS filters (name VARCHAR(255) PRIMARY KEY, regex BOOLEAN, ignoreCase BOOLEAN);"; + private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS filters (name VARCHAR(255) PRIMARY KEY, reason VARCHAR(255), regex BOOLEAN, ignoreCase BOOLEAN);"; private static final String LIST_FILTERS = "SELECT * FROM filters;"; - private static final String INSERT_FILTER = "INSERT INTO filters (name, regex, ignoreCase) VALUES (?, ?, ?);"; + private static final String INSERT_FILTER = "INSERT INTO filters (name, reason, regex, ignoreCase) VALUES (?, ?, ?, ?);"; private static final String REMOVE_FILTER = "DELETE FROM filters WHERE name = ?;"; private static final String CLEAR_FILTER = "DELETE FROM filters;"; @@ -79,6 +79,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { while (result.next()) { final FilteredPlayer filteredPlayer = new FilteredPlayer( result.getString("name"), + result.getString("reason"), result.getBoolean("regex"), result.getBoolean("ignoreCase") ); @@ -139,7 +140,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { if (player == null) return; - doAll(target); + doAll(target, player.reason); }); } @@ -170,7 +171,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { command.startsWith("/essentials:emute") || command.startsWith("/essentials:silence") || command.startsWith("/essentials:esilence") - ) mute(entry); + ) mute(entry, player.reason); deOp(entry); gameMode(entry); @@ -184,11 +185,12 @@ public class FilterPlugin extends PlayersPlugin.Listener { if (player == null || message.sender.profile.getId().equals(new UUID(0L, 0L))) return; - doAll(message.sender); + doAll(message.sender, player.reason); } - public void doAll (PlayerEntry entry) { - mute(entry); + public void doAll (PlayerEntry entry) { doAll(entry, ""); } + public void doAll (PlayerEntry entry, String reason) { + mute(entry, reason); deOp(entry); gameMode(entry); bot.exploits.kick(entry.profile.getId()); @@ -217,13 +219,14 @@ public class FilterPlugin extends PlayersPlugin.Listener { } } - public void add (String playerName, boolean regex, boolean ignoreCase) { + public void add (String playerName, String reason, boolean regex, boolean ignoreCase) { try { final PreparedStatement statement = bot.database.connection.prepareStatement(INSERT_FILTER); statement.setString(1, playerName); - statement.setBoolean(2, regex); - statement.setBoolean(3, ignoreCase); + statement.setString(2, reason); + statement.setBoolean(3, regex); + statement.setBoolean(4, ignoreCase); statement.executeUpdate(); @@ -236,7 +239,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { if (target == null) return; - doAll(target); + doAll(target, reason); } public void remove (String playerName) {