From 381b3bdc6928d13b97e8c029dddada430523f800 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 16 Mar 2025 08:48:21 +0700 Subject: [PATCH] feat: core resizing (finally !!!) --- build-number.txt | 2 +- .../chayapak1/chomens_bot/Configuration.java | 2 - .../chomens_bot/plugins/CorePlugin.java | 108 ++++++++++++++---- src/main/resources/default-config.yml | 8 -- 4 files changed, 85 insertions(+), 35 deletions(-) diff --git a/build-number.txt b/build-number.txt index a3ace50d..ec2b293e 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1793 \ No newline at end of file +1830 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/Configuration.java b/src/main/java/me/chayapak1/chomens_bot/Configuration.java index f53fa9d2..c994eda0 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Configuration.java +++ b/src/main/java/me/chayapak1/chomens_bot/Configuration.java @@ -75,8 +75,6 @@ public class Configuration { } public static class Core { - public Position start = new Position(); - public Position end = new Position(); public int refillInterval = (60 * 5) * 1000; // 5 minutes public String customName = "{\"text\":\"@\"}"; } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java index 1d8c2a88..05e87d80 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java @@ -48,7 +48,7 @@ public class CorePlugin extends PositionPlugin.Listener { private ScheduledFuture refillTask; public final Vector3i fromSize; - public final Vector3i toSize; + public Vector3i toSize; public Vector3i from; public Vector3i to; @@ -57,6 +57,7 @@ public class CorePlugin extends PositionPlugin.Listener { public final ConcurrentLinkedQueue placeBlockQueue = new ConcurrentLinkedQueue<>(); + private int commandsPerTick = 0; private int commandsPerSecond = 0; private boolean shouldRefill = false; @@ -65,14 +66,14 @@ public class CorePlugin extends PositionPlugin.Listener { this.bot = bot; this.fromSize = Vector3i.from( - bot.config.core.start.x, - bot.config.core.start.y, - bot.config.core.start.z + 0, + -64, + 0 ); this.toSize = Vector3i.from( - bot.config.core.end.x, - bot.config.core.end.y, - bot.config.core.end.z + 15, + -64, + 15 ); bot.position.addListener(this); @@ -89,6 +90,8 @@ public class CorePlugin extends PositionPlugin.Listener { bot.executor.scheduleAtFixedRate(() -> { checkCoreTick(); + resizeTick(); + if (!shouldRefill) return; refill(); @@ -116,6 +119,8 @@ public class CorePlugin extends PositionPlugin.Listener { bot.tick.addListener(new TickPlugin.Listener() { @Override public void onTick() { + if (commandsPerTick > 0) commandsPerTick--; + try { if (placeBlockQueue.size() > 300) { placeBlockQueue.clear(); @@ -147,6 +152,12 @@ public class CorePlugin extends PositionPlugin.Listener { } private void forceRun (String command) { + // if the core isn't ready yet, it is still pretty useless + // to still run the command, even if forced. + if (!ready || command.length() > 32767) return; + + commandsPerTick++; + if (bot.serverPluginsManager.hasPlugin(ServerPluginsManagerPlugin.EXTRAS)) { bot.session.send(new ServerboundSetCommandBlockPacket( block, @@ -311,12 +322,39 @@ public class CorePlugin extends PositionPlugin.Listener { session.send(new ServerboundUseItemOnPacket(temporaryBlockPosition, Direction.UP, Hand.MAIN_HAND, 0.5f, 0.5f, 0.5f, false, false, 1)); } + private void resizeTick () { + if (!ready) return; + + // fixes a bug where the Y positions are more than the ones in toSize + if (to.getY() > toSize.getY() || from.getY() > toSize.getY()) recalculateRelativePositions(); + + final Vector3i oldSize = toSize; + + int x = toSize.getX(); + int y = -64; + int z = toSize.getZ(); + + while (commandsPerTick > (16 * 16)) { + y++; + commandsPerTick -= 16 * 16; + } + + toSize = Vector3i.from(x, y, z); + + if (oldSize.getY() != toSize.getY()) { + recalculateRelativePositions(); + + // this will be run just after this function finishes, since it runs in the same interval + shouldRefill = true; + } + } + public boolean isCoreComplete () { if (!ready) return false; - for (int x = from.getX(); x <= to.getX(); x++) { - for (int y = from.getY(); y <= to.getY(); y++) { - for (int z = from.getZ(); z <= to.getZ(); z++) { + for (int y = from.getY(); y <= to.getY(); y++) { + for (int z = from.getZ(); z <= to.getZ(); z++) { + for (int x = from.getX(); x <= to.getX(); x++) { final int block = bot.world.getBlock(x, y, z); if (!isCommandBlockState(block)) return false; @@ -413,7 +451,7 @@ public class CorePlugin extends PositionPlugin.Listener { refill(); } - public void reset () { + public void recalculateRelativePositions() { final int botChunkPosX = (int) Math.floor(bot.position.position.getX() / 16); final int botChunkPosZ = (int) Math.floor(bot.position.position.getZ() / 16); @@ -428,6 +466,10 @@ public class CorePlugin extends PositionPlugin.Listener { MathUtilities.clamp(toSize.getY(), bot.world.minY, bot.world.maxY), toSize.getZ() + botChunkPosZ * 16 ); + } + + public void reset () { + recalculateRelativePositions(); block = Vector3i.from(from); } @@ -435,25 +477,43 @@ public class CorePlugin extends PositionPlugin.Listener { public void refill () { if (!ready) return; - final String command = String.format( - "minecraft:fill %s %s %s %s %s %s minecraft:command_block{CustomName:'%s'}", + final Map refilledMap = new HashMap<>(); - from.getX(), - from.getY(), - from.getZ(), + for (int y = from.getY(); y <= to.getY(); y++) { + for (int z = from.getZ(); z <= to.getZ(); z++) { + for (int x = from.getX(); x <= to.getX(); x++) { + final int block = bot.world.getBlock(x, y, z); - to.getX(), - to.getY(), - to.getZ(), + final Boolean refilled = refilledMap.get(y); - bot.config.core.customName - ); + if (isCommandBlockState(block) || (refilled != null && refilled)) continue; -// bot.chat.send(command); + final String command = String.format( + "minecraft:fill %s %s %s %s %s %s minecraft:command_block{CustomName:'%s'}", - runPlaceBlock(command); + from.getX(), + y, + from.getZ(), - for (Listener listener : listeners) listener.refilled(); + to.getX(), + y, + to.getZ(), + + bot.config.core.customName + ); + + // bot.chat.send(command); + + runPlaceBlock(command); + + refilledMap.put(y, true); + } + } + } + + if (refilledMap.containsValue(true)) { + for (Listener listener : listeners) listener.refilled(); + } } public static class Listener { diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index a0990095..a9740ea1 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -97,14 +97,6 @@ namespace: 'default_chomens_bot' # useful when you make a clone of the bot teamName: 'default_chomens_bot' # i recommend having this value the same as namespace core: - start: - x: 0 - y: 0 - z: 0 - end: - x: 15 - y: 2 - z: 15 refillInterval: 300000 # (60 * 5) * 1000 (5 minutes) # PLEASE give valid JSON component here else the core don't refill at all customName: '[{"text":"ChomeNS ","color":"yellow"},{"text":"Core","color":"green"},{"text":"™","color":"gold"}]'