From 464a2e7be681be84cb4db34a8ef7a7d8a65db842 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 26 Aug 2018 01:13:21 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=20World=20=D0=B2=20Location=20=D0=B8=20ChunkSection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/mc/core/EntityLocation.java | 42 +++++++++- core/src/main/java/mc/core/Location.java | 43 +--------- .../mc/core/world/block/BlockFactory.java | 8 +- .../mc/core/world/chunk/ChunkSection.java | 2 - .../test/java/mc/core/TestEntityLocation.java | 78 ++++++++++++++++++- core/src/test/java/mc/core/TestLocation.java | 74 +----------------- .../packets/PlayerBlockPlacementPacket.java | 2 +- .../packets/PlayerDiggingPacket.java | 2 +- .../packets/TabCompletePacket.java | 2 +- .../packets/TestChunkdataPacket.java | 8 +- .../mc/world/simple/SimpleChunkSection.java | 24 +----- .../java/mc/world/simple/SimpleWorld.java | 2 +- 12 files changed, 133 insertions(+), 154 deletions(-) diff --git a/core/src/main/java/mc/core/EntityLocation.java b/core/src/main/java/mc/core/EntityLocation.java index 0eec9d2..960fd08 100644 --- a/core/src/main/java/mc/core/EntityLocation.java +++ b/core/src/main/java/mc/core/EntityLocation.java @@ -6,16 +6,24 @@ package mc.core; import lombok.Getter; import lombok.Setter; +import mc.core.exception.ResourceUnloadedException; import mc.core.world.World; +import mc.core.world.chunk.Chunk; +import mc.core.world.chunk.ChunkSection; + +import java.lang.ref.Reference; +import java.lang.ref.WeakReference; public class EntityLocation extends Location implements Cloneable { @Getter @Setter private float yaw, pitch; + private Reference refWorld; public EntityLocation(double x, double y, double z, float yaw, float pitch, World world) { - super(x, y, z, world); + super(x, y, z); setYawPitch(yaw, pitch); + setWorld(world); } public void setYawPitch(float yaw, float pitch) { @@ -27,6 +35,38 @@ public class EntityLocation extends Location implements Cloneable { setYawPitch(entityLocation.yaw, entityLocation.pitch); } + public World getWorld() { + if (refWorld == null) { + return null; + } else if (refWorld.get() == null) { + throw new ResourceUnloadedException("World unloaded"); + } else { + return refWorld.get(); + } + } + + public void setWorld (World world) { + this.refWorld = new WeakReference<>(world); + } + + public Chunk getChunk() { + World world = getWorld(); + if (world == null) { + return null; + } else { + return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4); + } + } + + public ChunkSection getChunkSection() { + Chunk chunk = getChunk(); + if (chunk == null) { + return null; + } else { + return chunk.getChunkSection(getBlockY() >> 4); + } + } + @Override public EntityLocation clone() { return (EntityLocation) super.clone(); diff --git a/core/src/main/java/mc/core/Location.java b/core/src/main/java/mc/core/Location.java index 5d87626..62ca57d 100644 --- a/core/src/main/java/mc/core/Location.java +++ b/core/src/main/java/mc/core/Location.java @@ -6,23 +6,14 @@ package mc.core; import lombok.Getter; import lombok.Setter; -import mc.core.exception.ResourceUnloadedException; -import mc.core.world.World; -import mc.core.world.chunk.Chunk; -import mc.core.world.chunk.ChunkSection; - -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; public class Location implements Cloneable { @Getter @Setter private double x, y, z; - private Reference refWorld; - public Location (double x, double y, double z, World world) { + public Location (double x, double y, double z) { setXYZ(x, y, z); - setWorld(world); } public void setXYZ(double x, double y, double z) { @@ -35,20 +26,6 @@ public class Location implements Cloneable { setXYZ(location.x, location.y, location.z); } - public World getWorld() { - if (refWorld == null) { - return null; - } else if (refWorld.get() == null) { - throw new ResourceUnloadedException("World unloaded"); - } else { - return refWorld.get(); - } - } - - public void setWorld (World world) { - this.refWorld = new WeakReference<>(world); - } - public int getBlockX() { return Double.valueOf(Math.floor(x)).intValue(); } @@ -61,24 +38,6 @@ public class Location implements Cloneable { return Double.valueOf(Math.floor(z)).intValue(); } - public Chunk getChunk() { - World world = getWorld(); - if (world == null) { - return null; - } else { - return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4); - } - } - - public ChunkSection getChunkSection() { - Chunk chunk = getChunk(); - if (chunk == null) { - return null; - } else { - return chunk.getChunkSection(getBlockY() >> 4); - } - } - @Override public Location clone() { try { diff --git a/core/src/main/java/mc/core/world/block/BlockFactory.java b/core/src/main/java/mc/core/world/block/BlockFactory.java index caceb58..095e312 100644 --- a/core/src/main/java/mc/core/world/block/BlockFactory.java +++ b/core/src/main/java/mc/core/world/block/BlockFactory.java @@ -5,15 +5,15 @@ import mc.core.world.World; public class BlockFactory { - public Block create(BlockType blockType, int x, int y, int z, World world) { - return new EmbeddedBlock(blockType, x, y, z, world); + public Block create(BlockType blockType, int x, int y, int z) { + return new EmbeddedBlock(blockType, x, y, z); } /** For first-time generation */ private class EmbeddedBlock extends AbstractBlock { - EmbeddedBlock(BlockType type, int x, int y, int z, World world) { + EmbeddedBlock(BlockType type, int x, int y, int z) { super(type); - setLocation(new Location(x,y,z, world)); + setLocation(new Location(x, y, z)); } } } diff --git a/core/src/main/java/mc/core/world/chunk/ChunkSection.java b/core/src/main/java/mc/core/world/chunk/ChunkSection.java index 3c10125..a829963 100644 --- a/core/src/main/java/mc/core/world/chunk/ChunkSection.java +++ b/core/src/main/java/mc/core/world/chunk/ChunkSection.java @@ -24,6 +24,4 @@ public interface ChunkSection { void setAddition(int x, int y, int z, int value); Biome getBiome(int localX, int localZ); - - World getWorld(); } diff --git a/core/src/test/java/mc/core/TestEntityLocation.java b/core/src/test/java/mc/core/TestEntityLocation.java index 06fbfa7..1b8e5fb 100644 --- a/core/src/test/java/mc/core/TestEntityLocation.java +++ b/core/src/test/java/mc/core/TestEntityLocation.java @@ -1,19 +1,37 @@ package mc.core; import mc.core.world.World; +import mc.core.world.chunk.Chunk; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; +import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class TestEntityLocation { + private World world; + + @Before + public void prepareWorld() { + this.world = mock(World.class); + when(world.getChunk(anyInt(), anyInt())).thenAnswer(invocation -> { + Object[] args = invocation.getArguments(); + + Chunk chunk = mock(Chunk.class); + when(chunk.getX()).thenReturn((int) args[0]); + when(chunk.getZ()).thenReturn((int) args[1]); + + return chunk; + }); + } + @Test public void cloneTest() { - World dummyWorld = mock(World.class); - - EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, dummyWorld); - assertSame("Lost world reference before cloning", dummyWorld, firstLocation.getWorld()); + EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, world); + assertSame("Lost world reference before cloning", world, firstLocation.getWorld()); EntityLocation locationClone = firstLocation.clone(); assertEquals("X mismatch", firstLocation.getX(), locationClone.getX(), 0); @@ -23,4 +41,56 @@ public class TestEntityLocation { assertEquals("Yaw mismatch", firstLocation.getYaw(), locationClone.getYaw(), 0); assertSame("World mismatch (accidental clone of the World object?)", firstLocation.getWorld(), locationClone.getWorld()); } + + @Test + public void testGetChunk() { + EntityLocation location; + Chunk chunk; + + location = new EntityLocation(0d, 0, 0d, 0f, 0f, 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()); + } } diff --git a/core/src/test/java/mc/core/TestLocation.java b/core/src/test/java/mc/core/TestLocation.java index 3d6715e..4faef07 100644 --- a/core/src/test/java/mc/core/TestLocation.java +++ b/core/src/test/java/mc/core/TestLocation.java @@ -1,35 +1,15 @@ package mc.core; -import mc.core.world.World; -import mc.core.world.chunk.Chunk; -import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; public class TestLocation { - private World world; - - @Before - public void prepareWorld() { - this.world = mock(World.class); - when(world.getChunk(anyInt(), anyInt())).thenAnswer(invocation -> { - Object[] args = invocation.getArguments(); - - Chunk chunk = mock(Chunk.class); - when(chunk.getX()).thenReturn((int) args[0]); - when(chunk.getZ()).thenReturn((int) args[1]); - - return chunk; - }); - } - @Test public void testGetBlockXZ() { Location location; - location = new Location(0d, 0, 0d, world); + location = new Location(0d, 0, 0d); assertEquals(0, location.getBlockX()); assertEquals(0, location.getBlockZ()); @@ -69,56 +49,4 @@ public class TestLocation { assertEquals(-2, location.getBlockX()); assertEquals(-2, location.getBlockZ()); } - - @Test - public void testGetChunk() { - 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()); - } } diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerBlockPlacementPacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerBlockPlacementPacket.java index 742370e..1a1ac1d 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerBlockPlacementPacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerBlockPlacementPacket.java @@ -21,7 +21,7 @@ public class PlayerBlockPlacementPacket implements CSPacket { public void readSelf(NetInputStream netStream) { long compactedCoords = netStream.readLong(); double[] xyz = CompactedCoords.uncompressXYZ(compactedCoords); - location = new Location(xyz[0], xyz[1], xyz[2], null); + location = new Location(xyz[0], xyz[1], xyz[2]); face = Direction.getById(netStream.readVarInt()); hand = (netStream.readVarInt() == 1); cursorX = netStream.readFloat(); diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerDiggingPacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerDiggingPacket.java index e60d409..a2bde1e 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerDiggingPacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerDiggingPacket.java @@ -50,7 +50,7 @@ public class PlayerDiggingPacket implements CSPacket { status = Status.getById(netStream.readVarInt()); long compactCoord = netStream.readLong(); double[] xyz = CompactedCoords.uncompressXYZ(compactCoord); - location = new Location(xyz[0], xyz[1], xyz[2], null); + location = new Location(xyz[0], xyz[1], xyz[2]); face = Direction.getById(netStream.readByte()); } } diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/TabCompletePacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/TabCompletePacket.java index ffbfe50..09f686d 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/TabCompletePacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/TabCompletePacket.java @@ -27,7 +27,7 @@ public class TabCompletePacket implements CSPacket { double y = (compactValue >> 26) & 0xFFF; double z = compactValue << 38 >> 38; // is normal? - this.location = new Location(x, y, z, null); + this.location = new Location(x, y, z); } } } diff --git a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java index ba9147a..449a672 100644 --- a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java +++ b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java @@ -50,10 +50,10 @@ public class TestChunkdataPacket { BlockFactory blockFactory = new BlockFactory(); - if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z, null); - else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z, null); - else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z, null); - else return blockFactory.create(BlockType.AIR, x, y, z, null); + if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z); + else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z); + else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z); + else return blockFactory.create(BlockType.AIR, x, y, z); }); world = mock(World.class); diff --git a/simple_world/src/main/java/mc/world/simple/SimpleChunkSection.java b/simple_world/src/main/java/mc/world/simple/SimpleChunkSection.java index 3fdf5ae..a0c8cc6 100644 --- a/simple_world/src/main/java/mc/world/simple/SimpleChunkSection.java +++ b/simple_world/src/main/java/mc/world/simple/SimpleChunkSection.java @@ -4,26 +4,20 @@ */ package mc.world.simple; -import mc.core.exception.ResourceUnloadedException; import mc.core.world.Biome; -import mc.core.world.World; import mc.core.world.block.Block; import mc.core.world.block.BlockFactory; import mc.core.world.block.BlockType; import mc.core.world.chunk.ChunkSection; -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; import java.util.List; public class SimpleChunkSection implements ChunkSection { private final BlockFactory blockFactory = new BlockFactory(); private final List layersBlock; - private Reference refWorld; - public SimpleChunkSection(List layersBlock, World world) { + public SimpleChunkSection(List layersBlock) { this.layersBlock = layersBlock; - this.refWorld = new WeakReference<>(world); } @Override @@ -72,22 +66,12 @@ public class SimpleChunkSection implements ChunkSection { @Override public Block getBlock(int x, int y, int z) { if (y >= layersBlock.size()) { - return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); + return blockFactory.create(BlockType.AIR, x, y, z); } BlockType blockType = layersBlock.get(y); - if (blockType == null) return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); + if (blockType == null) return blockFactory.create(BlockType.AIR, x, y, z); - return blockFactory.create(blockType, x, y, z, getWorld()); - } - - @Override - public World getWorld() { - World world = refWorld.get(); - if (world == null) { - throw new ResourceUnloadedException("World unloaded"); - } - - return world; + return blockFactory.create(blockType, x, y, z); } } diff --git a/simple_world/src/main/java/mc/world/simple/SimpleWorld.java b/simple_world/src/main/java/mc/world/simple/SimpleWorld.java index c1b1b19..76d3217 100644 --- a/simple_world/src/main/java/mc/world/simple/SimpleWorld.java +++ b/simple_world/src/main/java/mc/world/simple/SimpleWorld.java @@ -68,6 +68,6 @@ public class SimpleWorld implements World { } } - this.chunkSection = new SimpleChunkSection(layoutsBlock, this); + this.chunkSection = new SimpleChunkSection(layoutsBlock); } }