From 0ddf042311272efd7c4023294ef4ddc14364301f Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 2 Nov 2024 19:27:08 +0700 Subject: [PATCH] feat: automatically make the bot go down if currentY > maxY --- .../chomens_bot/plugins/CorePlugin.java | 2 + .../chomens_bot/plugins/PositionPlugin.java | 76 ++++++++++++++++--- 2 files changed, 69 insertions(+), 9 deletions(-) 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 2bceb96..b13524d 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java @@ -374,6 +374,8 @@ public class CorePlugin extends PositionPlugin.Listener { @Override public void positionChange (Vector3i position) { + if (bot.position.isGoingDownFromHeightLimit) return; + from = Vector3i.from( (int) (fromSize.getX() + Math.floor((double) bot.position.position.getX() / 16) * 16), MathUtilities.clamp(fromSize.getY(), bot.world.minY, bot.world.maxY), diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PositionPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PositionPlugin.java index fb5b151..6564f33 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PositionPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PositionPlugin.java @@ -30,9 +30,11 @@ public class PositionPlugin extends Bot.Listener { public Vector3i position = Vector3i.from(0, 0, 0); - public final Map entityIdMap = new HashMap<>(); - public final Map positionMap = new HashMap<>(); - public final Map rotationMap = new HashMap<>(); + public boolean isGoingDownFromHeightLimit = false; // cool variable name + + private final Map entityIdMap = new HashMap<>(); + private final Map positionMap = new HashMap<>(); + private final Map rotationMap = new HashMap<>(); public PositionPlugin (Bot bot) { this.bot = bot; @@ -40,12 +42,23 @@ public class PositionPlugin extends Bot.Listener { bot.addListener(this); // notchian clients also does this, sends the position packet every second - bot.executor.scheduleAtFixedRate(() -> bot.session.send(new ServerboundMovePlayerPosPacket( - false, - position.getX(), - position.getY(), - position.getZ() - )), 0, 1, TimeUnit.SECONDS); + bot.executor.scheduleAtFixedRate(() -> { + if (isGoingDownFromHeightLimit) return; + + bot.session.send(new ServerboundMovePlayerPosPacket( + false, + position.getX(), + position.getY(), + position.getZ() + )); + }, 0, 1, TimeUnit.SECONDS); + + bot.tick.addListener(new TickPlugin.Listener() { + @Override + public void onTick() { + handleHeightLimit(); + } + }); } @Override @@ -145,6 +158,51 @@ public class PositionPlugin extends Bot.Listener { for (Listener listener : listeners) listener.playerMoved(player, position, rotation); } + // for now this is used in CorePlugin when placing the command block + private void handleHeightLimit () { + final int y = position.getY(); + final int maxY = bot.world.maxY; + + if (y < maxY) { + if (isGoingDownFromHeightLimit) { + isGoingDownFromHeightLimit = false; + + for (Listener listener : listeners) { listener.positionChange(position); } + } + + return; + } + + isGoingDownFromHeightLimit = true; + + final Vector3i newPosition = Vector3i.from( + position.getX(), + position.getY() - 2, + position.getZ() + ); + + position = newPosition; + + if (position.getY() > maxY + 500) { + String command = "/"; + + if (bot.serverPluginsManager.hasPlugin(ServerPluginsManagerPlugin.ESSENTIALS)) command += "essentials:"; + + command += String.format("tp %s %s %s", position.getX(), maxY - 1, position.getZ()); + + bot.chat.send(command); + + return; + } + + bot.session.send(new ServerboundMovePlayerPosPacket( + false, + newPosition.getX(), + newPosition.getY(), + newPosition.getZ() + )); + } + public Vector3f getPlayerPosition (String playerName) { int entityId = -1; for (Map.Entry entry : entityIdMap.entrySet()) {