From 78eac00461eb147abf12e03587b4b07de2b3f78b Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 6 Jul 2023 20:27:56 +0700 Subject: [PATCH] make a commandspy plugin instead of making a chat parser that parses commandspy --- .../chipmunk/chayapak/chomens_bot/Bot.java | 2 + .../commandSpy/CommandSpyParser.java | 49 --------------- .../plugins/ChatCommandHandlerPlugin.java | 55 +++++++++++++---- .../chomens_bot/plugins/ChatPlugin.java | 9 --- .../chomens_bot/plugins/CommandSpyPlugin.java | 60 +++++++++++++++++++ .../chomens_bot/plugins/FilterPlugin.java | 18 +++--- 6 files changed, 115 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java 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 ec2907f..492ec3e 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -47,6 +47,7 @@ public class Bot { public TickPlugin tick; public ChatPlugin chat; + public CommandSpyPlugin commandSpy; public PositionPlugin position; public SelfCarePlugin selfCare; public CorePlugin core; @@ -92,6 +93,7 @@ public class Bot { public void ready () { this.tick = new TickPlugin(this); this.chat = new ChatPlugin(this); + this.commandSpy = new CommandSpyPlugin(this); this.position = new PositionPlugin(this); this.selfCare = new SelfCarePlugin(this); this.core = new CorePlugin(this); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java deleted file mode 100644 index a3d8eb1..0000000 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java +++ /dev/null @@ -1,49 +0,0 @@ -package land.chipmunk.chayapak.chomens_bot.chatParsers.commandSpy; - -import land.chipmunk.chayapak.chomens_bot.Bot; -import land.chipmunk.chayapak.chomens_bot.data.chat.ChatParser; -import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry; -import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage; -import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.format.NamedTextColor; - -import java.util.List; - -public class CommandSpyParser implements ChatParser { - private final Bot bot; - - public CommandSpyParser (Bot bot) { - this.bot = bot; - } - - @Override - public PlayerMessage parse (Component message) { - if (message instanceof TextComponent) return parse((TextComponent) message); - return null; - } - - public PlayerMessage parse (TextComponent message) { - final List children = message.children(); - - if ( - ( - message.color() != NamedTextColor.AQUA || - message.color() != NamedTextColor.YELLOW || - message.style().isEmpty() - ) && - children.size() < 2 - ) return null; - - final Component username = Component.text(message.content()); - final Component command = children.get(1); - - final String stringUsername = ComponentUtilities.stringify(username); - MutablePlayerListEntry sender = bot.players.getEntry(stringUsername); - - if (sender == null) return null; - - return new PlayerMessage(sender, username, command); - } -} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java index 37671fa..62984b9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java @@ -1,6 +1,7 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry; import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage; import land.chipmunk.chayapak.chomens_bot.command.PlayerCommandContext; import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; @@ -22,15 +23,17 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener { this.commandSpyPrefixes = bot.config.commandSpyPrefixes; bot.chat.addListener(this); + + bot.commandSpy.addListener(new CommandSpyPlugin.Listener() { + @Override + public void commandReceived(MutablePlayerListEntry sender, String command) { + ChatCommandHandlerPlugin.this.commandSpyMessageReceived(sender, command); + } + }); } @Override - public void playerMessageReceived (PlayerMessage message) { listener(message, false); } - - @Override - public void commandSpyMessageReceived (PlayerMessage message) { listener(message, true); } - - private void listener (PlayerMessage message, boolean cspy) { + public void playerMessageReceived (PlayerMessage message) { try { if (message.sender.profile.getId().equals(bot.profile.getId())) return; } catch (Exception ignored) {} // kinda sus ngl @@ -44,7 +47,7 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener { String prefix = null; - for (String eachPrefix : (cspy ? commandSpyPrefixes : prefixes)) { + for (String eachPrefix : prefixes) { if (!contents.startsWith(eachPrefix)) continue; prefix = eachPrefix; } @@ -53,14 +56,42 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener { final String commandString = contents.substring(prefix.length()); - final String selector = cspy ? UUIDUtilities.selector(message.sender.profile.getId()) : "@a"; - - final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, message.sender, bot.hashing.hash, bot.hashing.ownerHash); + final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, "@a", message.sender, bot.hashing.hash, bot.hashing.ownerHash); final Component output = bot.commandHandler.executeCommand(commandString, context, true, false, false, null); - if (output != null) { - context.sendOutput(output); + if (output != null) context.sendOutput(output); + } + + public void commandSpyMessageReceived (MutablePlayerListEntry sender, String command) { + try { + if (sender.profile.getId().equals(bot.profile.getId())) return; + } catch (Exception ignored) { + } // kinda sus ngl + + final Component displayNameComponent = Component.text(sender.profile.getName()); + final Component messageComponent = Component.text(command); + + final String displayName = ComponentUtilities.stringify(displayNameComponent); + final String contents = ComponentUtilities.stringify(messageComponent); + + String prefix = null; + + for (String eachPrefix : commandSpyPrefixes) { + if (!contents.startsWith(eachPrefix)) continue; + prefix = eachPrefix; } + + if (prefix == null) return; + + final String commandString = contents.substring(prefix.length()); + + final String selector = UUIDUtilities.selector(sender.profile.getId()); + + final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, sender, bot.hashing.hash, bot.hashing.ownerHash); + + final Component output = bot.commandHandler.executeCommand(commandString, context, true, false, false, null); + + if (output != null) context.sendOutput(output); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java index 683d124..ec304a2 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java @@ -12,7 +12,6 @@ import land.chipmunk.chayapak.chomens_bot.chatParsers.CreayunChatParser; import land.chipmunk.chayapak.chomens_bot.chatParsers.KaboomChatParser; import land.chipmunk.chayapak.chomens_bot.chatParsers.MinecraftChatParser; import land.chipmunk.chayapak.chomens_bot.chatParsers.U203aChatParser; -import land.chipmunk.chayapak.chomens_bot.chatParsers.commandSpy.CommandSpyParser; import land.chipmunk.chayapak.chomens_bot.data.chat.ChatParser; import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry; import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage; @@ -37,8 +36,6 @@ public class ChatPlugin extends Bot.Listener { private final List chatParsers; - private final CommandSpyParser commandSpyParser; - private final List queue = new ArrayList<>(); public final int queueDelay; @@ -50,8 +47,6 @@ public class ChatPlugin extends Bot.Listener { queueDelay = bot.options.chatQueueDelay; - this.commandSpyParser = new CommandSpyParser(bot); - bot.addListener(this); chatParsers = new ArrayList<>(); @@ -89,12 +84,9 @@ public class ChatPlugin extends Bot.Listener { if (playerMessage != null) break; } - final PlayerMessage commandSpyMessage = commandSpyParser.parse(component); - for (Listener listener : listeners) { listener.systemMessageReceived(component); if (playerMessage != null) listener.playerMessageReceived(playerMessage); - if (commandSpyMessage != null) listener.commandSpyMessageReceived(commandSpyMessage); } } @@ -315,7 +307,6 @@ public class ChatPlugin extends Bot.Listener { public static class Listener { public void playerMessageReceived (PlayerMessage message) {} - public void commandSpyMessageReceived (PlayerMessage message) {} public void systemMessageReceived (Component component) {} } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java new file mode 100644 index 0000000..edb0b25 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java @@ -0,0 +1,60 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry; +import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; + +public class CommandSpyPlugin extends ChatPlugin.Listener { + private final Bot bot; + + private final List listeners = new ArrayList<>(); + + public CommandSpyPlugin (Bot bot) { + this.bot = bot; + + bot.chat.addListener(this); + } + + @Override + public void systemMessageReceived(Component component) { + TextComponent textComponent = null; + + try { + textComponent = (TextComponent) component; + } catch (ClassCastException e) { + return; + } + + final List children = textComponent.children(); + + if ( + ( + textComponent.color() != NamedTextColor.AQUA || + textComponent.color() != NamedTextColor.YELLOW || + textComponent.style().isEmpty() + ) && + children.size() < 2 + ) return; + + final String username = textComponent.content(); + final String command = ComponentUtilities.stringify(children.get(1)); + + final MutablePlayerListEntry sender = bot.players.getEntry(username); + + if (sender == null) return; + + for (Listener listener : listeners) listener.commandReceived(sender, command); + } + + public void addListener (Listener listener) { listeners.add(listener); } + + public static class Listener { + public void commandReceived (MutablePlayerListEntry sender, String 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 index 42891ce..caf5a54 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java @@ -34,17 +34,19 @@ public class FilterPlugin extends PlayersPlugin.Listener { bot.players.addListener(this); bot.chat.addListener(new ChatPlugin.Listener() { - @Override - public void commandSpyMessageReceived(PlayerMessage message) { - FilterPlugin.this.commandSpyMessageReceived(message); - } - @Override public void playerMessageReceived(PlayerMessage message) { FilterPlugin.this.playerMessageReceived(message); } }); + bot.commandSpy.addListener(new CommandSpyPlugin.Listener() { + @Override + public void commandReceived(MutablePlayerListEntry sender, String command) { + FilterPlugin.this.commandSpyMessageReceived(sender); + } + }); + bot.executor.scheduleAtFixedRate(this::kick, 0, 10, TimeUnit.SECONDS); } @@ -103,12 +105,12 @@ public class FilterPlugin extends PlayersPlugin.Listener { bot.exploits.kick(target.profile.getId()); } - public void commandSpyMessageReceived (PlayerMessage message) { - final FilteredPlayer player = getPlayer(message.sender.profile.getName()); + public void commandSpyMessageReceived (MutablePlayerListEntry sender) { + final FilteredPlayer player = getPlayer(sender.profile.getName()); if (player == null) return; - deOp(message.sender); + deOp(sender); } public void playerMessageReceived (PlayerMessage message) {