Jump to content

Damage Items when Crafted


jrockjake

Recommended Posts

Sorry if I seem repetitive, but I can't figure this thing out. I'm making a hammer item that, when crafted with an iron ingot, makes three chains to be used in chainmail armor. However, I want the hammer to not be destroyed during the crafting, instead taking damage. I've looked through TFC's code and can't figure it out, and I've rummaged through Forge itself and couldn't find anything. Some help would be appreciative, thanks.

Link to comment
Share on other sites

I'll go fetch the code I used to make an object like the one you're attempting to make. It's really quite simple.

Like atrain said, you need a Crafting Handler class, and you need to register it in your main mod class with

GameRegistry.registerCraftingHandler(new Your Crafting Handler());

 

and then inside the crafting handler, which implements ICraftingHandler, your onCrafting method should look a little something like this.

public void onCrafting(EntityPlayer player, ItemStack item, IInventory inv) 
{
  	for(int i=0; i < inv.getSizeInventory(); i++)
	{        	
    	if(inv.getStackInSlot(i) != null)
    	{
    		ItemStack j = inv.getStackInSlot(i);
    		if(j.getItem() != null && j.getItem() == Your Hammer Item)
    		{
    			ItemStack k = new ItemStack(Your Hammer Item, 2, (j.getItemDamage() + 1));
    			inv.setInventorySlotContents(i, k);
    		}
    	}  
	}
}

 

Basically what this does, is run a check every time something is crafted. It looks in every slot and if it finds Your Hammer Item, it replaces that stack with a new stack of 2 of your hammer item, one of which is used up in the crafting. It also adds one damage to the item.

 

Happy Modding :)

Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.