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) {
if (synchronize && spells.fromNbt(owner.getEntity().getDataTracker().get(param)) && sendUpdate) {
write();
owner.getEntity().getDataTracker().set(param, spells.toNbt());
}
return spells.getReferences();

View file

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

View file

@ -27,6 +27,7 @@ public class NetworkedReferenceSet<T> {
private final Supplier<NetworkedReference<T>> factory;
private boolean dirty;
private boolean reading;
public NetworkedReferenceSet(Function<T, UUID> uuidConverter, Supplier<NetworkedReference<T>> factory) {
this.uuidConverter = uuidConverter;
@ -78,7 +79,11 @@ public class NetworkedReferenceSet<T> {
}
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()));
@ -87,15 +92,18 @@ public class NetworkedReferenceSet<T> {
ids.stream().filter(id -> !incoming.contains(id)).toList().forEach(this::removeReference);
boolean[] send = new boolean[1];
incoming.forEach(kept -> {
NetworkedReference<T> i = addReference(kept);
send[0] |= i.fromNbt(comp.getCompound(kept.toString()));
incoming.forEach(key -> {
NetworkedReference<T> i = addReference(key);
send[0] |= i.fromNbt(comp.getCompound(key.toString()));
if (i.getReference().isEmpty()) {
removeReference(kept);
removeReference(key);
}
});
dirty = false;
dirty = send[0];
return send[0];
} finally {
reading = false;
}
}
public NbtCompound toNbt() {

View file

@ -31,7 +31,7 @@ public class SpellNetworkedReference<T extends Spell> 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<T extends Spell> implements NetworkedRefere
}
@Override
public Optional<T> 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<T extends Spell> implements NetworkedRefere
oldValue.onDestroyed(owner);
}
}
return currentValue;
}
@Override