Add TagSerializer.fromCompound

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-15 03:50:57 +02:00
parent 5839ec71cc
commit 1ad6caaf8b
5 changed files with 64 additions and 4 deletions

View File

@ -5,16 +5,16 @@ import net.minestom.server.item.firework.FireworkEffect;
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler;
import net.minestom.server.tag.TagReadable;
import net.minestom.server.tag.TagSerializer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnknownNullability;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.List;
public record FireworkMeta(TagReadable readable) implements ItemMetaView<FireworkMeta.Builder> {
private static final Tag<List<FireworkEffect>> EFFECTS = Tag.NBT("Explosions").path("Fireworks")
.map(nbt -> FireworkEffect.fromCompound((NBTCompound) nbt), FireworkEffect::asCompound)
.list().defaultValue(List.of());
private static final Tag<List<FireworkEffect>> EFFECTS = Tag.Structure("Explosions",
TagSerializer.fromCompound(FireworkEffect::fromCompound, FireworkEffect::asCompound))
.path("Fireworks").list().defaultValue(List.of());
private static final Tag<Byte> FLIGHT_DURATION = Tag.Byte("Flight").path("Fireworks");
public List<FireworkEffect> getEffects() {

View File

@ -23,6 +23,12 @@ final class TagNbtSeparator {
entry(NBTType.TAG_Double, Tag::Double),
entry(NBTType.TAG_String, Tag::String));
static void separate(NBTCompound nbtCompound, Consumer<Entry> consumer) {
for (var ent : nbtCompound) {
convert(new ArrayList<>(), ent.getKey(), ent.getValue(), consumer);
}
}
static void separate(String key, NBT nbt, Consumer<Entry> consumer) {
convert(new ArrayList<>(), key, nbt, consumer);
}

View File

@ -1,7 +1,11 @@
package net.minestom.server.tag;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.function.Function;
/**
* Interface used to create custom {@link Tag tags}.
@ -25,4 +29,10 @@ public interface TagSerializer<T> {
* @param value the value to serialize
*/
void write(@NotNull TagWritable writer, @NotNull T value);
@ApiStatus.Experimental
static <T> TagSerializer<T> fromCompound(@NotNull Function<NBTCompound, T> reader,
@NotNull Function<T, NBTCompound> writer) {
return TagSerializerImpl.fromCompound(reader, writer);
}
}

View File

@ -0,0 +1,25 @@
package net.minestom.server.tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.function.Function;
final class TagSerializerImpl {
static <T> TagSerializer<T> fromCompound(Function<NBTCompound, T> readFunc, Function<T, NBTCompound> writeFunc) {
return new TagSerializer<>() {
@Override
public @Nullable T read(@NotNull TagReadable reader) {
final NBTCompound compound = ((TagHandler) reader).asCompound();
return readFunc.apply(compound);
}
@Override
public void write(@NotNull TagWritable writer, @NotNull T value) {
final NBTCompound compound = writeFunc.apply(value);
TagNbtSeparator.separate(compound, entry -> writer.setTag(entry.tag(), entry.value()));
}
};
}
}

View File

@ -0,0 +1,19 @@
package net.minestom.server.tag;
import net.minestom.server.item.firework.FireworkEffect;
import net.minestom.server.item.firework.FireworkEffectType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
public class TagSerializerTest {
@Test
public void fromCompound(){
var serializer = TagSerializer.fromCompound(FireworkEffect::fromCompound, FireworkEffect::asCompound);
var effect = new FireworkEffect(false, false, FireworkEffectType.BURST, List.of(), List.of());
TagHandler handler = TagHandler.newHandler();
serializer.write(handler, effect);
Assertions.assertEquals(effect, serializer.read(handler));
}
}