From 84d154b8db1592d3f9725e4af8884cb8a8224b6c Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 13 Feb 2022 10:27:24 +0100 Subject: [PATCH] Add ChunkUtils#getChunkCount --- .../server/instance/EntityTrackerImpl.java | 4 +- .../server/utils/chunk/ChunkUtils.java | 8 ++++ .../server/coordinate/CoordinateTest.java | 47 ++++++++++++------- .../instance/ChunkViewerIntegrationTest.java | 8 ++-- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java b/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java index 2e293ec05..285d9a286 100644 --- a/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java +++ b/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java @@ -142,9 +142,11 @@ final class EntityTrackerImpl implements EntityTracker { var rangeRef = entry.references.computeIfAbsent(range, integer -> Long2ObjectSyncMap.hashmap()); return rangeRef.computeIfAbsent(ChunkUtils.getChunkIndex(chunkX, chunkZ), chunkIndex -> { - List> entities = new ArrayList<>(); + final int count = ChunkUtils.getChunkCount(range); + List> entities = new ArrayList<>(count); ChunkUtils.forChunksInRange(ChunkUtils.getChunkCoordX(chunkIndex), ChunkUtils.getChunkCoordZ(chunkIndex), range, (x, z) -> entities.add(entry.chunkEntities.computeIfAbsent(getChunkIndex(x, z), i -> (List) LIST_SUPPLIER.get()))); + assert entities.size() == count : "Expected " + count + " chunks, got " + entities.size(); return List.copyOf(entities); }); } 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 ec78ea2ba..63572db36 100644 --- a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java +++ b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java @@ -143,6 +143,14 @@ public final class ChunkUtils { return (int) index; } + public static int getChunkCount(int range) { + if (range < 0) { + throw new IllegalArgumentException("Range cannot be negative"); + } + final int square = range * 2 + 1; + return square * square; + } + public static void forDifferingChunksInRange(int newChunkX, int newChunkZ, int oldChunkX, int oldChunkZ, int range, @NotNull IntegerBiConsumer callback) { diff --git a/src/test/java/net/minestom/server/coordinate/CoordinateTest.java b/src/test/java/net/minestom/server/coordinate/CoordinateTest.java index 494efed84..2e834592e 100644 --- a/src/test/java/net/minestom/server/coordinate/CoordinateTest.java +++ b/src/test/java/net/minestom/server/coordinate/CoordinateTest.java @@ -1,39 +1,50 @@ package net.minestom.server.coordinate; -import net.minestom.server.coordinate.Vec; import net.minestom.server.utils.chunk.ChunkUtils; import org.junit.jupiter.api.Test; +import static net.minestom.server.utils.chunk.ChunkUtils.*; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class CoordinateTest { @Test public void chunkIndex() { - var index = ChunkUtils.getChunkIndex(2, 5); - assertEquals(2, ChunkUtils.getChunkCoordX(index)); - assertEquals(5, ChunkUtils.getChunkCoordZ(index)); + var index = getChunkIndex(2, 5); + assertEquals(2, getChunkCoordX(index)); + assertEquals(5, getChunkCoordZ(index)); - index = ChunkUtils.getChunkIndex(-5, 25); - assertEquals(-5, ChunkUtils.getChunkCoordX(index)); - assertEquals(25, ChunkUtils.getChunkCoordZ(index)); + index = getChunkIndex(-5, 25); + assertEquals(-5, getChunkCoordX(index)); + assertEquals(25, getChunkCoordZ(index)); - index = ChunkUtils.getChunkIndex(Integer.MAX_VALUE, Integer.MIN_VALUE); - assertEquals(Integer.MAX_VALUE, ChunkUtils.getChunkCoordX(index)); - assertEquals(Integer.MIN_VALUE, ChunkUtils.getChunkCoordZ(index)); + index = getChunkIndex(Integer.MAX_VALUE, Integer.MIN_VALUE); + assertEquals(Integer.MAX_VALUE, getChunkCoordX(index)); + assertEquals(Integer.MIN_VALUE, getChunkCoordZ(index)); } @Test public void chunkCoordinate() { - assertEquals(1, ChunkUtils.getChunkCoordinate(16)); - assertEquals(-1, ChunkUtils.getChunkCoordinate(-16)); - assertEquals(3, ChunkUtils.getChunkCoordinate(48)); + assertEquals(1, getChunkCoordinate(16)); + assertEquals(-1, getChunkCoordinate(-16)); + assertEquals(3, getChunkCoordinate(48)); - assertEquals(4, ChunkUtils.getChunkCoordinate(65)); - assertEquals(4, ChunkUtils.getChunkCoordinate(64)); - assertEquals(3, ChunkUtils.getChunkCoordinate(63)); - assertEquals(-2, ChunkUtils.getChunkCoordinate(-25)); - assertEquals(23, ChunkUtils.getChunkCoordinate(380)); + assertEquals(4, getChunkCoordinate(65)); + assertEquals(4, getChunkCoordinate(64)); + assertEquals(3, getChunkCoordinate(63)); + assertEquals(-2, getChunkCoordinate(-25)); + assertEquals(23, getChunkCoordinate(380)); + } + + @Test + public void chunkCount() { + assertEquals(289, getChunkCount(8)); + assertEquals(169, getChunkCount(6)); + assertEquals(121, getChunkCount(5)); + assertEquals(9, getChunkCount(1)); + assertEquals(1, getChunkCount(0)); + assertThrows(IllegalArgumentException.class, () -> getChunkCount(-1)); } @Test diff --git a/src/test/java/net/minestom/server/instance/ChunkViewerIntegrationTest.java b/src/test/java/net/minestom/server/instance/ChunkViewerIntegrationTest.java index ef5f3fc32..73a3c5a7b 100644 --- a/src/test/java/net/minestom/server/instance/ChunkViewerIntegrationTest.java +++ b/src/test/java/net/minestom/server/instance/ChunkViewerIntegrationTest.java @@ -5,6 +5,7 @@ import net.minestom.server.api.Env; import net.minestom.server.api.EnvTest; import net.minestom.server.coordinate.Pos; import net.minestom.server.network.packet.server.play.ChunkDataPacket; +import net.minestom.server.utils.chunk.ChunkUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -36,7 +37,7 @@ public class ChunkViewerIntegrationTest { @Test public void renderDistance(Env env) { final int viewRadius = MinecraftServer.getChunkViewDistance(); - final int viewLength = 1 + viewRadius * 2; + final int count = ChunkUtils.getChunkCount(viewRadius); var instance = env.createFlatInstance(); var connection = env.createConnection(); // Check initial load @@ -45,8 +46,7 @@ public class ChunkViewerIntegrationTest { var player = connection.connect(instance, new Pos(0, 40, 0)).join(); assertEquals(instance, player.getInstance()); assertEquals(new Pos(0, 40, 0), player.getPosition()); - - assertEquals(viewLength * viewLength, tracker.collect().size()); + assertEquals(count, tracker.collect().size()); } // Check chunk#sendChunk { @@ -56,7 +56,7 @@ public class ChunkViewerIntegrationTest { instance.getChunk(x, z).sendChunk(); } } - assertEquals(viewLength * viewLength, tracker.collect().size()); + assertEquals(count, tracker.collect().size()); } } }