Fixed dead spells not being removed correctly

This commit is contained in:
Sollace 2021-12-22 14:15:51 +02:00
parent f2a6bf36d4
commit eca5ab3e2b
4 changed files with 28 additions and 22 deletions

View file

@ -97,7 +97,7 @@ public class EffectSync implements SpellContainer {
private Stream<Spell> read(boolean synchronize, boolean sendUpdate) { private Stream<Spell> read(boolean synchronize, boolean sendUpdate) {
if (synchronize && spells.fromNbt(owner.getEntity().getDataTracker().get(param)) && sendUpdate) { if (synchronize && spells.fromNbt(owner.getEntity().getDataTracker().get(param)) && sendUpdate) {
write(); owner.getEntity().getDataTracker().set(param, spells.toNbt());
} }
return spells.getReferences(); return spells.getReferences();

View file

@ -9,7 +9,7 @@ import net.minecraft.nbt.NbtCompound;
public interface NetworkedReference<T> { public interface NetworkedReference<T> {
Optional<T> getReference(); Optional<T> getReference();
Optional<T> updateReference(@Nullable T newValue); void updateReference(@Nullable T newValue);
boolean fromNbt(NbtCompound comp); boolean fromNbt(NbtCompound comp);

View file

@ -27,6 +27,7 @@ public class NetworkedReferenceSet<T> {
private final Supplier<NetworkedReference<T>> factory; private final Supplier<NetworkedReference<T>> factory;
private boolean dirty; private boolean dirty;
private boolean reading;
public NetworkedReferenceSet(Function<T, UUID> uuidConverter, Supplier<NetworkedReference<T>> factory) { public NetworkedReferenceSet(Function<T, UUID> uuidConverter, Supplier<NetworkedReference<T>> factory) {
this.uuidConverter = uuidConverter; this.uuidConverter = uuidConverter;
@ -78,24 +79,31 @@ public class NetworkedReferenceSet<T> {
} }
public boolean fromNbt(NbtCompound comp) { public boolean fromNbt(NbtCompound comp) {
if (reading) {
return false;
}
reading = true;
try {
List<UUID> incoming = new ArrayList<>();
comp.getList("keys", NbtElement.STRING_TYPE).forEach(key -> {
incoming.add(UUID.fromString(key.asString()));
});
List<UUID> incoming = new ArrayList<>(); ids.stream().filter(id -> !incoming.contains(id)).toList().forEach(this::removeReference);
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); boolean[] send = new boolean[1];
incoming.forEach(key -> {
boolean[] send = new boolean[1]; NetworkedReference<T> i = addReference(key);
incoming.forEach(kept -> { send[0] |= i.fromNbt(comp.getCompound(key.toString()));
NetworkedReference<T> i = addReference(kept); if (i.getReference().isEmpty()) {
send[0] |= i.fromNbt(comp.getCompound(kept.toString())); removeReference(key);
if (i.getReference().isEmpty()) { }
removeReference(kept); });
} dirty = send[0];
}); return send[0];
dirty = false; } finally {
return send[0]; reading = false;
}
} }
public NbtCompound toNbt() { public NbtCompound toNbt() {

View file

@ -31,7 +31,7 @@ public class SpellNetworkedReference<T extends Spell> implements NetworkedRefere
} }
private boolean mustDelete(@Nullable NbtCompound comp) { 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) { private boolean mustReplace(NbtCompound comp) {
@ -51,7 +51,7 @@ public class SpellNetworkedReference<T extends Spell> implements NetworkedRefere
} }
@Override @Override
public Optional<T> updateReference(@Nullable T newValue) { public void updateReference(@Nullable T newValue) {
newValue = newValue == null || newValue.isDead() ? null : newValue; newValue = newValue == null || newValue.isDead() ? null : newValue;
@Nullable @Nullable
@ -65,8 +65,6 @@ public class SpellNetworkedReference<T extends Spell> implements NetworkedRefere
oldValue.onDestroyed(owner); oldValue.onDestroyed(owner);
} }
} }
return currentValue;
} }
@Override @Override