From 7a3b5f4baf488ad2ca72fd2410ee8e3438b6ed3a Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 19 Aug 2023 15:52:30 +0700 Subject: [PATCH] add world min y and max y check + clamp the core y to min y and max y --- .../chipmunk/chayapak/chomens_bot/Bot.java | 10 +-- .../chomens_bot/plugins/CorePlugin.java | 5 +- .../chomens_bot/plugins/WorldPlugin.java | 64 +++++++++++++++++++ .../chomens_bot/util/MathUtilities.java | 4 ++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WorldPlugin.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index fcf4eb3..90b1642 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -53,6 +53,8 @@ public class Bot { public ChatPlugin chat; public CommandSpyPlugin commandSpy; public PositionPlugin position; + public ServerPluginsManagerPlugin serverPluginsManager; + public PluginCheckerPlugin pluginChecker; public SelfCarePlugin selfCare; public CorePlugin core; public TeamPlugin team; @@ -75,8 +77,7 @@ public class Bot { public PacketSnifferPlugin packetSniffer; public VoiceChatPlugin voiceChat; public TagPlugin tag; - public ServerPluginsManagerPlugin serverPluginsManager; - public PluginCheckerPlugin pluginChecker; + public WorldPlugin world; public Bot (Configuration.BotOption botOption, List bots, Configuration config) { this.host = botOption.host; @@ -101,6 +102,8 @@ public class Bot { this.chat = new ChatPlugin(this); this.commandSpy = new CommandSpyPlugin(this); this.position = new PositionPlugin(this); + this.serverPluginsManager = new ServerPluginsManagerPlugin(this); + this.pluginChecker = new PluginCheckerPlugin(this); this.selfCare = new SelfCarePlugin(this); this.core = new CorePlugin(this); this.team = new TeamPlugin(this); @@ -123,8 +126,7 @@ public class Bot { this.packetSniffer = new PacketSnifferPlugin(this); this.voiceChat = new VoiceChatPlugin(this); this.tag = new TagPlugin(this); - this.serverPluginsManager = new ServerPluginsManagerPlugin(this); - this.pluginChecker = new PluginCheckerPlugin(this); + this.world = new WorldPlugin(this); for (Listener listener : listeners) listener.loadedPlugins(); 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 fbdcfdd..d64c56b 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 @@ -22,6 +22,7 @@ import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.util.MathUtilities; import org.cloudburstmc.math.vector.Vector3i; import java.util.ArrayList; @@ -330,11 +331,11 @@ public class CorePlugin extends PositionPlugin.Listener { "minecraft:fill %s %s %s %s %s %s minecraft:command_block{CustomName:'%s'}", from.getX(), - from.getY(), + MathUtilities.clamp(from.getY(), bot.world.minY, bot.world.maxY), from.getZ(), to.getX(), - to.getY(), + MathUtilities.clamp(to.getY(), bot.world.minY, bot.world.maxY), to.getZ(), bot.config.core.customName diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WorldPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WorldPlugin.java new file mode 100644 index 0000000..f64d9cd --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WorldPlugin.java @@ -0,0 +1,64 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket; +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundRespawnPacket; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.Tag; +import com.github.steveice10.packetlib.Session; +import com.github.steveice10.packetlib.packet.Packet; +import land.chipmunk.chayapak.chomens_bot.Bot; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +public class WorldPlugin extends Bot.Listener { + public int minY = 0; + public int maxY = 256; + + public CompoundTag registry = null; + + private final List listeners = new ArrayList<>(); + + public WorldPlugin (Bot bot) { + bot.addListener(this); + } + + @Override + public void packetReceived(Session session, Packet packet) { + if (packet instanceof ClientboundLoginPacket) packetReceived((ClientboundLoginPacket) packet); + else if (packet instanceof ClientboundRespawnPacket) packetReceived((ClientboundRespawnPacket) packet); + } + + @SuppressWarnings("unchecked") + private void worldChanged (String dimension) { + final Tag dimensionType = ((LinkedHashMap) registry.get("minecraft:dimension_type").getValue()).get("value"); + + final ArrayList dimensions = (ArrayList) dimensionType.getValue(); + + final CompoundTag currentDimension = dimensions.stream() + .filter((eachDimension) -> eachDimension.get("name").getValue().equals(dimension)) + .toArray(CompoundTag[]::new)[0]; + + final CompoundTag element = currentDimension.get("element"); + + minY = (int) element.get("min_y").getValue(); + maxY = (int) element.get("height").getValue(); + + for (Listener listener : listeners) listener.worldChanged(dimension); + } + + public void packetReceived (ClientboundLoginPacket packet) { + registry = packet.getRegistry(); + + worldChanged(packet.getDimension()); + } + + public void packetReceived (ClientboundRespawnPacket packet) { + worldChanged(packet.getDimension()); + } + + public static class Listener { + public void worldChanged (String dimension) {} + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/MathUtilities.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/MathUtilities.java index bdd71d2..8402314 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/MathUtilities.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/MathUtilities.java @@ -7,6 +7,10 @@ public class MathUtilities { ); } + public static int clamp (int value, int min, int max) { + return Math.max(Math.min(value, max), min); + } + public static float clamp (float value, float min, float max) { return Math.max(Math.min(value, max), min); }