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?
Hi there,
In more recent versions, under "tool properties", towards the bottom of the screen, you can see three check boxes; "Is tool Immune to fire?", "Does item stay in crafting grid when crafted?", "Damage item instead on crafting."
If you click the bottom one, or "Damage item instead on crafting", it will damage the tool when the tool is used within a crafting recipe, but note, you also need to click the one above it, or "Does item stay in crafting grid when crafted?".
However, I have discovered, that you cannot use damaged tools in a crafting recipe. If anyone knows how to use damaged/used tools, please let me know. Thanks!
Hey, i want to do when i craft my pickaxe he as put on 1 durability how can i do that pls?