diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java b/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java index cdf9c53..d8b1f97 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java @@ -4,6 +4,7 @@ import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.data.BlockArea; import land.chipmunk.chipmunkmod.listeners.Listener; import land.chipmunk.chipmunkmod.listeners.ListenerManager; +import land.chipmunk.chipmunkmod.util.MathUtilities; import net.minecraft.block.entity.CommandBlockBlockEntity; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; @@ -28,11 +29,11 @@ public class CommandCore { public boolean runFillCommand = true; - public static CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance(), ChipmunkMod.CONFIG.core.relativeArea); + public static CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance()); - public CommandCore (MinecraftClient client, BlockArea relativeArea) { + public CommandCore (MinecraftClient client) { this.client = client; - this.relativeArea = relativeArea; + reloadRelativeArea(); } public void init () { @@ -56,20 +57,37 @@ public class CommandCore { final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); if (networkHandler == null) cleanup(); + + reloadRelativeArea(); } public void reloadRelativeArea () { - relativeArea = ChipmunkMod.CONFIG.core.relativeArea; + // makes a copy + relativeArea = new BlockArea( + new BlockPos(ChipmunkMod.CONFIG.core.relativeArea.start), + new BlockPos(ChipmunkMod.CONFIG.core.relativeArea.end) + ); + + relativeArea.start = new BlockPos( + relativeArea.start.getX(), + (int) MathUtilities.clamp(relativeArea.start.getY(), client.world.getDimension().minY(), client.world.getDimension().height()), + relativeArea.start.getZ() + ); + relativeArea.end = new BlockPos( + relativeArea.end.getX(), + (int) MathUtilities.clamp(relativeArea.end.getY(), client.world.getDimension().minY(), client.world.getDimension().height()), + relativeArea.end.getZ() + ); } public void move (Vec3d position) { origin = new BlockPos( ((int) position.getX() / 16) * 16, - 0, // TODO: Use the actual bottom of the world instead of hardcoding to 0 + 0, ((int) position.getZ() / 16) * 16 ); - if (currentBlockRelative == null) currentBlockRelative = new BlockPos(relativeArea.start); + currentBlockRelative = new BlockPos(relativeArea.start); refill(); for (Listener listener : ListenerManager.listeners) listener.coreMoved(); diff --git a/src/main/java/land/chipmunk/chipmunkmod/util/MathUtilities.java b/src/main/java/land/chipmunk/chipmunkmod/util/MathUtilities.java index 37cd5c2..981e98e 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/util/MathUtilities.java +++ b/src/main/java/land/chipmunk/chipmunkmod/util/MathUtilities.java @@ -1,6 +1,10 @@ package land.chipmunk.chipmunkmod.util; public class MathUtilities { + public static double clamp (double value, double min, double 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); }