From 54f0f36952ea2985c1b3a93ae7096cbf6339f391 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Wed, 9 Apr 2025 11:01:38 +0700 Subject: [PATCH] refactor: some fixes and improvements to filtering and ip querying stuff - ip filter delay decreased to 5 seconds, since we already got the `ip` field in PlayerEntry so we can check however we want without spamming the server console (though we still do have to worry about resource usages) - validate the regex in FilterCommand and log the regex error to console instead of chat, though it shouldn't happen anymore since we got the validation in the command already - use the newly made event `queriedPlayerIP` in the players plugin listener instead of `playerJoined`, since the ip will 95% be null and will basically just do nothing --- build-number.txt | 2 +- .../chomens_bot/commands/FilterCommand.java | 16 ++++++++ .../chomens_bot/plugins/IPFilterPlugin.java | 8 ++-- .../plugins/PlayerFilterPlugin.java | 16 ++++---- .../plugins/PlayersDatabasePlugin.java | 40 ++++++++----------- .../chomens_bot/plugins/PlayersPlugin.java | 10 ++++- 6 files changed, 55 insertions(+), 37 deletions(-) diff --git a/build-number.txt b/build-number.txt index b2ae9591..018e3be6 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2498 \ No newline at end of file +2502 \ 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 b330ee8f..094416c7 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java @@ -14,6 +14,8 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; public class FilterCommand extends Command { public FilterCommand () { @@ -73,6 +75,20 @@ public class FilterCommand extends Command { ); } + if (regex) { + // try validating the regex + try { + Pattern.compile(player); + } catch (PatternSyntaxException e) { + throw new CommandException( + Component.translatable( + "Failed to parse filter regex: %s", + Component.text(e.toString()) + ) + ); + } + } + final boolean finalRegex = regex; final boolean finalIgnoreCase = ignoreCase; 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 dda04b82..7b3dfbb9 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java @@ -45,7 +45,7 @@ public class IPFilterPlugin implements PlayersPlugin.Listener, CorePlugin.Listen bot.players.addListener(this); bot.core.addListener(this); - bot.executor.scheduleAtFixedRate(this::checkAllPlayers, 5, 15, TimeUnit.SECONDS); + bot.executor.scheduleAtFixedRate(this::checkAllPlayers, 5, 5, TimeUnit.SECONDS); } @Override @@ -54,7 +54,7 @@ public class IPFilterPlugin implements PlayersPlugin.Listener, CorePlugin.Listen } @Override - public void playerJoined (PlayerEntry target) { + public void queriedPlayerIP (PlayerEntry target, String ip) { if (localList.isEmpty()) return; check(target); @@ -142,9 +142,7 @@ public class IPFilterPlugin implements PlayersPlugin.Listener, CorePlugin.Listen final String eachIP = ipEntry.getKey(); final String reason = ipEntry.getValue(); - if (!eachIP.equals(ip)) continue; - - if (entry.profile.equals(bot.profile)) continue; + if (entry.profile.equals(bot.profile) || !eachIP.equals(ip)) continue; bot.filterManager.add(entry, reason); } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayerFilterPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayerFilterPlugin.java index 8a1f4160..c80c8cba 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayerFilterPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayerFilterPlugin.java @@ -5,8 +5,6 @@ import me.chayapak1.chomens_bot.Main; import me.chayapak1.chomens_bot.data.filter.FilteredPlayer; import me.chayapak1.chomens_bot.data.player.PlayerEntry; import me.chayapak1.chomens_bot.util.LoggerUtilities; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -112,7 +110,8 @@ public class PlayerFilterPlugin implements PlayersPlugin.Listener { return Pattern.compile(player.playerName, flags); } catch (Exception e) { - bot.chat.tellraw(Component.text(e.toString()).color(NamedTextColor.RED)); + bot.logger.error("Error compiling player filter regex " + player.playerName + " (this shouldn't happen):"); + bot.logger.error(e); return null; } } @@ -153,12 +152,15 @@ public class PlayerFilterPlugin implements PlayersPlugin.Listener { final List matches = getPlayers(playerName); - for (FilteredPlayer match : matches) { - final PlayerEntry entry = bot.players.getEntry(match.playerName); + // loop through all the servers too + for (Bot bot : bot.bots) { + for (FilteredPlayer match : matches) { + final PlayerEntry entry = bot.players.getEntry(match.playerName); - if (entry == null) continue; + if (entry == null) continue; - bot.filterManager.add(entry, match.reason); + bot.filterManager.add(entry, match.reason); + } } } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersDatabasePlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersDatabasePlugin.java index a6dde6ad..568288e4 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersDatabasePlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersDatabasePlugin.java @@ -16,7 +16,6 @@ import java.sql.SQLException; import java.time.Instant; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.CompletableFuture; public class PlayersDatabasePlugin implements PlayersPlugin.Listener { private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS players (username VARCHAR(255) PRIMARY KEY, data LONGTEXT);"; @@ -152,34 +151,29 @@ public class PlayersDatabasePlugin implements PlayersPlugin.Listener { insertPlayerStatement.setString(2, objectMapper.writeValueAsString(baseObject)); insertPlayerStatement.executeUpdate(); + } catch (SQLException | JsonProcessingException e) { + bot.logger.error(e); + } + }); + } - final CompletableFuture future = bot.players.getPlayerIP(target, true); + @Override + public void queriedPlayerIP (PlayerEntry target, String ip) { + DatabasePlugin.EXECUTOR_SERVICE.submit(() -> { + try { + final PreparedStatement updatePlayerStatement = Main.database.connection.prepareStatement(UPDATE_PLAYER); - if (future == null) return; + updatePlayerStatement.setString(1, "$.ips"); + updatePlayerStatement.setString(2, "$.ips"); - future.thenApply(output -> { - if (output == null) return null; + final ObjectNode ipsObject = JsonNodeFactory.instance.objectNode(); + ipsObject.put(bot.getServerString(true), ip); - try { - final PreparedStatement updatePlayerStatement = Main.database.connection.prepareStatement(UPDATE_PLAYER); + updatePlayerStatement.setString(3, objectMapper.writeValueAsString(ipsObject)); - updatePlayerStatement.setString(1, "$.ips"); - updatePlayerStatement.setString(2, "$.ips"); + updatePlayerStatement.setString(4, target.profile.getName()); - final ObjectNode ipsObject = JsonNodeFactory.instance.objectNode(); - ipsObject.put(bot.getServerString(true), output); - - updatePlayerStatement.setString(3, objectMapper.writeValueAsString(ipsObject)); - - updatePlayerStatement.setString(4, target.profile.getName()); - - updatePlayerStatement.executeUpdate(); - } catch (SQLException | JsonProcessingException e) { - bot.logger.error(e); - } - - return output; - }); + updatePlayerStatement.executeUpdate(); } catch (SQLException | JsonProcessingException e) { bot.logger.error(e); } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java index 1cf1cafd..ae6e7b0e 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java @@ -76,7 +76,13 @@ public class PlayersPlugin extends Bot.Listener implements TickPlugin.Listener { final CompletableFuture future = getPlayerIP(target, true); - future.thenApply(ip -> target.ip = ip); + future.thenApply(ip -> { + target.ip = ip; + + for (Listener listener : listeners) listener.queriedPlayerIP(target, ip); + + return null; + }); } public CompletableFuture getPlayerIP (PlayerEntry target) { return getPlayerIP(target, false); } @@ -361,5 +367,7 @@ public class PlayersPlugin extends Bot.Listener implements TickPlugin.Listener { default void playerVanished (PlayerEntry target) { } default void playerChangedUsername (PlayerEntry target) { } + + default void queriedPlayerIP (PlayerEntry target, String ip) { } } }