Add synchronization to keep it from messing up

This commit is contained in:
Sollace 2024-05-21 00:23:46 +01:00
parent f8f60a8d3d
commit b9015c9220
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
2 changed files with 25 additions and 19 deletions

View file

@ -59,13 +59,15 @@ public class DataTracker {
Pair<T> pair = getPair(entry); Pair<T> pair = getPair(entry);
if (!Objects.equals(pair.value, value)) { if (!Objects.equals(pair.value, value)) {
synchronized (this) {
pair.value = value; pair.value = value;
dirtyIndices.add(entry.id()); dirtyIndices.add(entry.id());
} }
} }
}
@Nullable @Nullable
MsgTrackedValues.TrackerEntries getDirtyPairs() { synchronized MsgTrackedValues.TrackerEntries getDirtyPairs() {
writethroughCallback.values().forEach(Runnable::run); writethroughCallback.values().forEach(Runnable::run);
if (initial) { if (initial) {
@ -88,7 +90,7 @@ public class DataTracker {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
void load(boolean wipe, List<Pair<?>> values) { synchronized void load(boolean wipe, List<Pair<?>> values) {
if (wipe) { if (wipe) {
codecs.clear(); codecs.clear();
codecs.addAll(values); codecs.addAll(values);

View file

@ -30,13 +30,13 @@ public class DataTrackerManager implements Tickable {
return primaryTracker; return primaryTracker;
} }
public DataTracker checkoutTracker() { public synchronized DataTracker checkoutTracker() {
DataTracker tracker = new DataTracker(this, nextId++); DataTracker tracker = new DataTracker(this, nextId++);
trackers.put(tracker.id, tracker); trackers.put(tracker.id, tracker);
return tracker; return tracker;
} }
void closeTracker(int id) { synchronized void closeTracker(int id) {
if (id <= 0) { if (id <= 0) {
return; return;
} }
@ -53,6 +53,7 @@ public class DataTrackerManager implements Tickable {
return; return;
} }
synchronized (this) {
List<MsgTrackedValues.TrackerEntries> toTransmit = new ArrayList<>(); List<MsgTrackedValues.TrackerEntries> toTransmit = new ArrayList<>();
for (var entry : trackers.int2ObjectEntrySet()) { for (var entry : trackers.int2ObjectEntrySet()) {
@ -68,16 +69,19 @@ public class DataTrackerManager implements Tickable {
Channel.SERVER_TRACKED_ENTITY_DATA.sendToSurroundingPlayers(packet, entity); Channel.SERVER_TRACKED_ENTITY_DATA.sendToSurroundingPlayers(packet, entity);
} }
} }
}
void load(MsgTrackedValues packet) { synchronized void load(MsgTrackedValues packet) {
for (int id : packet.removedTrackers()) { for (int id : packet.removedTrackers()) {
closeTracker(id); closeTracker(id);
} }
for (var update : packet.updatedTrackers()) { for (var update : packet.updatedTrackers()) {
DataTracker tracker = trackers.get(update.id()); DataTracker tracker = trackers.get(update.id());
if (tracker != null) { if (tracker == null) {
tracker = new DataTracker(this, update.id());
trackers.put(update.id(), tracker);
}
tracker.load(update.wipe(), update.values()); tracker.load(update.wipe(), update.values());
} }
} }
}
} }