From b9015c9220675e84e8e92f877a5d1f309c100671 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 21 May 2024 00:23:46 +0100 Subject: [PATCH] Add synchronization to keep it from messing up --- .../unicopia/network/track/DataTracker.java | 10 +++--- .../network/track/DataTrackerManager.java | 34 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java index 71116e23..7aa6dc99 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java @@ -59,13 +59,15 @@ public class DataTracker { Pair pair = getPair(entry); if (!Objects.equals(pair.value, value)) { - pair.value = value; - dirtyIndices.add(entry.id()); + synchronized (this) { + pair.value = value; + dirtyIndices.add(entry.id()); + } } } @Nullable - MsgTrackedValues.TrackerEntries getDirtyPairs() { + synchronized MsgTrackedValues.TrackerEntries getDirtyPairs() { writethroughCallback.values().forEach(Runnable::run); if (initial) { @@ -88,7 +90,7 @@ public class DataTracker { } @SuppressWarnings("unchecked") - void load(boolean wipe, List> values) { + synchronized void load(boolean wipe, List> values) { if (wipe) { codecs.clear(); codecs.addAll(values); diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java b/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java index e4e9e01e..10f7b83a 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java @@ -30,13 +30,13 @@ public class DataTrackerManager implements Tickable { return primaryTracker; } - public DataTracker checkoutTracker() { + public synchronized DataTracker checkoutTracker() { DataTracker tracker = new DataTracker(this, nextId++); trackers.put(tracker.id, tracker); return tracker; } - void closeTracker(int id) { + synchronized void closeTracker(int id) { if (id <= 0) { return; } @@ -53,31 +53,35 @@ public class DataTrackerManager implements Tickable { return; } - List toTransmit = new ArrayList<>(); + synchronized (this) { + List toTransmit = new ArrayList<>(); - for (var entry : trackers.int2ObjectEntrySet()) { - MsgTrackedValues.TrackerEntries dirtyPairs = entry.getValue().getDirtyPairs(); - if (dirtyPairs != null) { - toTransmit.add(dirtyPairs); + for (var entry : trackers.int2ObjectEntrySet()) { + MsgTrackedValues.TrackerEntries dirtyPairs = entry.getValue().getDirtyPairs(); + if (dirtyPairs != null) { + toTransmit.add(dirtyPairs); + } } - } - if (!toTransmit.isEmpty() || !discardedTrackers.isEmpty()) { - MsgTrackedValues packet = new MsgTrackedValues(entity.getId(), toTransmit, discardedTrackers.toIntArray()); - discardedTrackers = new IntOpenHashSet(); - Channel.SERVER_TRACKED_ENTITY_DATA.sendToSurroundingPlayers(packet, entity); + if (!toTransmit.isEmpty() || !discardedTrackers.isEmpty()) { + MsgTrackedValues packet = new MsgTrackedValues(entity.getId(), toTransmit, discardedTrackers.toIntArray()); + discardedTrackers = new IntOpenHashSet(); + Channel.SERVER_TRACKED_ENTITY_DATA.sendToSurroundingPlayers(packet, entity); + } } } - void load(MsgTrackedValues packet) { + synchronized void load(MsgTrackedValues packet) { for (int id : packet.removedTrackers()) { closeTracker(id); } for (var update : packet.updatedTrackers()) { DataTracker tracker = trackers.get(update.id()); - if (tracker != null) { - tracker.load(update.wipe(), update.values()); + if (tracker == null) { + tracker = new DataTracker(this, update.id()); + trackers.put(update.id(), tracker); } + tracker.load(update.wipe(), update.values()); } } }