From 44410bfcbf9dd51c5d09eba1d314a6af9765bd85 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 25 Jun 2023 11:06:14 +0700 Subject: [PATCH] i am not sure if this works --- .../plugins/BossbarManagerPlugin.java | 1 - .../chomens_bot/plugins/CorePlugin.java | 47 +++++++++++--- .../util/PersistentDataUtilities.java | 63 ++++++------------- 3 files changed, 55 insertions(+), 56 deletions(-) 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 26a46349..da8a43ba 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 @@ -19,7 +19,6 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -// TODO: players can make the bossbar with the same exact component and fard the bossbar manager so mabe fix it // yes this has been rewritten to be not spammy public class BossbarManagerPlugin extends Bot.Listener { private final Bot bot; 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 9b56d437..9b59a582 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 @@ -6,6 +6,7 @@ import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction; import com.github.steveice10.mc.protocol.data.game.level.block.BlockChangeEntry; import com.github.steveice10.mc.protocol.data.game.level.block.CommandBlockMode; +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundCommandsPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundBlockUpdatePacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundLevelChunkWithLightPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundSectionBlocksUpdatePacket; @@ -55,6 +56,8 @@ public class CorePlugin extends PositionPlugin.Listener { private final boolean kaboom; + @Getter private final List queue = new ArrayList<>(); + public CorePlugin (Bot bot) { this.bot = bot; this.kaboom = bot.options().kaboom(); @@ -74,6 +77,15 @@ public class CorePlugin extends PositionPlugin.Listener { bot.position().addListener(this); + bot.tick().addListener(new TickPlugin.Listener() { + @Override + public void onTick() { + CorePlugin.this.onTick(); + } + }); + + bot.executor().scheduleAtFixedRate(() -> commandsPerSecond = 0, 0, 1, TimeUnit.SECONDS); + bot.addListener(new Bot.Listener() { @Override public void disconnected (DisconnectedEvent event) { @@ -94,20 +106,35 @@ public class CorePlugin extends PositionPlugin.Listener { }); } + private int commandsPerSecond = 0; + + private void onTick () { + if (queue.isEmpty()) return; + + forceRun(queue.get(0)); + + queue.remove(0); + } + + private void forceRun (String command) { + bot.session().send(new ServerboundSetCommandBlockPacket( + absoluteCorePosition(), + command, + kaboom ? CommandBlockMode.AUTO : CommandBlockMode.REDSTONE, + true, + false, + true + )); + + incrementBlock(); + } + public void run (String command) { if (!ready) return; if (bot.options().useCore()) { - bot.session().send(new ServerboundSetCommandBlockPacket( - absoluteCorePosition(), - command, - kaboom ? CommandBlockMode.AUTO : CommandBlockMode.REDSTONE, - true, - false, - true - )); - - incrementBlock(); + if (commandsPerSecond >= 100) queue.add(command); + else forceRun(command); } else if (command.length() < 256) { bot.chat().send("/" + command); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java index 7c1cfd98..93155bc9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java @@ -3,8 +3,11 @@ package land.chipmunk.chayapak.chomens_bot.util; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import land.chipmunk.chayapak.chomens_bot.Main; import java.io.*; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class PersistentDataUtilities { public static File file = new File("persistent.json"); @@ -13,6 +16,8 @@ public class PersistentDataUtilities { public static JsonObject jsonObject = new JsonObject(); + private static final ScheduledExecutorService executor = Main.executor; + static { init(); } @@ -35,75 +40,43 @@ public class PersistentDataUtilities { } catch (IOException e) { e.printStackTrace(); } + + executor.scheduleAtFixedRate(PersistentDataUtilities::write, 0, 100, TimeUnit.MILLISECONDS); + } + + private static void write () { + try { + writer = new FileWriter(file, false); + + writer.write(jsonObject.toString()); + writer.flush(); + } catch (IOException e) { + e.printStackTrace(); + } } public static synchronized void put (String property, JsonElement value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.add(property, value); - - try { - writer = new FileWriter(file, false); - - writer.write(jsonObject.toString()); - writer.flush(); - } catch (IOException e) { - e.printStackTrace(); - } } public static synchronized void put (String property, String value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - - try { - writer = new FileWriter(file, false); - - writer.write(jsonObject.toString()); - writer.flush(); - } catch (IOException e) { - e.printStackTrace(); - } } public static synchronized void put (String property, boolean value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - - try { - writer = new FileWriter(file, false); - - writer.write(jsonObject.toString()); - writer.flush(); - } catch (IOException e) { - e.printStackTrace(); - } } public static synchronized void put (String property, int value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - - try { - writer = new FileWriter(file, false); - - writer.write(jsonObject.toString()); - writer.flush(); - } catch (IOException e) { - e.printStackTrace(); - } } public static synchronized void put (String property, char value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - - try { - writer = new FileWriter(file, false); - - writer.write(jsonObject.toString()); - writer.flush(); - } catch (IOException e) { - e.printStackTrace(); - } } }