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,20 +111,20 @@ final class TagHandlerImpl implements TagHandler {
} }
entries[index] = new Entry<>(tag, nbt); entries[index] = new Entry<>(tag, nbt);
} }
synchronized (this) {
this.cache = null; this.cache = null;
this.entries = entries; this.entries = entries;
} }
}
@Override @Override
public @NotNull NBTCompound asCompound() { public @NotNull NBTCompound asCompound() {
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) {
if ((cache = this.cache) != null) return cache;
Entry<?>[] entries = this.entries; Entry<?>[] entries = this.entries;
if (entries.length > 0) { if (entries.length > 0) {
entries = entries.clone(); entries = entries.clone();
@ -146,7 +146,6 @@ final class TagHandlerImpl implements TagHandler {
} }
this.cache = cache; this.cache = cache;
} }
}
return cache; return cache;
} }