hollow-cube/tag-transient

Signed-off-by: mworzala <mattheworzala@gmail.com>

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 207b658bcd)
This commit is contained in:
mworzala 2023-03-18 09:13:51 -04:00 committed by Matt Worzala
parent 30650f9a29
commit e2b5fbd054
4 changed files with 42 additions and 2 deletions

View File

@ -30,6 +30,8 @@ final class Serializers {
static final Entry<Component, NBTString> COMPONENT = new Entry<>(NBTType.TAG_String, input -> GsonComponentSerializer.gson().deserialize(input.getValue()),
component -> NBT.String(GsonComponentSerializer.gson().serialize(component)));
static final Entry<Object, NBTByte> EMPTY = new Entry<>(NBTType.TAG_Byte, unused -> null, component -> null);
static <T> Entry<T, NBTCompound> fromTagSerializer(TagSerializer<T> serializer) {
return new Serializers.Entry<>(NBTType.TAG_Compound,
(NBTCompound compound) -> {

View File

@ -319,4 +319,9 @@ public class Tag<T> {
public static <T extends Record> @NotNull Tag<T> View(@NotNull Class<T> type) {
return View(TagRecord.serializer(type));
}
public static <T> @NotNull Tag<T> Transient(@NotNull String key) {
//noinspection unchecked
return (Tag<T>) tag(key, Serializers.EMPTY);
}
}

View File

@ -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)

View File

@ -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<String> tag1 = Tag.Transient("a");
Tag<String> tag2 = Tag.Transient("b");
tagHandler.setTag(tag1, "abcdef");
var result = tagHandler.getTag(tag2);
assertNull(result);
}
@Test
public void twoTransientTagsEqual() {
var tagHandler = TagHandler.newHandler();
Tag<String> tag1 = Tag.Transient("a");
Tag<String> tag2 = Tag.Transient("a");
tagHandler.setTag(tag1, "abcdef");
var result = tagHandler.getTag(tag2);
assertEquals("abcdef", result);
}
}