From d02499e3b74f7b823dc3d6de8e9c5a4e2cde4fef Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Fri, 17 Aug 2018 13:49:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=82=D0=B5=D1=81=D1=82=20=D0=B4=D0=BB=D1=8F=20Locati?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/main/java/mc/core/Location.java | 10 +- core/src/test/java/mc/core/TestLocation.java | 173 +++++++++++++++++++ 2 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/mc/core/TestLocation.java diff --git a/core/src/main/java/mc/core/Location.java b/core/src/main/java/mc/core/Location.java index afb3582..4a4c1b4 100644 --- a/core/src/main/java/mc/core/Location.java +++ b/core/src/main/java/mc/core/Location.java @@ -50,15 +50,15 @@ public class Location implements Cloneable { } public int getBlockX() { - return (int) x; + return Double.valueOf(Math.floor(x)).intValue(); } public int getBlockY() { - return (int) y; + return Double.valueOf(Math.floor(y)).intValue(); } public int getBlockZ() { - return (int) z; + return Double.valueOf(Math.floor(z)).intValue(); } public Chunk getChunk() { @@ -66,7 +66,7 @@ public class Location implements Cloneable { if (world == null) { return null; } else { - return world.getChunk((int)(x / 16), (int)(z / 16)); + return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4); } } @@ -75,7 +75,7 @@ public class Location implements Cloneable { if (chunk == null) { return null; } else { - return chunk.getChunkSection((int)(y / 16)); + return chunk.getChunkSection(getBlockY() >> 4); } } diff --git a/core/src/test/java/mc/core/TestLocation.java b/core/src/test/java/mc/core/TestLocation.java new file mode 100644 index 0000000..9b9cf61 --- /dev/null +++ b/core/src/test/java/mc/core/TestLocation.java @@ -0,0 +1,173 @@ +package mc.core; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import mc.core.world.Biome; +import mc.core.world.World; +import mc.core.world.WorldType; +import mc.core.world.chunk.Chunk; +import mc.core.world.chunk.ChunkSection; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestLocation { + private class DummyWorld implements World { + @RequiredArgsConstructor + private class DummyChunk implements Chunk { + @Getter + private final int x, z; + + @Override + public ChunkSection getChunkSection(int height) { + return null; + } + + @Override + public void setChunkSection(int height, ChunkSection chunkSection) { + } + + @Override + public Biome getBiome(int localX, int localZ) { + return null; + } + + @Override + public void setBiome(int localX, int localZ, Biome biome) { + } + + @Override + public World getWorld() { + return null; + } + + @Override + public void setWorld(World world) { + } + } + + @Override + public WorldType getWorldType() { + return null; + } + + @Override + public EntityLocation getSpawn() { + return null; + } + + @Override + public void setSpawn(EntityLocation location) { + } + + @Override + public Chunk getChunk(int x, int z) { + return new DummyChunk(x, z); + } + + @Override + public void setChunk(int x, int z, Chunk chunkSection) { + + } + } + + @Test + public void testGetBlockXZ() { + World world = new DummyWorld(); + Location location; + + location = new Location(0d, 0, 0d, world); + assertEquals(0, location.getBlockX()); + assertEquals(0, location.getBlockZ()); + + location.setXYZ(0.1d, 0, 0.1d); + assertEquals(0, location.getBlockX()); + assertEquals(0, location.getBlockZ()); + + location.setXYZ(0.5d, 0, 0.5d); + assertEquals(0, location.getBlockX()); + assertEquals(0, location.getBlockZ()); + + location.setXYZ(0.9d, 0, 0.9d); + assertEquals(0, location.getBlockX()); + assertEquals(0, location.getBlockZ()); + + location.setXYZ(1d, 0, 1d); + assertEquals(1, location.getBlockX()); + assertEquals(1, location.getBlockZ()); + + location.setXYZ(-0.1d, 0, -0.1d); + assertEquals(-1, location.getBlockX()); + assertEquals(-1, location.getBlockZ()); + + location.setXYZ(-0.5d, 0, -0.5d); + assertEquals(-1, location.getBlockX()); + assertEquals(-1, location.getBlockZ()); + + location.setXYZ(-0.9d, 0, -0.9d); + assertEquals(-1, location.getBlockX()); + assertEquals(-1, location.getBlockZ()); + + location.setXYZ(-1d, 0, -1d); + assertEquals(-1, location.getBlockX()); + assertEquals(-1, location.getBlockZ()); + + location.setXYZ(-1.1d, 0, -1.1d); + assertEquals(-2, location.getBlockX()); + assertEquals(-2, location.getBlockZ()); + } + + @Test + public void testGetChunk() { + World world = new DummyWorld(); + Location location; + Chunk chunk; + + location = new Location(0d, 0, 0d, world); + chunk = location.getChunk(); + assertEquals(0, chunk.getX()); + assertEquals(0, chunk.getZ()); + + location.setXYZ(1d, 0, 1d); + chunk = location.getChunk(); + assertEquals(0, chunk.getX()); + assertEquals(0, chunk.getZ()); + + location.setXYZ(15d, 0, 15d); + chunk = location.getChunk(); + assertEquals(0, chunk.getX()); + assertEquals(0, chunk.getZ()); + + location.setXYZ(16d, 0, 16d); + chunk = location.getChunk(); + assertEquals(1, chunk.getX()); + assertEquals(1, chunk.getZ()); + + location.setXYZ(-0.1d, 0, -0.1d); + chunk = location.getChunk(); + assertEquals(-1, chunk.getX()); + assertEquals(-1, chunk.getZ()); + + location.setXYZ(-1d, 0, -1d); + chunk = location.getChunk(); + assertEquals(-1, chunk.getX()); + assertEquals(-1, chunk.getZ()); + + location.setXYZ(-15d, 0, -15d); + chunk = location.getChunk(); + assertEquals(-1, chunk.getX()); + assertEquals(-1, chunk.getZ()); + + //TODO на практике, таких точных значений не встретиться, но тем не менее данный тест не проходит + //location.setXYZ(-16.0d, 0, -16.0d); + //chunk = location.getChunk(); + //assertEquals(-2, chunk.getX()); + //assertEquals(-2, chunk.getZ()); + + location.setXYZ(-16.001d, 0, -16.001d); + chunk = location.getChunk(); + assertEquals(-2, chunk.getX()); + assertEquals(-2, chunk.getZ()); + } +}