Jump to content

SOLVED Making a block change the player's currently held item when right-clicked


Recommended Posts

Posted

Hey everyone,

I'm trying to make a block give take the item that it's right-clicked with and give you back another one (for example, right click it with a diamond and you get an emerald). Surprisingly, this seems harder than it sounds. I know how to do something similar with items when they are right clicked like so:

public ItemStack onItemRightClick(ItemStack is, World world, EntityPlayer ep) {
--is.stackSize;
is = new ItemStack(Techcraft.unmagneticMagnet,1);
}

Hopefully I explained what I want to do well enough  ;)

So far the block removes the currently held item but doesn't replace it. I have looked through vanilla code and nothing in there seems to have the same functionality (things like jukeboxes just take the item which I know how to do, and when you take the item out it puts it on the ground, not into the inventory).

Here's my code so far:

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) {
	if (player.getHeldItem()!=null) {
		String theitem = player.getHeldItem().getUnlocalizedName().substring(5);
		if (theitem.equals("unmagneticMagnet")) {
			ItemStack is = player.getHeldItem();
			--is.stackSize;
			is = new ItemStack(Techcraft.normalMagnet,1);
		}
	}
	return true;
}

Please help :)

-Toastrackenigma

Posted

Thanks, that worked  :)

I was going to use the local is variable as what to set the new item to, I never thought that that would change the item in the player's hand ;)

 

For future reference my final code now is:

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) {
	if (player.getHeldItem()!=null) {
		String theitem = player.getHeldItem().getUnlocalizedName().substring(5);
		if (theitem.equals("unmagneticMagnet")) {
			player.setCurrentItemOrArmor(0, new ItemStack(Techcraft.normalMagnet,1));
		}
	}
	return true;
}

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.