GenuineSounds

Item damage in crafting recipes without leaving the box.

Jun 10th, 2012
207
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. /** Item init and shizzle */
  2. // I shouldn't need to tell you this :P
  3. YOURITEM = new YOURITEM(BLAHID1982);
  4. // Or this.
  5. ModLoader.addName(YOURITEM, "Something Awesome!");
  6. // Dirt blocks are used up as per usual. This let's you use the crafting recipe so long as the item isn't damaged beyond the limit. And will leave the 'depleted' item in there.
  7. for (int damage = 0; damage < genuineSword.maxStackSize; damage++)
  8.     ModLoader.addShapelessRecipe(SOMETHINGELSE, new ItemStack(YOURITEM, 1, damage), Block.dirt);
  9.  
  10.  
  11. /** This goes in your BaseMod */
  12. @Override
  13. public void takenFromCrafting(EntityPlayer entityplayer, ItemStack itemstack, IInventory iinventory) {
  14.     for (int i = 0; i < iinventory.getSizeInventory(); i++) {
  15.         ItemStack item = iinventory.getStackInSlot(i);
  16.         // You can do other checks to make sure it's the right recipe if you have other recipes in which the item is not damaged but is used as normal.
  17.         if (item != null && item.getItem() == YOURITEM) {
  18.             // Damage the item.
  19.             item.setItemDamage(item.getItemDamage() + 1);
  20.             // Dirty hack to keep one in there, you should set your items max stack size to 1 as damaged items in stacks all items in the stack are damaged not just one.
  21.             item.stackSize = 2;
  22.             // Dirty hack to compensate for some weird glitch where only half the items are created when shift clicking.
  23.             if (itemstack.stackSize == 0 && item.getItemDamage() % 2 == 0) {
  24.                 ItemStack newItem = itemstack.copy();
  25.                 newItem.stackSize = 1;
  26.                 entityplayer.inventory.addItemStackToInventory(newItem);
  27.             }
  28.             // Debug. As you'll notice that the itemstack that is introduced is actually what you clicked on the crafting result with plus that item.
  29.             // When shift clicking the itemstack has a stack size of 0 for some reason.
  30.             System.out.println("It worked with " + item.getItemDamage() + " damage and resulted in " + itemstack);
  31.         }
  32.     }
  33.     // This isn't needed I don't think, but just in case :3
  34.     super.takenFromCrafting(entityplayer, itemstack, iinventory);
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. // -------------------------------------------------------------------------------------
  43. // -------------------------------------------------------------------------------------
  44. // This is the item I was using to test it out with.
  45. // -------------------------------------------------------------------------------------
  46. // -------------------------------------------------------------------------------------
  47.  
  48. package net.minecraft.src;
  49.  
  50. public class GenuineItemSword extends ItemSword {
  51.  
  52.     public GenuineItemSword(int par1) {
  53.         super(par1, EnumToolMaterial.EMERALD);
  54.         this.setMaxStackSize(1);
  55.         this.setMaxDamage(10);
  56.         this.setItemName("genuineSword");
  57.     }
  58.  
  59.     // A recharge method. My sword absorbs souls!!!
  60.     @Override
  61.     public boolean hitEntity(ItemStack itemStack, EntityLiving par2EntityLiving, EntityLiving par3EntityLiving) {
  62.         if (itemStack.getItemDamage() > 0)
  63.             itemStack.setItemDamage(itemStack.getItemDamage() - 1);
  64.         return true;
  65.     }
  66. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment