From 6e91fe82d458e4154602c8da9cc9b60c268c9c62 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 24 Apr 2023 13:19:25 +0700 Subject: [PATCH] add filter (ignore messy code plz) --- .idea/workspace.xml | 69 +++++---- .../chipmunk/chayapak/chomens_bot/Bot.java | 2 + .../chomens_bot/commands/FilterCommand.java | 141 ++++++++++++++++++ .../chomens_bot/data/FilteredPlayer.java | 10 ++ .../plugins/CommandHandlerPlugin.java | 1 + .../chomens_bot/plugins/FilterPlugin.java | 103 +++++++++++++ 6 files changed, 294 insertions(+), 32 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/data/FilteredPlayer.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bb2a287..b42b018 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,8 +4,13 @@ - - @@ -480,7 +483,9 @@ - diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index 66925f7..d7bfd35 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -62,6 +62,7 @@ public class Bot { @Getter private CloopPlugin cloop; @Getter private MazePlugin maze; @Getter private ExploitsPlugin exploits; + @Getter private FilterPlugin filter; public Bot (Configuration.BotOption botOption, List allBots, Configuration config) { this.host = botOption.host; @@ -102,6 +103,7 @@ public class Bot { this.cloop = new CloopPlugin(this); this.maze = new MazePlugin(this); this.exploits = new ExploitsPlugin(this); + this.filter = new FilterPlugin(this); reconnect(); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java new file mode 100644 index 0000000..86a4693 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java @@ -0,0 +1,141 @@ +package land.chipmunk.chayapak.chomens_bot.commands; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.command.Command; +import land.chipmunk.chayapak.chomens_bot.command.CommandContext; +import land.chipmunk.chayapak.chomens_bot.data.FilteredPlayer; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.JoinConfiguration; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class FilterCommand implements Command { + public String name() { return "filter"; } + + public String description() { + return "Filter players"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add(" add <{player}>"); + usages.add(" -ignorecase add <{player}>"); + usages.add(" -regex add <{player}>"); + usages.add(" -ignorecase -regex add <{player}>"); + usages.add(" remove "); + usages.add(" clear"); + usages.add(" list"); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("filterplayer"); + + return aliases; + } + + public int trustLevel() { + return 1; + } + + // most of these codes are from cloop and greplog + public Component execute(CommandContext context, String[] _args, String[] fullArgs) { + final Bot bot = context.bot(); + + boolean ignoreCase = false; + boolean regex = false; + + String[] args = _args; + + // this is a mess + if (_args[0].equals("-ignorecase")) { + ignoreCase = true; + args = Arrays.copyOfRange(_args, 1, _args.length); + } else if (_args[0].equals("-regex")) { + regex = true; + args = Arrays.copyOfRange(_args, 1, _args.length); + } + + if (_args.length > 1 && _args[1].equals("-ignorecase")) { + ignoreCase = true; + args = Arrays.copyOfRange(_args, 2, _args.length); + } else if (_args.length > 1 && _args[1].equals("-regex")) { + regex = true; + args = Arrays.copyOfRange(_args, 2, _args.length); + } + + switch (args[0]) { + case "add" -> { + final String player = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + + bot.filter().add(player, regex, ignoreCase); + context.sendOutput( + Component.translatable( + "Added %s to the filters", + Component.text(player).color(NamedTextColor.AQUA) + ) + ); + } + case "remove" -> { + try { + final int index = Integer.parseInt(args[1]); + + context.sendOutput( + Component.translatable( + "Removed %s from the filters", + Component.text(bot.filter().filteredPlayers().get(index).playerName).color(NamedTextColor.AQUA) + ) + ); + + bot.filter().remove(index); + } catch (IndexOutOfBoundsException | IllegalArgumentException | NullPointerException ignored) { + return Component.text("Invalid index").color(NamedTextColor.RED); + } + } + case "clear" -> { + bot.filter().clear(); + context.sendOutput( + Component.text("Cleared the filter") + ); + } + case "list" -> { + final List filtersComponents = new ArrayList<>(); + + int index = 0; + for (FilteredPlayer player : bot.filter().filteredPlayers()) { + filtersComponents.add( + Component.translatable( + "%s › %s", + Component.text(index).color(NamedTextColor.GREEN), + Component.text(player.playerName).color(NamedTextColor.AQUA) + ).color(NamedTextColor.DARK_GRAY) + ); + + index++; + } + + final Component component = Component.empty() + .append(Component.text("Filtered players ").color(NamedTextColor.GREEN)) + .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) + .append(Component.text(bot.filter().filteredPlayers().size()).color(NamedTextColor.GRAY)) + .append(Component.text(")").color(NamedTextColor.DARK_GRAY)) + .append(Component.newline()) + .append( + Component.join(JoinConfiguration.newlines(), filtersComponents) + ); + + context.sendOutput(component); + } + default -> { + return Component.text("Invalid argument").color(NamedTextColor.RED); + } + } + + return Component.text("success"); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/data/FilteredPlayer.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/FilteredPlayer.java new file mode 100644 index 0000000..fd0768c --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/FilteredPlayer.java @@ -0,0 +1,10 @@ +package land.chipmunk.chayapak.chomens_bot.data; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class FilteredPlayer { + public String playerName; + public boolean regex; + public boolean ignoreCase; +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index 2fd9b47..e346406 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -59,6 +59,7 @@ public class CommandHandlerPlugin { registerCommand(new TranslateCommand()); registerCommand(new KickCommand()); registerCommand(new ClearChatQueueCommand()); + registerCommand(new FilterCommand()); } public void registerCommand (Command command) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java new file mode 100644 index 0000000..46113a3 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java @@ -0,0 +1,103 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.chatParsers.data.MutablePlayerListEntry; +import land.chipmunk.chayapak.chomens_bot.data.FilteredPlayer; +import lombok.Getter; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; + +public class FilterPlugin extends PlayersPlugin.PlayerListener { + private final Bot bot; + + @Getter private final List filteredPlayers = new ArrayList<>(); + + public FilterPlugin (Bot bot) { + this.bot = bot; + + bot.players().addListener(this); + + bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS); + } + + private FilteredPlayer getPlayer (String name) { + // mess + // also regex and ignorecase codes from greplog plugin + FilteredPlayer filteredPlayer = null; + for (FilteredPlayer _filteredPlayer : filteredPlayers) { + if (_filteredPlayer.regex) { + Pattern pattern = null; + if (_filteredPlayer.ignoreCase) { + pattern = Pattern.compile("\\b" + _filteredPlayer.playerName + "\\b", Pattern.CASE_INSENSITIVE); + } else { + try { + pattern = Pattern.compile("\\b" + _filteredPlayer.playerName + "\\b"); + } catch (Exception e) { + bot.chat().tellraw(Component.text(e.toString()).color(NamedTextColor.RED)); + } + } + + if (pattern.matcher(name).find()) { + filteredPlayer = _filteredPlayer; + break; + } + } else { + if (_filteredPlayer.ignoreCase) { + if (_filteredPlayer.playerName.toLowerCase().equals(name)) { + filteredPlayer = _filteredPlayer; + break; + } + } else { + if (_filteredPlayer.playerName.equals(name)) { + filteredPlayer = _filteredPlayer; + break; + } + } + } + } + + return filteredPlayer; + } + + @Override + public void playerJoined (MutablePlayerListEntry target) { + final FilteredPlayer player = getPlayer(target.profile().getName()); + + if (player == null) return; + + System.out.println("joined event kicking " + target.profile().getName()); + + /* bot.core().run("essentials:mute " + target.profile().getIdAsString() + " 10y"); + bot.core().run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile().getId())); + bot.exploits().kick(target.profile().getId()); */ + } + + public void tick () { + for (MutablePlayerListEntry target : bot.players().list()) { + final FilteredPlayer player = getPlayer(target.profile().getName()); + + if (player == null) continue; + + System.out.println("kicking " + target.profile().getName()); + + // bot.exploits().kick(target.profile().getId()); + } + } + + public void add (String playerName, boolean regex, boolean ignoreCase) { + filteredPlayers.add(new FilteredPlayer(playerName, regex, ignoreCase)); + } + + public void remove (int index) { + filteredPlayers.remove(index); + } + + public void clear () { + filteredPlayers.clear(); + } +}