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
This commit is contained in:
ChomeNS
2025-04-09 11:01:38 +07:00
parent 297b84801f
commit 54f0f36952
6 changed files with 55 additions and 37 deletions

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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<FilteredPlayer> 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);
}
}
}

View File

@@ -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<String> 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);
}

View File

@@ -76,7 +76,13 @@ public class PlayersPlugin extends Bot.Listener implements TickPlugin.Listener {
final CompletableFuture<String> 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<String> 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) { }
}
}