Synchronize cache access

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-03-26 13:05:21 +01:00
parent 85559abb28
commit ac4d78acf1

View File

@ -111,8 +111,10 @@ final class TagHandlerImpl implements TagHandler {
} }
entries[index] = new Entry<>(tag, nbt); entries[index] = new Entry<>(tag, nbt);
} }
this.cache = null; synchronized (this) {
this.entries = entries; this.cache = null;
this.entries = entries;
}
} }
@Override @Override
@ -120,32 +122,29 @@ final class TagHandlerImpl implements TagHandler {
return updatedCache().compound; return updatedCache().compound;
} }
private Cache updatedCache() { private synchronized Cache updatedCache() {
Cache cache = this.cache; Cache cache = this.cache;
if (cache == null) { if (cache == null) {
synchronized (this) { Entry<?>[] entries = this.entries;
if ((cache = this.cache) != null) return cache; if (entries.length > 0) {
Entry<?>[] entries = this.entries; entries = entries.clone();
if (entries.length > 0) { MutableNBTCompound tmp = new MutableNBTCompound();
entries = entries.clone(); for (Entry<?> entry : entries) {
MutableNBTCompound tmp = new MutableNBTCompound(); if (entry == null) continue;
for (Entry<?> entry : entries) { final Tag<?> tag = entry.tag;
if (entry == null) continue; final Object value = entry.value;
final Tag<?> tag = entry.tag; if (value instanceof TagHandler handler) {
final Object value = entry.value; // Path-able entry
if (value instanceof TagHandler handler) { tmp.put(tag.getKey(), handler.asCompound());
// Path-able entry } else {
tmp.put(tag.getKey(), handler.asCompound()); tag.writeUnsafe(tmp, value);
} else {
tag.writeUnsafe(tmp, value);
}
} }
cache = !tmp.isEmpty() ? new Cache(entries, tmp.toCompound()) : Cache.EMPTY;
} else {
cache = Cache.EMPTY;
} }
this.cache = cache; cache = !tmp.isEmpty() ? new Cache(entries, tmp.toCompound()) : Cache.EMPTY;
} else {
cache = Cache.EMPTY;
} }
this.cache = cache;
} }
return cache; return cache;
} }