diff --git a/src/main/java/net/minestom/server/collision/CollisionUtils.java b/src/main/java/net/minestom/server/collision/CollisionUtils.java index 476e09b4e..ec86bf2c9 100644 --- a/src/main/java/net/minestom/server/collision/CollisionUtils.java +++ b/src/main/java/net/minestom/server/collision/CollisionUtils.java @@ -152,16 +152,13 @@ public class CollisionUtils { blockPos.setY((int) Math.floor(corner.getY())); blockPos.setZ((int) Math.floor(corner.getZ())); - Chunk chunk = originChunk; - if (!ChunkUtils.same(originChunk, blockPos.getX(), blockPos.getZ())) { - chunk = instance.getChunkAt(blockPos); - if (!ChunkUtils.isLoaded(chunk)) { - // Collision at chunk border - return false; - } + Chunk chunk = ChunkUtils.retrieve(instance, originChunk, blockPos); + if (!ChunkUtils.isLoaded(chunk)) { + // Collision at chunk border + return false; } - final Block block = chunk.getBlock(blockPos.getX(), blockPos.getY(), blockPos.getZ()); + final Block block = chunk.getBlock(blockPos); // TODO: block collision boxes // TODO: for the moment, always consider a full block diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 696734508..140268796 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -538,14 +538,10 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer, // World border collision final Position finalVelocityPosition = CollisionUtils.applyWorldBorder(instance, position, newPosition); - Chunk finalChunk = currentChunk; - if (!ChunkUtils.same(position, finalVelocityPosition)) { - finalChunk = instance.getChunkAt(finalVelocityPosition); - + final Chunk finalChunk = ChunkUtils.retrieve(instance, currentChunk, finalVelocityPosition); + if (!ChunkUtils.isLoaded(finalChunk)) { // Entity shouldn't be updated when moving in an unloaded chunk - if (!ChunkUtils.isLoaded(finalChunk)) { - return; - } + return; } // Apply the position if changed @@ -612,12 +608,9 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer, for (int y = minY; y <= maxY; y++) { for (int x = minX; x <= maxX; x++) { for (int z = minZ; z <= maxZ; z++) { - Chunk chunk = currentChunk; - if (!ChunkUtils.same(currentChunk, x, z)) { - chunk = instance.getChunkAt(x, z); - if (!ChunkUtils.isLoaded(chunk)) - continue; - } + final Chunk chunk = ChunkUtils.retrieve(instance, currentChunk, x, z); + if (!ChunkUtils.isLoaded(chunk)) + continue; final Block block = chunk.getBlock(x, y, z); final BlockHandler handler = block.getHandler(); diff --git a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java index 22be4d414..b81b1b93e 100644 --- a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java +++ b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java @@ -98,6 +98,21 @@ public final class ChunkUtils { return x1 == x2 && z1 == z2; } + public static Chunk retrieve(Instance instance, Chunk originChunk, double x, double z) { + if (!ChunkUtils.same(originChunk, x, z)) { + return instance.getChunkAt(x, z); + } + return originChunk; + } + + public static Chunk retrieve(Instance instance, Chunk originChunk, Position position) { + return retrieve(instance, originChunk, position.getX(), position.getZ()); + } + + public static Chunk retrieve(Instance instance, Chunk originChunk, BlockPosition blockPosition) { + return retrieve(instance, originChunk, blockPosition.getX(), blockPosition.getZ()); + } + /** * @param xz the instance coordinate to convert * @return the chunk X or Z based on the argument