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.
Crafting Durability
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:
[Spoiler]
@Override
public boolean hasContainerItem(ItemStack itemStack)
{
return true
}
@Override
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
if (itemStack.isItemEnchanted())
{
NBTTagCompound nbtcompound = itemStack.getTagCompound();
returnItem.setTagCompound(nbtcompound);
}
return returnItem;
}
[/Spoiler]
Common Problems
- 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.
That's useful information :). Thanks you, i really needed that for my future mod
Hey Man! I have some coding knowlage, mainly python and a bit of java, so I understand code, but don't know how to implement it into mcreator. If you could help I would really thank u.
I need help with this, can someone help me?