diff --git a/src/main/java/net/minestom/server/item/ItemMeta.java b/src/main/java/net/minestom/server/item/ItemMeta.java index 04d0023ba..2f1b4657e 100644 --- a/src/main/java/net/minestom/server/item/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/ItemMeta.java @@ -4,6 +4,8 @@ import io.netty.buffer.ByteBuf; import net.kyori.adventure.text.Component; import net.minestom.server.instance.block.Block; import net.minestom.server.item.attribute.ItemAttribute; +import net.minestom.server.tag.Tag; +import net.minestom.server.tag.TagReader; import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.Contract; @@ -14,7 +16,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.*; import java.util.function.Consumer; -public class ItemMeta implements Writeable { +public class ItemMeta implements TagReader, Writeable { private final int damage; private final boolean unbreakable; @@ -110,7 +112,7 @@ public class ItemMeta implements Writeable { } @Contract(pure = true) - public T getOrDefault(@NotNull ItemTag tag, @Nullable T defaultValue) { + public T getOrDefault(@NotNull Tag tag, @Nullable T defaultValue) { var key = tag.getKey(); if (nbt.containsKey(key)) { return tag.read(toNBT()); @@ -119,8 +121,18 @@ public class ItemMeta implements Writeable { } } - public @Nullable T get(@NotNull ItemTag tag) { - return tag.read(toNBT()); + @Override + public @Nullable T getTag(@NotNull Tag tag) { + return tag.read(nbt); + } + + @Override + public boolean hasTag(@NotNull Tag tag) { + return nbt.containsKey(tag.getKey()); + } + + public @Nullable T get(@NotNull Tag tag) { + return getTag(tag); } public @NotNull NBTCompound toNBT() { diff --git a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java index 58b23f92e..351a413db 100644 --- a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java +++ b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java @@ -5,6 +5,8 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.minestom.server.adventure.AdventureSerializer; import net.minestom.server.instance.block.Block; import net.minestom.server.item.attribute.ItemAttribute; +import net.minestom.server.tag.Tag; +import net.minestom.server.tag.TagWriter; import net.minestom.server.utils.NBTUtils; import net.minestom.server.utils.Utils; import org.jetbrains.annotations.Contract; @@ -16,7 +18,7 @@ import java.util.*; import java.util.function.Consumer; import java.util.function.Supplier; -public abstract class ItemMetaBuilder { +public abstract class ItemMetaBuilder implements TagWriter { protected NBTCompound nbt = new NBTCompound(); @@ -183,12 +185,13 @@ public abstract class ItemMetaBuilder { return canDestroy(Set.of(blocks)); } - public @NotNull ItemMetaBuilder set(@NotNull ItemTag tag, @Nullable T value) { - if (value != null) { - tag.write(nbt, value); - } else { - this.nbt.removeTag(tag.getKey()); - } + @Override + public void setTag(@NotNull Tag tag, @Nullable T value) { + tag.write(nbt, value); + } + + public @NotNull ItemMetaBuilder set(@NotNull Tag tag, @Nullable T value) { + setTag(tag, value); return this; } diff --git a/src/main/java/net/minestom/server/item/ItemTag.java b/src/main/java/net/minestom/server/item/ItemTag.java index 6f0c02a65..70ad3b527 100644 --- a/src/main/java/net/minestom/server/item/ItemTag.java +++ b/src/main/java/net/minestom/server/item/ItemTag.java @@ -1,112 +1,65 @@ package net.minestom.server.item; +import net.minestom.server.tag.Tag; import org.jetbrains.annotations.NotNull; import org.jglrxavpok.hephaistos.nbt.NBT; import org.jglrxavpok.hephaistos.nbt.NBTCompound; -import org.jglrxavpok.hephaistos.nbt.NBTList; import java.util.function.BiConsumer; import java.util.function.Function; -public class ItemTag { +/** + * @deprecated use {@link Tag}. + */ +@Deprecated +public class ItemTag extends Tag { - private final String key; - private final Function readFunction; - private final BiConsumer writeConsumer; - - private ItemTag(@NotNull String key, - @NotNull Function readFunction, - @NotNull BiConsumer writeConsumer) { - this.key = key; - this.readFunction = readFunction; - this.writeConsumer = writeConsumer; + protected ItemTag(@NotNull String key, @NotNull Function readFunction, @NotNull BiConsumer writeConsumer) { + super(key, readFunction, writeConsumer); } - public @NotNull String getKey() { - return key; + public static @NotNull Tag Byte(@NotNull String key) { + return Tag.Byte(key); } - protected T read(@NotNull NBTCompound nbtCompound) { - return readFunction.apply(nbtCompound); + public static @NotNull Tag Short(@NotNull String key) { + return Tag.Short(key); } - protected void write(@NotNull NBTCompound nbtCompound, @NotNull T value) { - this.writeConsumer.accept(nbtCompound, value); + public static @NotNull Tag Integer(@NotNull String key) { + return Tag.Integer(key); } - public static @NotNull ItemTag Byte(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getByte(key), - (nbtCompound, value) -> nbtCompound.setByte(key, value)); + public static @NotNull Tag Long(@NotNull String key) { + return Tag.Long(key); } - public static @NotNull ItemTag Short(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getShort(key), - (nbtCompound, value) -> nbtCompound.setShort(key, value)); + public static @NotNull Tag Float(@NotNull String key) { + return Tag.Float(key); } - public static @NotNull ItemTag Integer(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getInt(key), - (nbtCompound, integer) -> nbtCompound.setInt(key, integer)); + public static @NotNull Tag Double(@NotNull String key) { + return Tag.Double(key); } - public static @NotNull ItemTag Long(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getLong(key), - (nbtCompound, value) -> nbtCompound.setLong(key, value)); + public static @NotNull Tag ByteArray(@NotNull String key) { + return Tag.ByteArray(key); } - public static @NotNull ItemTag Float(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getFloat(key), - (nbtCompound, value) -> nbtCompound.setFloat(key, value)); + public static @NotNull Tag String(@NotNull String key) { + return Tag.String(key); } - public static @NotNull ItemTag Double(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getDouble(key), - (nbtCompound, value) -> nbtCompound.setDouble(key, value)); + public static @NotNull Tag NBT(@NotNull String key) { + return Tag.NBT(key); } - public static @NotNull ItemTag ByteArray(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getByteArray(key), - (nbtCompound, value) -> nbtCompound.setByteArray(key, value)); + public static @NotNull Tag IntArray(@NotNull String key) { + return Tag.IntArray(key); } - public static @NotNull ItemTag String(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getString(key), - (nbtCompound, value) -> nbtCompound.setString(key, value)); - } - - public static @NotNull ItemTag NBT(@NotNull String key) { - return new ItemTag<>(key, - nbt -> { - var currentNBT = nbt.get(key); - - // Avoid a NPE when cloning a null variable. - if (currentNBT == null) { - return null; - } - - return currentNBT.deepClone(); - }, - ((nbt, value) -> nbt.set(key, value.deepClone()))); - } - - public static @NotNull ItemTag IntArray(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getIntArray(key), - (nbtCompound, value) -> nbtCompound.setIntArray(key, value)); - } - - public static @NotNull ItemTag LongArray(@NotNull String key) { - return new ItemTag<>(key, - nbtCompound -> nbtCompound.getLongArray(key), - (nbtCompound, value) -> nbtCompound.setLongArray(key, value)); + public static @NotNull Tag LongArray(@NotNull String key) { + return Tag.LongArray(key); } } diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index d1f546709..797413253 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -1,5 +1,6 @@ package net.minestom.server.tag; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBT; @@ -16,14 +17,15 @@ import java.util.function.Supplier; * * @param the tag type */ -public final class Tag { +@ApiStatus.NonExtendable +public class Tag { private final String key; private final Function readFunction; private final BiConsumer writeConsumer; private volatile Supplier defaultValue; - private Tag(@NotNull String key, + protected Tag(@NotNull String key, @NotNull Function readFunction, @NotNull BiConsumer writeConsumer) { this.key = key;