diff --git a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java index 24b149e..2fecc17 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java +++ b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java @@ -14,9 +14,11 @@ import lombok.Getter; import land.chipmunk.chipmunkbot.plugins.*; public class ChipmunkBot extends Client { - @Getter public ChatPlugin chat = new ChatPlugin(this); - @Getter public PlayerListPlugin playerList = new PlayerListPlugin(this); - @Getter public CommandManagerPlugin commandManager = new CommandManagerPlugin(this); + @Getter public final ChatPlugin chat = new ChatPlugin(this); + @Getter public final PlayerListPlugin playerList = new PlayerListPlugin(this); + @Getter public final CommandManagerPlugin commandManager = new CommandManagerPlugin(this); + @Getter public final PositionManager position = new PositionManager(this); + @Getter public final CommandCore core = new CommandCore(this); public ChipmunkBot (ClientOptions options) { super(options); } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/data/BlockArea.java b/src/main/java/land/chipmunk/chipmunkbot/data/BlockArea.java new file mode 100644 index 0000000..66ddbc3 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/data/BlockArea.java @@ -0,0 +1,12 @@ +package land.chipmunk.chipmunkbot.data; + +import com.nukkitx.math.vector.Vector3i; +import lombok.Data; +import lombok.AllArgsConstructor; + +@Data +@AllArgsConstructor +public class BlockArea { + private Vector3i start; + private Vector3i end; +} diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java new file mode 100644 index 0000000..4fafbc9 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java @@ -0,0 +1,133 @@ +package land.chipmunk.chipmunkbot.plugins; + +import land.chipmunk.chipmunkbot.ChipmunkBot; +import land.chipmunk.chipmunkbot.plugins.PositionManager; +import land.chipmunk.chipmunkbot.data.BlockArea; +import com.nukkitx.math.vector.Vector3i; +import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.packetlib.Session; +import com.github.steveice10.packetlib.event.session.SessionListener; +import com.github.steveice10.packetlib.event.session.SessionAdapter; +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCreativeModeSlotPacket; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCommandBlockPacket; +// import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundBlockEntityTagQuery; +import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; +import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; +import com.github.steveice10.mc.protocol.data.game.entity.object.Direction; +import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction; +import com.github.steveice10.mc.protocol.data.game.level.block.CommandBlockMode; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; +import com.github.steveice10.opennbt.tag.builtin.ByteTag; +import com.google.gson.JsonObject; +import lombok.Getter; +import lombok.Setter; + +public class CommandCore extends SessionAdapter { + private ChipmunkBot client; + private Vector3i origin; + // TODO: Make it configurable + @Getter private final BlockArea relativeArea = new BlockArea(Vector3i.from(0, 0, 0), Vector3i.from(15, 0, 15)); + @Getter @Setter private Vector3i currentBlockRelative; + + public CommandCore (ChipmunkBot client) { + this.client = client; + client.session().addListener((SessionListener) this); + } + + @Override + public void packetReceived (Session session, Packet packet) { + if (packet instanceof ClientboundPlayerPositionPacket) packetReceived(session, (ClientboundPlayerPositionPacket) packet); + } + + public void packetReceived (Session session, ClientboundPlayerPositionPacket packet) { + origin = Vector3i.from( + ((int) packet.getX() / 16) * 16, + 0, // TODO: Use the actual bottom of the world instead of hardcoding to 0 + ((int) packet.getZ() / 16) * 16 + ); + + if (currentBlockRelative == null) currentBlockRelative = Vector3i.from(relativeArea.start()); + refill(); + } + + public void refill () { + // final PositionManager position = client.position(); + final Vector3i relStart = relativeArea.start(); + final Vector3i relEnd = relativeArea.end(); + + final String command = String.format( + "fill %s %s %s %s %s %s minecraft:chain_command_block", + relStart.getX() + origin.getX(), + relStart.getY() + origin.getY(), + relStart.getZ() + origin.getZ(), + + relEnd.getX() + origin.getX(), + relEnd.getY() + origin.getY(), + relEnd.getZ() + origin.getZ() + ); + + // TODO: Figure out how to use OpenNBT + /* CompoundTag itemTag = new CompoundTag("tag"); + CompoundTag blockEntityTag = new CompoundTag("BlockEntityTag"); + blockEntityTag.getValue().put("Command", new StringTag("Command", command)); + blockEntityTag.getValue().put("auto", new ByteTag("auto", (byte) 1)); + itemTag.getValue().put("BlockEntityTag", blockEntityTag); + + Vector3i temporaryBlockPosition = Vector3i.from((int) position.x(), (int) position.y() - 1, (int) position.z()); */ + + client.chat().command(command); + + // final Session session = client.session(); + // session.send(new ServerboundSetCreativeModeSlotPacket(45, new ItemStack(347 /* command_block */, 1, itemTag))); + // session.send(new ServerboundPlayerActionPacket(PlayerAction.START_DIGGING, temporaryBlockPosition, Direction.NORTH, 0)); + // session.send(new ServerboundUseItemOnPacket(temporaryBlockPosition, Direction.NORTH, Hand.OFF_HAND, 0.5f, 0.5f, 0.5f, false, 0)); + } + + public void incrementCurrentBlock () { + final Vector3i start = relativeArea.start(); + final Vector3i end = relativeArea.end(); + + int x = currentBlockRelative.getX(); + int y = currentBlockRelative.getY(); + int z = currentBlockRelative.getZ(); + + x++; + + if (x > end.getX()) { + x = start.getX(); + z++; + } + + if (z > end.getZ()) { + z = start.getZ(); + y++; + } + + if (y > end.getY()) { + x = start.getX(); + y = start.getY(); + z = start.getZ(); + } + + currentBlockRelative = Vector3i.from(x, y, z); + } + + public Vector3i currentBlockAbsolute () { + return currentBlockRelative.add(origin); + } + + public void run (String command) { + final Session session = client.session(); + final Vector3i currentBlock = currentBlockAbsolute(); + + // TODO: Support using repeating command blocks (on kaboom-like servers) (because less packets) + session.send(new ServerboundSetCommandBlockPacket(currentBlock, "", CommandBlockMode.SEQUENCE, false, false, false)); + session.send(new ServerboundSetCommandBlockPacket(currentBlock, command, CommandBlockMode.REDSTONE, false, false, true)); + + incrementCurrentBlock(); + } +} diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/PositionManager.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/PositionManager.java new file mode 100644 index 0000000..fd02937 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/PositionManager.java @@ -0,0 +1,48 @@ +package land.chipmunk.chipmunkbot.plugins; + +import land.chipmunk.chipmunkbot.Client; +import com.nukkitx.math.vector.Vector3d; +import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.packetlib.Session; +import com.github.steveice10.packetlib.event.session.SessionListener; +import com.github.steveice10.packetlib.event.session.SessionAdapter; +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket; +import com.nukkitx.math.vector.Vector3d; +import com.nukkitx.math.vector.Vector3i; +import lombok.Getter; +import lombok.Setter; + +public class PositionManager extends SessionAdapter { + private final Client client; + + @Getter @Setter double x; + @Getter @Setter double y; + @Getter @Setter double z; + @Getter @Setter float yaw; + @Getter @Setter float pitch; + + public PositionManager (Client client) { + this.client = client; + client.session().addListener((SessionListener) this); + } + + @Override + public void packetReceived (Session session, Packet packet) { + if (packet instanceof ClientboundPlayerPositionPacket) packetReceived((ClientboundPlayerPositionPacket) packet, session); + } + + public void packetReceived (ClientboundPlayerPositionPacket packet, Session session) { + // TODO: Relative positions + x = packet.getX(); + y = packet.getY(); + z = packet.getZ(); + yaw = packet.getYaw(); + pitch = packet.getPitch(); + + client.session().send(new ServerboundAcceptTeleportationPacket(packet.getTeleportId())); + } + + public Vector3d vector3d () { return Vector3d.from(x, y, z); } + public Vector3i vector3i () { return Vector3i.from((int) x, (int) y, (int) z); } +}