From 2afb170ff018d1185a9fde19af81379c63d5d8c5 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 11 Aug 2019 08:57:23 +0200 Subject: [PATCH] Fixed chunk --- .../fr/themode/minestom/entity/Entity.java | 4 +- .../minestom/entity/EntityManager.java | 42 ++++++++++--------- .../themode/minestom/instance/Instance.java | 12 +++--- .../packet/client/login/LoginStartPacket.java | 3 +- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/java/fr/themode/minestom/entity/Entity.java b/src/main/java/fr/themode/minestom/entity/Entity.java index 38e7ac1d3..5a67e09c5 100644 --- a/src/main/java/fr/themode/minestom/entity/Entity.java +++ b/src/main/java/fr/themode/minestom/entity/Entity.java @@ -16,8 +16,6 @@ public class Entity { private boolean isActive; // False if entity has only been instanced without being added somewhere private boolean shouldRemove; - private Object monitor = new Object(); - public Entity() { this.id = generateId(); this.uuid = UUID.randomUUID(); @@ -68,7 +66,7 @@ public class Entity { if (instance != null) { Chunk lastChunk = instance.getChunkAt(lastX, lastZ); Chunk newChunk = instance.getChunkAt(x, z); - if (newChunk != null && lastChunk != newChunk) { + if (lastChunk != null && newChunk != null && lastChunk != newChunk) { synchronized (lastChunk) { synchronized (newChunk) { lastChunk.removeEntity(this); diff --git a/src/main/java/fr/themode/minestom/entity/EntityManager.java b/src/main/java/fr/themode/minestom/entity/EntityManager.java index 487eb0b84..30f83b0ed 100644 --- a/src/main/java/fr/themode/minestom/entity/EntityManager.java +++ b/src/main/java/fr/themode/minestom/entity/EntityManager.java @@ -17,27 +17,31 @@ public class EntityManager { public void update() { for (Instance instance : instanceManager.getInstances()) { - // Creatures - for (EntityCreature creature : instance.getCreatures()) { - creaturesPool.submit(() -> { - creature.update(); - boolean shouldRemove = creature.shouldRemove(); - if (shouldRemove) { - instance.removeEntity(creature); - } - }); + + synchronized (instance) { + // Creatures + for (EntityCreature creature : instance.getCreatures()) { + creaturesPool.submit(() -> { + creature.update(); + boolean shouldRemove = creature.shouldRemove(); + if (shouldRemove) { + instance.removeEntity(creature); + } + }); + } + + // Players + for (Player player : instance.getPlayers()) { + playersPool.submit(() -> { + player.update(); + boolean shouldRemove = player.shouldRemove(); + if (shouldRemove) { + instance.removeEntity(player); + } + }); + } } - // Players - for (Player player : instance.getPlayers()) { - playersPool.submit(() -> { - player.update(); - boolean shouldRemove = player.shouldRemove(); - if (shouldRemove) { - instance.removeEntity(player); - } - }); - } } } diff --git a/src/main/java/fr/themode/minestom/instance/Instance.java b/src/main/java/fr/themode/minestom/instance/Instance.java index dc47c5ed4..25f3e3add 100644 --- a/src/main/java/fr/themode/minestom/instance/Instance.java +++ b/src/main/java/fr/themode/minestom/instance/Instance.java @@ -20,11 +20,11 @@ public class Instance { } // TODO BlockBatch with pool - public void setBlock(int x, int y, int z, Block block) { - Chunk chunk = getChunkAt(x, z); + public synchronized void setBlock(int x, int y, int z, Block block) { + int chunkX = Math.floorDiv(x, 16); + int chunkZ = Math.floorDiv(z, 16); + Chunk chunk = getChunk(chunkX, chunkZ); if (chunk == null) { - int chunkX = x / 16; - int chunkZ = z / 16; chunk = new Chunk(Biome.VOID, chunkX, chunkZ); this.chunksSet.add(chunk); } @@ -42,8 +42,8 @@ public class Instance { } public Chunk getChunkAt(double x, double z) { - int chunkX = (int) (x / 16); - int chunkZ = (int) (z / 16); + int chunkX = Math.floorDiv((int) x, 16); + int chunkZ = Math.floorDiv((int) z, 16); return getChunk(chunkX, chunkZ); } diff --git a/src/main/java/fr/themode/minestom/net/packet/client/login/LoginStartPacket.java b/src/main/java/fr/themode/minestom/net/packet/client/login/LoginStartPacket.java index 4ac181627..f93d01849 100644 --- a/src/main/java/fr/themode/minestom/net/packet/client/login/LoginStartPacket.java +++ b/src/main/java/fr/themode/minestom/net/packet/client/login/LoginStartPacket.java @@ -31,8 +31,9 @@ public class LoginStartPacket implements ClientPreplayPacket { static { instance = Main.getInstanceManager().createInstance(); for (int x = -64; x < 64; x++) - for (int z = -64; z < 64; z++) + for (int z = -64; z < 64; z++) { instance.setBlock(x, 4, z, new Block(1)); + } } @Override