Topic category: Advanced modding
I saw many people asking how to do this, but very few answers. Many of the answers that did exist were for older versions of minecraft and didn't work anymore. So I thought I'd share the fruits of my labor.
Objective: Create an item that loses durability when it's used in crafting.
Solution: Override two functions in the items class.
Add the following code to your Item class:
public boolean hasContainerItem(ItemStack itemStack)
public ItemStack getContainerItem(ItemStack itemStack)
// copy our item.
ItemStack returnItem = new ItemStack(itemStack.getItem(), 1, itemStack.getItemDamage()+1);
// is it enchanted
// if so, copy the enchantment
NBTTagCompound nbtcompound = itemStack.getTagCompound();
- When I use the item, the texture breaks.
- Be sure to setMaxDamge(#). When I created my item in MCreator, it was set to 0 and my items texture would break. It'll return a null item.
- Also, you can edit thetrue statement in hasContinerItem() to "return itemStack.getItemDamage()<x" where X is the Max Damage value of your item. This will prevent it from returning the item once it breaks.
- When I use the item, I can't use the damaged item to craft my item.
- Change the recipe for the item your crafting to use the OreDictionary.WILDCARD_VALUE. So that the recipe will accept all durability levels.