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 47e8b8ee..cd8707dc 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Configuration.java @@ -43,7 +43,7 @@ public class Configuration { @Getter public Position start = new Position(); @Getter public Position end = new Position(); @Getter public int refillInterval = (60 * 5) * 1000; // 5 minutes - @Getter public String customName = "[{\"text\":\"ChomeNS \",\"color\":\"yellow\"},{\"text\":\"Core\",\"color\":\"green\"},{\"text\":\"™\",\"color\":\"gold\"}]"; + @Getter public String customName = "{\"text\":\"@\"}"; } public static class Position { @@ -122,5 +122,11 @@ public class Configuration { @Getter public int reconnectDelay = 2000; @Getter public boolean removeNamespaces = false; @Getter public int chatQueueDelay = 125; + @Getter public CoreRateLimit coreRateLimit = new CoreRateLimit(); + + public static class CoreRateLimit { + @Getter public int limit = 0; + @Getter public int reset = 0; + } } } 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 da8a43ba..6ab0780c 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 @@ -210,16 +210,7 @@ public class BossbarManagerPlugin extends Bot.Listener { if (bossBar.id != null && bossBar.id.equals(bossBarPrefix + name)) return bossBars.get(bossBar.uuid); } - return new BotBossBar( - Component.empty(), - "@a", - BossBarColor.WHITE, - BossBarDivision.NONE, - false, - 0, - 0, - bot - ); + return null; } public BotBossBar get (UUID uuid) { @@ -227,15 +218,6 @@ public class BossbarManagerPlugin extends Bot.Listener { if (bossBar.getValue().uuid == uuid) return bossBar.getValue(); } - return new BotBossBar( - Component.empty(), - "@a", - BossBarColor.WHITE, - BossBarDivision.NONE, - false, - 0, - 0, - bot - ); + return null; } } 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 9b59a582..41cdb525 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,7 +6,6 @@ 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; @@ -56,7 +55,7 @@ public class CorePlugin extends PositionPlugin.Listener { private final boolean kaboom; - @Getter private final List queue = new ArrayList<>(); + @Getter private int commandsPerSecond = 0; public CorePlugin (Bot bot) { this.bot = bot; @@ -77,14 +76,14 @@ 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); + if (bot.options().coreRateLimit().limit() != 0 && bot.options().coreRateLimit().reset() != 0) { + bot.executor().scheduleAtFixedRate( + () -> commandsPerSecond = 0, + 0, + bot.options().coreRateLimit().reset(), + TimeUnit.MILLISECONDS + ); + } bot.addListener(new Bot.Listener() { @Override @@ -106,16 +105,6 @@ 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(), @@ -133,8 +122,10 @@ public class CorePlugin extends PositionPlugin.Listener { if (!ready) return; if (bot.options().useCore()) { - if (commandsPerSecond >= 100) queue.add(command); - else forceRun(command); + if (bot.options().coreRateLimit().limit() != 0 && commandsPerSecond > bot.options().coreRateLimit().limit()) return; + + forceRun(command); + commandsPerSecond++; } else if (command.length() < 256) { bot.chat().send("/" + command); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java index 4b020f16..16d1f48c 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java @@ -16,7 +16,6 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.io.File; import java.net.URL; import java.nio.file.Path; -import java.text.DecimalFormat; import java.time.Instant; import java.util.ArrayList; import java.util.List; @@ -114,16 +113,22 @@ public class MusicPlayerPlugin extends Bot.Listener { currentSong.play(); } + if ( + bot.options().coreRateLimit().limit() != 0 && + bot.core().commandsPerSecond() > bot.options().coreRateLimit().limit() + ) currentSong.pause(); + else currentSong.play(); + if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return; else ticksUntilPausedBossbar = 20 - (((int) bot.tps().getTickRate()) - 20); BotBossBar bossBar = bot.bossbar().get(bossbarName); - if (bossBar == null) bossBar = addBossBar(); + if (bossBar == null && bot.bossbar().enabled()) bossBar = addBossBar(); final long currentTime = Instant.now().toEpochMilli(); - if (currentTime >= nextBossBarUpdate) { + if (currentTime >= nextBossBarUpdate && bossBar != null) { bossBar.setTitle(generateBossbar()); bossBar.setColor(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW); bossBar.setValue((int) Math.floor(currentSong.time * speed)); @@ -214,20 +219,10 @@ public class MusicPlayerPlugin extends Bot.Listener { } public Component generateBossbar () { - final DecimalFormat formatter = new DecimalFormat("#,###"); - Component component = Component.empty() .append(Component.empty().append(currentSong.name).color(pitch > 0 ? NamedTextColor.LIGHT_PURPLE : NamedTextColor.GREEN)) .append(Component.text(" | ").color(NamedTextColor.DARK_GRAY)) - .append(Component.translatable("%s / %s", formatTime((long) (currentSong.time * speed)).color(NamedTextColor.GRAY), formatTime(currentSong.length).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY)) - .append(Component.text(" | ").color(NamedTextColor.DARK_GRAY)) - .append( - Component.translatable( - "%s / %s", - Component.text(formatter.format(currentSong.position), NamedTextColor.GRAY), - Component.text(formatter.format(currentSong.size()), NamedTextColor.GRAY) - ).color(NamedTextColor.DARK_GRAY) - ); + .append(Component.translatable("%s / %s", formatTime((long) (currentSong.time * speed)).color(NamedTextColor.GRAY), formatTime(currentSong.length).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY)); if (currentSong.paused) { return component 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 93155bc9..59d093df 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 @@ -7,6 +7,7 @@ import land.chipmunk.chayapak.chomens_bot.Main; import java.io.*; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; public class PersistentDataUtilities { @@ -18,6 +19,8 @@ public class PersistentDataUtilities { private static final ScheduledExecutorService executor = Main.executor; + private static ScheduledFuture future = null; + static { init(); } @@ -41,11 +44,21 @@ public class PersistentDataUtilities { e.printStackTrace(); } - executor.scheduleAtFixedRate(PersistentDataUtilities::write, 0, 100, TimeUnit.MILLISECONDS); + future = executor.scheduleAtFixedRate(PersistentDataUtilities::write, 0, 100, TimeUnit.MILLISECONDS); + + Runtime.getRuntime().addShutdownHook( + new Thread(() -> { + future.cancel(true); + + write(); + }) + ); } private static void write () { try { + writer.close(); + writer = new FileWriter(file, false); writer.write(jsonObject.toString()); diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index 67defb7b..92dab1e0 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -90,6 +90,7 @@ bots: # useChat - when the bot tellraws it will chat instead of using the core to run tellraw # hasEssentials - if the server has essentials plugin just set it to true!! ! !!31 # removeNamespaces - when the bot sends a command it will remove like `minecraft:` for example if set to true + # coreRateLimit - will ignore commands if reached the ratelimit - host: 'localhost' port: 25565 @@ -103,6 +104,9 @@ bots: reconnectDelay: 2000 removeNamespaces: false chatQueueDelay: 125 + coreRateLimit: + limit: 10 + reset: 1000 # in milliseconds # or without the optional ones: # - host: 'localhost' # port: 25565