[Tips] Crafting Durability

Started by GenreShinobi on

Topic category: Advanced modding

Last seen on 00:22, 28. Dec 2016
Joined Dec 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Tips] Crafting Durability
Tue, 12/27/2016 - 14:47 (edited)

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.
Edited by GenreShinobi on Tue, 12/27/2016 - 14:47
Last seen on 20:12, 9. Nov 2021
Joined Aug 2015
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
That's useful information :).
Tue, 12/27/2016 - 14:31

That's useful information :). Thanks you, i really needed that for my future mod

Last seen on 20:24, 12. Mar 2018
Joined Feb 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey Man! I have some coding
Thu, 12/29/2016 - 01:55

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.

Last seen on 20:22, 30. Jul 2019
Joined Jan 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I need help with this, can…
Wed, 05/09/2018 - 20:16

I need help with this, can someone help me?

Last seen on 03:13, 25. Dec 2023
Joined Dec 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hi there, In more recent…
Fri, 12/22/2023 - 02:23

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!