mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-16 07:35:35 +01:00
Support Tag.Component and for record auto-structure
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
86526ab219
commit
3f172a8d2c
@ -1,5 +1,7 @@
|
|||||||
package net.minestom.server.tag;
|
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.item.ItemStack;
|
||||||
import org.jglrxavpok.hephaistos.nbt.*;
|
import org.jglrxavpok.hephaistos.nbt.*;
|
||||||
|
|
||||||
@ -22,6 +24,8 @@ final class Serializers {
|
|||||||
static final Entry<NBT, NBT> NBT_ENTRY = new Entry<>(Function.identity(), Function.identity());
|
static final Entry<NBT, NBT> NBT_ENTRY = new Entry<>(Function.identity(), Function.identity());
|
||||||
|
|
||||||
static final Entry<ItemStack, NBTCompound> ITEM = new Entry<>(ItemStack::fromItemNBT, ItemStack::toItemNBT);
|
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)));
|
||||||
|
|
||||||
static <T> Entry<T, NBTCompound> fromTagSerializer(TagSerializer<T> serializer) {
|
static <T> Entry<T, NBTCompound> fromTagSerializer(TagSerializer<T> serializer) {
|
||||||
return new Serializers.Entry<>(
|
return new Serializers.Entry<>(
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.minestom.server.tag;
|
package net.minestom.server.tag;
|
||||||
|
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
import net.minestom.server.utils.collection.IndexMap;
|
import net.minestom.server.utils.collection.IndexMap;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
@ -277,6 +278,10 @@ public class Tag<T> {
|
|||||||
return tag(key, Serializers.ITEM);
|
return tag(key, Serializers.ITEM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static @NotNull Tag<Component> Component(@NotNull String key) {
|
||||||
|
return tag(key, Serializers.COMPONENT);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a flexible tag able to read and write any {@link NBT} objects.
|
* Creates a flexible tag able to read and write any {@link NBT} objects.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.minestom.server.tag;
|
package net.minestom.server.tag;
|
||||||
|
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@ -18,6 +19,7 @@ import static java.util.Map.entry;
|
|||||||
final class TagRecord {
|
final class TagRecord {
|
||||||
static final Map<Class<?>, Function<String, Tag<?>>> SUPPORTED_TYPES = Map.ofEntries(
|
static final Map<Class<?>, Function<String, Tag<?>>> SUPPORTED_TYPES = Map.ofEntries(
|
||||||
entry(Byte.class, Tag::Byte), entry(byte.class, Tag::Byte),
|
entry(Byte.class, Tag::Byte), entry(byte.class, Tag::Byte),
|
||||||
|
entry(Boolean.class, Tag::Boolean), entry(boolean.class, Tag::Boolean),
|
||||||
entry(Short.class, Tag::Short), entry(short.class, Tag::Short),
|
entry(Short.class, Tag::Short), entry(short.class, Tag::Short),
|
||||||
entry(Integer.class, Tag::Integer), entry(int.class, Tag::Integer),
|
entry(Integer.class, Tag::Integer), entry(int.class, Tag::Integer),
|
||||||
entry(Long.class, Tag::Long), entry(long.class, Tag::Long),
|
entry(Long.class, Tag::Long), entry(long.class, Tag::Long),
|
||||||
@ -25,7 +27,8 @@ final class TagRecord {
|
|||||||
entry(Double.class, Tag::Double), entry(double.class, Tag::Double),
|
entry(Double.class, Tag::Double), entry(double.class, Tag::Double),
|
||||||
entry(String.class, Tag::String),
|
entry(String.class, Tag::String),
|
||||||
|
|
||||||
entry(ItemStack.class, Tag::ItemStack));
|
entry(ItemStack.class, Tag::ItemStack),
|
||||||
|
entry(Component.class, Tag::Component));
|
||||||
|
|
||||||
static final ClassValue<Serializer<? extends Record>> serializers = new ClassValue<>() {
|
static final ClassValue<Serializer<? extends Record>> serializers = new ClassValue<>() {
|
||||||
@Override
|
@Override
|
||||||
|
34
src/test/java/net/minestom/server/tag/TagComponentTest.java
Normal file
34
src/test/java/net/minestom/server/tag/TagComponentTest.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package net.minestom.server.tag;
|
||||||
|
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
|
|
||||||
|
public class TagComponentTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void get() {
|
||||||
|
var component = Component.text("Hey");
|
||||||
|
var tag = Tag.Component("component");
|
||||||
|
var handler = TagHandler.newHandler();
|
||||||
|
handler.setTag(tag, component);
|
||||||
|
assertSame(component, handler.getTag(tag));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void empty() {
|
||||||
|
var tag = Tag.Component("component");
|
||||||
|
var handler = TagHandler.newHandler();
|
||||||
|
assertNull(handler.getTag(tag));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void invalidTag() {
|
||||||
|
var tag = Tag.Component("entry");
|
||||||
|
var handler = TagHandler.newHandler();
|
||||||
|
handler.setTag(Tag.Integer("entry"), 1);
|
||||||
|
assertNull(handler.getTag(tag));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user