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)) {
pair.value = value; synchronized (this) {
dirtyIndices.add(entry.id()); pair.value = value;
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,31 +53,35 @@ public class DataTrackerManager implements Tickable {
return; return;
} }
List<MsgTrackedValues.TrackerEntries> toTransmit = new ArrayList<>(); synchronized (this) {
List<MsgTrackedValues.TrackerEntries> toTransmit = new ArrayList<>();
for (var entry : trackers.int2ObjectEntrySet()) { for (var entry : trackers.int2ObjectEntrySet()) {
MsgTrackedValues.TrackerEntries dirtyPairs = entry.getValue().getDirtyPairs(); MsgTrackedValues.TrackerEntries dirtyPairs = entry.getValue().getDirtyPairs();
if (dirtyPairs != null) { if (dirtyPairs != null) {
toTransmit.add(dirtyPairs); toTransmit.add(dirtyPairs);
}
} }
}
if (!toTransmit.isEmpty() || !discardedTrackers.isEmpty()) { if (!toTransmit.isEmpty() || !discardedTrackers.isEmpty()) {
MsgTrackedValues packet = new MsgTrackedValues(entity.getId(), toTransmit, discardedTrackers.toIntArray()); MsgTrackedValues packet = new MsgTrackedValues(entity.getId(), toTransmit, discardedTrackers.toIntArray());
discardedTrackers = new IntOpenHashSet(); discardedTrackers = new IntOpenHashSet();
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.load(update.wipe(), update.values()); tracker = new DataTracker(this, update.id());
trackers.put(update.id(), tracker);
} }
tracker.load(update.wipe(), update.values());
} }
} }
} }