Add ChunkUtils#getChunkCount

This commit is contained in:
themode 2022-02-13 10:27:24 +01:00
parent 4459bbeebd
commit 84d154b8db
4 changed files with 44 additions and 23 deletions

View File

@ -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<List<T>> entities = new ArrayList<>();
final int count = ChunkUtils.getChunkCount(range);
List<List<T>> 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<T>) LIST_SUPPLIER.get())));
assert entities.size() == count : "Expected " + count + " chunks, got " + entities.size();
return List.copyOf(entities);
});
}

View File

@ -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) {

View File

@ -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

View File

@ -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());
}
}
}