2022-04-07 11:05:11 +02:00
|
|
|
package net.minestom.server.tag;
|
|
|
|
|
2022-04-12 16:20:17 +02:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
2022-04-07 11:05:11 +02:00
|
|
|
import net.minestom.server.item.ItemStack;
|
|
|
|
import org.jglrxavpok.hephaistos.nbt.*;
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
2022-04-10 10:01:39 +02:00
|
|
|
/**
|
|
|
|
* Basic serializers for {@link Tag tags}.
|
|
|
|
*/
|
2022-04-07 11:05:11 +02:00
|
|
|
final class Serializers {
|
2022-04-10 10:01:39 +02:00
|
|
|
static final Entry<TagHandlerImpl, NBTCompound> PATH = new Entry<>(TagHandlerImpl::fromCompound, TagHandlerImpl::asCompound);
|
2022-04-07 11:05:11 +02:00
|
|
|
|
|
|
|
static final Entry<Byte, NBTByte> BYTE = new Entry<>(NBTByte::getValue, NBT::Byte);
|
2022-04-10 15:38:13 +02:00
|
|
|
static final Entry<Boolean, NBTByte> BOOLEAN = new Entry<>(NBTByte::asBoolean, NBT::Boolean);
|
2022-04-07 11:05:11 +02:00
|
|
|
static final Entry<Short, NBTShort> SHORT = new Entry<>(NBTShort::getValue, NBT::Short);
|
|
|
|
static final Entry<Integer, NBTInt> INT = new Entry<>(NBTInt::getValue, NBT::Int);
|
|
|
|
static final Entry<Long, NBTLong> LONG = new Entry<>(NBTLong::getValue, NBT::Long);
|
|
|
|
static final Entry<Float, NBTFloat> FLOAT = new Entry<>(NBTFloat::getValue, NBT::Float);
|
|
|
|
static final Entry<Double, NBTDouble> DOUBLE = new Entry<>(NBTDouble::getValue, NBT::Double);
|
|
|
|
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<ItemStack, NBTCompound> ITEM = new Entry<>(ItemStack::fromItemNBT, ItemStack::toItemNBT);
|
2022-04-12 16:20:17 +02:00
|
|
|
static final Entry<Component, NBTString> COMPONENT = new Entry<>(input -> GsonComponentSerializer.gson().deserialize(input.getValue()),
|
|
|
|
component -> NBT.String(GsonComponentSerializer.gson().serialize(component)));
|
2022-04-07 11:05:11 +02:00
|
|
|
|
2022-04-10 12:26:33 +02:00
|
|
|
static <T> Entry<T, NBTCompound> fromTagSerializer(TagSerializer<T> serializer) {
|
2022-04-07 11:05:11 +02:00
|
|
|
return new Serializers.Entry<>(
|
|
|
|
(NBTCompound compound) -> {
|
|
|
|
if (compound.isEmpty()) return null;
|
|
|
|
return serializer.read(TagHandler.fromCompound(compound));
|
|
|
|
},
|
|
|
|
(value) -> {
|
|
|
|
if (value == null) return NBTCompound.EMPTY;
|
|
|
|
TagHandler handler = TagHandler.newHandler();
|
|
|
|
serializer.write(handler, value);
|
|
|
|
return handler.asCompound();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
record Entry<T, N extends NBT>(Function<N, T> read, Function<T, N> write) {
|
|
|
|
}
|
|
|
|
}
|