diff --git a/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/BlockPos.java b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/BlockPos.java new file mode 100644 index 00000000..f8d6140f --- /dev/null +++ b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/BlockPos.java @@ -0,0 +1,33 @@ +package com.tisawesomeness.minecord.mc.pos; + +import com.tisawesomeness.minecord.util.Mth; + +public class BlockPos extends Vec3i { + + // Max distance from origin nether portals can generate, 128 blocks from max border + public static final int MAX_PORTAL_DISTANCE = 29_999_872; + + public BlockPos(int x, int y, int z) { + super(x, y, z); + } + public BlockPos(Vec3i vec) { + super(vec.getX(), vec.getY(), vec.getZ()); + } + + public BlockPos overworldToNether() { + int netherY = Mth.clamp(y, 0, 127); + return new BlockPos(horizontal().floorDiv(8).withY(netherY)); + } + public BlockPos netherToOverworld() { + int overworldX = Mth.clamp(x * 8, -MAX_PORTAL_DISTANCE, MAX_PORTAL_DISTANCE); + int overworldZ = Mth.clamp(z * 8, -MAX_PORTAL_DISTANCE, MAX_PORTAL_DISTANCE); + return new BlockPos(overworldX, y, overworldZ); + } + + public SectionPos getSection() { + return new SectionPos(floorDiv(16)); + } + public Vec3i getPosWithinSection() { + return floorMod(16); + } +} diff --git a/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/SectionPos.java b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/SectionPos.java new file mode 100644 index 00000000..e439cf59 --- /dev/null +++ b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/SectionPos.java @@ -0,0 +1,18 @@ +package com.tisawesomeness.minecord.mc.pos; + +public class SectionPos extends Vec3i { + public SectionPos(int x, int y, int z) { + super(x, y, z); + } + public SectionPos(Vec3i vec) { + super(vec.getX(), vec.getY(), vec.getZ()); + } + + public String getRegionFileName() { + Vec2i regionVec = horizontal().floorDiv(32); + return String.format("r.%d.%d.mca", regionVec.getX(), regionVec.getZ()); + } + public Vec2i getPosWithinRegion() { + return horizontal().floorMod(32); + } +} diff --git a/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/Vec2i.java b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/Vec2i.java new file mode 100644 index 00000000..4327609a --- /dev/null +++ b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/Vec2i.java @@ -0,0 +1,29 @@ +package com.tisawesomeness.minecord.mc.pos; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +@Getter +@AllArgsConstructor +@EqualsAndHashCode +public class Vec2i { + protected final int x; + protected final int z; + + public Vec2i floorDiv(int n) { + return new Vec2i(Math.floorDiv(x, n), Math.floorDiv(z, n)); + } + public Vec2i floorMod(int n) { + return new Vec2i(Math.floorMod(x, n), Math.floorMod(z, n)); + } + + public Vec3i withY(int y) { + return new Vec3i(x, y, z); + } + + @Override + public String toString() { + return x + ", " + z; + } +} diff --git a/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/Vec3i.java b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/Vec3i.java new file mode 100644 index 00000000..9b8bb1af --- /dev/null +++ b/minecord-bot/src/main/java/com/tisawesomeness/minecord/mc/pos/Vec3i.java @@ -0,0 +1,30 @@ +package com.tisawesomeness.minecord.mc.pos; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +@Getter +@AllArgsConstructor +@EqualsAndHashCode +public class Vec3i { + protected final int x; + protected final int y; + protected final int z; + + public Vec3i floorDiv(int n) { + return new Vec3i(Math.floorDiv(x, n), Math.floorDiv(y, n), Math.floorDiv(z, n)); + } + public Vec3i floorMod(int n) { + return new Vec3i(Math.floorMod(x, n), Math.floorMod(y, n), Math.floorMod(z, n)); + } + + public Vec2i horizontal() { + return new Vec2i(x, z); + } + + @Override + public String toString() { + return x + ", " + y + ", " + z; + } +} diff --git a/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/BlockPosTest.java b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/BlockPosTest.java new file mode 100644 index 00000000..2b9b8edc --- /dev/null +++ b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/BlockPosTest.java @@ -0,0 +1,74 @@ +package com.tisawesomeness.minecord.mc.pos; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BlockPosTest { + + @Test + public void testOverworldToNether() { + assertThat(new BlockPos(1872, 62, -5661).overworldToNether()) + .isEqualTo(new BlockPos(234, 62, -708)); + } + @Test + public void testOverworldToNetherZero() { + assertThat(new BlockPos(0, 0, 0).overworldToNether()) + .isEqualTo(new BlockPos(0, 0, 0)); + } + @Test + public void testOverworldToNetherLimit() { + assertThat(new BlockPos(0, -16, 0).overworldToNether()) + .isEqualTo(new BlockPos(0, 0, 0)); + } + @Test + public void testOverworldToNetherLimit2() { + assertThat(new BlockPos(0, 306, 0).overworldToNether()) + .isEqualTo(new BlockPos(0, 127, 0)); + } + + @Test + public void testNetherToOverworld() { + assertThat(new BlockPos(1872, 62, -5661).netherToOverworld()) + .isEqualTo(new BlockPos(14976, 62, -45288)); + } + @Test + public void testNetherToOverworldZero() { + assertThat(new BlockPos(0, 0, 0).netherToOverworld()) + .isEqualTo(new BlockPos(0, 0, 0)); + } + @Test + public void testNetherToOverworldLimit() { + assertThat(new BlockPos(10_000_000, 64, -10_000_000).netherToOverworld()) + .isEqualTo(new BlockPos(BlockPos.MAX_PORTAL_DISTANCE, 64, -BlockPos.MAX_PORTAL_DISTANCE)); + } + + @Test + public void testSection() { + assertThat(new BlockPos(-115, 60, -30).getSection()) + .isEqualTo(new SectionPos(-8, 3, -2)); + } + @Test + public void testSectionZero() { + assertThat(new BlockPos(0, 0, 0).getSection()) + .isEqualTo(new SectionPos(0, 0, 0)); + } + + @Test + public void testWithinSection() { + assertThat(new BlockPos(-115, 60, -30).getPosWithinSection()) + .isEqualTo(new Vec3i(13, 12, 2)); + } + @Test + public void testWithinSectionZero() { + assertThat(new BlockPos(0, 0, 0).getPosWithinSection()) + .isEqualTo(new Vec3i(0, 0, 0)); + } + + @Test + public void testToString() { + assertThat(new BlockPos(27, 95, -205)) + .hasToString("27, 95, -205"); + } + +} diff --git a/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/SectionPosTest.java b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/SectionPosTest.java new file mode 100644 index 00000000..f7394d62 --- /dev/null +++ b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/SectionPosTest.java @@ -0,0 +1,37 @@ +package com.tisawesomeness.minecord.mc.pos; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SectionPosTest { + + @Test + public void testRegion() { + assertThat(new SectionPos(1500, 0, -600).getRegionFileName()) + .isEqualTo("r.46.-19.mca"); + } + @Test + public void testRegionZero() { + assertThat(new SectionPos(0, 0, 0).getRegionFileName()) + .isEqualTo("r.0.0.mca"); + } + + @Test + public void testWithinRegion() { + assertThat(new SectionPos(30, 0, -3).getPosWithinRegion()) + .isEqualTo(new Vec2i(30, 29)); + } + @Test + public void testWithinRegionZero() { + assertThat(new SectionPos(0, 0, 0).getPosWithinRegion()) + .isEqualTo(new Vec2i(0, 0)); + } + + @Test + public void testToString() { + assertThat(new SectionPos(27, 95, -205)) + .hasToString("27, 95, -205"); + } + +} diff --git a/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/Vec2iTest.java b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/Vec2iTest.java new file mode 100644 index 00000000..0e1343b4 --- /dev/null +++ b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/Vec2iTest.java @@ -0,0 +1,33 @@ +package com.tisawesomeness.minecord.mc.pos; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class Vec2iTest { + + @Test + public void testNew() { + assertThat(new Vec2i(0, 0)) + .extracting(Vec2i::getX, Vec2i::getZ) + .containsExactly(0, 0); + } + + @Test + public void testDivide() { + assertThat(new Vec2i(-4, 11).floorDiv(4)) + .isEqualTo(new Vec2i(-1, 2)); + } + @Test + public void testModulo() { + assertThat(new Vec2i(-2, 15).floorMod(7)) + .isEqualTo(new Vec2i(5, 1)); + } + + @Test + public void testToString() { + assertThat(new Vec2i(27, -205)) + .hasToString("27, -205"); + } + +} diff --git a/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/Vec3iTest.java b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/Vec3iTest.java new file mode 100644 index 00000000..63bf0229 --- /dev/null +++ b/minecord-bot/src/test/java/com/tisawesomeness/minecord/mc/pos/Vec3iTest.java @@ -0,0 +1,33 @@ +package com.tisawesomeness.minecord.mc.pos; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class Vec3iTest { + + @Test + public void testNew() { + assertThat(new Vec3i(0, 0, 0)) + .extracting(Vec3i::getX, Vec3i::getY, Vec3i::getZ) + .containsExactly(0, 0, 0); + } + + @Test + public void testDivide() { + assertThat(new Vec3i(-4, 0, 11).floorDiv(4)) + .isEqualTo(new Vec3i(-1, 0, 2)); + } + @Test + public void testModulo() { + assertThat(new Vec3i(-2, 14, 15).floorMod(7)) + .isEqualTo(new Vec3i(5, 0, 1)); + } + + @Test + public void testToString() { + assertThat(new Vec3i(27, 95, -205)) + .hasToString("27, 95, -205"); + } + +}