Skip to content

Commit

Permalink
Fix Viewable API-related deadlock
Browse files Browse the repository at this point in the history
 * SceneManager no longer calls `updateViewerRule`

 * InstanceScene#joinSpectators no longer sets the viewable rule to a local method reference -- uses a static class and WeakReference to `this` instead
  • Loading branch information
Steanky committed Mar 9, 2024
1 parent ee48bca commit 7f51d8f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
18 changes: 17 additions & 1 deletion core/src/main/java/org/phantazm/core/scene2/InstanceScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import org.phantazm.commons.FutureUtils;
import org.phantazm.core.player.PlayerView;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;

/**
* Further specialization of {@link SceneAbstract} that assumes a single instance devoted to this {@link Scene}. The
Expand All @@ -24,6 +27,18 @@ public abstract class InstanceScene extends SceneAbstract implements WatchableSc
private final Set<PlayerView> spectators;
private final Set<PlayerView> spectatorsView;

private record ViewPredicate(Reference<InstanceScene> sceneReference) implements Predicate<Player> {
@Override
public boolean test(Player player) {
InstanceScene instance = sceneReference.get();
if (instance != null) {
return instance.hasSpectator(player);
}

return true;
}
}

public InstanceScene(@NotNull Instance instance, int timeout) {
super(timeout);
this.instance = Objects.requireNonNull(instance);
Expand Down Expand Up @@ -69,7 +84,8 @@ public void joinSpectators(@NotNull Set<? extends @NotNull PlayerView> players,
}

Player actualPlayer = playerOptional.get();
actualPlayer.updateViewableRule(this::hasSpectator);

actualPlayer.updateViewableRule(new ViewPredicate(new WeakReference<>(this)));
actualPlayer.setGameMode(GameMode.SPECTATOR);

futures[i++] = joinSpectator(actualPlayer, ghost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,6 @@ private void resetViewableRules(Iterable<PlayerView> playerViews) {
for (PlayerView playerView : playerViews) {
playerView.getPlayer().ifPresent(player -> {
player.updateViewableRule(null);
player.updateViewerRule(null);
});
}
}
Expand Down

0 comments on commit 7f51d8f

Please sign in to comment.