diff --git a/build-number.txt b/build-number.txt index 37021f4a..6005e309 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1500 \ No newline at end of file +1502 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java index 09001da4..49970a24 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java @@ -14,6 +14,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.util.ArrayList; import java.util.List; +import java.util.Map; public class IPFilterCommand extends Command { public IPFilterCommand() { @@ -21,7 +22,7 @@ public class IPFilterCommand extends Command { "ipfilter", "Filters IPs", new String[] { - "add ", + "add [reason]", "remove ", "clear", "list" @@ -41,9 +42,10 @@ public class IPFilterCommand extends Command { switch (action) { case "add" -> { - final String ip = context.getString(true, true); + final String ip = context.getString(false, true); + final String reason = context.getString(true, false); - if (IPFilterPlugin.localList.contains(ip)) { + if (IPFilterPlugin.localList.containsKey(ip)) { throw new CommandException( Component.translatable( "The IP %s is already in the filters", @@ -52,18 +54,27 @@ public class IPFilterCommand extends Command { ); } - DatabasePlugin.executorService.submit(() -> bot.ipFilter.add(ip)); - return Component.translatable( - "Added %s to the filters", - Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) - ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + DatabasePlugin.executorService.submit(() -> bot.ipFilter.add(ip, reason)); + + if (reason.isEmpty()) { + return Component.translatable( + "Added %s to the filters", + Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } else { + return Component.translatable( + "Added %s to the filters with reason %s", + Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), + Component.text(reason).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } } case "remove" -> { context.checkOverloadArgs(2); final int index = context.getInteger(true); - final String targetIP = IPFilterPlugin.localList.get(index); + final String targetIP = new ArrayList<>(IPFilterPlugin.localList.keySet()).get(index); if (targetIP == null) throw new CommandException(Component.text("Invalid index")); @@ -71,7 +82,7 @@ public class IPFilterCommand extends Command { return Component.translatable( "Removed %s from the filters", - Component.text(targetIP).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + Component.text(targetIP).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "clear" -> { @@ -86,12 +97,30 @@ public class IPFilterCommand extends Command { final List filtersComponents = new ArrayList<>(); int index = 0; - for (String ip : IPFilterPlugin.localList) { + for (Map.Entry entry : IPFilterPlugin.localList.entrySet()) { + final String ip = entry.getKey(); + final String reason = entry.getValue(); + + Component reasonComponent = Component.empty().color(NamedTextColor.DARK_GRAY); + + if (!reason.isEmpty()) { + reasonComponent = reasonComponent + .append(Component.text("(")) + .append(Component.text("reason: ").color(NamedTextColor.GRAY)) + .append( + Component + .text(reason) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) + ) + .append(Component.text(")")); + } + filtersComponents.add( Component.translatable( - "%s › %s", + "%s › %s %s", Component.text(index).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), - Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)), + reasonComponent ).color(NamedTextColor.DARK_GRAY) ); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java index 9dbd8653..d89f8955 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java @@ -8,19 +8,19 @@ import me.chayapak1.chomens_bot.util.LoggerUtilities; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; public class IPFilterPlugin extends PlayersPlugin.Listener { - private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ipFilters (ip VARCHAR(255) PRIMARY KEY);"; + private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ipFilters (ip VARCHAR(255) PRIMARY KEY, reason VARCHAR(255));"; private static final String LIST_FILTERS = "SELECT * FROM ipFilters;"; - private static final String INSERT_FILTER = "INSERT INTO ipFilters (ip) VALUES (?);"; + private static final String INSERT_FILTER = "INSERT INTO ipFilters (ip, reason) VALUES (?, ?);"; private static final String REMOVE_FILTER = "DELETE FROM ipFilters WHERE ip = ?;"; private static final String CLEAR_FILTER = "DELETE FROM ipFilters;"; - public static List localList = new ArrayList<>(); + public static Map localList = new LinkedHashMap<>(); static { if (Main.database != null) { @@ -78,13 +78,15 @@ public class IPFilterPlugin extends PlayersPlugin.Listener { }); } - public static List list () { - final List output = new ArrayList<>(); + public static Map list () { + final Map output = new LinkedHashMap<>(); try (ResultSet result = Main.database.query(LIST_FILTERS)) { if (result == null) return output; - while (result.next()) output.add(result.getString("ip")); + while (result.next()) { + output.put(result.getString("ip"), result.getString("reason")); + } } catch (SQLException e) { LoggerUtilities.error(e); } @@ -94,11 +96,12 @@ public class IPFilterPlugin extends PlayersPlugin.Listener { return output; } - public void add (String ip) { + public void add (String ip, String reason) { try { final PreparedStatement statement = bot.database.connection.prepareStatement(INSERT_FILTER); statement.setString(1, ip); + statement.setString(2, reason); statement.executeUpdate(); @@ -143,12 +146,15 @@ public class IPFilterPlugin extends PlayersPlugin.Listener { } private void handleFilterManager (String ip, PlayerEntry entry) { - for (String eachIP : localList) { + for (Map.Entry ipEntry : localList.entrySet()) { + final String eachIP = ipEntry.getKey(); + final String reason = ipEntry.getValue(); + if (!eachIP.equals(ip)) continue; if (entry.profile.equals(bot.profile)) continue; - bot.filterManager.add(entry, ""); + bot.filterManager.add(entry, reason); } } }