From 2e05c3a76a74b65989112f4604dfd95c28ccb782 Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Wed, 8 Apr 2026 01:49:47 +0200 Subject: [PATCH] add ItemStackNBT helper (#323) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../gtnhlib/item/ItemStackNBT.java | 628 ++++++++++++++++++ 1 file changed, 628 insertions(+) create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/item/ItemStackNBT.java diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/item/ItemStackNBT.java b/src/main/java/com/gtnewhorizon/gtnhlib/item/ItemStackNBT.java new file mode 100644 index 00000000..bc8f7c26 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/item/ItemStackNBT.java @@ -0,0 +1,628 @@ +package com.gtnewhorizon.gtnhlib.item; + +import java.util.Collections; +import java.util.Objects; +import java.util.Set; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.util.Constants.NBT; + +import org.apache.commons.lang3.StringUtils; + +/** + * Utility class for safe and convenient manipulation of {@link ItemStack} NBT data. + * + *

+ * This class provides two complementary APIs: + *

+ * + * + * + *

Key Features

+ * + * + *

Static vs Instance Usage

+ * + *

Static API

+ *

+ * The static methods avoid any additional object allocation and should be preferred in performance-sensitive contexts + * such as ticking logic, inventory iteration, or rendering. + *

+ * + *
+ * {@code
+ * ItemStack stack = ...;
+ *
+ * // Write values
+ * ItemStackNBT.setInteger(stack, "energy", 1000);
+ * ItemStackNBT.setBoolean(stack, "active", true);
+ *
+ * // Read values
+ * int energy = ItemStackNBT.getInteger(stack, "energy");
+ * boolean active = ItemStackNBT.getBoolean(stack, "active");
+ * }
+ * 
+ * + *

Instance API

+ *

+ * The instance API wraps an {@link ItemStack} and allows method chaining for improved readability. This is slightly + * less efficient due to object allocation, but often negligible outside hot loops. + *

+ * + *
+ * {@code
+ * ItemStack stack = ...;
+ *
+ * ItemStackNBT.of(stack)
+ *     .setInteger("energy", 1000)
+ *     .setInteger("bla", 87)
+ *     .setInteger("ducks", 55)
+ *     .setBoolean("active", true);
+ *
+ * int energy = ItemStackNBT.of(stack).getInteger("energy");
+ * }
+ * 
+ * + *

Behavior Notes

+ * + */ +public final class ItemStackNBT { + + private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + private static final int[] EMPTY_INT_ARRAY = new int[0]; + + @Nonnull + private final ItemStack stack; + + private ItemStackNBT(@Nonnull ItemStack stack) { + this.stack = stack; + } + + public static ItemStackNBT of(ItemStack stack) { + return new ItemStackNBT(Objects.requireNonNull(stack)); + } + + // region instance setters methods + + public ItemStackNBT setTag(String key, NBTBase value) { + Objects.requireNonNull(value); + ensureInitialized(); + stack.getTagCompound().setTag(key, value); + return this; + } + + public ItemStackNBT setByte(String key, byte value) { + ensureInitialized(); + stack.getTagCompound().setByte(key, value); + return this; + } + + public ItemStackNBT setShort(String key, short value) { + ensureInitialized(); + stack.getTagCompound().setShort(key, value); + return this; + } + + public ItemStackNBT setInteger(String key, int value) { + ensureInitialized(); + stack.getTagCompound().setInteger(key, value); + return this; + } + + public ItemStackNBT setLong(String key, long value) { + ensureInitialized(); + stack.getTagCompound().setLong(key, value); + return this; + } + + public ItemStackNBT setFloat(String key, float value) { + ensureInitialized(); + stack.getTagCompound().setFloat(key, value); + return this; + } + + public ItemStackNBT setDouble(String key, double value) { + ensureInitialized(); + stack.getTagCompound().setDouble(key, value); + return this; + } + + public ItemStackNBT setString(String key, String value) { + Objects.requireNonNull(value); + ensureInitialized(); + stack.getTagCompound().setString(key, value); + return this; + } + + public ItemStackNBT setByteArray(String key, byte[] value) { + Objects.requireNonNull(value); + ensureInitialized(); + stack.getTagCompound().setByteArray(key, value); + return this; + } + + public ItemStackNBT setIntArray(String key, int[] value) { + Objects.requireNonNull(value); + ensureInitialized(); + stack.getTagCompound().setIntArray(key, value); + return this; + } + + public ItemStackNBT setCompoundTag(String key, NBTTagCompound value) { + Objects.requireNonNull(value); + ensureInitialized(); + stack.getTagCompound().setTag(key, value); + return this; + } + + public ItemStackNBT setTagList(String key, NBTTagList value) { + Objects.requireNonNull(value); + ensureInitialized(); + stack.getTagCompound().setTag(key, value); + return this; + } + + public ItemStackNBT setBoolean(String key, boolean value) { + ensureInitialized(); + stack.getTagCompound().setBoolean(key, value); + return this; + } + + /** + * Sets the display name for this stack, if the parameter is null or empty it resets the display name of this stack. + */ + public ItemStackNBT setDisplayName(String name) { + if (StringUtils.isBlank(name)) { + stack.func_135074_t(); + } else { + stack.setStackDisplayName(name); + } + return this; + } + + // endregion + // region instance getter methods + + public Set getKeySet() { + if (stack.hasTagCompound()) { + return stack.getTagCompound().func_150296_c(); + } + return Collections.emptySet(); + } + + public byte getTagId(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().func_150299_b(key); + } + return 0; + } + + public NBTBase getTag(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getTag(key); + } + return null; + } + + public byte getByte(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getByte(key); + } + return 0; + } + + public short getShort(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getShort(key); + } + return 0; + } + + public int getInteger(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getInteger(key); + } + return 0; + } + + public long getLong(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getLong(key); + } + return 0L; + } + + public float getFloat(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getFloat(key); + } + return 0.0F; + } + + public double getDouble(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getDouble(key); + } + return 0.0D; + } + + public String getString(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getString(key); + } + return ""; + } + + public byte[] getByteArray(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getByteArray(key); + } + return EMPTY_BYTE_ARRAY; + } + + public int[] getIntArray(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getIntArray(key); + } + return EMPTY_INT_ARRAY; + } + + @Nullable + public NBTTagCompound getCompoundTag(String key) { + if (stack.hasTagCompound()) { + final NBTTagCompound nbt = stack.getTagCompound(); + if (nbt.hasKey(key, NBT.TAG_COMPOUND)) { + return nbt.getCompoundTag(key); + } + } + return null; + } + + @Nullable + public NBTTagList getTagList(String key, int type) { + if (stack.hasTagCompound()) { + final NBTTagCompound nbt = stack.getTagCompound(); + if (nbt.hasKey(key, NBT.TAG_LIST)) { + return nbt.getTagList(key, type); + } + } + return null; + } + + public boolean getBoolean(String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getBoolean(key); + } + return false; + } + + // endregion + // region instance util methods + + public boolean hasKey(String key) { + return stack.hasTagCompound() && stack.getTagCompound().hasKey(key); + } + + public boolean hasKey(String key, int type) { + return stack.hasTagCompound() && stack.getTagCompound().hasKey(key, type); + } + + public ItemStackNBT removeTag(String key) { + if (stack.hasTagCompound()) { + final NBTTagCompound nbt = stack.getTagCompound(); + nbt.removeTag(key); + if (nbt.hasNoTags()) { + stack.setTagCompound(null); + } + } + return this; + } + + public boolean hasNoTags() { + return !stack.hasTagCompound() || stack.getTagCompound().hasNoTags(); + } + + @Nullable + public NBTTagCompound copy() { + if (stack.hasTagCompound()) { + return (NBTTagCompound) stack.getTagCompound().copy(); + } + return null; + } + + private void ensureInitialized() { + if (!stack.hasTagCompound()) { + stack.setTagCompound(new NBTTagCompound()); + } + } + + // endregion + // region static setter methods + + public static void setTag(ItemStack stack, String key, NBTBase value) { + Objects.requireNonNull(value); + ensureInitialized(stack); + stack.getTagCompound().setTag(key, value); + } + + public static void setByte(ItemStack stack, String key, byte value) { + ensureInitialized(stack); + stack.getTagCompound().setByte(key, value); + } + + public static void setShort(ItemStack stack, String key, short value) { + ensureInitialized(stack); + stack.getTagCompound().setShort(key, value); + } + + public static void setInteger(ItemStack stack, String key, int value) { + ensureInitialized(stack); + stack.getTagCompound().setInteger(key, value); + } + + public static void setLong(ItemStack stack, String key, long value) { + ensureInitialized(stack); + stack.getTagCompound().setLong(key, value); + } + + public static void setFloat(ItemStack stack, String key, float value) { + ensureInitialized(stack); + stack.getTagCompound().setFloat(key, value); + } + + public static void setDouble(ItemStack stack, String key, double value) { + ensureInitialized(stack); + stack.getTagCompound().setDouble(key, value); + } + + public static void setString(ItemStack stack, String key, String value) { + Objects.requireNonNull(value); + ensureInitialized(stack); + stack.getTagCompound().setString(key, value); + } + + public static void setByteArray(ItemStack stack, String key, byte[] value) { + Objects.requireNonNull(value); + ensureInitialized(stack); + stack.getTagCompound().setByteArray(key, value); + } + + public static void setIntArray(ItemStack stack, String key, int[] value) { + Objects.requireNonNull(value); + ensureInitialized(stack); + stack.getTagCompound().setIntArray(key, value); + } + + public static void setCompoundTag(ItemStack stack, String key, NBTTagCompound value) { + Objects.requireNonNull(value); + ensureInitialized(stack); + stack.getTagCompound().setTag(key, value); + } + + public static void setTagList(ItemStack stack, String key, NBTTagList value) { + Objects.requireNonNull(value); + ensureInitialized(stack); + stack.getTagCompound().setTag(key, value); + } + + public static void setBoolean(ItemStack stack, String key, boolean value) { + ensureInitialized(stack); + stack.getTagCompound().setBoolean(key, value); + } + + /** + * Sets the display name for this stack, if the parameter is null or empty it resets the display name of this stack. + */ + public static void setDisplayName(ItemStack stack, String name) { + if (StringUtils.isBlank(name)) { + stack.func_135074_t(); + } else { + stack.setStackDisplayName(name); + } + } + + // endregion + // region static getter methods + + public static Set getKeySet(ItemStack stack) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().func_150296_c(); + } + return Collections.emptySet(); + } + + public static byte getTagId(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().func_150299_b(key); + } + return 0; + } + + public static NBTBase getTag(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getTag(key); + } + return null; + } + + public static byte getByte(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getByte(key); + } + return 0; + } + + public static short getShort(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getShort(key); + } + return 0; + } + + public static int getInteger(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getInteger(key); + } + return 0; + } + + public static long getLong(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getLong(key); + } + return 0L; + } + + public static float getFloat(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getFloat(key); + } + return 0.0F; + } + + public static double getDouble(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getDouble(key); + } + return 0.0D; + } + + public static String getString(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getString(key); + } + return ""; + } + + public static byte[] getByteArray(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getByteArray(key); + } + return EMPTY_BYTE_ARRAY; + } + + public static int[] getIntArray(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getIntArray(key); + } + return EMPTY_INT_ARRAY; + } + + @Nullable + public static NBTTagCompound getCompoundTag(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + final NBTTagCompound nbt = stack.getTagCompound(); + if (nbt.hasKey(key, NBT.TAG_COMPOUND)) { + return nbt.getCompoundTag(key); + } + } + return null; + } + + @Nullable + public static NBTTagList getTagList(ItemStack stack, String key, int type) { + if (stack.hasTagCompound()) { + final NBTTagCompound nbt = stack.getTagCompound(); + if (nbt.hasKey(key, NBT.TAG_LIST)) { + return nbt.getTagList(key, type); + } + } + return null; + } + + public static boolean getBoolean(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().getBoolean(key); + } + return false; + } + + // endregion + // region static util methods + + public static boolean hasKey(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().hasKey(key); + } + return false; + } + + public static boolean hasKey(ItemStack stack, String key, int type) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().hasKey(key, type); + } + return false; + } + + public static void removeTag(ItemStack stack, String key) { + if (stack.hasTagCompound()) { + final NBTTagCompound nbt = stack.getTagCompound(); + nbt.removeTag(key); + if (nbt.hasNoTags()) { + stack.setTagCompound(null); + } + } + } + + public static boolean hasNoTags(ItemStack stack) { + if (stack.hasTagCompound()) { + return stack.getTagCompound().hasNoTags(); + } + return true; + } + + @Nullable + public static NBTTagCompound copy(ItemStack stack) { + if (stack.hasTagCompound()) { + return (NBTTagCompound) stack.getTagCompound().copy(); + } + return null; + } + + /** + * This method returns the NBTTagCompound attached to the ItemStack, if it is not present it creates one and returns + * it. This method defeats the whole point of the api, only use if you absolutely need the NBTTagCompound to exist. + */ + @Nonnull + public static NBTTagCompound get(ItemStack stack) { + if (!stack.hasTagCompound()) { + stack.setTagCompound(new NBTTagCompound()); + } + return stack.getTagCompound(); + } + + private static void ensureInitialized(ItemStack stack) { + if (!stack.hasTagCompound()) { + stack.setTagCompound(new NBTTagCompound()); + } + } + // endregion +}