Fix tests

This commit is contained in:
themode 2022-01-26 18:37:21 +01:00
parent 1cedb8b49a
commit a111271113
3 changed files with 13 additions and 15 deletions

View File

@ -1,6 +1,5 @@
package net.minestom.server.instance; package net.minestom.server.instance;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Point;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.ExperienceOrb; import net.minestom.server.entity.ExperienceOrb;
@ -81,13 +80,10 @@ public sealed interface EntityTracker permits EntityTrackerImpl {
visibleEntities(point.chunkX(), point.chunkZ(), target, query); visibleEntities(point.chunkX(), point.chunkZ(), target, query);
} }
/** <T extends Entity> @NotNull List<List<T>> references(int chunkX, int chunkZ, int range, @NotNull Target<T> target);
* Gets a list containing references to all the entity from {@link MinecraftServer#getChunkViewDistance()} range.
*/
<T extends Entity> @NotNull List<List<T>> references(int chunkX, int chunkZ, @NotNull Target<T> target);
default <T extends Entity> @NotNull List<List<T>> references(@NotNull Point point, @NotNull Target<T> target) { default <T extends Entity> @NotNull List<List<T>> references(@NotNull Point point, int range, @NotNull Target<T> target) {
return references(point.chunkX(), point.chunkZ(), target); return references(point.chunkX(), point.chunkZ(), range, target);
} }
/** /**

View File

@ -128,22 +128,22 @@ final class EntityTrackerImpl implements EntityTracker {
@Override @Override
public <T extends Entity> void visibleEntities(int chunkX, int chunkZ, @NotNull Target<T> target, @NotNull Query<T> query) { public <T extends Entity> void visibleEntities(int chunkX, int chunkZ, @NotNull Target<T> target, @NotNull Query<T> query) {
for (List<T> entities : references(chunkX, chunkZ, target)) { for (List<T> entities : references(chunkX, chunkZ, MinecraftServer.getEntityViewDistance(), target)) {
if (entities.isEmpty()) continue; if (entities.isEmpty()) continue;
for (T entity : entities) query.consume(entity); for (T entity : entities) query.consume(entity);
} }
} }
@Override @Override
public @NotNull <T extends Entity> List<List<T>> references(int chunkX, int chunkZ, @NotNull Target<T> target) { public @NotNull <T extends Entity> List<List<T>> references(int chunkX, int chunkZ, int range, @NotNull Target<T> target) {
// Gets reference to all chunk entities lists within the range // Gets reference to all chunk entities lists within the range
// This is used to avoid a map lookup per chunk // This is used to avoid a map lookup per chunk
final TargetEntry<T> entry = (TargetEntry<T>) entries[target.ordinal()]; final TargetEntry<T> entry = (TargetEntry<T>) entries[target.ordinal()];
return entry.chunkRangeEntities.computeIfAbsent(ChunkUtils.getChunkIndex(chunkX, chunkZ), var rangeRef = entry.references.computeIfAbsent(range, integer -> Long2ObjectSyncMap.hashmap());
return rangeRef.computeIfAbsent(ChunkUtils.getChunkIndex(chunkX, chunkZ),
chunkIndex -> { chunkIndex -> {
List<List<T>> entities = new ArrayList<>(); List<List<T>> entities = new ArrayList<>();
ChunkUtils.forChunksInRange(ChunkUtils.getChunkCoordX(chunkIndex), ChunkUtils.getChunkCoordZ(chunkIndex), ChunkUtils.forChunksInRange(ChunkUtils.getChunkCoordX(chunkIndex), ChunkUtils.getChunkCoordZ(chunkIndex), range,
MinecraftServer.getChunkViewDistance(),
(x, z) -> entities.add(entry.chunkEntities.computeIfAbsent(getChunkIndex(x, z), i -> (List<T>) LIST_SUPPLIER.get()))); (x, z) -> entities.add(entry.chunkEntities.computeIfAbsent(getChunkIndex(x, z), i -> (List<T>) LIST_SUPPLIER.get())));
return List.copyOf(entities); return List.copyOf(entities);
}); });
@ -177,7 +177,7 @@ final class EntityTrackerImpl implements EntityTracker {
// Chunk index -> entities inside it // Chunk index -> entities inside it
private final Long2ObjectSyncMap<List<T>> chunkEntities = Long2ObjectSyncMap.hashmap(); private final Long2ObjectSyncMap<List<T>> chunkEntities = Long2ObjectSyncMap.hashmap();
// Chunk index -> lists of visible entities (references to chunkEntities entries) // Chunk index -> lists of visible entities (references to chunkEntities entries)
private final Long2ObjectSyncMap<List<List<T>>> chunkRangeEntities = Long2ObjectSyncMap.hashmap(); private final Int2ObjectSyncMap<Long2ObjectSyncMap<List<List<T>>>> references = Int2ObjectSyncMap.hashmap();
TargetEntry(Target<T> target) { TargetEntry(Target<T> target) {
this.target = target; this.target = target;

View File

@ -2,6 +2,7 @@ package net.minestom.server.utils;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.ints.IntSet;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Point;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
@ -55,8 +56,9 @@ public final class ViewEngine {
synchronized (mutex) { synchronized (mutex) {
this.tracker = tracker; this.tracker = tracker;
if (tracker != null) { if (tracker != null) {
this.viewableOption.references = tracker.references(point, EntityTracker.Target.PLAYERS); final int range = entity != null ? MinecraftServer.getEntityViewDistance() : MinecraftServer.getChunkViewDistance();
this.viewerOption.references = tracker.references(point, EntityTracker.Target.ENTITIES); this.viewableOption.references = tracker.references(point, range, EntityTracker.Target.PLAYERS);
this.viewerOption.references = tracker.references(point, range, EntityTracker.Target.ENTITIES);
} else { } else {
this.viewableOption.references = null; this.viewableOption.references = null;
this.viewerOption.references = null; this.viewerOption.references = null;