From 4459bbeebd6fa1f326768c5c898358d8fd7f8c7d Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 13 Feb 2022 07:51:47 +0100 Subject: [PATCH] Cleanup view tracking --- .../net/minestom/server/entity/Entity.java | 5 ++++- .../net/minestom/server/instance/Chunk.java | 3 +-- .../net/minestom/server/utils/ViewEngine.java | 20 +++++++++---------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index f60d6874a..04e28ebbc 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -129,7 +129,10 @@ public class Entity implements Viewable, Tickable, Schedulable, TagHandler, Perm @Override public void referenceUpdate(@NotNull Point point, @Nullable EntityTracker tracker) { - viewEngine.updateTracker(instance, point, tracker); + final Instance currentInstance = tracker != null ? instance : null; + assert currentInstance == null || currentInstance.getEntityTracker() == tracker : + "EntityTracker does not match current instance"; + viewEngine.updateTracker(currentInstance, point); } }; diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index bf6c29e80..6570debe4 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -68,8 +68,7 @@ public abstract class Chunk implements Block.Getter, Block.Setter, Biome.Getter, this.minSection = instance.getDimensionType().getMinY() / CHUNK_SECTION_SIZE; this.maxSection = (instance.getDimensionType().getMinY() + instance.getDimensionType().getHeight()) / CHUNK_SECTION_SIZE; - final EntityTracker tracker = instance.getEntityTracker(); - this.viewers.updateTracker(instance, toPosition(), tracker); + this.viewers.updateTracker(instance, toPosition()); } /** diff --git a/src/main/java/net/minestom/server/utils/ViewEngine.java b/src/main/java/net/minestom/server/utils/ViewEngine.java index f4151a5e6..05dc10d56 100644 --- a/src/main/java/net/minestom/server/utils/ViewEngine.java +++ b/src/main/java/net/minestom/server/utils/ViewEngine.java @@ -9,6 +9,7 @@ import net.minestom.server.entity.Player; import net.minestom.server.instance.EntityTracker; import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceContainer; +import net.minestom.server.instance.SharedInstance; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,7 +31,6 @@ public final class ViewEngine { private final Set manualViewers = new HashSet<>(); private Instance instance; - private EntityTracker tracker; private Point lastPoint; // Decide if this entity should be viewable to X players @@ -58,11 +58,10 @@ public final class ViewEngine { this(null); } - public void updateTracker(@NotNull Instance instance, @NotNull Point point, @Nullable EntityTracker tracker) { + public void updateTracker(@Nullable Instance instance, @NotNull Point point) { synchronized (mutex) { this.instance = instance; this.lastPoint = point; - this.tracker = tracker; } } @@ -208,18 +207,19 @@ public final class ViewEngine { } private Stream references() { - if (tracker == null) return Stream.empty(); - var references = tracker.references(lastPoint, range, target); - var result = references.stream().flatMap(Collection::stream); - if (instance != null && instance instanceof InstanceContainer container) { + final Instance instance = ViewEngine.this.instance; + if (instance == null) return Stream.empty(); + var references = instance.getEntityTracker().references(lastPoint, range, target); + Stream result = references.stream().flatMap(Collection::stream); + if (instance instanceof InstanceContainer container) { // References from shared instances must be added to the result. - var shared = container.getSharedInstances(); + final List shared = container.getSharedInstances(); if (!shared.isEmpty()) { - var tmp = shared.stream().>mapMulti((inst, consumer) -> { + Stream sharedInstanceStream = shared.stream().>mapMulti((inst, consumer) -> { var ref = inst.getEntityTracker().references(lastPoint, range, target); ref.forEach(consumer); }).flatMap(Collection::stream); - result = Stream.concat(result, tmp); + result = Stream.concat(result, sharedInstanceStream); } } return result;