Fix viewers mutating after submitting async packet sending

This commit is contained in:
filoghost 2024-03-09 23:43:21 +01:00
parent 9dcf6035fe
commit 6f343c5fc0
2 changed files with 6 additions and 7 deletions

View File

@ -5,15 +5,16 @@
*/ */
package me.filoghost.holographicdisplays.core.tracking; package me.filoghost.holographicdisplays.core.tracking;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.function.Consumer; import java.util.function.Consumer;
public class DelegateViewers<T extends Viewer> implements Viewers<T> { public class ImmutableViewers<T extends Viewer> implements Viewers<T> {
private final Collection<T> viewers; private final Collection<T> viewers;
public DelegateViewers(Collection<T> viewers) { public ImmutableViewers(Collection<T> viewers) {
this.viewers = viewers; this.viewers = new ArrayList<>(viewers);
} }
@Override @Override

View File

@ -22,7 +22,6 @@ import java.util.concurrent.ConcurrentMap;
public abstract class LineTracker<T extends Viewer> { public abstract class LineTracker<T extends Viewer> {
private final ConcurrentMap<Player, T> viewers; private final ConcurrentMap<Player, T> viewers;
private final Viewers<T> iterableViewers;
private String positionWorldName; private String positionWorldName;
protected PositionCoordinates positionCoordinates; protected PositionCoordinates positionCoordinates;
@ -33,7 +32,6 @@ public abstract class LineTracker<T extends Viewer> {
protected LineTracker() { protected LineTracker() {
this.viewers = new ConcurrentHashMap<>(); this.viewers = new ConcurrentHashMap<>();
this.iterableViewers = new DelegateViewers<>(viewers.values());
} }
protected abstract BaseHologramLine getLine(); protected abstract BaseHologramLine getLine();
@ -67,7 +65,7 @@ public abstract class LineTracker<T extends Viewer> {
// Then, send the changes (if any) to already tracked players // Then, send the changes (if any) to already tracked players
if (sendChangesPackets && hasViewers()) { if (sendChangesPackets && hasViewers()) {
sendChangesPackets(iterableViewers); sendChangesPackets(new ImmutableViewers<>(viewers.values()));
} }
// Finally, add/remove viewers sending them the full spawn/destroy packets // Finally, add/remove viewers sending them the full spawn/destroy packets
@ -223,7 +221,7 @@ public abstract class LineTracker<T extends Viewer> {
return; return;
} }
sendDestroyPackets(iterableViewers); sendDestroyPackets(new ImmutableViewers<>(viewers.values()));
viewers.clear(); viewers.clear();
} }