From eca5ab3e2b3ccce519d7b711494a7b03bb5ffbc4 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 22 Dec 2021 14:15:51 +0200 Subject: [PATCH] Fixed dead spells not being removed correctly --- .../unicopia/network/datasync/EffectSync.java | 2 +- .../network/datasync/NetworkedReference.java | 2 +- .../datasync/NetworkedReferenceSet.java | 40 +++++++++++-------- .../datasync/SpellNetworkedReference.java | 6 +-- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java index 7dd7f26a..4c379d1a 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java @@ -97,7 +97,7 @@ public class EffectSync implements SpellContainer { private Stream read(boolean synchronize, boolean sendUpdate) { if (synchronize && spells.fromNbt(owner.getEntity().getDataTracker().get(param)) && sendUpdate) { - write(); + owner.getEntity().getDataTracker().set(param, spells.toNbt()); } return spells.getReferences(); diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReference.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReference.java index 6203b101..cb1a42d3 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReference.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReference.java @@ -9,7 +9,7 @@ import net.minecraft.nbt.NbtCompound; public interface NetworkedReference { Optional getReference(); - Optional updateReference(@Nullable T newValue); + void updateReference(@Nullable T newValue); boolean fromNbt(NbtCompound comp); diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java index c7a32576..43dfca27 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java @@ -27,6 +27,7 @@ public class NetworkedReferenceSet { private final Supplier> factory; private boolean dirty; + private boolean reading; public NetworkedReferenceSet(Function uuidConverter, Supplier> factory) { this.uuidConverter = uuidConverter; @@ -78,24 +79,31 @@ public class NetworkedReferenceSet { } public boolean fromNbt(NbtCompound comp) { + if (reading) { + return false; + } + reading = true; + try { + List incoming = new ArrayList<>(); + comp.getList("keys", NbtElement.STRING_TYPE).forEach(key -> { + incoming.add(UUID.fromString(key.asString())); + }); - List incoming = new ArrayList<>(); - comp.getList("keys", NbtElement.STRING_TYPE).forEach(key -> { - incoming.add(UUID.fromString(key.asString())); - }); + ids.stream().filter(id -> !incoming.contains(id)).toList().forEach(this::removeReference); - ids.stream().filter(id -> !incoming.contains(id)).toList().forEach(this::removeReference); - - boolean[] send = new boolean[1]; - incoming.forEach(kept -> { - NetworkedReference i = addReference(kept); - send[0] |= i.fromNbt(comp.getCompound(kept.toString())); - if (i.getReference().isEmpty()) { - removeReference(kept); - } - }); - dirty = false; - return send[0]; + boolean[] send = new boolean[1]; + incoming.forEach(key -> { + NetworkedReference i = addReference(key); + send[0] |= i.fromNbt(comp.getCompound(key.toString())); + if (i.getReference().isEmpty()) { + removeReference(key); + } + }); + dirty = send[0]; + return send[0]; + } finally { + reading = false; + } } public NbtCompound toNbt() { diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java index f957c9b6..605959fc 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java @@ -31,7 +31,7 @@ public class SpellNetworkedReference implements NetworkedRefere } private boolean mustDelete(@Nullable NbtCompound comp) { - return (comp == null || !comp.contains("effect_id") || !comp.contains("uuid")) && currentValue.isPresent(); + return comp == null || !comp.contains("effect_id") || !comp.contains("uuid"); } private boolean mustReplace(NbtCompound comp) { @@ -51,7 +51,7 @@ public class SpellNetworkedReference implements NetworkedRefere } @Override - public Optional updateReference(@Nullable T newValue) { + public void updateReference(@Nullable T newValue) { newValue = newValue == null || newValue.isDead() ? null : newValue; @Nullable @@ -65,8 +65,6 @@ public class SpellNetworkedReference implements NetworkedRefere oldValue.onDestroyed(owner); } } - - return currentValue; } @Override