From 1a2ebcfa0ace7e4d48cbfdb3b34197d8fbeace3d Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 02:11:54 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20Location's?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 7 ++-- core/src/test/java/mc/core/SpringConfig.java | 34 +++++++++++++++++++ .../test/java/mc/core/TestEntityLocation.java | 17 +++++++--- core/src/test/java/mc/core/TestLocation.java | 23 +++++-------- 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 core/src/test/java/mc/core/SpringConfig.java diff --git a/build.gradle b/build.gradle index af58978..7f5349d 100644 --- a/build.gradle +++ b/build.gradle @@ -35,15 +35,14 @@ subprojects { exclude group: 'commons-logging' } - /* Components */ + /* Lombok */ compile (group: 'org.projectlombok', name: 'lombok', version: '1.16.16') - /* JUnit */ + /* Testing */ testCompile (group: 'junit', name: 'junit', version: '4.12') - /* Simple log */ testCompile (group: 'org.slf4j', name: 'slf4j-simple', version: slf4j_version) - /* Mockito */ testCompile (group: 'org.mockito', name: 'mockito-core', version: '1.9.5') + testCompile (group: 'org.springframework', name: 'spring-test', version: spring_version) } task copyDep(type: Copy) { diff --git a/core/src/test/java/mc/core/SpringConfig.java b/core/src/test/java/mc/core/SpringConfig.java new file mode 100644 index 0000000..9484ce9 --- /dev/null +++ b/core/src/test/java/mc/core/SpringConfig.java @@ -0,0 +1,34 @@ +package mc.core; + +import mc.core.world.World; +import mc.core.world.chunk.Chunk; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@Configuration +public class SpringConfig { + @Bean() + public World simpleMockWorld() { + return mock(World.class); + } + + @Bean + public World chunkedMockWorld() { + World 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; + }); + + return world; + } +} diff --git a/core/src/test/java/mc/core/TestEntityLocation.java b/core/src/test/java/mc/core/TestEntityLocation.java index 06fbfa7..5a09623 100644 --- a/core/src/test/java/mc/core/TestEntityLocation.java +++ b/core/src/test/java/mc/core/TestEntityLocation.java @@ -2,18 +2,27 @@ package mc.core; import mc.core.world.World; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import static org.mockito.Mockito.mock; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SpringConfig.class) public class TestEntityLocation { + @Autowired + @Qualifier("simpleMockWorld") + private World mockWorld; + @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, mockWorld); + assertSame("Lost world reference before cloning", mockWorld, firstLocation.getWorld()); EntityLocation locationClone = firstLocation.clone(); assertEquals("X mismatch", firstLocation.getX(), locationClone.getX(), 0); diff --git a/core/src/test/java/mc/core/TestLocation.java b/core/src/test/java/mc/core/TestLocation.java index 3d6715e..eafd792 100644 --- a/core/src/test/java/mc/core/TestLocation.java +++ b/core/src/test/java/mc/core/TestLocation.java @@ -4,27 +4,22 @@ import mc.core.world.World; import mc.core.world.chunk.Chunk; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SpringConfig.class) public class TestLocation { + @Autowired + @Qualifier("chunkedMockWorld") 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; From 6808ae34f90273f879da8f7994d6100ae933c0a5 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 02:17:27 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=82=D0=B5=D1=81=D1=82=20CompactedCoords?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mc/core/utils/TestCompactedCoords.java | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/core/src/test/java/mc/core/utils/TestCompactedCoords.java b/core/src/test/java/mc/core/utils/TestCompactedCoords.java index 0aef3da..bced6f2 100644 --- a/core/src/test/java/mc/core/utils/TestCompactedCoords.java +++ b/core/src/test/java/mc/core/utils/TestCompactedCoords.java @@ -3,40 +3,21 @@ package mc.core.utils; import org.junit.Test; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; import static org.junit.Assert.assertEquals; public class TestCompactedCoords { @Test - public void testXZSimple() { - for (int z = -100; z <= 100; z++) { - for (int x = -100; x <= 100; x++) { - int compressXZ = CompactedCoords.compressXZ(x, z); - int[] xz = CompactedCoords.uncompressXZ(compressXZ); - - assertEquals(x, xz[0]); - assertEquals(z, xz[1]); - } - } - } - - @Test - public void testXZRandom() { - Random random = new Random(); - int x,z; + public void testXZ() { + ThreadLocalRandom random = ThreadLocalRandom.current(); for (int i = 0; i < 100; i++) { - do { - x = random.nextInt(); - } while (x < Short.MIN_VALUE || x > Short.MAX_VALUE); + final int x = random.nextInt(Short.MIN_VALUE, Short.MAX_VALUE); + final int z = random.nextInt(Short.MIN_VALUE, Short.MAX_VALUE); - do { - z = random.nextInt(); - } while (z < Short.MIN_VALUE || z > Short.MAX_VALUE); - - - int compressXZ = CompactedCoords.compressXZ(x, z); + final int compressXZ = CompactedCoords.compressXZ(x, z); int[] xz = CompactedCoords.uncompressXZ(compressXZ); assertEquals(x, xz[0]); From 09c7626c2dd9b37fbebeb991c9d51318134c4117 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 05:25:24 +0300 Subject: [PATCH 3/7] refactory Location's --- .../main/java/mc/core/CoreEventListener.java | 10 +- .../src/main/java/mc/core/EntityLocation.java | 53 +++++--- core/src/main/java/mc/core/Location.java | 91 -------------- .../mc/core/embedded/FakePlayerManager.java | 5 +- .../mc/core/player/InMemoryPlayerManager.java | 8 +- core/src/main/java/mc/core/player/Player.java | 4 +- .../java/mc/core/player/PlayerManager.java | 3 +- .../java/mc/core/player/SimplePlayer.java | 28 ++++- core/src/main/java/mc/core/world/World.java | 9 ++ .../mc/core/world/block/AbstractBlock.java | 3 +- .../main/java/mc/core/world/block/Block.java | 3 +- .../mc/core/world/block/BlockFactory.java | 11 +- .../mc/core/world/block/BlockLocation.java | 26 ++++ .../test/java/mc/core/TestBlockLocation.java | 40 ++++++ .../test/java/mc/core/TestEntityLocation.java | 102 +++++++++++---- core/src/test/java/mc/core/TestLocation.java | 119 ------------------ .../main/java/mc/world/flat/FlatWorld.java | 10 +- .../mc/world/flat/SimpleChunkSection.java | 12 +- .../network/proto_1_12_2/TeleportManager.java | 3 +- .../packets/PlayerBlockPlacementPacket.java | 6 +- .../packets/PlayerDiggingPacket.java | 6 +- .../packets/PlayerPositionAndLookPacket.java | 3 +- .../packets/TabCompletePacket.java | 6 +- .../packets/TestChunkdataPacket.java | 8 +- .../netty/PlayerEventListener.java | 2 +- .../netty/handlers/LoginHandler.java | 11 +- .../netty/handlers/PlayHandler.java | 6 +- 27 files changed, 269 insertions(+), 319 deletions(-) delete mode 100644 core/src/main/java/mc/core/Location.java create mode 100644 core/src/main/java/mc/core/world/block/BlockLocation.java create mode 100644 core/src/test/java/mc/core/TestBlockLocation.java delete mode 100644 core/src/test/java/mc/core/TestLocation.java diff --git a/core/src/main/java/mc/core/CoreEventListener.java b/core/src/main/java/mc/core/CoreEventListener.java index 070ae5b..201b60e 100644 --- a/core/src/main/java/mc/core/CoreEventListener.java +++ b/core/src/main/java/mc/core/CoreEventListener.java @@ -24,10 +24,10 @@ public class CoreEventListener { log.trace("(GameLoop) playerMoveEventHandler()"); Chunk chunk; - chunk = event.getOldLocation().getChunk(); // Old chunk + chunk = event.getPlayer().getWorld().getChunk(event.getOldLocation()); // Old chunk int ccX = chunk.getX(); int ccZ = chunk.getZ(); - chunk = event.getNewLocation().getChunk(); // Next chunk + chunk = event.getPlayer().getWorld().getChunk(event.getNewLocation()); // Next chunk int ncX = chunk.getX(); int ncZ = chunk.getZ(); @@ -71,7 +71,11 @@ public class CoreEventListener { } } - event.getPlayer().getLocation().setXYZ(event.getNewLocation()); + event.getPlayer().getLocation().setXYZ( + event.getNewLocation().getX(), + event.getNewLocation().getY(), + event.getNewLocation().getZ() + ); // TODO отсылать клиенту только(!) для корректировки позиции // SC_PlayerMoveEvent nextEvent = new SC_PlayerMoveEvent(event.getPlayer()); diff --git a/core/src/main/java/mc/core/EntityLocation.java b/core/src/main/java/mc/core/EntityLocation.java index 0eec9d2..0b4e237 100644 --- a/core/src/main/java/mc/core/EntityLocation.java +++ b/core/src/main/java/mc/core/EntityLocation.java @@ -1,21 +1,29 @@ -/* - * DmitriyMX - * 2018-08-08 - */ package mc.core; -import lombok.Getter; -import lombok.Setter; -import mc.core.world.World; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; -public class EntityLocation extends Location implements Cloneable { - @Getter - @Setter +@NoArgsConstructor +@AllArgsConstructor +@Data +public class EntityLocation implements Cloneable { + private double x, y, z; private float yaw, pitch; - public EntityLocation(double x, double y, double z, float yaw, float pitch, World world) { - super(x, y, z, world); - setYawPitch(yaw, pitch); + public static EntityLocation ZERO() { + return new EntityLocation(0d,0d,0d,0f,0f); + } + + public void set(EntityLocation location) { + setXYZ(location.x, location.y, location.z); + setYawPitch(location.yaw, location.pitch); + } + + public void setXYZ(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; } public void setYawPitch(float yaw, float pitch) { @@ -23,12 +31,25 @@ public class EntityLocation extends Location implements Cloneable { this.pitch = pitch; } - public void setYawPitch(EntityLocation entityLocation) { - setYawPitch(entityLocation.yaw, entityLocation.pitch); + public int getBlockX() { + return Double.valueOf(Math.floor(x)).intValue(); + } + + public int getBlockY() { + return Double.valueOf(Math.floor(y)).intValue(); + } + + public int getBlockZ() { + return Double.valueOf(Math.floor(z)).intValue(); } @Override public EntityLocation clone() { - return (EntityLocation) super.clone(); + try { + return (EntityLocation) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + return null; + } } } diff --git a/core/src/main/java/mc/core/Location.java b/core/src/main/java/mc/core/Location.java deleted file mode 100644 index 4a4c1b4..0000000 --- a/core/src/main/java/mc/core/Location.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * DmitriyMX - * 2018-08-08 - */ -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) { - setXYZ(x, y, z); - setWorld(world); - } - - public void setXYZ(double x, double y, double z) { - this.x = x; - this.y = y; - this.z = z; - } - - public void setXYZ(Location location) { - setXYZ(location.x, location.y, location.z); - } - - public World getWorld() { - if (refWorld == null) { - return null; - } else if (refWorld.get() == null) { - throw new ResourceUnloadedException("You're trying to get unloaded world"); - } else { - return refWorld.get(); - } - } - - public void setWorld (World world) { - this.refWorld = new WeakReference<>(world); - } - - public int getBlockX() { - return Double.valueOf(Math.floor(x)).intValue(); - } - - public int getBlockY() { - return Double.valueOf(Math.floor(y)).intValue(); - } - - public int getBlockZ() { - 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 { - return (Location) super.clone(); - } catch (CloneNotSupportedException e) { // такое в нашем случае вообще возможно? - e.printStackTrace(); - return null; - } - } -} diff --git a/core/src/main/java/mc/core/embedded/FakePlayerManager.java b/core/src/main/java/mc/core/embedded/FakePlayerManager.java index 612de62..a1932e3 100644 --- a/core/src/main/java/mc/core/embedded/FakePlayerManager.java +++ b/core/src/main/java/mc/core/embedded/FakePlayerManager.java @@ -12,8 +12,7 @@ import mc.core.player.Player; import mc.core.player.PlayerManager; import mc.core.text.Text; import mc.core.text.Title; -import mc.core.world.chunk.Chunk; -import mc.core.world.chunk.ChunkSection; +import mc.core.world.World; import java.util.Collections; import java.util.List; @@ -53,7 +52,7 @@ public class FakePlayerManager implements PlayerManager { private static final NetChannel FAKE_NET_CHANNEL = new FakeNetChannet(); @Override - public Player createPlayer(String name, EntityLocation defaultLocation) { + public Player createPlayer(String name, EntityLocation location, World world) { return null; } diff --git a/core/src/main/java/mc/core/player/InMemoryPlayerManager.java b/core/src/main/java/mc/core/player/InMemoryPlayerManager.java index d373c59..765201e 100644 --- a/core/src/main/java/mc/core/player/InMemoryPlayerManager.java +++ b/core/src/main/java/mc/core/player/InMemoryPlayerManager.java @@ -10,6 +10,7 @@ import mc.core.Config; import mc.core.EntityLocation; import mc.core.network.BroadcastNetChannel; import mc.core.network.NetChannel; +import mc.core.world.World; import org.springframework.beans.factory.annotation.Autowired; import java.util.*; @@ -31,14 +32,13 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable { } @Override - public Player createPlayer(String name, EntityLocation defaultLocation) { + public Player createPlayer(String name, EntityLocation location, World world) { SimplePlayer player = new SimplePlayer(); player.setId(rand.nextInt(10000)); player.setUUID(UUID.nameUUIDFromBytes(name.getBytes())); player.setName(name); - player.getLocation().setXYZ(defaultLocation); - player.getLocation().setYawPitch(defaultLocation); - player.getLocation().setWorld(defaultLocation.getWorld()); + player.getLocation().set(location); + player.setWorld(world); player.setSettings(new PlayerSettings()); synchronized (lock) { diff --git a/core/src/main/java/mc/core/player/Player.java b/core/src/main/java/mc/core/player/Player.java index 11c41a3..175b0cf 100644 --- a/core/src/main/java/mc/core/player/Player.java +++ b/core/src/main/java/mc/core/player/Player.java @@ -6,6 +6,7 @@ package mc.core.player; import mc.core.EntityLocation; import mc.core.network.NetChannel; +import mc.core.world.World; import java.util.List; import java.util.UUID; @@ -23,7 +24,8 @@ public interface Player { void setChannel(NetChannel channel); EntityLocation getLocation(); - //TODO надо определиться - нужно ли здесь setLocation() или нет + World getWorld(); + void setWorld(World world); boolean isFlying(); void setFlying(boolean value); diff --git a/core/src/main/java/mc/core/player/PlayerManager.java b/core/src/main/java/mc/core/player/PlayerManager.java index 8e07d6e..1ac8543 100644 --- a/core/src/main/java/mc/core/player/PlayerManager.java +++ b/core/src/main/java/mc/core/player/PlayerManager.java @@ -6,12 +6,13 @@ package mc.core.player; import mc.core.EntityLocation; import mc.core.network.NetChannel; +import mc.core.world.World; import java.util.List; import java.util.Optional; public interface PlayerManager { - Player createPlayer(String name, EntityLocation defaultLocation); + Player createPlayer(String name, EntityLocation location, World world); void joinServer(Player player); void leftServer(Player player); Optional getPlayer(String name); diff --git a/core/src/main/java/mc/core/player/SimplePlayer.java b/core/src/main/java/mc/core/player/SimplePlayer.java index 2472758..8fb40b6 100644 --- a/core/src/main/java/mc/core/player/SimplePlayer.java +++ b/core/src/main/java/mc/core/player/SimplePlayer.java @@ -6,8 +6,12 @@ package mc.core.player; import lombok.Data; import mc.core.EntityLocation; +import mc.core.exception.ResourceUnloadedException; import mc.core.network.NetChannel; +import mc.core.world.World; +import java.lang.ref.Reference; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -19,21 +23,33 @@ public class SimplePlayer implements Player { private String name; private boolean online = false; private NetChannel channel; - private EntityLocation location = new EntityLocation(0d, 0d, 0d, 0f, 0f, null); + private EntityLocation location = EntityLocation.ZERO(); + private Reference $refWorld; private boolean flying = false; private PlayerSettings settings; private List loadedChunks = new ArrayList<>(); - public void setLocation(EntityLocation location) { - this.location.setXYZ(location); - this.location.setYawPitch(location); - } - @Override public UUID getUUID() { return uuid; } + @Override + public World getWorld() { + if ($refWorld == null) { + return null; + } else if ($refWorld.get() == null) { + throw new ResourceUnloadedException("You're trying to get unloaded world"); + } else { + return $refWorld.get(); + } + } + + @Override + public void setWorld(World world) { + this.$refWorld = new WeakReference<>(world); + } + public void setUUID(UUID uuid) { this.uuid = uuid; } diff --git a/core/src/main/java/mc/core/world/World.java b/core/src/main/java/mc/core/world/World.java index 20bf437..85f79cb 100644 --- a/core/src/main/java/mc/core/world/World.java +++ b/core/src/main/java/mc/core/world/World.java @@ -5,6 +5,7 @@ package mc.core.world; import mc.core.EntityLocation; +import mc.core.world.block.BlockLocation; import mc.core.world.chunk.Chunk; public interface World { @@ -15,4 +16,12 @@ public interface World { Chunk getChunk(int x, int z); void setChunk(int x, int z, Chunk chunkSection); + + default Chunk getChunk(BlockLocation location) { + return getChunk(location.getX() >> 4, location.getZ() >> 4); + } + + default Chunk getChunk(EntityLocation location) { + return getChunk(location.getBlockX() >> 4, location.getBlockZ() >> 4); + } } diff --git a/core/src/main/java/mc/core/world/block/AbstractBlock.java b/core/src/main/java/mc/core/world/block/AbstractBlock.java index 0c6d90b..b00b8af 100644 --- a/core/src/main/java/mc/core/world/block/AbstractBlock.java +++ b/core/src/main/java/mc/core/world/block/AbstractBlock.java @@ -3,7 +3,6 @@ package mc.core.world.block; import com.flowpowered.nbt.Tag; import lombok.Getter; import lombok.Setter; -import mc.core.Location; import java.util.HashMap; import java.util.Map; @@ -12,7 +11,7 @@ import java.util.stream.Stream; public abstract class AbstractBlock implements Block { @Getter @Setter - private Location location; + private BlockLocation location; @Getter private int light = 0; @Getter diff --git a/core/src/main/java/mc/core/world/block/Block.java b/core/src/main/java/mc/core/world/block/Block.java index d1a392f..b54a7d0 100644 --- a/core/src/main/java/mc/core/world/block/Block.java +++ b/core/src/main/java/mc/core/world/block/Block.java @@ -1,11 +1,10 @@ package mc.core.world.block; -import mc.core.Location; import mc.core.nbt.Taggable; public interface Block extends Taggable{ int getLight(); void setLight(int light); BlockType getBlockType(); - Location getLocation(); + BlockLocation getLocation(); } 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..04d039f 100644 --- a/core/src/main/java/mc/core/world/block/BlockFactory.java +++ b/core/src/main/java/mc/core/world/block/BlockFactory.java @@ -1,19 +1,16 @@ package mc.core.world.block; -import mc.core.Location; -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 BlockLocation(x, y, z)); } } } diff --git a/core/src/main/java/mc/core/world/block/BlockLocation.java b/core/src/main/java/mc/core/world/block/BlockLocation.java new file mode 100644 index 0000000..cabccaa --- /dev/null +++ b/core/src/main/java/mc/core/world/block/BlockLocation.java @@ -0,0 +1,26 @@ +package mc.core.world.block; + +import lombok.*; + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class BlockLocation implements Cloneable { + private int x, y, z; + + public void setXYZ(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public BlockLocation clone() { + try { + return (BlockLocation) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/core/src/test/java/mc/core/TestBlockLocation.java b/core/src/test/java/mc/core/TestBlockLocation.java new file mode 100644 index 0000000..620919f --- /dev/null +++ b/core/src/test/java/mc/core/TestBlockLocation.java @@ -0,0 +1,40 @@ +package mc.core; + +import mc.core.world.block.BlockLocation; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.ThreadLocalRandom; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class TestBlockLocation { + private static final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + private static final int minI = 0, maxI = 10; + private int x, y, z; + + @Before + public void before() { + x = rnd.nextInt(minI, maxI); + y = rnd.nextInt(minI, maxI); + z = rnd.nextInt(minI, maxI); + } + + @Test + public void testEquals() { + BlockLocation loc1 = new BlockLocation(x, y, z); + BlockLocation loc2 = new BlockLocation(x, y, z); + assertEquals(loc1, loc2); + + loc2 = new BlockLocation(x+1, y+2, z-3); + assertNotEquals(loc1, loc2); + } + + @Test + public void testClone() { + BlockLocation locOrig = new BlockLocation(x, y, z); + BlockLocation locClone = locOrig.clone(); + assertEquals(locOrig, locClone); + } +} diff --git a/core/src/test/java/mc/core/TestEntityLocation.java b/core/src/test/java/mc/core/TestEntityLocation.java index 5a09623..c6b1c62 100644 --- a/core/src/test/java/mc/core/TestEntityLocation.java +++ b/core/src/test/java/mc/core/TestEntityLocation.java @@ -1,35 +1,91 @@ package mc.core; -import mc.core.world.World; +import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.concurrent.ThreadLocalRandom; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.mockito.Mockito.mock; +import static org.junit.Assert.assertNotEquals; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = SpringConfig.class) public class TestEntityLocation { - @Autowired - @Qualifier("simpleMockWorld") - private World mockWorld; + private static final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + private static final double minD = 0.0d, maxD = 10.0d; + private static final float minF = 0.0f, maxF = 359.9f; + private double x, y, z; + private float yaw, pitch; + + @Before + public void before() { + x = rnd.nextDouble(minD, maxD); + y = rnd.nextDouble(minD, maxD); + z = rnd.nextDouble(minD, maxD); + yaw = rnd.nextFloat() * (maxF - minF) + minF; + pitch = rnd.nextFloat() * (maxF - minF) + minF; + } @Test - public void cloneTest() { - EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, mockWorld); - assertSame("Lost world reference before cloning", mockWorld, firstLocation.getWorld()); - EntityLocation locationClone = firstLocation.clone(); + public void testEquals() { + EntityLocation loc1 = new EntityLocation(x, y, z, yaw, pitch); + EntityLocation loc2 = new EntityLocation(x, y, z, yaw, pitch); + assertEquals(loc1, loc2); - assertEquals("X mismatch", firstLocation.getX(), locationClone.getX(), 0); - assertEquals("Y mismatch", firstLocation.getY(), locationClone.getY(), 0); - assertEquals("Z mismatch", firstLocation.getZ(), locationClone.getZ(), 0); - assertEquals("Pitch mismatch", firstLocation.getPitch(), locationClone.getPitch(), 0); - assertEquals("Yaw mismatch", firstLocation.getYaw(), locationClone.getYaw(), 0); - assertSame("World mismatch (accidental clone of the World object?)", firstLocation.getWorld(), locationClone.getWorld()); + loc2 = new EntityLocation(x+1, y+2, z-3, yaw, pitch); + assertNotEquals(loc1, loc2); + + loc2 = new EntityLocation(x, y, z, yaw-1, pitch+2); + assertNotEquals(loc1, loc2); + } + + @Test + public void testClone() { + EntityLocation locOrig = new EntityLocation(x, y, z, yaw, pitch); + EntityLocation locClone = locOrig.clone(); + assertEquals(locOrig, locClone); + } + + @Test + public void testGetBlockXZ() { + EntityLocation location; + + location = new EntityLocation(0d, 0, 0d, 0f, 0f); + 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()); } } diff --git a/core/src/test/java/mc/core/TestLocation.java b/core/src/test/java/mc/core/TestLocation.java deleted file mode 100644 index eafd792..0000000 --- a/core/src/test/java/mc/core/TestLocation.java +++ /dev/null @@ -1,119 +0,0 @@ -package mc.core; - -import mc.core.world.World; -import mc.core.world.chunk.Chunk; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = SpringConfig.class) -public class TestLocation { - @Autowired - @Qualifier("chunkedMockWorld") - private World world; - - @Test - public void testGetBlockXZ() { - 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() { - 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/flat_world/src/main/java/mc/world/flat/FlatWorld.java b/flat_world/src/main/java/mc/world/flat/FlatWorld.java index 6ec745f..ca5d1db 100644 --- a/flat_world/src/main/java/mc/world/flat/FlatWorld.java +++ b/flat_world/src/main/java/mc/world/flat/FlatWorld.java @@ -5,6 +5,7 @@ package mc.world.flat; import lombok.Getter; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import mc.core.EntityLocation; import mc.core.world.World; @@ -16,6 +17,7 @@ import mc.core.world.chunk.ChunkSection; public class FlatWorld implements World { @Getter private final WorldType worldType = WorldType.FLAT; + @Setter private EntityLocation spawn; private ChunkSection chunkSection = new SimpleChunkSection(); @@ -23,18 +25,12 @@ public class FlatWorld implements World { public EntityLocation getSpawn() { if (this.spawn == null) { log.warn("Spawn is not defined! Set spawn [0, 6, 0]"); - this.spawn = new EntityLocation(0d, 6d, 0d, 0f, 0f, this); + this.spawn = new EntityLocation(0d, 6d, 0d, 0f, 0f); } return this.spawn; } - @Override - public void setSpawn(EntityLocation location) { - this.spawn = location; - this.spawn.setWorld(this); - } - @Override public Chunk getChunk(int x, int z) { Chunk chunk = new SimpleChunk(x, z, this); diff --git a/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java b/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java index 414cbb4..4c5b8a8 100644 --- a/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java +++ b/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java @@ -9,12 +9,8 @@ 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.Chunk; import mc.core.world.chunk.ChunkSection; -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; - public class SimpleChunkSection implements ChunkSection { @Override public int getSkyLight(int x, int y, int z) { @@ -63,10 +59,10 @@ public class SimpleChunkSection implements ChunkSection { public Block getBlock(int x, int y, int z) { BlockFactory blockFactory = new BlockFactory(); - if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z, getWorld()); - else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z, getWorld()); - else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z, getWorld()); - else return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); + 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); } @Override diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/TeleportManager.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/TeleportManager.java index 951f098..ba9cd07 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/TeleportManager.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/TeleportManager.java @@ -45,8 +45,7 @@ public class TeleportManager { public void apply(int teleportId) { if (teleportMap.containsKey(teleportId)) { TpData data = teleportMap.remove(teleportId); - data.player.getLocation().setXYZ(data.newLocation); - data.player.getLocation().setYawPitch(data.newLocation); + data.player.getLocation().set(data.newLocation); } } 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..3586c01 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 @@ -2,7 +2,7 @@ package mc.core.network.proto_1_12_2.packets; import lombok.Getter; import lombok.ToString; -import mc.core.Location; +import mc.core.world.block.BlockLocation; import mc.core.network.CSPacket; import mc.core.network.NetInputStream; import mc.core.network.proto_1_12_2.Direction; @@ -11,7 +11,7 @@ import mc.core.utils.CompactedCoords; @Getter @ToString public class PlayerBlockPlacementPacket implements CSPacket { - private Location location; + private BlockLocation location; private Direction face; /** true - main hand; false - off hand */ private boolean hand; @@ -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 BlockLocation((int)xyz[0], (int)xyz[1], (int)xyz[2]); //FIXME 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..2081e7c 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 @@ -3,7 +3,7 @@ package mc.core.network.proto_1_12_2.packets; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; -import mc.core.Location; +import mc.core.world.block.BlockLocation; import mc.core.network.CSPacket; import mc.core.network.NetInputStream; import mc.core.network.proto_1_12_2.Direction; @@ -42,7 +42,7 @@ public class PlayerDiggingPacket implements CSPacket { } private Status status; - private Location location; + private BlockLocation location; private Direction face; @Override @@ -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 BlockLocation((int)xyz[0], (int)xyz[1], (int)xyz[2]); //FIXME face = Direction.getById(netStream.readByte()); } } diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerPositionAndLookPacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerPositionAndLookPacket.java index 985d8df..3c783a7 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerPositionAndLookPacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerPositionAndLookPacket.java @@ -48,8 +48,7 @@ public class PlayerPositionAndLookPacket implements SCPacket, CSPacket { netStream.readDouble(), netStream.readDouble(), netStream.readFloat(), - netStream.readFloat(), - null + netStream.readFloat() ); this.onGround = netStream.readBoolean(); 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..558e8dc 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 @@ -4,7 +4,7 @@ */ package mc.core.network.proto_1_12_2.packets; -import mc.core.Location; +import mc.core.world.block.BlockLocation; import mc.core.network.CSPacket; import mc.core.network.NetInputStream; @@ -12,7 +12,7 @@ public class TabCompletePacket implements CSPacket { private String text; private boolean assumeCommand; private boolean hasPosition; - private Location location; + private BlockLocation location; @Override public void readSelf(NetInputStream netStream) { @@ -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 BlockLocation((int)x, (int)y, (int)z); //FIXME } } } 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/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java index bcfe393..4c7e533 100644 --- a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java +++ b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java @@ -29,7 +29,7 @@ class PlayerEventListener { public void playerChunkLoadHandler(SC_ChunkLoadEvent event) { for(Integer compressXZ : event.getNeedLoadChunks()) { int[] xz = CompactedCoords.uncompressXZ(compressXZ); - Chunk chunk = event.getPlayer().getLocation().getWorld().getChunk(xz[0], xz[1]); + Chunk chunk = event.getPlayer().getWorld().getChunk(xz[0], xz[1]); ChunkDataPacket packet = new ChunkDataPacket(); packet.setX(xz[0]); diff --git a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java index 771b1c3..d38af9c 100644 --- a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java +++ b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java @@ -20,6 +20,7 @@ import mc.core.text.TextColor; import mc.core.text.TextStyle; import mc.core.utils.CompactedCoords; import mc.core.world.World; +import mc.core.world.chunk.Chunk; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -50,7 +51,8 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand Player player = playerManager.getPlayer(packet.getPlayerName()) .orElseGet(() -> playerManager.createPlayer( packet.getPlayerName(), - world.getSpawn())); + world.getSpawn(), + world)); channel.writeAndFlush(new LoginSuccessPacket( player.getUUID(), @@ -83,9 +85,10 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand // First Chunk ChunkDataPacket pkt8 = new ChunkDataPacket(); - pkt8.setX(player.getLocation().getChunk().getX()); - pkt8.setZ(player.getLocation().getChunk().getZ()); - pkt8.setChunk(player.getLocation().getChunk()); + Chunk chunk = player.getWorld().getChunk(player.getLocation()); + pkt8.setX(chunk.getX()); + pkt8.setZ(chunk.getZ()); + pkt8.setChunk(chunk); pkt8.setInitChunk(true); channel.writeAndFlush(pkt8); player.getLoadedChunks().add(CompactedCoords.compressXZ(0, 0)); diff --git a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/PlayHandler.java b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/PlayHandler.java index d7636e3..d132486 100644 --- a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/PlayHandler.java +++ b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/PlayHandler.java @@ -55,8 +55,7 @@ public class PlayHandler extends AbstractStateHandler implements PlayStateHandle @Handler public void onPositionAndLook(Channel channel, PlayerPositionAndLookPacket packet) { Player player = channel.attr(ATTR_PLAYER).get(); - player.getLocation().setXYZ(packet.getLocation()); - player.getLocation().setYawPitch(packet.getLocation()); + player.getLocation().set(packet.getLocation()); } @Handler @@ -82,8 +81,7 @@ public class PlayHandler extends AbstractStateHandler implements PlayStateHandle event.setNewLocation(new EntityLocation( packet.getX(), packet.getY(), packet.getZ(), player.getLocation().getYaw(), - player.getLocation().getPitch(), - player.getLocation().getWorld() + player.getLocation().getPitch() )); EventBusGetter.getInstance().post(event); } From 27c40e86e60fc3f3cec09e697c3946a8ff1c2993 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 14:26:05 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82?= =?UTF-8?q?=D0=B0=20PlayerAbilities=20(+=D1=82=D0=B5=D1=81=D1=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../packets/PlayerAbilitiesPacket.java | 22 +++--- .../packets/ByteArrayInputNetStream.java | 73 +++++++++++++++++++ .../packets/TestByteArrayInputNetStream.java | 49 +++++++++++++ .../packets/TestPlayerAbilitiesPacket.java | 40 ++++++++++ 4 files changed, 175 insertions(+), 9 deletions(-) create mode 100644 proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/ByteArrayInputNetStream.java create mode 100644 proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestByteArrayInputNetStream.java create mode 100644 proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestPlayerAbilitiesPacket.java diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerAbilitiesPacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerAbilitiesPacket.java index b3a8312..6258bc5 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerAbilitiesPacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/PlayerAbilitiesPacket.java @@ -18,6 +18,11 @@ import mc.core.network.SCPacket; @Setter @ToString public class PlayerAbilitiesPacket implements SCPacket, CSPacket { + private static final byte $GOD_MODE_MASK = 0x01, + $FLYING_MASK = 0x02, + $CAN_FLY_MASK = 0x04, + $IDB_MASK = 0x08; + private boolean godMode = false; private boolean flying = false; private boolean canFly = false; @@ -29,10 +34,10 @@ public class PlayerAbilitiesPacket implements SCPacket, CSPacket { @Override public void writeSelf(NetOutputStream netStream) { byte flag = 0; - if (godMode) flag = (byte)(flag | 0x01); - if (flying) flag = (byte)(flag | 0x02); - if (canFly) flag = (byte)(flag | 0x04); - if (instantDestroyBlocks) flag = (byte)(flag | 0x08); + if (godMode) flag = (byte)(flag | $GOD_MODE_MASK); + if (flying) flag = (byte)(flag | $FLYING_MASK); + if (canFly) flag = (byte)(flag | $CAN_FLY_MASK); + if (instantDestroyBlocks) flag = (byte)(flag | $IDB_MASK); netStream.writeByte(flag); netStream.writeFloat(flyingSpeed); @@ -42,11 +47,10 @@ public class PlayerAbilitiesPacket implements SCPacket, CSPacket { @Override public void readSelf(NetInputStream netStream) { byte flag = netStream.readByte(); - //FIXME треубет проверки - godMode = (flag == 0x08); - canFly = (flag == 0x04); - flying = (flag == 0x02); - instantDestroyBlocks = (flag == 0x01); + godMode = (flag & $GOD_MODE_MASK) > 0; + canFly = (flag & $CAN_FLY_MASK) > 0; + flying = (flag & $FLYING_MASK) > 0; + instantDestroyBlocks = (flag & $IDB_MASK) > 0; flyingSpeed = netStream.readFloat(); walkingSpeed = netStream.readFloat(); diff --git a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/ByteArrayInputNetStream.java b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/ByteArrayInputNetStream.java new file mode 100644 index 0000000..2c17c19 --- /dev/null +++ b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/ByteArrayInputNetStream.java @@ -0,0 +1,73 @@ +package mc.core.network.proto_1_12_2.packets; + +import mc.core.network.proto_1_12_2.NetInputStream_p340; + +import java.io.ByteArrayInputStream; +import java.io.EOFException; + +public class ByteArrayInputNetStream extends NetInputStream_p340 { + private ByteArrayInputStream bais; + + public ByteArrayInputNetStream(byte[] buff) { + bais = new ByteArrayInputStream(buff); + } + + @Override + public boolean readBoolean() { + return false; + } + + @Override + public byte readByte() { + return (byte) bais.read(); + } + + @Override + public void readBytes(byte[] buffer) { + } + + @Override + public int readUnsignedByte() { + return 0; + } + + @Override + public int readUnsignedShort() { + return 0; + } + + @Override + public short readShort() { + return 0; + } + + @Override + public int readInt() { + int ch1 = bais.read(); + int ch2 = bais.read(); + int ch3 = bais.read(); + int ch4 = bais.read(); + if ((ch1 | ch2 | ch3 | ch4) < 0) return 0; + return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); + } + + @Override + public long readLong() { + return 0; + } + + @Override + public float readFloat() { + return Float.intBitsToFloat(readInt()); + } + + @Override + public double readDouble() { + return 0; + } + + @Override + public void skipBytes(int count) { + + } +} diff --git a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestByteArrayInputNetStream.java b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestByteArrayInputNetStream.java new file mode 100644 index 0000000..85bfc7c --- /dev/null +++ b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestByteArrayInputNetStream.java @@ -0,0 +1,49 @@ +package mc.core.network.proto_1_12_2.packets; + +import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.util.Random; + +import static org.junit.Assert.*; + +public class TestByteArrayInputNetStream { + private Random rnd = new Random(); + + @Test + public void testReadByte() { + final byte b0 = (byte) rnd.nextInt(); + ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream(); + netStream.writeByte(b0); + byte[] buffer = netStream.toByteArray(); + + ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(buffer); + byte b1 = netInputStream.readByte(); + assertEquals(b0, b1); + } + + @Test + public void testReadInt() { + final int i0 = rnd.nextInt(); + ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream(); + netStream.writeInt(i0); + byte[] buffer = netStream.toByteArray(); + + ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(buffer); + int i1 = netInputStream.readInt(); + assertEquals(i0, i1); + } + + @Test + public void testReadFloat() { + final float f0 = rnd.nextFloat(); + ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream(); + netStream.writeFloat(f0); + byte[] buffer = netStream.toByteArray(); + + ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(buffer); + float f1 = netInputStream.readFloat(); + assertEquals(f0, f1, 0.0f); + } +} \ No newline at end of file diff --git a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestPlayerAbilitiesPacket.java b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestPlayerAbilitiesPacket.java new file mode 100644 index 0000000..ec9b150 --- /dev/null +++ b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestPlayerAbilitiesPacket.java @@ -0,0 +1,40 @@ +package mc.core.network.proto_1_12_2.packets; + +import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream; +import org.junit.Before; +import org.junit.Test; + +import java.util.Random; + +import static org.junit.Assert.assertEquals; + +public class TestPlayerAbilitiesPacket { + private Random rnd = new Random(); + private PlayerAbilitiesPacket packet; + + @Before + public void before() { + packet = new PlayerAbilitiesPacket(); + packet.setGodMode(rnd.nextBoolean()); + packet.setFlying(rnd.nextBoolean()); + packet.setCanFly(rnd.nextBoolean()); + packet.setInstantDestroyBlocks(rnd.nextBoolean()); + packet.setFlyingSpeed(rnd.nextFloat()); + } + + @Test + public void test() { + ByteArrayOutputNetStream netOutputStream = new ByteArrayOutputNetStream(); + packet.writeSelf(netOutputStream); + + ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(netOutputStream.toByteArray()); + PlayerAbilitiesPacket outPkt = new PlayerAbilitiesPacket(); + outPkt.readSelf(netInputStream); + + assertEquals("god mode", packet.isGodMode(), outPkt.isGodMode()); + assertEquals("flying", packet.isFlying(), outPkt.isFlying()); + assertEquals("can fly", packet.isCanFly(), outPkt.isCanFly()); + assertEquals("instant destroy block", packet.isInstantDestroyBlocks(), outPkt.isInstantDestroyBlocks()); + assertEquals("flying speed", packet.getFlyingSpeed(), outPkt.getFlyingSpeed(), 0.0f); + } +} From ad31a904555c0226517c2fdbd8ade950beb8d7cb Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 14:54:29 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20ImmutableEntityLocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/mc/core/ImmutableEntityLocation.java | 57 +++++++++++++++++++ .../eventbus/events/CS_PlayerMoveEvent.java | 11 ++-- .../mc/core/TestImmutableEntityLocation.java | 35 ++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/mc/core/ImmutableEntityLocation.java create mode 100644 core/src/test/java/mc/core/TestImmutableEntityLocation.java diff --git a/core/src/main/java/mc/core/ImmutableEntityLocation.java b/core/src/main/java/mc/core/ImmutableEntityLocation.java new file mode 100644 index 0000000..f71c1e3 --- /dev/null +++ b/core/src/main/java/mc/core/ImmutableEntityLocation.java @@ -0,0 +1,57 @@ +package mc.core; + +public class ImmutableEntityLocation extends EntityLocation { + public ImmutableEntityLocation(double x, double y, double z, float yaw, float pitch) { + super(x, y, z, yaw, pitch); + } + + public ImmutableEntityLocation(EntityLocation location) { + this( + location.getX(), + location.getY(), + location.getZ(), + location.getYaw(), + location.getPitch() + ); + } + + @Override + public void setX(double x) { + throw new UnsupportedOperationException(); + } + + @Override + public void setY(double y) { + throw new UnsupportedOperationException(); + } + + @Override + public void setZ(double z) { + throw new UnsupportedOperationException(); + } + + @Override + public void setYaw(float yaw) { + throw new UnsupportedOperationException(); + } + + @Override + public void setPitch(float pitch) { + throw new UnsupportedOperationException(); + } + + @Override + public void set(EntityLocation location) { + throw new UnsupportedOperationException(); + } + + @Override + public void setXYZ(double x, double y, double z) { + throw new UnsupportedOperationException(); + } + + @Override + public void setYawPitch(float yaw, float pitch) { + throw new UnsupportedOperationException(); + } +} diff --git a/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java b/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java index ae60200..4d759ab 100644 --- a/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java +++ b/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java @@ -5,20 +5,23 @@ package mc.core.eventbus.events; import lombok.Getter; -import lombok.RequiredArgsConstructor; import lombok.Setter; import mc.core.EntityLocation; +import mc.core.ImmutableEntityLocation; import mc.core.eventbus.EventBase; import mc.core.player.Player; -@RequiredArgsConstructor @Getter public class CS_PlayerMoveEvent extends EventBase { private final Player player; - private final EntityLocation oldLocation; // TODO сомнительное решение - // вообще нужно будет создать реализацию "иммутабл локейшен" для подобных ситуаций + private final ImmutableEntityLocation oldLocation; @Setter private EntityLocation newLocation; @Setter private boolean recalcChunk = false; + + public CS_PlayerMoveEvent(Player player, EntityLocation oldLocation) { + this.player = player; + this.oldLocation = new ImmutableEntityLocation(oldLocation); + } } diff --git a/core/src/test/java/mc/core/TestImmutableEntityLocation.java b/core/src/test/java/mc/core/TestImmutableEntityLocation.java new file mode 100644 index 0000000..2f6a958 --- /dev/null +++ b/core/src/test/java/mc/core/TestImmutableEntityLocation.java @@ -0,0 +1,35 @@ +package mc.core; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; + +public class TestImmutableEntityLocation { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testSetValue() { + EntityLocation location = new ImmutableEntityLocation(1d, 2d, 3d, 4f, 5f); + + thrown.expect(UnsupportedOperationException.class); + location.setX(1); + location.setY(1); + location.setZ(1); + location.setYaw(1); + location.setPitch(1); + location.setXYZ(1, 2, 3); + location.setYawPitch(1, 2); + location.set(EntityLocation.ZERO()); + } + + @Test + public void testClone() { + EntityLocation locOrig = new ImmutableEntityLocation(1d, 2d, 3d, 4f, 5f); + EntityLocation locClone = locOrig.clone(); + + assertEquals(locOrig, locClone); + } +} \ No newline at end of file From debb75a0802ea0e3f8df3eba43a9a46af3aacf2f Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 17:04:58 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20BlockLocationSerializer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mc/core/world/block/BlockLocation.java | 4 ++ .../serializers/BlockLocationSerializer.java | 63 +++++++++++++++++++ .../TestBlockLocationSerializer.java | 32 ++++++++++ 3 files changed, 99 insertions(+) create mode 100644 proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/serializers/BlockLocationSerializer.java create mode 100644 proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/serializers/TestBlockLocationSerializer.java diff --git a/core/src/main/java/mc/core/world/block/BlockLocation.java b/core/src/main/java/mc/core/world/block/BlockLocation.java index cabccaa..7c9ca85 100644 --- a/core/src/main/java/mc/core/world/block/BlockLocation.java +++ b/core/src/main/java/mc/core/world/block/BlockLocation.java @@ -8,6 +8,10 @@ import lombok.*; public class BlockLocation implements Cloneable { private int x, y, z; + public static BlockLocation ZERO() { + return new BlockLocation(0,0,0); + } + public void setXYZ(int x, int y, int z) { this.x = x; this.y = y; diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/serializers/BlockLocationSerializer.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/serializers/BlockLocationSerializer.java new file mode 100644 index 0000000..9aea2f5 --- /dev/null +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/serializers/BlockLocationSerializer.java @@ -0,0 +1,63 @@ +package mc.core.network.proto_1_12_2.serializers; + +import mc.core.world.block.BlockLocation; + +import static com.google.common.math.IntMath.isPowerOfTwo; + +public class BlockLocationSerializer { + private static final int[] MULTIPLY_DE_BRUIJN_BIT_POSITION = new int[] {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9}; + private static final int NUM_X_BITS = 1 + log2(smallestEncompassingPowerOfTwo(30000000)); + private static final int NUM_Z_BITS = NUM_X_BITS; + private static final int NUM_Y_BITS = 64 - NUM_X_BITS - NUM_Z_BITS; + private static final int Y_SHIFT = NUM_Z_BITS; + private static final int X_SHIFT = Y_SHIFT + NUM_Y_BITS; + private static final long X_MASK = (1L << NUM_X_BITS) - 1L; + private static final long Y_MASK = (1L << NUM_Y_BITS) - 1L; + private static final long Z_MASK = (1L << NUM_Z_BITS) - 1L; + + /* + * net.minecraft.util.math.MathHelper#log2(int) + */ + private static int log2(int value) { + return log2DeBruijn(value) - (isPowerOfTwo(value) ? 0 : 1); + } + + /* + * net.minecraft.util.math.MathHelper#log2DeBruijn(int) + */ + private static int log2DeBruijn(int value) { + value = isPowerOfTwo(value) ? value : smallestEncompassingPowerOfTwo(value); + return MULTIPLY_DE_BRUIJN_BIT_POSITION[(int)((long)value * 125613361L >> 27) & 31]; + } + + /* + * net.minecraft.util.math.MathHelper#smallestEncompassingPowerOfTwo(int) + */ + private static int smallestEncompassingPowerOfTwo(int value) { + int i = value - 1; + i = i | i >> 1; + i = i | i >> 2; + i = i | i >> 4; + i = i | i >> 8; + i = i | i >> 16; + return i + 1; + } + + public static long toLong(BlockLocation location) { + return ((long)location.getX() & X_MASK) << X_SHIFT | + ((long)location.getY() & Y_MASK) << Y_SHIFT | + ((long)location.getZ() & Z_MASK); + } + + public static BlockLocation fromLong(long value) { + BlockLocation location = BlockLocation.ZERO(); + fromLong(value, location); + return location; + } + + public static void fromLong(long value, BlockLocation location) { + location.setX((int)(value << 64 - X_SHIFT - NUM_X_BITS >> 64 - NUM_X_BITS)); + location.setY((int)(value << 64 - Y_SHIFT - NUM_Y_BITS >> 64 - NUM_Y_BITS)); + location.setZ((int)(value << 64 - NUM_Z_BITS >> 64 - NUM_Z_BITS)); + } +} diff --git a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/serializers/TestBlockLocationSerializer.java b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/serializers/TestBlockLocationSerializer.java new file mode 100644 index 0000000..28db9ea --- /dev/null +++ b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/serializers/TestBlockLocationSerializer.java @@ -0,0 +1,32 @@ +package mc.core.network.proto_1_12_2.serializers; + +import mc.core.world.block.BlockLocation; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.ThreadLocalRandom; + +import static org.junit.Assert.*; + +public class TestBlockLocationSerializer { + private static final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + private static final int minI = 0, maxI = 10; + private int x, y, z; + + @Before + public void before() { + x = rnd.nextInt(minI, maxI); + y = rnd.nextInt(minI, maxI); + z = rnd.nextInt(minI, maxI); + } + + @Test + public void test() { + BlockLocation location = new BlockLocation(x, y, z); + final long serializedCoords = BlockLocationSerializer.toLong(location); + + BlockLocation deserLoc = BlockLocationSerializer.fromLong(serializedCoords); + + assertEquals(location, deserLoc); + } +} \ No newline at end of file From fb6ced9c9bc6470790545e902d6274d91b4e7853 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 17:05:26 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4?= =?UTF-8?q?=20=D0=BD=D0=B0=20BlockLocationSerializer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/mc/core/utils/CompactedCoords.java | 14 -------------- .../java/mc/core/utils/TestCompactedCoords.java | 17 ----------------- .../packets/PlayerBlockPlacementPacket.java | 4 ++-- .../packets/PlayerDiggingPacket.java | 4 ++-- 4 files changed, 4 insertions(+), 35 deletions(-) diff --git a/core/src/main/java/mc/core/utils/CompactedCoords.java b/core/src/main/java/mc/core/utils/CompactedCoords.java index 43daef0..cac3db6 100644 --- a/core/src/main/java/mc/core/utils/CompactedCoords.java +++ b/core/src/main/java/mc/core/utils/CompactedCoords.java @@ -25,18 +25,4 @@ public class CompactedCoords { int i = (int)value; return value < (double)i ? i - 1 : i; } - - public static long compressXYZ(double x, double y, double z) { - return ((floor_double(x) & 0x3FFFFFF) << 38) - | ((floor_double(y) & 0xFFF) << 26) - | (floor_double(z) & 0x3FFFFFF); - } - - public static double[] uncompressXYZ(long compactValue) { - return new double[]{ - compactValue >> 38, - (compactValue >> 26) & 0x0FFF, - compactValue << 38 >> 38 // is normal? - }; - } } diff --git a/core/src/test/java/mc/core/utils/TestCompactedCoords.java b/core/src/test/java/mc/core/utils/TestCompactedCoords.java index bced6f2..b357cef 100644 --- a/core/src/test/java/mc/core/utils/TestCompactedCoords.java +++ b/core/src/test/java/mc/core/utils/TestCompactedCoords.java @@ -2,7 +2,6 @@ package mc.core.utils; import org.junit.Test; -import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import static org.junit.Assert.assertEquals; @@ -24,20 +23,4 @@ public class TestCompactedCoords { assertEquals(z, xz[1]); } } - -// @Test - public void testXYZSimple() { - for (int z = -100; z <= 100; z++) { - for (int x = -100; x <= 100; x++) { - for (int y = -100; y <= 100; y++) { - long compressXYZ = CompactedCoords.compressXYZ(x, y, z); - double[] xyz = CompactedCoords.uncompressXYZ(compressXYZ); - - assertEquals(x, xyz[0], 0.001d); - assertEquals(y, xyz[1], 0.001d); - assertEquals(z, xyz[2], 0.001d); - } - } - } - } } 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 3586c01..c40fffd 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 @@ -2,6 +2,7 @@ package mc.core.network.proto_1_12_2.packets; import lombok.Getter; import lombok.ToString; +import mc.core.network.proto_1_12_2.serializers.BlockLocationSerializer; import mc.core.world.block.BlockLocation; import mc.core.network.CSPacket; import mc.core.network.NetInputStream; @@ -20,8 +21,7 @@ public class PlayerBlockPlacementPacket implements CSPacket { @Override public void readSelf(NetInputStream netStream) { long compactedCoords = netStream.readLong(); - double[] xyz = CompactedCoords.uncompressXYZ(compactedCoords); - location = new BlockLocation((int)xyz[0], (int)xyz[1], (int)xyz[2]); //FIXME + location = BlockLocationSerializer.fromLong(compactedCoords); 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 2081e7c..9ce0dc9 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 @@ -3,6 +3,7 @@ package mc.core.network.proto_1_12_2.packets; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; +import mc.core.network.proto_1_12_2.serializers.BlockLocationSerializer; import mc.core.world.block.BlockLocation; import mc.core.network.CSPacket; import mc.core.network.NetInputStream; @@ -49,8 +50,7 @@ public class PlayerDiggingPacket implements CSPacket { public void readSelf(NetInputStream netStream) { status = Status.getById(netStream.readVarInt()); long compactCoord = netStream.readLong(); - double[] xyz = CompactedCoords.uncompressXYZ(compactCoord); - location = new BlockLocation((int)xyz[0], (int)xyz[1], (int)xyz[2]); //FIXME + location = BlockLocationSerializer.fromLong(compactCoord); face = Direction.getById(netStream.readByte()); } }