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 f214229185c9b54f54b146487232b432ef1314e6..a242b68b6d603d3da46d9e0635a86a07a7149b3b 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java +++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java @@ -314,7 +314,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, @@ -552,6 +559,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; } @@ -573,8 +587,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 7554c109c35397bc1a43dd80e87764fd78645bbf..d60f30f7afb15cc90c1bd4b816136d00b23a53e4 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java +++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java @@ -722,6 +722,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; @@ -753,7 +760,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 125f61e6560790bed25e2f7aedaa189caf31a8aa..dc3c1d354bd4fb557f295c8dca14a31048def456 100644 --- a/net/minecraft/world/level/Level.java +++ b/net/minecraft/world/level/Level.java @@ -1795,10 +1795,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) { @@ -1815,7 +1823,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 {