Create TagReader/Writer from compound

This commit is contained in:
TheMode 2021-05-17 14:05:03 +02:00
parent d3b4b2ac56
commit 1ec631fa5b
3 changed files with 27 additions and 17 deletions

View File

@ -136,22 +136,7 @@ public class Tag<T> {
public static <T> @NotNull Tag<T> Custom(@NotNull String key, @NotNull TagSerializer<T> serializer) {
return new Tag<>(key,
nbtCompound -> serializer.read(new TagReader() {
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
return tag.read(nbtCompound);
}
@Override
public boolean hasTag(@NotNull Tag<?> tag) {
return nbtCompound.containsKey(tag.key);
}
}),
(nbtCompound, value) -> new TagWriter() {
@Override
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
tag.write(nbtCompound, value);
}
});
nbtCompound -> serializer.read(TagReader.fromCompound(nbtCompound)),
(nbtCompound, value) -> serializer.write(TagWriter.fromCompound(nbtCompound), value));
}
}

View File

@ -2,9 +2,24 @@ package net.minestom.server.tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
public interface TagReader {
<T> @Nullable T getTag(@NotNull Tag<T> tag);
boolean hasTag(@NotNull Tag<?> tag);
static @NotNull TagReader fromCompound(@NotNull NBTCompound compound) {
return new TagReader() {
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
return tag.read(compound);
}
@Override
public boolean hasTag(@NotNull Tag<?> tag) {
return compound.containsKey(tag.getKey());
}
};
}
}

View File

@ -2,7 +2,17 @@ package net.minestom.server.tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
public interface TagWriter {
<T> void setTag(@NotNull Tag<T> tag, @Nullable T value);
static @NotNull TagWriter fromCompound(@NotNull NBTCompound compound) {
return new TagWriter() {
@Override
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
tag.write(compound, value);
}
};
}
}