mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-21 06:41:43 +01:00
Add TagSerializer.fromCompound
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
5839ec71cc
commit
1ad6caaf8b
@ -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() {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
25
src/main/java/net/minestom/server/tag/TagSerializerImpl.java
Normal file
25
src/main/java/net/minestom/server/tag/TagSerializerImpl.java
Normal 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()));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
19
src/test/java/net/minestom/server/tag/TagSerializerTest.java
Normal file
19
src/test/java/net/minestom/server/tag/TagSerializerTest.java
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user