mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-01 04:51:29 +01:00
Temporary tag fix
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
acd3345fc9
commit
a74b6ce220
@ -21,12 +21,6 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
private volatile SPMCMap entries;
|
private volatile SPMCMap entries;
|
||||||
private Cache cache;
|
private Cache cache;
|
||||||
|
|
||||||
TagHandlerImpl(TagHandlerImpl parent, SPMCMap entries, Cache cache) {
|
|
||||||
this.parent = parent;
|
|
||||||
this.entries = entries;
|
|
||||||
this.cache = cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
TagHandlerImpl(TagHandlerImpl parent) {
|
TagHandlerImpl(TagHandlerImpl parent) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.entries = new SPMCMap(this);
|
this.entries = new SPMCMap(this);
|
||||||
@ -60,7 +54,7 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
tag.write(viewCompound, value);
|
tag.write(viewCompound, value);
|
||||||
updateContent(viewCompound);
|
updateContent(viewCompound);
|
||||||
} else {
|
} else {
|
||||||
final Entry<?> entry = valueToEntry(tag, value);
|
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 = entry != null;
|
||||||
@ -71,6 +65,11 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SPMCMap entries = local.entries;
|
SPMCMap entries = local.entries;
|
||||||
|
if (entry instanceof PathEntry pathEntry) {
|
||||||
|
var childHandler = new TagHandlerImpl(local);
|
||||||
|
childHandler.updateContent(pathEntry.updatedNbt());
|
||||||
|
entry = new PathEntry(tag.getKey(), childHandler);
|
||||||
|
}
|
||||||
if (present) entries.put(tagIndex, entry);
|
if (present) entries.put(tagIndex, entry);
|
||||||
else entries.remove(tagIndex);
|
else entries.remove(tagIndex);
|
||||||
entries.invalidate();
|
entries.invalidate();
|
||||||
@ -102,9 +101,8 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized @NotNull TagHandler copy() {
|
public @NotNull TagHandler copy() {
|
||||||
assert parent == null;
|
return fromCompound(asCompound());
|
||||||
return new TagHandlerImpl(null, entries.clone(), cache);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -132,6 +130,12 @@ 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
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
@ -170,21 +174,18 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
return local;
|
return local;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Cache updatedCache() {
|
private synchronized Cache updatedCache() {
|
||||||
VarHandle.fullFence();
|
|
||||||
Cache cache;
|
Cache cache;
|
||||||
if (!CACHE_ENABLE || (cache = this.cache) == null) {
|
if (!CACHE_ENABLE || (cache = this.cache) == null) {
|
||||||
synchronized (this) {
|
final SPMCMap entries = this.entries;
|
||||||
final SPMCMap entries = this.entries;
|
if (!entries.isEmpty()) {
|
||||||
if (!entries.isEmpty()) {
|
MutableNBTCompound tmp = new MutableNBTCompound();
|
||||||
MutableNBTCompound tmp = new MutableNBTCompound();
|
for (Entry<?> entry : entries.values()) {
|
||||||
for (Entry<?> entry : entries.values()) {
|
if (entry != null) tmp.put(entry.tag().getKey(), entry.updatedNbt());
|
||||||
if (entry != null) tmp.put(entry.tag().getKey(), entry.updatedNbt());
|
}
|
||||||
}
|
cache = new Cache(entries.clone(), tmp.toCompound());
|
||||||
cache = new Cache(entries.clone(), tmp.toCompound());
|
} else cache = Cache.EMPTY;
|
||||||
} else cache = Cache.EMPTY;
|
this.cache = cache;
|
||||||
this.cache = cache;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package net.minestom.server.tag;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static net.minestom.server.api.TestUtils.assertEqualsSNBT;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
@ -9,11 +10,64 @@ public class TagHandlerCopyTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void copy() {
|
public void copy() {
|
||||||
var handler1 = TagHandler.newHandler();
|
var handler = TagHandler.newHandler();
|
||||||
handler1.setTag(Tag.String("key"), "test");
|
handler.setTag(Tag.String("key"), "test");
|
||||||
|
|
||||||
var handler2 = handler1.copy();
|
var copy = handler.copy();
|
||||||
assertEquals(handler1.getTag(Tag.String("key")), handler2.getTag(Tag.String("key")));
|
assertEquals(handler.getTag(Tag.String("key")), copy.getTag(Tag.String("key")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void copyCachePath() {
|
||||||
|
var tag = Tag.String("key").path("path");
|
||||||
|
var handler = TagHandler.newHandler();
|
||||||
|
handler.setTag(tag, "test");
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"key":"test"}}
|
||||||
|
""", handler.asCompound());
|
||||||
|
|
||||||
|
var copy = handler.copy();
|
||||||
|
handler.setTag(tag, "test2");
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"key":"test2"}}
|
||||||
|
""", handler.asCompound());
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"key":"test"}}
|
||||||
|
""", copy.asCompound());
|
||||||
|
|
||||||
|
copy.setTag(tag, "test3");
|
||||||
|
assertEquals("test3", copy.getTag(tag));
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"key":"test3"}}
|
||||||
|
""", copy.asCompound());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void copyCache() {
|
||||||
|
var tag = Tag.String("key");
|
||||||
|
var handler = TagHandler.newHandler();
|
||||||
|
handler.setTag(tag, "test");
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":"test"}
|
||||||
|
""", handler.asCompound());
|
||||||
|
|
||||||
|
var copy = handler.copy();
|
||||||
|
handler.setTag(tag, "test2");
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":"test2"}
|
||||||
|
""", handler.asCompound());
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":"test"}
|
||||||
|
""", copy.asCompound());
|
||||||
|
|
||||||
|
copy.setTag(tag, "test3");
|
||||||
|
assertEquals("test3", copy.getTag(tag));
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":"test2"}
|
||||||
|
""", handler.asCompound());
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":"test3"}
|
||||||
|
""", copy.asCompound());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -50,6 +50,75 @@ public class TagNbtTest {
|
|||||||
assertEqualsSNBT("{}", handler.asCompound());
|
assertEqualsSNBT("{}", handler.asCompound());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromCompoundModify() {
|
||||||
|
var compound = NBT.Compound(Map.of("key", NBT.Int(5)));
|
||||||
|
var handler = TagHandler.fromCompound(compound);
|
||||||
|
assertEquals(compound, handler.asCompound());
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":5}
|
||||||
|
""", handler.asCompound());
|
||||||
|
|
||||||
|
handler.setTag(Tag.Integer("key"), 10);
|
||||||
|
assertEquals(10, handler.getTag(Tag.Integer("key")));
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":10}
|
||||||
|
""", handler.asCompound());
|
||||||
|
handler.setTag(Tag.Integer("key"), 15);
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"key":15}
|
||||||
|
""", handler.asCompound());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromCompoundModifyPath() {
|
||||||
|
var compound = NBT.Compound(Map.of("path", NBT.Compound(Map.of("key", NBT.Int(5)))));
|
||||||
|
var handler = TagHandler.fromCompound(compound);
|
||||||
|
var tag = Tag.Integer("key").path("path");
|
||||||
|
|
||||||
|
handler.setTag(tag, 10);
|
||||||
|
assertEquals(10, handler.getTag(tag));
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"key":10}}
|
||||||
|
""", handler.asCompound());
|
||||||
|
handler.setTag(tag, 15);
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"key":15}}
|
||||||
|
""", handler.asCompound());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromCompoundModifyDoublePath() {
|
||||||
|
var compound = NBT.Compound(Map.of("path", NBT.Compound(Map.of("path2",
|
||||||
|
NBT.Compound(Map.of("key", NBT.Int(5)))))));
|
||||||
|
var handler = TagHandler.fromCompound(compound);
|
||||||
|
var tag = Tag.Integer("key").path("path", "path2");
|
||||||
|
|
||||||
|
handler.setTag(tag, 10);
|
||||||
|
assertEquals(10, handler.getTag(tag));
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"path2":{"key":10}}}
|
||||||
|
""", handler.asCompound());
|
||||||
|
handler.setTag(tag, 15);
|
||||||
|
assertEqualsSNBT("""
|
||||||
|
{"path":{"path2":{"key":15}}}
|
||||||
|
""", handler.asCompound());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void compoundOverride() {
|
||||||
|
var handler = TagHandler.newHandler();
|
||||||
|
var nbtTag = Tag.NBT("path1");
|
||||||
|
|
||||||
|
var nbt1 = NBT.Compound(Map.of("key", NBT.Int(5)));
|
||||||
|
var nbt2 = NBT.Compound(Map.of("other-key", NBT.Int(5)));
|
||||||
|
handler.setTag(nbtTag, nbt1);
|
||||||
|
assertEquals(nbt1, handler.getTag(nbtTag));
|
||||||
|
|
||||||
|
handler.setTag(nbtTag, nbt2);
|
||||||
|
assertEquals(nbt2, handler.getTag(nbtTag));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void compoundRead() {
|
public void compoundRead() {
|
||||||
var handler = TagHandler.newHandler();
|
var handler = TagHandler.newHandler();
|
||||||
|
Loading…
Reference in New Issue
Block a user