Add Tag.UUID

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-12 19:16:11 +02:00
parent 3f172a8d2c
commit f073cc46c8
4 changed files with 66 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package net.minestom.server.tag;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.Utils;
import org.jglrxavpok.hephaistos.nbt.*;
import java.util.function.Function;
@ -23,6 +24,8 @@ final class Serializers {
static final Entry<String, NBTString> STRING = new Entry<>(NBTString::getValue, NBT::String);
static final Entry<NBT, NBT> NBT_ENTRY = new Entry<>(Function.identity(), Function.identity());
static final Entry<java.util.UUID, NBTIntArray> UUID = new Entry<>(intArray -> Utils.intArrayToUuid(intArray.getValue().copyArray()),
uuid -> NBT.IntArray(Utils.uuidToIntArray(uuid)));
static final Entry<ItemStack, NBTCompound> ITEM = new Entry<>(ItemStack::fromItemNBT, ItemStack::toItemNBT);
static final Entry<Component, NBTString> COMPONENT = new Entry<>(input -> GsonComponentSerializer.gson().deserialize(input.getValue()),
component -> NBT.String(GsonComponentSerializer.gson().serialize(component)));

View File

@ -16,6 +16,7 @@ import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
@ -274,6 +275,11 @@ public class Tag<T> {
return tag(key, Serializers.STRING);
}
@ApiStatus.Experimental
public static @NotNull Tag<UUID> UUID(@NotNull String key) {
return tag(key, Serializers.UUID);
}
public static @NotNull Tag<ItemStack> ItemStack(@NotNull String key) {
return tag(key, Serializers.ITEM);
}

View File

@ -12,6 +12,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.RecordComponent;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import static java.util.Map.entry;
@ -27,6 +28,7 @@ final class TagRecord {
entry(Double.class, Tag::Double), entry(double.class, Tag::Double),
entry(String.class, Tag::String),
entry(UUID.class, Tag::UUID),
entry(ItemStack.class, Tag::ItemStack),
entry(Component.class, Tag::Component));

View File

@ -0,0 +1,55 @@
package net.minestom.server.tag;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTIntArray;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
public class TagUuidTest {
@Test
public void get() {
var uuid = UUID.randomUUID();
var tag = Tag.UUID("uuid");
var handler = TagHandler.newHandler();
handler.setTag(tag, uuid);
assertSame(uuid, handler.getTag(tag));
}
@Test
public void empty() {
var tag = Tag.UUID("uuid");
var handler = TagHandler.newHandler();
assertNull(handler.getTag(tag));
}
@Test
public void invalidTag() {
var tag = Tag.UUID("entry");
var handler = TagHandler.newHandler();
handler.setTag(Tag.Integer("entry"), 1);
assertNull(handler.getTag(tag));
}
@Test
public void toNbt() {
var tag = Tag.UUID("uuid");
var handler = TagHandler.newHandler();
handler.setTag(tag, UUID.fromString("9ab8ca63-3d7b-43ba-b805-a20a352dae9c"));
var nbt = handler.asCompound();
NBTIntArray array = (NBTIntArray) nbt.get("uuid");
assertArrayEquals(new int[]{-1699165597, 1031488442, -1207590390, 892186268},
array.getValue().copyArray());
}
@Test
public void fromNbt() {
var tag = Tag.UUID("uuid");
var handler = TagHandler.newHandler();
handler.setTag(Tag.NBT("uuid"), NBT.IntArray(-1699165597, 1031488442, -1207590390, 892186268));
assertEquals(UUID.fromString("9ab8ca63-3d7b-43ba-b805-a20a352dae9c"), handler.getTag(tag));
}
}