refactor: use a central filtering manager

This commit is contained in:
ChomeNS
2025-01-18 15:20:15 +07:00
parent 54389f2e2b
commit ae7fe63846
8 changed files with 239 additions and 172 deletions

View File

@@ -81,7 +81,8 @@ public class Bot {
public BruhifyPlugin bruhify;
public CloopPlugin cloop;
public ExploitsPlugin exploits;
public FilterPlugin filter;
public FilterManagerPlugin filterManager;
public PlayerFilterPlugin playerFilter;
public CommandSuggestionPlugin commandSuggestion;
public MailPlugin mail;
public PacketSnifferPlugin packetSniffer;
@@ -132,7 +133,8 @@ public class Bot {
this.bruhify = new BruhifyPlugin(this);
this.cloop = new CloopPlugin(this);
this.exploits = new ExploitsPlugin(this);
this.filter = new FilterPlugin(this);
this.filterManager = new FilterManagerPlugin(this);
this.playerFilter = new PlayerFilterPlugin(this);
this.commandSuggestion = new CommandSuggestionPlugin(this);
this.mail = new MailPlugin(this);
this.packetSniffer = new PacketSnifferPlugin(this);

View File

@@ -7,7 +7,7 @@ import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.data.FilteredPlayer;
import me.chayapak1.chomens_bot.plugins.DatabasePlugin;
import me.chayapak1.chomens_bot.plugins.FilterPlugin;
import me.chayapak1.chomens_bot.plugins.PlayerFilterPlugin;
import me.chayapak1.chomens_bot.util.ColorUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
@@ -62,7 +62,7 @@ public class FilterCommand extends Command {
final String reason = context.getString(true, false);
if (
FilterPlugin.localList.stream()
PlayerFilterPlugin.localList.stream()
.map(filteredPlayer -> filteredPlayer.playerName)
.toList()
.contains(player)
@@ -78,7 +78,7 @@ public class FilterCommand extends Command {
final boolean finalRegex = regex;
final boolean finalIgnoreCase = ignoreCase;
DatabasePlugin.executorService.submit(() -> bot.filter.add(player, reason, finalRegex, finalIgnoreCase));
DatabasePlugin.executorService.submit(() -> bot.playerFilter.add(player, reason, finalRegex, finalIgnoreCase));
if (reason.isEmpty()) {
return Component.translatable(
@@ -98,11 +98,11 @@ public class FilterCommand extends Command {
final int index = context.getInteger(true);
final FilteredPlayer player = FilterPlugin.localList.get(index);
final FilteredPlayer player = PlayerFilterPlugin.localList.get(index);
if (player == null) throw new CommandException(Component.text("Invalid index"));
DatabasePlugin.executorService.submit(() -> bot.filter.remove(player.playerName));
DatabasePlugin.executorService.submit(() -> bot.playerFilter.remove(player.playerName));
return Component.translatable(
"Removed %s from the filters",
@@ -112,7 +112,7 @@ public class FilterCommand extends Command {
case "clear" -> {
context.checkOverloadArgs(1);
DatabasePlugin.executorService.submit(() -> bot.filter.clear());
DatabasePlugin.executorService.submit(() -> bot.playerFilter.clear());
return Component.text("Cleared the filter").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "list" -> {
@@ -121,7 +121,7 @@ public class FilterCommand extends Command {
final List<Component> filtersComponents = new ArrayList<>();
int index = 0;
for (FilteredPlayer player : FilterPlugin.localList) {
for (FilteredPlayer player : PlayerFilterPlugin.localList) {
Component options = Component.empty().color(NamedTextColor.DARK_GRAY);
if (player.ignoreCase || player.regex) {
@@ -171,7 +171,7 @@ public class FilterCommand extends Command {
return Component.empty()
.append(Component.text("Filtered players ").color(NamedTextColor.GREEN))
.append(Component.text("(").color(NamedTextColor.DARK_GRAY))
.append(Component.text(FilterPlugin.localList.size()).color(NamedTextColor.GRAY))
.append(Component.text(PlayerFilterPlugin.localList.size()).color(NamedTextColor.GRAY))
.append(Component.text(")").color(NamedTextColor.DARK_GRAY))
.append(Component.newline())
.append(

View File

@@ -51,7 +51,7 @@ public class AuthPlugin extends PlayersPlugin.Listener {
target.profile.getId()
);
} else {
bot.filter.doAll(target, bot.config.ownerAuthentication.muteReason);
bot.filterManager.doAll(target, bot.config.ownerAuthentication.muteReason);
}
return ip;

View File

@@ -0,0 +1,144 @@
package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.data.PlayerEntry;
import me.chayapak1.chomens_bot.data.chat.PlayerMessage;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.UUIDUtilities;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class FilterManagerPlugin extends PlayersPlugin.Listener {
private final Bot bot;
public final Map<PlayerEntry, String> list = Collections.synchronizedMap(new HashMap<>());
public FilterManagerPlugin (Bot bot) {
this.bot = bot;
bot.players.addListener(this);
bot.chat.addListener(new ChatPlugin.Listener() {
@Override
public boolean playerMessageReceived(PlayerMessage message) {
FilterManagerPlugin.this.playerMessageReceived(message);
return true;
}
});
bot.commandSpy.addListener(new CommandSpyPlugin.Listener() {
@Override
public void commandReceived(PlayerEntry sender, String command) {
FilterManagerPlugin.this.commandSpyMessageReceived(sender, command);
}
});
bot.executor.scheduleAtFixedRate(this::kick, 0, 10, TimeUnit.SECONDS);
}
@Override
public void playerLeft (PlayerEntry target) {
remove(target.profile.getName());
}
@Override
public void playerDisplayNameUpdated (PlayerEntry target, Component displayName) {
final Pair<PlayerEntry, String> player = getFilteredFromName(target.profile.getName());
if (player == null) return;
// we use the stringified instead of the component because you can configure the OP and DeOP tag in
// the extras config
final String stringifiedDisplayName = ComponentUtilities.stringify(displayName);
if (stringifiedDisplayName.startsWith("[OP] ")) deOp(target);
}
public void commandSpyMessageReceived (PlayerEntry entry, String command) {
final Pair<PlayerEntry, String> player = getFilteredFromName(entry.profile.getName());
if (player == null) return;
if (
command.startsWith("/mute") ||
command.startsWith("/emute") ||
command.startsWith("/silence") ||
command.startsWith("/esilence") ||
command.startsWith("/essentials:mute") ||
command.startsWith("/essentials:emute") ||
command.startsWith("/essentials:silence") ||
command.startsWith("/essentials:esilence")
) mute(entry, player.getRight());
deOp(entry);
gameMode(entry);
bot.exploits.kick(entry.profile.getId());
}
public void playerMessageReceived (PlayerMessage message) {
if (message.sender.profile.getName() == null) return;
final Pair<PlayerEntry, String> player = getFilteredFromName(message.sender.profile.getName());
if (player == null || message.sender.profile.getId().equals(new UUID(0L, 0L))) return;
doAll(message.sender, player.getRight());
}
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());
}
public void mute (PlayerEntry target) { mute(target, ""); }
public void mute (PlayerEntry target, String reason) {
bot.core.run("essentials:mute " + target.profile.getIdAsString() + " 10y " + reason);
}
public void deOp (PlayerEntry target) {
bot.core.run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile.getId()));
}
public void gameMode (PlayerEntry target) {
bot.core.run("minecraft:gamemode adventure " + UUIDUtilities.selector(target.profile.getId()));
}
public void kick () {
for (PlayerEntry filtered : list.keySet()) {
bot.exploits.kick(filtered.profile.getId());
}
}
public void add (PlayerEntry entry, String reason) {
if (
getFilteredFromName(entry.profile.getName()) != null || // prevent already existing filters
entry.profile.equals(bot.profile) // prevent self-harm !!!!!!
) return;
list.put(entry, reason);
doAll(entry, reason);
}
public void remove (String name) {
list.entrySet().removeIf(filtered -> filtered.getKey().profile.getName().equals(name));
}
public Pair<PlayerEntry, String> getFilteredFromName (String name) {
for (Map.Entry<PlayerEntry, String> entry : list.entrySet()) {
if (entry.getKey().profile.getName().equals(name)) return Pair.of(entry.getKey(), entry.getValue());
}
return null;
}
}

View File

@@ -45,11 +45,20 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
bot.players.addListener(this);
bot.executor.scheduleAtFixedRate(this::checkAllPlayers, 0, 10, TimeUnit.SECONDS);
bot.core.addListener(new CorePlugin.Listener() {
@Override
public void ready() {
IPFilterPlugin.this.coreReady();
}
});
bot.executor.scheduleAtFixedRate(this::checkAllPlayers, 5, 15, TimeUnit.SECONDS);
}
private void coreReady () { checkAllPlayers(); }
@Override
public void playerJoined(PlayerEntry target) {
public void playerJoined (PlayerEntry target) {
if (localList.isEmpty()) return;
check(target);
@@ -63,14 +72,12 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
if (future == null) return;
future.thenApplyAsync(output -> {
handleIP(output, target);
handleFilterManager(output, target);
return output;
});
}
public static List<String> list () {
final List<String> output = new ArrayList<>();
@@ -135,13 +142,13 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
}
}
private void handleIP (String ip, PlayerEntry entry) {
private void handleFilterManager (String ip, PlayerEntry entry) {
for (String eachIP : localList) {
if (!eachIP.equals(ip)) continue;
if (entry.profile.getId().equals(bot.profile.getId())) continue;
if (entry.profile.equals(bot.profile)) continue;
bot.filter.doAll(entry);
bot.filterManager.add(entry, "");
}
}
}

View File

@@ -4,10 +4,7 @@ import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.data.FilteredPlayer;
import me.chayapak1.chomens_bot.data.PlayerEntry;
import me.chayapak1.chomens_bot.data.chat.PlayerMessage;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import me.chayapak1.chomens_bot.util.UUIDUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
@@ -16,11 +13,10 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
public class FilterPlugin extends PlayersPlugin.Listener {
public class PlayerFilterPlugin extends PlayersPlugin.Listener {
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, reason, regex, ignoreCase) VALUES (?, ?, ?, ?);";
@@ -39,36 +35,18 @@ public class FilterPlugin extends PlayersPlugin.Listener {
}
});
Main.executor.scheduleAtFixedRate(FilterPlugin::list, 3, 5, TimeUnit.SECONDS);
Main.executor.scheduleAtFixedRate(PlayerFilterPlugin::list, 3, 5, TimeUnit.SECONDS);
}
}
private final Bot bot;
public FilterPlugin (Bot bot) {
public PlayerFilterPlugin(Bot bot) {
this.bot = bot;
if (Main.database == null) return;
bot.players.addListener(this);
bot.chat.addListener(new ChatPlugin.Listener() {
@Override
public boolean playerMessageReceived(PlayerMessage message) {
FilterPlugin.this.playerMessageReceived(message);
return true;
}
});
bot.commandSpy.addListener(new CommandSpyPlugin.Listener() {
@Override
public void commandReceived(PlayerEntry sender, String command) {
FilterPlugin.this.commandSpyMessageReceived(sender, command);
}
});
bot.executor.scheduleAtFixedRate(this::kick, 0, 10, TimeUnit.SECONDS);
}
public static List<FilteredPlayer> list () {
@@ -106,6 +84,18 @@ public class FilterPlugin extends PlayersPlugin.Listener {
return null;
}
private List<FilteredPlayer> getPlayers (String name) {
final List<FilteredPlayer> matches = new ArrayList<>();
for (FilteredPlayer filteredPlayer : localList) {
if (matchesPlayer(name, filteredPlayer)) {
matches.add(filteredPlayer);
}
}
return matches;
}
private boolean matchesPlayer (String name, FilteredPlayer player) {
if (player.regex) {
final Pattern pattern = compilePattern(player);
@@ -141,85 +131,10 @@ public class FilterPlugin extends PlayersPlugin.Listener {
if (player == null) return;
doAll(target, player.reason);
bot.filterManager.add(target, player.reason);
});
}
@Override
public void playerDisplayNameUpdated(PlayerEntry target, Component displayName) {
final FilteredPlayer player = getPlayer(target.profile.getName());
if (player == null) return;
// we use the stringified instead of the component because you can configure the OP and DeOP tag in
// the extras config
final String stringifiedDisplayName = ComponentUtilities.stringify(displayName);
if (stringifiedDisplayName.startsWith("[OP] ")) deOp(target);
}
public void commandSpyMessageReceived (PlayerEntry entry, String command) {
final FilteredPlayer player = getPlayer(entry.profile.getName());
if (player == null) return;
if (
command.startsWith("/mute") ||
command.startsWith("/emute") ||
command.startsWith("/silence") ||
command.startsWith("/esilence") ||
command.startsWith("/essentials:mute") ||
command.startsWith("/essentials:emute") ||
command.startsWith("/essentials:silence") ||
command.startsWith("/essentials:esilence")
) mute(entry, player.reason);
deOp(entry);
gameMode(entry);
bot.exploits.kick(entry.profile.getId());
}
public void playerMessageReceived (PlayerMessage message) {
if (message.sender.profile.getName() == null) return;
final FilteredPlayer player = getPlayer(message.sender.profile.getName());
if (player == null || message.sender.profile.getId().equals(new UUID(0L, 0L))) return;
doAll(message.sender, player.reason);
}
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());
}
public void mute (PlayerEntry target) { mute(target, ""); }
public void mute (PlayerEntry target, String reason) {
bot.core.run("essentials:mute " + target.profile.getIdAsString() + " 10y " + reason);
}
public void deOp (PlayerEntry target) {
bot.core.run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile.getId()));
}
public void gameMode(PlayerEntry target) {
bot.core.run("minecraft:gamemode adventure " + UUIDUtilities.selector(target.profile.getId()));
}
public void kick () {
for (PlayerEntry target : bot.players.list) {
final FilteredPlayer player = getPlayer(target.profile.getName());
if (player == null) continue;
bot.exploits.kick(target.profile.getId());
}
}
public void add (String playerName, String reason, boolean regex, boolean ignoreCase) {
try {
final PreparedStatement statement = bot.database.connection.prepareStatement(INSERT_FILTER);
@@ -236,14 +151,20 @@ public class FilterPlugin extends PlayersPlugin.Listener {
bot.logger.error(e);
}
final PlayerEntry target = bot.players.getEntry(playerName); // fix not working for regex and ignorecase
final List<FilteredPlayer> matches = getPlayers(playerName);
if (target == null) return;
for (FilteredPlayer match : matches) {
final PlayerEntry entry = bot.players.getEntry(match.playerName);
doAll(target, reason);
if (entry == null) continue;
bot.filterManager.add(entry, match.reason);
}
}
public void remove (String playerName) {
bot.filterManager.remove(playerName);
try {
final PreparedStatement statement = bot.database.connection.prepareStatement(REMOVE_FILTER);
@@ -258,6 +179,8 @@ public class FilterPlugin extends PlayersPlugin.Listener {
}
public void clear () {
for (FilteredPlayer player : localList) bot.filterManager.remove(player.playerName);
try {
bot.database.update(CLEAR_FILTER);

View File

@@ -2,7 +2,6 @@ package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.data.PlayerEntry;
import me.chayapak1.chomens_bot.data.chat.PlayerMessage;
import java.util.ArrayList;
import java.util.List;
@@ -18,22 +17,6 @@ public class WhitelistPlugin extends PlayersPlugin.Listener {
this.bot = bot;
bot.players.addListener(this);
bot.chat.addListener(new ChatPlugin.Listener() {
@Override
public boolean playerMessageReceived(PlayerMessage message) {
WhitelistPlugin.this.playerMessageReceived(message);
return true;
}
});
bot.commandSpy.addListener(new CommandSpyPlugin.Listener() {
@Override
public void commandReceived(PlayerEntry sender, String command) {
WhitelistPlugin.this.commandReceived(sender, command);
}
});
}
public void enable () {
@@ -43,49 +26,57 @@ public class WhitelistPlugin extends PlayersPlugin.Listener {
if (list.contains(entry.profile.getName())) continue;
list.add(entry.profile.getName());
bot.filterManager.remove(entry.profile.getName());
}
}
public void disable () {
enabled = false;
for (PlayerEntry entry : bot.players.list) {
bot.filterManager.remove(entry.profile.getName());
}
}
public void add (String player) {
list.add(player);
bot.filterManager.remove(player);
}
public String remove (int index) {
final String removed = list.remove(index);
checkAndAddToFilterManager(removed);
return removed;
}
public void add (String player) { list.add(player); }
public String remove (int index) { return list.remove(index); }
public void clear () {
list.removeIf(eachPlayer -> !eachPlayer.equals(bot.profile.getName()));
for (PlayerEntry entry : bot.players.list) {
if (entry.profile.equals(bot.profile)) continue;
bot.filterManager.add(entry, "");
}
}
public boolean isBlacklisted (String name) { return !list.contains(name); }
private void checkAndAddToFilterManager (String player) {
final PlayerEntry entry = bot.players.getEntry(player);
if (entry == null) return;
bot.filterManager.add(entry, "");
}
@Override
public void playerJoined(PlayerEntry target) {
handle(target);
}
public void playerJoined (PlayerEntry target) {
if (!enabled) return;
public void playerMessageReceived (PlayerMessage message) {
handle(message.sender);
}
public void commandReceived (PlayerEntry entry, String command) {
if (!enabled || list.contains(entry.profile.getName()) || entry.profile.equals(bot.profile)) return;
if (
command.startsWith("mute") ||
command.startsWith("silence") ||
command.startsWith("emute") ||
command.startsWith("esilence") ||
command.startsWith("essentials:mute") ||
command.startsWith("essentials:silence") ||
command.startsWith("essentials:emute") ||
command.startsWith("essentials:esilence")
) bot.filter.mute(entry);
bot.filter.deOp(entry);
bot.filter.gameMode(entry);
bot.exploits.kick(entry.profile.getId());
}
private void handle (PlayerEntry entry) {
if (!enabled || list.contains(entry.profile.getName()) || entry.profile.equals(bot.profile)) return;
bot.filter.doAll(entry);
if (isBlacklisted(target.profile.getName())) bot.filterManager.add(target, "");
}
}