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)
This commit is contained in:
mworzala 2023-03-18 09:13:51 -04:00
parent 52dccf793f
commit 207b658bcd
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
4 changed files with 42 additions and 2 deletions

View File

@ -28,6 +28,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() || !((NBTCompound) nbt).isEmpty()) {
if (nbt != null && (!tag.entry.isPath() || !((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 (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);
}
}