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);
if (!Objects.equals(pair.value, value)) {
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<Pair<?>> values) {
synchronized void load(boolean wipe, List<Pair<?>> values) {
if (wipe) {
codecs.clear();
codecs.addAll(values);

View file

@ -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,6 +53,7 @@ public class DataTrackerManager implements Tickable {
return;
}
synchronized (this) {
List<MsgTrackedValues.TrackerEntries> toTransmit = new ArrayList<>();
for (var entry : trackers.int2ObjectEntrySet()) {
@ -68,16 +69,19 @@ public class DataTrackerManager implements Tickable {
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) {
if (tracker == null) {
tracker = new DataTracker(this, update.id());
trackers.put(update.id(), tracker);
}
tracker.load(update.wipe(), update.values());
}
}
}
}