Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

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

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.