From 122d3ebaf3235edfb97c02abaadb4e52be223a42 Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 3 Mar 2022 16:12:05 +0100 Subject: [PATCH] Test `hasPredictableViewers` --- .../minestom/server/entity/EntityView.java | 5 +++- .../entity/EntityViewIntegrationTest.java | 30 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/EntityView.java b/src/main/java/net/minestom/server/entity/EntityView.java index e2acc67e8..b31a23b70 100644 --- a/src/main/java/net/minestom/server/entity/EntityView.java +++ b/src/main/java/net/minestom/server/entity/EntityView.java @@ -103,7 +103,7 @@ final class EntityView { public boolean hasPredictableViewers() { // Verify if this entity's viewers can be predicted from surrounding entities synchronized (mutex) { - return viewableOption.isAuto() && manualViewers.isEmpty(); + return viewableOption.isAuto() && viewableOption.predictable && manualViewers.isEmpty(); } } @@ -140,6 +140,8 @@ final class EntityView { // The custom rule used to determine if an entity is viewable. private Predicate predicate = entity -> true; + private boolean predictable = true; + public Option(EntityTracker.Target target, Predicate loopPredicate, Consumer addition, Consumer removal) { this.target = target; @@ -192,6 +194,7 @@ final class EntityView { } void updateRule0(Predicate predicate) { + this.predictable = false; update(loopPredicate, entity -> { final boolean result = predicate.test(entity); if (result != isRegistered(entity)) { diff --git a/src/test/java/net/minestom/server/entity/EntityViewIntegrationTest.java b/src/test/java/net/minestom/server/entity/EntityViewIntegrationTest.java index d549889e1..687d61375 100644 --- a/src/test/java/net/minestom/server/entity/EntityViewIntegrationTest.java +++ b/src/test/java/net/minestom/server/entity/EntityViewIntegrationTest.java @@ -6,8 +6,7 @@ import net.minestom.server.coordinate.Pos; import net.minestom.server.network.packet.server.play.SpawnLivingEntityPacket; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; @EnvTest public class EntityViewIntegrationTest { @@ -96,6 +95,33 @@ public class EntityViewIntegrationTest { assertEquals(1, p2.getViewers().size()); } + @Test + public void predictableViewers(Env env) { + var instance = env.createFlatInstance(); + var p = env.createPlayer(instance, new Pos(0, 42, 0)); + assertTrue(p.hasPredictableViewers()); + p.setAutoViewable(false); + assertFalse(p.hasPredictableViewers()); + p.setAutoViewable(true); + assertTrue(p.hasPredictableViewers()); + // MANUAL VIEWERS + { + var tmpPlayer = env.createPlayer(instance, new Pos(0, 42, 0)); + p.addViewer(tmpPlayer); + assertFalse(p.hasPredictableViewers()); + p.removeViewer(tmpPlayer); + tmpPlayer.remove(); + assertTrue(p.hasPredictableViewers()); + } + // CHANGE RULE + { + // May cause all subsequent `hasPredictableViewers` to return false + // due to the server not able detect always-true predicate + p.updateViewableRule(player -> false); + assertFalse(p.hasPredictableViewers()); + } + } + @Test public void livingVehicle(Env env) { var instance = env.createFlatInstance();