[Tips] Crafting Durability

Started by GenreShinobi on Tue, 12/27/2016 - 00:24

Topic category: Advanced modding

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

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
[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 19:45, 15. Dec 2018
Joined Aug 2015
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
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
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
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
User points:

Statistics:

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

I need help with this, can someone help me?