Adding NBT Data to Items

I understand you want to have more data stored in your item than the 16-bit signed item damage value given by Minecraft. You data obsessed modder.

First of all, let’s clear things up. It’s itemstacks that you can store data onto, not the items. This is because Items are only of one, and only ONE instance per.. per everything. You only get to register one instance of an item, or you’ll crash the game. Boo hoo. A way around this? ItemStacks. Every item in your inventory slot is a different instance of itemstack. And thus, they don’t need to be identical and thus, may have different data.

Let’s get started. In my case, I am going to make an item that can store the amount of uses in it, and display it in its lore.

Let’s make a basic item class:

public class ItemCounter extends Item
{
    private static final String name = "counter";

    public ItemCounter()
    {
        setUnlocalizedName(name);
        setCreativeTab(CreativeTabs.tabTools);
        GameRegistry.registerItem(this, name);
    }
}

There we go, a simple item. No biggie. Like I said, i want it to count every time I right click with it. So, let’s use a vanilla provided function, onItemRightClick().

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
    return stack;
}

Now comes the real deal. First, we add a check if the itemstack has a compound tag. Or basically we check if the itemstack is ready for being stored data. Because if it doesn’t have one, we wouldn’t be able to put our data in it. Anyway, if it exists, use that. If it doesn’t, make a new one. Simple enough.

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
    NBTTagCompound nbt;
    if (stack.hasTagCompound())
    {
        nbt = stack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }

    return stack;
}

There we go. Now, we store our data.

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
    NBTTagCompound nbt;
    if (stack.hasTagCompound())
    {
        nbt = stack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }

    if (nbt.hasKey("Uses"))
    {
        nbt.setInteger("Uses", nbt.getInteger("Uses") + 1);
    }
    else
    {
        nbt.setInteger("Uses", 1);
    }
    stack.setTagCompound(nbt);

    return stack;
}

Done! Notice that I set the new NBT data to the itemstack again. Now we simply add a way to display this, I’ll use lores.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List lores, boolean b)
{
    if (stack.hasTagCompound() && stack.getTagCompound().hasKey("Uses"))
    {
        lores.add(Integer.toString(stack.getTagCompound().getInteger("Uses")));
    }
}

So our item code should look like so:

public class ItemCounter extends Item
{
    private static final String name = "counter";

    public ItemCounter()
    {
        setUnlocalizedName(name);
        setCreativeTab(CreativeTabs.tabTools);
        GameRegistry.registerItem(this, name);
    }

    @Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
    {
        NBTTagCompound nbt;
        if (stack.hasTagCompound())
        {
            nbt = stack.getTagCompound();
        }
        else
        {
            nbt = new NBTTagCompound();
        }

        if (nbt.hasKey("Uses"))
        {
            nbt.setInteger("Uses", nbt.getInteger("Uses") + 1);
        }
        else
        {
            nbt.setInteger("Uses", 1);
        }
        stack.setTagCompound(nbt);

        return stack;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List lores, boolean b)
    {
        if (stack.hasTagCompound() && stack.getTagCompound().hasKey("Uses"))
        {
            lores.add(Integer.toString(stack.getTagCompound().getInteger("Uses")));
        }
    }
}

That’s it this time, cya!

Minecraft 1.7.10 1_27_2016 11_04_43 PM