Test hasPredictableViewers

This commit is contained in:
themode 2022-03-03 16:12:05 +01:00
parent 708307dbde
commit 122d3ebaf3
2 changed files with 32 additions and 3 deletions

View File

@ -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<T> predicate = entity -> true;
private boolean predictable = true;
public Option(EntityTracker.Target<T> target, Predicate<T> loopPredicate,
Consumer<T> addition, Consumer<T> removal) {
this.target = target;
@ -192,6 +194,7 @@ final class EntityView {
}
void updateRule0(Predicate<T> predicate) {
this.predictable = false;
update(loopPredicate, entity -> {
final boolean result = predicate.test(entity);
if (result != isRegistered(entity)) {

View File

@ -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();