Added TagSerializer

This commit is contained in:
TheMode 2021-05-17 14:02:14 +02:00
parent 1715e55b4d
commit d3b4b2ac56
4 changed files with 34 additions and 2 deletions

View File

@ -133,4 +133,25 @@ public class Tag<T> {
nbtCompound -> nbtCompound.getLongArray(key),
(nbtCompound, value) -> nbtCompound.setLongArray(key, value));
}
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);
}
});
}
}

View File

@ -3,7 +3,7 @@ package net.minestom.server.tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface TagGetter {
public interface TagReader {
<T> @Nullable T getTag(@NotNull Tag<T> tag);
boolean hasTag(@NotNull Tag<?> tag);

View File

@ -0,0 +1,11 @@
package net.minestom.server.tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface TagSerializer<T> {
@Nullable T read(@NotNull TagReader reader);
void write(@NotNull TagWriter writer, @NotNull T value);
}

View File

@ -3,6 +3,6 @@ package net.minestom.server.tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface TagSetter {
public interface TagWriter {
<T> void setTag(@NotNull Tag<T> tag, @Nullable T value);
}