mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Revamp tag api
This commit is contained in:
parent
d6a2a5b316
commit
c312bf59fc
@ -21,6 +21,8 @@ import java.util.function.Supplier;
|
|||||||
@ApiStatus.NonExtendable
|
@ApiStatus.NonExtendable
|
||||||
public class Tag<T> {
|
public class Tag<T> {
|
||||||
|
|
||||||
|
private static final String EMPTY_KEY = "";
|
||||||
|
|
||||||
private final String key;
|
private final String key;
|
||||||
private final Function<NBTCompound, T> readFunction;
|
private final Function<NBTCompound, T> readFunction;
|
||||||
private final BiConsumer<NBTCompound, T> writeConsumer;
|
private final BiConsumer<NBTCompound, T> writeConsumer;
|
||||||
@ -85,16 +87,16 @@ public class Tag<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable T read(@NotNull NBTCompound nbtCompound) {
|
public @Nullable T read(@NotNull NBTCompound nbtCompound) {
|
||||||
if (nbtCompound.containsKey(key)) {
|
T result = readFunction.apply(nbtCompound);
|
||||||
return readFunction.apply(nbtCompound);
|
if (result == null) {
|
||||||
} else {
|
|
||||||
final var supplier = defaultValue;
|
final var supplier = defaultValue;
|
||||||
return supplier != null ? supplier.get() : null;
|
result = supplier != null ? supplier.get() : null;
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(@NotNull NBTCompound nbtCompound, @Nullable T value) {
|
public void write(@NotNull NBTCompound nbtCompound, @Nullable T value) {
|
||||||
if (value != null) {
|
if (value != null || key.equals(EMPTY_KEY)) {
|
||||||
this.writeConsumer.accept(nbtCompound, value);
|
this.writeConsumer.accept(nbtCompound, value);
|
||||||
} else {
|
} else {
|
||||||
nbtCompound.removeTag(key);
|
nbtCompound.removeTag(key);
|
||||||
@ -180,8 +182,35 @@ public class Tag<T> {
|
|||||||
(nbtCompound, value) -> nbtCompound.setLongArray(key, value));
|
(nbtCompound, value) -> nbtCompound.setLongArray(key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> @NotNull Tag<T> Custom(@NotNull String key, @NotNull TagSerializer<T> serializer) {
|
/**
|
||||||
|
* Create a wrapper around a compound.
|
||||||
|
*
|
||||||
|
* @param key the tag key
|
||||||
|
* @param serializer the tag serializer
|
||||||
|
* @param <T> the tag type
|
||||||
|
* @return the created tag
|
||||||
|
*/
|
||||||
|
public static <T> @NotNull Tag<T> Structure(@NotNull String key, @NotNull TagSerializer<T> serializer) {
|
||||||
return new Tag<>(key,
|
return new Tag<>(key,
|
||||||
|
nbtCompound -> {
|
||||||
|
final var compound = nbtCompound.getCompound(key);
|
||||||
|
if (compound == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return serializer.read(TagReadable.fromCompound(compound));
|
||||||
|
},
|
||||||
|
(nbtCompound, value) -> {
|
||||||
|
var compound = nbtCompound.getCompound(key);
|
||||||
|
if (compound == null) {
|
||||||
|
compound = new NBTCompound();
|
||||||
|
nbtCompound.set(key, compound);
|
||||||
|
}
|
||||||
|
serializer.write(TagWritable.fromCompound(compound), value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> @NotNull Tag<T> View(@NotNull TagSerializer<T> serializer) {
|
||||||
|
return new Tag<>(EMPTY_KEY,
|
||||||
nbtCompound -> serializer.read(TagReadable.fromCompound(nbtCompound)),
|
nbtCompound -> serializer.read(TagReadable.fromCompound(nbtCompound)),
|
||||||
(nbtCompound, value) -> serializer.write(TagWritable.fromCompound(nbtCompound), value));
|
(nbtCompound, value) -> serializer.write(TagWritable.fromCompound(nbtCompound), value));
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface used to create custom types compatible with {@link Tag#Custom(String, TagSerializer)}.
|
* Interface used to create custom {@link Tag tags}.
|
||||||
*
|
*
|
||||||
* @param <T> the type to serialize
|
* @param <T> the type to serialize
|
||||||
*/
|
*/
|
||||||
@ -14,7 +14,7 @@ public interface TagSerializer<T> {
|
|||||||
* Reads the custom tag from a {@link TagReadable}.
|
* Reads the custom tag from a {@link TagReadable}.
|
||||||
*
|
*
|
||||||
* @param reader the reader
|
* @param reader the reader
|
||||||
* @return the deserialized value
|
* @return the deserialized value, null if invalid
|
||||||
*/
|
*/
|
||||||
@Nullable T read(@NotNull TagReadable reader);
|
@Nullable T read(@NotNull TagReadable reader);
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ public interface TagSerializer<T> {
|
|||||||
* Writes the custom tag to a {@link TagWritable}.
|
* Writes the custom tag to a {@link TagWritable}.
|
||||||
*
|
*
|
||||||
* @param writer the writer
|
* @param writer the writer
|
||||||
* @param value the value to serialize
|
* @param value the value to serialize, null to remove
|
||||||
*/
|
*/
|
||||||
void write(@NotNull TagWritable writer, @NotNull T value);
|
void write(@NotNull TagWritable writer, @Nullable T value);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,10 @@ public interface TagWritable {
|
|||||||
*/
|
*/
|
||||||
<T> void setTag(@NotNull Tag<T> tag, @Nullable T value);
|
<T> void setTag(@NotNull Tag<T> tag, @Nullable T value);
|
||||||
|
|
||||||
|
default void removeTag(@NotNull Tag<?> tag) {
|
||||||
|
setTag(tag, null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an nbt compound to a tag writer.
|
* Converts an nbt compound to a tag writer.
|
||||||
*
|
*
|
||||||
|
@ -25,8 +25,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class CampfireHandler implements BlockHandler {
|
public class CampfireHandler implements BlockHandler {
|
||||||
|
|
||||||
public static final Tag<List<ItemStack>> ITEMS = Tag.Custom("Items", new TagSerializer<>() {
|
public static final Tag<List<ItemStack>> ITEMS = Tag.View(new TagSerializer<>() {
|
||||||
|
|
||||||
private final Tag<NBT> internal = Tag.NBT("Items");
|
private final Tag<NBT> internal = Tag.NBT("Items");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -44,7 +43,11 @@ public class CampfireHandler implements BlockHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(@NotNull TagWritable writer, @NotNull List<ItemStack> value) {
|
public void write(@NotNull TagWritable writer, @Nullable List<ItemStack> value) {
|
||||||
|
if (value == null) {
|
||||||
|
writer.removeTag(internal);
|
||||||
|
return;
|
||||||
|
}
|
||||||
NBTList<NBTCompound> items = new NBTList<>(NBTTypes.TAG_Compound);
|
NBTList<NBTCompound> items = new NBTList<>(NBTTypes.TAG_Compound);
|
||||||
for (var item : value) {
|
for (var item : value) {
|
||||||
NBTCompound compound = new NBTCompound()
|
NBTCompound compound = new NBTCompound()
|
||||||
|
Loading…
Reference in New Issue
Block a user