From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samsuik Date: Thu, 1 May 2025 22:28:50 +0200 Subject: [PATCH] Sakura: copy-EntityList-implementation-to-BasicEntityList diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java index 956d48fb7146b9eb2a5b5b4e23a83f60d0e40b4c..5847cbe6229f217d0361eda0950668bd0e9573f4 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java +++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java @@ -415,6 +415,13 @@ public final class ChunkEntitySlices { private E[] storage; private int size; + // Sakura start - use methods from EntityList + private it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap entityToIndex = null; + private void setupIndexMap() { + this.entityToIndex = new it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap(2, 0.8f); + this.entityToIndex.defaultReturnValue(Integer.MIN_VALUE); + } + // Sakura end - use methods from EntityList public BasicEntityList() { this(0); @@ -435,6 +442,7 @@ public final class ChunkEntitySlices { private void resize() { if (this.storage == me.titaniumtown.ArrayConstants.emptyEntityArray) { // Gale - JettPack - reduce array allocations this.storage = (E[])new Entity[DEFAULT_CAPACITY]; + this.setupIndexMap(); // Sakura - use methods from EntityList } else { this.storage = Arrays.copyOf(this.storage, this.storage.length * 2); } @@ -448,6 +456,7 @@ public final class ChunkEntitySlices { } else { this.storage[idx] = entity; } + this.entityToIndex.put(entity.getId(), idx); // Sakura - use methods from EntityList } public int indexOf(final E entity) { @@ -463,24 +472,32 @@ public final class ChunkEntitySlices { } public boolean remove(final E entity) { - final int idx = this.indexOf(entity); - if (idx == -1) { + // Sakura start - use methods from EntityList + if (this.entityToIndex == null) { return false; } - final int size = --this.size; - final E[] storage = this.storage; - if (idx != size) { - System.arraycopy(storage, idx + 1, storage, idx, size - idx); + final int index = this.entityToIndex.remove(entity.getId()); + if (index == Integer.MIN_VALUE) { + return false; } - storage[size] = null; + // move the entity at the end to this index + final int endIndex = --this.size; + final E end = this.storage[endIndex]; + if (index != endIndex) { + // not empty after this call + this.entityToIndex.put(end.getId(), index); // update index + } + this.storage[index] = end; + this.storage[endIndex] = null; + // Sakura end - use methods from EntityList return true; } public boolean has(final E entity) { - return this.indexOf(entity) != -1; + return this.entityToIndex != null && this.entityToIndex.containsKey(entity.getId()); // Sakura - use methods from EntityList } }