Fix handler invalidation

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-26 06:37:26 +02:00
parent a74b6ce220
commit 42c6e75906

View File

@ -31,13 +31,10 @@ final class TagHandlerImpl implements TagHandler {
this(null); this(null);
} }
static TagHandlerImpl fromCompound(NBTCompoundLike compound) { static TagHandlerImpl fromCompound(NBTCompoundLike compoundLike) {
final NBTCompound compound = compoundLike.toCompound();
TagHandlerImpl handler = new TagHandlerImpl(null); TagHandlerImpl handler = new TagHandlerImpl(null);
for (var entry : compound) { TagNbtSeparator.separate(compound, entry -> handler.setTag(entry.tag(), entry.value()));
final Tag<NBT> tag = Tag.NBT(entry.getKey());
final NBT nbt = entry.getValue();
handler.setTag(tag, nbt);
}
return handler; return handler;
} }
@ -54,10 +51,9 @@ final class TagHandlerImpl implements TagHandler {
tag.write(viewCompound, value); tag.write(viewCompound, value);
updateContent(viewCompound); updateContent(viewCompound);
} else { } else {
Entry<?> entry = valueToEntry(tag, value);
final int tagIndex = tag.index; final int tagIndex = tag.index;
final Tag.PathEntry[] paths = tag.path; final Tag.PathEntry[] paths = tag.path;
final boolean present = entry != null; final boolean present = value != null;
TagHandlerImpl local = this; TagHandlerImpl local = this;
synchronized (this) { synchronized (this) {
if (paths != null) { if (paths != null) {
@ -65,12 +61,7 @@ final class TagHandlerImpl implements TagHandler {
return; return;
} }
SPMCMap entries = local.entries; SPMCMap entries = local.entries;
if (entry instanceof PathEntry pathEntry) { if (present) entries.put(tagIndex, valueToEntry(local, tag, value));
var childHandler = new TagHandlerImpl(local);
childHandler.updateContent(pathEntry.updatedNbt());
entry = new PathEntry(tag.getKey(), childHandler);
}
if (present) entries.put(tagIndex, entry);
else entries.remove(tagIndex); else entries.remove(tagIndex);
entries.invalidate(); entries.invalidate();
assert !local.entries.rehashed; assert !local.entries.rehashed;
@ -78,11 +69,11 @@ final class TagHandlerImpl implements TagHandler {
} }
} }
private <T> Entry<?> valueToEntry(Tag<T> tag, @Nullable T value) { private <T> Entry<?> valueToEntry(TagHandlerImpl parent, Tag<T> tag, @NotNull T value) {
if (value == null) return null;
if (value instanceof NBT nbt) { if (value instanceof NBT nbt) {
if (nbt instanceof NBTCompound compound) { if (nbt instanceof NBTCompound compound) {
final TagHandlerImpl handler = fromCompound(compound); var handler = new TagHandlerImpl(parent);
handler.updateContent(compound);
return new PathEntry(tag.getKey(), handler); return new PathEntry(tag.getKey(), handler);
} else { } else {
final var nbtEntry = TagNbtSeparator.separateSingle(tag.getKey(), nbt); final var nbtEntry = TagNbtSeparator.separateSingle(tag.getKey(), nbt);
@ -130,12 +121,7 @@ final class TagHandlerImpl implements TagHandler {
final Entry<?> entry = local.entries.get(pathIndex); final Entry<?> entry = local.entries.get(pathIndex);
if (entry instanceof PathEntry pathEntry) { if (entry instanceof PathEntry pathEntry) {
// Existing path, continue navigating // Existing path, continue navigating
{ assert pathEntry.value.parent == local : "Path parent is invalid: " + pathEntry.value.parent + " != " + local;
// FIXME
//assert pathEntry.value.parent == local : "Path parent is invalid: " + pathEntry.value.parent + " != " + local;
pathEntry.value.cache = null;
pathEntry.value.parent.cache = null;
}
local = pathEntry.value; local = pathEntry.value;
} else { } else {
if (!present) return null; if (!present) return null;