From e2b5fbd054954424dcd6391da561160d37ec7c86 Mon Sep 17 00:00:00 2001 From: mworzala Date: Sat, 18 Mar 2023 09:13:51 -0400 Subject: [PATCH] hollow-cube/tag-transient Signed-off-by: mworzala allow null tag entries (cherry picked from commit 0a57fd346bcb8bb966f866b6943fb2e57845c110) allow null tag entries (cherry picked from commit 3ef62831603163c2ac13432e3f2dd8ffebfb5fef) add Tag.Transient (cherry picked from commit 9fbbe563c2dce1418a66d2ed8695a4309807c084) (cherry picked from commit 207b658bcd5331f3a634a2bf9cfef30280a921eb) --- .../net/minestom/server/tag/Serializers.java | 2 ++ .../java/net/minestom/server/tag/Tag.java | 5 +++ .../minestom/server/tag/TagHandlerImpl.java | 5 +-- .../minestom/server/tag/TagTransientTest.java | 32 +++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/minestom/server/tag/TagTransientTest.java diff --git a/src/main/java/net/minestom/server/tag/Serializers.java b/src/main/java/net/minestom/server/tag/Serializers.java index fb46b8190..0266a123a 100644 --- a/src/main/java/net/minestom/server/tag/Serializers.java +++ b/src/main/java/net/minestom/server/tag/Serializers.java @@ -30,6 +30,8 @@ final class Serializers { static final Entry COMPONENT = new Entry<>(NBTType.TAG_String, input -> GsonComponentSerializer.gson().deserialize(input.getValue()), component -> NBT.String(GsonComponentSerializer.gson().serialize(component))); + static final Entry EMPTY = new Entry<>(NBTType.TAG_Byte, unused -> null, component -> null); + static Entry fromTagSerializer(TagSerializer serializer) { return new Serializers.Entry<>(NBTType.TAG_Compound, (NBTCompound compound) -> { diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index dee669f0f..807cc3678 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -319,4 +319,9 @@ public class Tag { public static @NotNull Tag View(@NotNull Class type) { return View(TagRecord.serializer(type)); } + + public static @NotNull Tag Transient(@NotNull String key) { + //noinspection unchecked + return (Tag) tag(key, Serializers.EMPTY); + } } diff --git a/src/main/java/net/minestom/server/tag/TagHandlerImpl.java b/src/main/java/net/minestom/server/tag/TagHandlerImpl.java index 67ec75db6..03dc1211c 100644 --- a/src/main/java/net/minestom/server/tag/TagHandlerImpl.java +++ b/src/main/java/net/minestom/server/tag/TagHandlerImpl.java @@ -281,7 +281,7 @@ final class TagHandlerImpl implements TagHandler { this.entries.forValues(entry -> { final Tag tag = entry.tag; final NBT nbt = entry.updatedNbt(); - if (!tag.entry.isPath() || (!Serializers.SERIALIZE_EMPTY_COMPOUND) && !((NBTCompound) nbt).isEmpty()) { + if (nbt != null && !tag.entry.isPath() || (!Serializers.SERIALIZE_EMPTY_COMPOUND) && !((NBTCompound) nbt).isEmpty()) { tmp.put(tag.getKey(), nbt); } }); @@ -310,7 +310,8 @@ final class TagHandlerImpl implements TagHandler { nbt = entry.updatedNbt(); } - tmp.put(tag.getKey(), nbt); + if (nbt != null) + tmp.put(tag.getKey(), nbt); entries.put(tag.index, valueToEntry(result, tag, value)); }); if ((!Serializers.SERIALIZE_EMPTY_COMPOUND) && tmp.isEmpty() && parent != null) diff --git a/src/test/java/net/minestom/server/tag/TagTransientTest.java b/src/test/java/net/minestom/server/tag/TagTransientTest.java new file mode 100644 index 000000000..dcaee6727 --- /dev/null +++ b/src/test/java/net/minestom/server/tag/TagTransientTest.java @@ -0,0 +1,32 @@ +package net.minestom.server.tag; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class TagTransientTest { + + @Test + public void twoTransientTags() { + var tagHandler = TagHandler.newHandler(); + Tag tag1 = Tag.Transient("a"); + Tag tag2 = Tag.Transient("b"); + + tagHandler.setTag(tag1, "abcdef"); + var result = tagHandler.getTag(tag2); + assertNull(result); + } + + @Test + public void twoTransientTagsEqual() { + var tagHandler = TagHandler.newHandler(); + Tag tag1 = Tag.Transient("a"); + Tag tag2 = Tag.Transient("a"); + + tagHandler.setTag(tag1, "abcdef"); + var result = tagHandler.getTag(tag2); + assertEquals("abcdef", result); + } + +}