diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index 110de707d..28d08d49b 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -26,7 +26,7 @@ import java.util.UUID; public class PlayerInit { - private static InstanceContainer instanceContainer; + private static volatile InstanceContainer instanceContainer; static { ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo(); @@ -143,6 +143,7 @@ public class PlayerInit { }); player.setEventCallback(PlayerLoginEvent.class, event -> { + System.out.println("event: " + instanceContainer.hashCode()); event.setSpawningInstance(instanceContainer); }); diff --git a/src/main/java/fr/themode/minestom/entity/EntityManager.java b/src/main/java/fr/themode/minestom/entity/EntityManager.java index ccf26cd8b..70e1c07fd 100644 --- a/src/main/java/fr/themode/minestom/entity/EntityManager.java +++ b/src/main/java/fr/themode/minestom/entity/EntityManager.java @@ -32,16 +32,16 @@ public class EntityManager { // Update entities switch (updateType) { case PER_CHUNK: - chunkUpdate(instances, time); + chunkUpdate(time); break; case PER_ENTITY_TYPE: - entityTypeUpdate(instances, time); + entityTypeUpdate(time); break; case PER_INSTANCE: - instanceUpdate(instances, time); + instanceUpdate(time); break; case SINGLE_THREADED: - singleThreaded(instances, time); + singleThreaded(time); break; } @@ -52,9 +52,8 @@ public class EntityManager { * * @param time */ - private void chunkUpdate(Set instances, long time) { - // TODO optimize for when there are too many entities on one chunk - for (Instance instance : instanceManager.getInstances()) { + private void chunkUpdate(long time) { + for (Instance instance : instances) { for (Chunk chunk : instance.getChunks()) { Set entities = instance.getChunkEntities(chunk); @@ -75,8 +74,8 @@ public class EntityManager { * * @param time */ - private void entityTypeUpdate(Set instances, long time) { - for (Instance instance : instanceManager.getInstances()) { + private void entityTypeUpdate(long time) { + for (Instance instance : instances) { Set players = instance.getPlayers(); Set creatures = instance.getCreatures(); Set objects = instance.getObjectEntities(); @@ -105,10 +104,9 @@ public class EntityManager { /** * Each instance get its pool, should suppress most of the problems related to thread-safety * - * @param instances * @param time */ - private void instanceUpdate(Set instances, long time) { + private void instanceUpdate(long time) { for (Instance instance : instances) { Set players = instance.getPlayers(); Set creatures = instance.getCreatures(); @@ -133,10 +131,9 @@ public class EntityManager { /** * Single threaded update (like the notchian server) * - * @param instances * @param time */ - private void singleThreaded(Set instances, long time) { + private void singleThreaded(long time) { for (Instance instance : instances) { Set players = instance.getPlayers(); Set creatures = instance.getCreatures(); @@ -164,7 +161,9 @@ public class EntityManager { playersPool.execute(() -> { PlayerLoginEvent loginEvent = new PlayerLoginEvent(); playerCache.callEvent(PlayerLoginEvent.class, loginEvent); - Instance spawningInstance = loginEvent.getSpawningInstance() == null ? instanceManager.createInstanceContainer() : loginEvent.getSpawningInstance(); + Instance spawningInstance = loginEvent.getSpawningInstance(); + if (spawningInstance == null) + throw new NullPointerException("You need to specify a spawning instance in the PlayerLoginEvent"); playerCache.setInstance(spawningInstance); }); diff --git a/src/main/java/fr/themode/minestom/entity/Player.java b/src/main/java/fr/themode/minestom/entity/Player.java index 1217cfbe9..aca235a92 100644 --- a/src/main/java/fr/themode/minestom/entity/Player.java +++ b/src/main/java/fr/themode/minestom/entity/Player.java @@ -310,7 +310,6 @@ public class Player extends LivingEntity { int length = visibleChunks.length; AtomicInteger counter = new AtomicInteger(0); - for (int i = 0; i < length; i++) { int[] chunkPos = ChunkUtils.getChunkCoord(visibleChunks[i]); int chunkX = chunkPos[0]; @@ -322,7 +321,6 @@ public class Player extends LivingEntity { } boolean isLast = counter.get() == length - 1; if (isLast) { - System.out.println("DEBUG CHUNK: " + chunk); // This is the last chunk to be loaded , spawn player super.setInstance(instance); PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(instance);