feat: automatically make the bot go down if currentY > maxY

This commit is contained in:
Chayapak 2024-11-02 19:27:08 +07:00
parent 4764a8947e
commit 0ddf042311
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
2 changed files with 69 additions and 9 deletions

View file

@ -374,6 +374,8 @@ public class CorePlugin extends PositionPlugin.Listener {
@Override @Override
public void positionChange (Vector3i position) { public void positionChange (Vector3i position) {
if (bot.position.isGoingDownFromHeightLimit) return;
from = Vector3i.from( from = Vector3i.from(
(int) (fromSize.getX() + Math.floor((double) bot.position.position.getX() / 16) * 16), (int) (fromSize.getX() + Math.floor((double) bot.position.position.getX() / 16) * 16),
MathUtilities.clamp(fromSize.getY(), bot.world.minY, bot.world.maxY), MathUtilities.clamp(fromSize.getY(), bot.world.minY, bot.world.maxY),

View file

@ -30,9 +30,11 @@ public class PositionPlugin extends Bot.Listener {
public Vector3i position = Vector3i.from(0, 0, 0); public Vector3i position = Vector3i.from(0, 0, 0);
public final Map<Integer, PlayerEntry> entityIdMap = new HashMap<>(); public boolean isGoingDownFromHeightLimit = false; // cool variable name
public final Map<Integer, Vector3f> positionMap = new HashMap<>();
public final Map<Integer, Rotation> rotationMap = new HashMap<>(); private final Map<Integer, PlayerEntry> entityIdMap = new HashMap<>();
private final Map<Integer, Vector3f> positionMap = new HashMap<>();
private final Map<Integer, Rotation> rotationMap = new HashMap<>();
public PositionPlugin (Bot bot) { public PositionPlugin (Bot bot) {
this.bot = bot; this.bot = bot;
@ -40,12 +42,23 @@ public class PositionPlugin extends Bot.Listener {
bot.addListener(this); bot.addListener(this);
// notchian clients also does this, sends the position packet every second // notchian clients also does this, sends the position packet every second
bot.executor.scheduleAtFixedRate(() -> bot.session.send(new ServerboundMovePlayerPosPacket( bot.executor.scheduleAtFixedRate(() -> {
false, if (isGoingDownFromHeightLimit) return;
position.getX(),
position.getY(), bot.session.send(new ServerboundMovePlayerPosPacket(
position.getZ() false,
)), 0, 1, TimeUnit.SECONDS); position.getX(),
position.getY(),
position.getZ()
));
}, 0, 1, TimeUnit.SECONDS);
bot.tick.addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
handleHeightLimit();
}
});
} }
@Override @Override
@ -145,6 +158,51 @@ public class PositionPlugin extends Bot.Listener {
for (Listener listener : listeners) listener.playerMoved(player, position, rotation); 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) { public Vector3f getPlayerPosition (String playerName) {
int entityId = -1; int entityId = -1;
for (Map.Entry<Integer, PlayerEntry> entry : entityIdMap.entrySet()) { for (Map.Entry<Integer, PlayerEntry> entry : entityIdMap.entrySet()) {