From f09dacf7953fb72b2e9310fd4d89234f309291eb Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 24 Feb 2022 23:29:41 +0100 Subject: [PATCH] Misc cleanup --- .../minestom/server/instance/EntityTrackerImpl.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java b/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java index 283ffc45d..9fddc6717 100644 --- a/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java +++ b/src/main/java/net/minestom/server/instance/EntityTrackerImpl.java @@ -15,13 +15,11 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; -import java.util.function.Supplier; import static net.minestom.server.utils.chunk.ChunkUtils.*; final class EntityTrackerImpl implements EntityTracker { static final AtomicInteger TARGET_COUNTER = new AtomicInteger(); - private static final Supplier> LIST_SUPPLIER = CopyOnWriteArrayList::new; // Store all data associated to a Target // The array index is the Target enum ordinal @@ -120,7 +118,7 @@ final class EntityTrackerImpl implements EntityTracker { public @Unmodifiable Collection chunkEntities(int chunkX, int chunkZ, @NotNull Target target) { final TargetEntry entry = entries[target.ordinal()]; //noinspection unchecked - var chunkEntities = (List) entry.chunkEntities.computeIfAbsent(getChunkIndex(chunkX, chunkZ), i -> LIST_SUPPLIER.get()); + var chunkEntities = (List) entry.chunkEntities(getChunkIndex(chunkX, chunkZ)); return Collections.unmodifiableList(chunkEntities); } @@ -142,8 +140,7 @@ final class EntityTrackerImpl implements EntityTracker { chunkIndex -> { final int count = getChunkCount(range); List> entities = new ArrayList<>(count); - forChunksInRange(chunkX, chunkZ, range, - (x, z) -> entities.add(entry.chunkEntities.computeIfAbsent(getChunkIndex(x, z), i -> (List) LIST_SUPPLIER.get()))); + forChunksInRange(chunkX, chunkZ, range, (x, z) -> entities.add(entry.chunkEntities(getChunkIndex(x, z)))); assert entities.size() == count : "Expected " + count + " chunks, got " + entities.size(); return List.copyOf(entities); }); @@ -183,8 +180,12 @@ final class EntityTrackerImpl implements EntityTracker { this.target = target; } + List chunkEntities(long index) { + return chunkEntities.computeIfAbsent(index, i -> (List) new CopyOnWriteArrayList()); + } + void addToChunk(long index, T entity) { - this.chunkEntities.computeIfAbsent(index, i -> (List) LIST_SUPPLIER.get()).add(entity); + chunkEntities(index).add(entity); } void removeFromChunk(long index, T entity) {