mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-14 22:56:31 +01:00
Add experimental tags
This commit is contained in:
parent
60042effba
commit
bf44625730
@ -164,12 +164,7 @@ public class ItemMeta implements TagReadable, Writeable {
|
||||
@Deprecated
|
||||
@Contract(pure = true)
|
||||
public <T> T getOrDefault(@NotNull Tag<T> tag, @Nullable T defaultValue) {
|
||||
var key = tag.getKey();
|
||||
if (nbt.containsKey(key)) {
|
||||
return tag.read(toNBT());
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
return tag.defaultValue(defaultValue).read(toNBT());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
@ -21,7 +22,21 @@ import java.util.function.Supplier;
|
||||
@ApiStatus.NonExtendable
|
||||
public class Tag<T> {
|
||||
|
||||
private static final String EMPTY_KEY = "";
|
||||
/**
|
||||
* Reads the snbt of the tags holder.
|
||||
* <p>
|
||||
* Writing is not supported.
|
||||
*/
|
||||
@ApiStatus.Experimental
|
||||
public static final Tag<String> SNBT = new Tag<>(null, NBTCompound::toSNBT, null, null);
|
||||
|
||||
/**
|
||||
* Reads the complete tag holder compound.
|
||||
* <p>
|
||||
* Writing is not supported.
|
||||
*/
|
||||
@ApiStatus.Experimental
|
||||
public static final Tag<NBTCompound> NBT = new Tag<>(null, NBTCompound::deepClone, null, null);
|
||||
|
||||
private final String key;
|
||||
private final Function<NBTCompound, T> readFunction;
|
||||
@ -29,23 +44,31 @@ public class Tag<T> {
|
||||
|
||||
private final Supplier<T> defaultValue;
|
||||
|
||||
protected Tag(@NotNull String key,
|
||||
protected Tag(@Nullable String key,
|
||||
@NotNull Function<NBTCompound, T> readFunction,
|
||||
@NotNull BiConsumer<NBTCompound, T> writeConsumer,
|
||||
@Nullable BiConsumer<NBTCompound, T> writeConsumer,
|
||||
@Nullable Supplier<T> defaultValue) {
|
||||
this.key = key;
|
||||
this.readFunction = readFunction;
|
||||
this.writeConsumer = writeConsumer;
|
||||
this.writeConsumer = Objects.requireNonNullElse(writeConsumer, (compound, t) -> {
|
||||
});
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
protected Tag(@NotNull String key,
|
||||
protected Tag(@Nullable String key,
|
||||
@NotNull Function<NBTCompound, T> readFunction,
|
||||
@NotNull BiConsumer<NBTCompound, T> writeConsumer) {
|
||||
@Nullable BiConsumer<NBTCompound, T> writeConsumer) {
|
||||
this(key, readFunction, writeConsumer, null);
|
||||
}
|
||||
|
||||
public @NotNull String getKey() {
|
||||
/**
|
||||
* Returns the key used to navigate inside the holder nbt.
|
||||
* <p>
|
||||
* Can be null if unused (e.g. {@link #View(TagSerializer)}, {@link #SNBT} and {@link #NBT}).
|
||||
*
|
||||
* @return the tag key
|
||||
*/
|
||||
public @Nullable String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@ -96,7 +119,7 @@ public class Tag<T> {
|
||||
}
|
||||
|
||||
public void write(@NotNull NBTCompound nbtCompound, @Nullable T value) {
|
||||
if (value != null || key.equals(EMPTY_KEY)) {
|
||||
if (key == null || value != null) {
|
||||
this.writeConsumer.accept(nbtCompound, value);
|
||||
} else {
|
||||
nbtCompound.removeTag(key);
|
||||
@ -208,7 +231,7 @@ public class Tag<T> {
|
||||
}
|
||||
|
||||
public static <T> @NotNull Tag<T> View(@NotNull TagSerializer<T> serializer) {
|
||||
return new Tag<>(EMPTY_KEY,
|
||||
return new Tag<>(null,
|
||||
nbtCompound -> serializer.read(TagReadable.fromCompound(nbtCompound)),
|
||||
(nbtCompound, value) -> serializer.write(TagWritable.fromCompound(nbtCompound), value));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user