diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index ca96f7462..ea1a263b1 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -258,7 +258,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { return; } - if (ChunkUtils.isChunkUnloaded(getInstance(), getPosition().getX(), getPosition().getZ())) { + boolean chunkUnloaded = ChunkUtils.isChunkUnloaded(instance, position.getX(), position.getZ()); + if (chunkUnloaded) { // No update for entities in unloaded chunk return; } @@ -275,6 +276,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { Position newPosition = new Position(newX, newY, newZ); + chunkUnloaded = ChunkUtils.isChunkUnloaded(instance, newX, newZ); + if (chunkUnloaded) + return; + if (!(this instanceof Player) && !noGravity) { // players handle gravity by themselves velocity.setY(velocity.getY() - gravityDragPerTick * tps); } @@ -319,6 +324,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { for (int y = minY; y <= maxY; y++) { for (int x = minX; x <= maxX; x++) { for (int z = minZ; z <= maxZ; z++) { + chunkUnloaded = ChunkUtils.isChunkUnloaded(instance, x, z); + if (chunkUnloaded) + continue; CustomBlock customBlock = instance.getCustomBlock(x, y, z); if (customBlock != null) { tmpPosition.setX(x); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 414a25677..f50d2b32d 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -404,6 +404,7 @@ public class Player extends LivingEntity { viewableChunks.add(chunk); chunk.addViewer(this); instance.sendChunk(this, chunk); + updateViewPosition(chunk); } boolean isLast = counter.get() == length - 1; if (isLast) { @@ -411,7 +412,6 @@ public class Player extends LivingEntity { super.setInstance(instance); PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(instance, firstSpawn); callEvent(PlayerSpawnEvent.class, spawnEvent); - updateViewPosition(chunk); } else { // Increment the counter of current loaded chunks counter.incrementAndGet(); @@ -736,15 +736,16 @@ public class Player extends LivingEntity { int index = newChunks[i]; int[] chunkPos = ChunkUtils.getChunkCoord(updatedVisibleChunks[index]); instance.loadOptionalChunk(chunkPos[0], chunkPos[1], chunk -> { + if (isFar && isLast) { + updatePlayerPosition(); + } + if (chunk == null) { return; // Cannot load chunk (auto load is not enabled) } this.viewableChunks.add(chunk); chunk.addViewer(this); instance.sendChunk(this, chunk); - if (isFar && isLast) { - updatePlayerPosition(); - } }); } } diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 0e3ec898a..75cd114b8 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -409,6 +409,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Creates an explosion at the given position with the given strength. The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}. * If no {@link ExplosionSupplier} was supplied, this method will throw an {@link IllegalStateException} + * * @param centerX * @param centerY * @param centerZ @@ -421,6 +422,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Creates an explosion at the given position with the given strength. The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}. * If no {@link ExplosionSupplier} was supplied, this method will throw an {@link IllegalStateException} + * * @param centerX * @param centerY * @param centerZ @@ -429,7 +431,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta */ public void explode(float centerX, float centerY, float centerZ, float strength, Data additionalData) { ExplosionSupplier explosionSupplier = getExplosionSupplier(); - if(explosionSupplier == null) + if (explosionSupplier == null) throw new IllegalStateException("Tried to create an explosion with no explosion supplier"); Explosion explosion = explosionSupplier.createExplosion(centerX, centerY, centerZ, strength, additionalData); explosion.apply(this); @@ -437,6 +439,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Return the registered explosion supplier, or null if none was provided + * * @return */ public ExplosionSupplier getExplosionSupplier() { @@ -445,6 +448,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Registers the explosion supplier to use in this instance + * * @param supplier */ public void setExplosionSupplier(ExplosionSupplier supplier) {