From 2b9d29e52e94deda4a3cfc21a3ef9f21f1731018 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 21 Apr 2023 13:53:59 +0700 Subject: [PATCH] add/improve/fix stuff ig mabe mabe mabe useCore!1!1! --- .idea/workspace.xml | 47 +++++------------ .../chipmunk/chayapak/chomens_bot/Bot.java | 4 +- .../chayapak/chomens_bot/Configuration.java | 3 +- .../chipmunk/chayapak/chomens_bot/Main.java | 3 +- .../commands/ClearChatQueueCommand.java | 43 ++++++++++++++++ .../plugins/BossbarManagerPlugin.java | 2 + .../chomens_bot/plugins/ChatPlugin.java | 50 +++++++++++-------- .../plugins/CommandHandlerPlugin.java | 1 + .../chomens_bot/plugins/CorePlugin.java | 22 ++++---- src/main/resources/default-config.yml | 6 ++- 10 files changed, 113 insertions(+), 68 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index cec8a5f..27b09d1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,31 +4,8 @@ - @@ -500,7 +476,8 @@ - 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 cf0eb9f..1ae964d 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -25,6 +25,7 @@ public class Bot { private final String _username; @Getter private final boolean kaboom; @Getter private final String serverName; + @Getter @Setter private boolean useCore; @Getter @Setter private boolean useChat; @Getter private final boolean hasEssentials; @Getter private final List allBots; @@ -63,12 +64,13 @@ public class Bot { @Getter private MazePlugin maze; @Getter private ExploitsPlugin exploits; - public Bot (String host, int port, String _username, boolean kaboom, String serverName, boolean useChat, boolean hasEssentials, List allBots, Configuration config) { + public Bot (String host, int port, String _username, boolean kaboom, String serverName, boolean useCore, boolean useChat, boolean hasEssentials, List allBots, Configuration config) { this.host = host; this.port = port; this._username = _username; this.kaboom = kaboom; this.serverName = serverName; + this.useCore = useCore; this.useChat = useChat; this.hasEssentials = hasEssentials; this.allBots = allBots; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java index 7fb1361..e4ae346 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java @@ -79,7 +79,8 @@ public class Configuration { @Getter public String username; @Getter public boolean kaboom = false; @Getter public String serverName; + @Getter public boolean useCore = true; @Getter public boolean useChat = false; - @Getter public boolean hasEssentials = false; + @Getter public boolean hasEssentials = true; } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java index 38deec9..3bfaf88 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java @@ -70,10 +70,11 @@ public class Main { final String username = botOption.username(); final boolean kaboom = botOption.kaboom(); final String serverName = botOption.serverName(); + final boolean useCore = botOption.useCore(); final boolean useChat = botOption.useChat(); final boolean hasEssentials = botOption.hasEssentials(); - final Bot bot = new Bot(host, port, username, kaboom, serverName, useChat, hasEssentials, allBots, config); + final Bot bot = new Bot(host, port, username, kaboom, serverName, useCore, useChat, hasEssentials, allBots, config); allBots.add(bot); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java new file mode 100644 index 0000000..749876d --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java @@ -0,0 +1,43 @@ +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 net.kyori.adventure.text.Component; + +import java.util.ArrayList; +import java.util.List; + +public class ClearChatQueueCommand implements Command { + public String name() { return "clearchatqueue"; } + + public String description() { + return "Clears the bot's chat queue"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add(""); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("ccq"); + + return aliases; + } + + public int trustLevel() { + return 0; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final Bot bot = context.bot(); + + bot.chat().queue().clear(); + + return Component.text("success"); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java index d0baf00..e2f595b 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java @@ -52,6 +52,7 @@ public class BossbarManagerPlugin extends SessionAdapter { } private void update() { + if (!enabled) return; for (Map.Entry _bossBar : bossBars.entrySet()) { final String name = _bossBar.getKey(); final BossBar bossBar = _bossBar.getValue(); @@ -84,6 +85,7 @@ public class BossbarManagerPlugin extends SessionAdapter { } private void createBossBar (String name, String players) { + if (!enabled) return; bot.core().run("minecraft:bossbar add " + bossBarPrefix + name + " \"\""); bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " players " + players); } 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 dc395fd..96523c1 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 @@ -17,6 +17,7 @@ import land.chipmunk.chayapak.chomens_bot.chatParsers.data.MutablePlayerListEntr import land.chipmunk.chayapak.chomens_bot.chatParsers.data.PlayerMessage; import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; import land.chipmunk.chayapak.chomens_bot.util.UUIDUtilities; +import lombok.Getter; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; @@ -32,6 +33,8 @@ public class ChatPlugin extends SessionAdapter { private final CommandSpyParser commandSpyParser; + @Getter private final List queue = new ArrayList<>(); + private final List listeners = new ArrayList<>(); public ChatPlugin (Bot bot) { @@ -45,6 +48,8 @@ public class ChatPlugin extends SessionAdapter { chatParsers.add(new MinecraftChatParser(bot)); chatParsers.add(new KaboomChatParser(bot)); chatParsers.add(new ChomeNSCustomChatParser(bot)); + + bot.executor().scheduleAtFixedRate(this::sendChatTick, 0, 125, TimeUnit.MILLISECONDS); } @Override @@ -131,26 +136,28 @@ public class ChatPlugin extends SessionAdapter { } } - public void send (String message) { - final String[] splitted = message.split("(?<=\\G.{100})|\\n"); + private void sendChatTick () { + if (queue.size() == 0) return; + + final String message = queue.get(0); + + final String[] splitted = message.split("(?<=\\G.{255})|\\n"); - int i = 200; for (String splitMessage : splitted) { if (splitMessage.trim().equals("")) continue; - bot.executor().schedule(() -> { - if (splitMessage.startsWith("/")) { - bot.session().send(new ServerboundChatCommandPacket( - splitMessage.substring(1), - Instant.now().toEpochMilli(), - 0L, - Collections.emptyList(), - 0, - new BitSet() - )); - } else { - // Temporary fix for the bot getting kicked instead of chatting - bot.core().run("essentials:sudo " + bot.players().getBotEntry().profile().getIdAsString() + " c:" + splitMessage); + if (splitMessage.startsWith("/")) { + bot.session().send(new ServerboundChatCommandPacket( + splitMessage.substring(1), + Instant.now().toEpochMilli(), + 0L, + Collections.emptyList(), + 0, + new BitSet() + )); + } else { + // Temporary fix for the bot getting kicked instead of chatting + bot.core().run("essentials:sudo " + bot.players().getBotEntry().profile().getIdAsString() + " c:" + splitMessage); // bot.session().send(new ServerboundChatPacket( // splitMessage, // Instant.now().toEpochMilli(), @@ -159,11 +166,14 @@ public class ChatPlugin extends SessionAdapter { // 0, // new BitSet() // )); - } - }, i, TimeUnit.MILLISECONDS); - - i = i + 200; + } } + + queue.remove(0); + } + + public void send (String message) { + queue.add(message); } public void tellraw (Component component, String targets) { 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 40b8617..2fd9b47 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 @@ -58,6 +58,7 @@ public class CommandHandlerPlugin { registerCommand(new GenerateMazeCommand()); registerCommand(new TranslateCommand()); registerCommand(new KickCommand()); + registerCommand(new ClearChatQueueCommand()); } public void registerCommand (Command command) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java index 091c1ca..6db028f 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java @@ -78,16 +78,20 @@ public class CorePlugin extends PositionPlugin.PositionListener { public void run (String command) { if (!ready) return; - bot.session().send(new ServerboundSetCommandBlockPacket( - absoluteCorePosition(), - command, - kaboom ? CommandBlockMode.AUTO : CommandBlockMode.REDSTONE, - true, - false, - true - )); + if (bot.useCore()) { + bot.session().send(new ServerboundSetCommandBlockPacket( + absoluteCorePosition(), + command, + kaboom ? CommandBlockMode.AUTO : CommandBlockMode.REDSTONE, + true, + false, + true + )); - incrementBlock(); + incrementBlock(); + } else if (command.length() < 256) { + bot.chat().send("/" + command); + } } public CompletableFuture runTracked (String command) { diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index 9017990..ea0da9b 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -61,16 +61,20 @@ selfCare: prefix: true username: true # do NOT set this to false unless you know what you are doing bots: - # username and kaboom are optional. + # username, kaboom, useCore, useChat and hasEssentials are optional. + # for username if it doesn't exist it will default to random username # for kaboom it will just default to `false` # for serverName name it whatever you like, it will be used as server name in trusted broadcast + # for useCore it just sends the command using chat instead of using core. recommended to enable useChat too when this is enabled # for useChat when the bot sends output (tellraw) it will chat instead of using the core to run tellraw # for hasEssentials it is self-explanatory. if the server has essentials plugin just set it to true + - host: 'localhost' port: 25565 username: 'ChomeNS_Bot' kaboom: false serverName: 'Localhost' + useCore: true useChat: false hasEssentials: false