mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-27 14:13:24 +02:00
Create TagReader/Writer from compound
This commit is contained in:
parent
d3b4b2ac56
commit
1ec631fa5b
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user