From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samsuik Date: Thu, 27 Jun 2024 17:02:32 +0100 Subject: [PATCH] Add maxSearch to getEntities 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 8df264c86ab14f4e7aa9918a3341ef35ffaa5290..9d90db0c55df85ac26c91b42fb9e72baad940e8f 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java +++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java @@ -336,7 +336,14 @@ public final class ChunkEntitySlices { public boolean getEntities(final Entity except, final AABB box, final List into, final Predicate predicate, final int maxCount) { - return this.allEntities.getEntitiesLimited(except, box, into, predicate, maxCount); + // Sakura start - add maxSearch to getEntities + return this.getEntities(except, box, into, predicate, maxCount, Integer.MAX_VALUE); + } + + public boolean getEntities(final Entity except, final AABB box, final List into, final Predicate predicate, + final int maxCount, final int maxSearch) { + return this.allEntities.getEntitiesLimited(except, box, into, predicate, maxCount, maxSearch); + // Sakura end - add maxSearch to getEntities } public void getEntities(final EntityType type, final AABB box, final List into, @@ -574,6 +581,13 @@ public final class ChunkEntitySlices { public boolean getEntitiesLimited(final Entity except, final AABB box, final List into, final Predicate predicate, final int maxCount) { + // Sakura start - add maxSearch to getEntities + return this.getEntitiesLimited(except, box, into, predicate, maxCount, Integer.MAX_VALUE); + } + + public boolean getEntitiesLimited(final Entity except, final AABB box, final List into, final Predicate predicate, + final int maxCount, final int maxSearch) { + // Sakura end - add maxSearch to getEntities if (this.count == 0) { return false; } @@ -595,8 +609,14 @@ public final class ChunkEntitySlices { final Entity[] storage = list.storage; - for (int i = 0, len = Math.min(storage.length, list.size()); i < len; ++i) { - final Entity entity = storage[i]; + // Sakura start - add maxSearch to getEntities + final int len = Math.min(storage.length, list.size()); + final int offset = this.slices.world.random.nextInt(len); + for (int i = 0; i < len; ++i) { + final int pos = (i + offset) % len; + final Entity entity = storage[pos]; + if (i > maxSearch) break; + // Sakura end - add maxSearch to getEntities if (entity == null || entity == except || !entity.getBoundingBox().intersects(box)) { continue; diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java index 2d24d03bbdb5ee0d862cbfff2219f58afffafe12..1bf06038d51efcc103fad23670686c30d676dc0b 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java +++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java @@ -726,6 +726,13 @@ public abstract class EntityLookup implements LevelEntityGetter { public void getEntities(final Entity except, final AABB box, final List into, final Predicate predicate, final int maxCount) { + // Sakura start - add maxSearch to getEntities + this.getEntities(except, box, into, predicate, maxCount, Integer.MAX_VALUE); + } + + public void getEntities(final Entity except, final AABB box, final List into, final Predicate predicate, + final int maxCount, final int maxSearch) { + // Sakura end - add maxSearch to getEntities final int minChunkX = (Mth.floor(box.minX) - 2) >> 4; final int minChunkZ = (Mth.floor(box.minZ) - 2) >> 4; final int maxChunkX = (Mth.floor(box.maxX) + 2) >> 4; @@ -757,7 +764,7 @@ public abstract class EntityLookup implements LevelEntityGetter { continue; } - if (chunk.getEntities(except, box, into, predicate, maxCount)) { + if (chunk.getEntities(except, box, into, predicate, maxCount, maxSearch)) { // Sakura - add maxSearch to getEntities return; } } diff --git a/net/minecraft/world/level/Level.java b/net/minecraft/world/level/Level.java index e807aafed0295d8db7ba0782bb1e462561e9e1ec..af39222e08bc7957ac40ea9fd6aae517e7cee45a 100644 --- a/net/minecraft/world/level/Level.java +++ b/net/minecraft/world/level/Level.java @@ -1728,10 +1728,18 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl this.getEntities(entityTypeTest, bounds, predicate, output, Integer.MAX_VALUE); } - // Paper start - rewrite chunk system public void getEntities(final EntityTypeTest entityTypeTest, final AABB boundingBox, final Predicate predicate, final List into, final int maxCount) { + // Sakura start - add maxSearch to getEntities + this.getEntities(entityTypeTest, boundingBox, predicate, into, maxCount, Integer.MAX_VALUE); + } + + // Paper start - rewrite chunk system + public void getEntities(final EntityTypeTest entityTypeTest, + final AABB boundingBox, final Predicate predicate, + final List into, final int maxCount, final int maxSearch) { + // Sakura end - add maxSearch to getEntities Profiler.get().incrementCounter("getEntities"); if (entityTypeTest instanceof net.minecraft.world.entity.EntityType byType) { @@ -1748,7 +1756,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl if (entityTypeTest == null) { if (maxCount != Integer.MAX_VALUE) { - ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel)this).moonrise$getEntityLookup().getEntities((Entity)null, boundingBox, (List)into, (Predicate)predicate, maxCount); + ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel)this).moonrise$getEntityLookup().getEntities((Entity)null, boundingBox, (List)into, (Predicate)predicate, maxCount, maxSearch); // Sakura - add maxSearch to getEntities ca.spottedleaf.moonrise.common.PlatformHooks.get().addToGetEntities((Level)(Object)this, entityTypeTest, boundingBox, predicate, into, maxCount); return; } else {