Fix nearbyEntities not working with chunk borders

This commit is contained in:
themode 2022-03-01 03:15:13 +01:00
parent 9a7d5039a7
commit 69ab2c0045
2 changed files with 14 additions and 1 deletions

View File

@ -112,7 +112,14 @@ final class EntityTrackerImpl implements EntityTracker {
@Override @Override
public <T extends Entity> void nearbyEntities(@NotNull Point point, double range, @NotNull Target<T> target, @NotNull Consumer<T> query) { public <T extends Entity> void nearbyEntities(@NotNull Point point, double range, @NotNull Target<T> target, @NotNull Consumer<T> query) {
final TargetEntry<Entity> entry = entries[target.ordinal()]; final TargetEntry<Entity> entry = entries[target.ordinal()];
final int chunkRange = (int) (range / Chunk.CHUNK_SECTION_SIZE); final int chunkRange;
// No need to loop through neighbor chunk if the point is a chunk coordinate
if (point.x() % 16 == 0 && point.z() % 16 == 0) {
chunkRange = (int) (range / Chunk.CHUNK_SECTION_SIZE);
} else {
chunkRange = (int) (range / Chunk.CHUNK_SECTION_SIZE) + 1;
}
// Loop through range
if (range % 16 == 0) { if (range % 16 == 0) {
// Fast path for exact chunk range // Fast path for exact chunk range
forChunksInRange(point, chunkRange, (chunkX, chunkZ) -> { forChunksInRange(point, chunkRange, (chunkX, chunkZ) -> {

View File

@ -169,6 +169,12 @@ public class EntityTrackerTest {
entities.add(ent3); entities.add(ent3);
tracker.nearbyEntities(Vec.ZERO, 50, EntityTracker.Target.ENTITIES, entity -> assertTrue(entities.remove(entity))); tracker.nearbyEntities(Vec.ZERO, 50, EntityTracker.Target.ENTITIES, entity -> assertTrue(entities.remove(entity)));
assertEquals(0, entities.size()); assertEquals(0, entities.size());
// Chunk border
tracker.move(ent1, new Vec(16, 0, 0), EntityTracker.Target.ENTITIES, updater);
entities.add(ent1);
tracker.nearbyEntities(new Vec(15, 0, 0), 2, EntityTracker.Target.ENTITIES, entity -> assertTrue(entities.remove(entity)));
assertEquals(0, entities.size());
} }
@Test @Test