How to replace item with another item when right-clicking a block?

Started by holidaupy on

Topic category: Help with Minecraft modding (Java Edition)

Joined Dec 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to replace item with another item when right-clicking a block?

So I'm making a feature where you click on a block and it replaces a specific item with another item. The only semi-sufficient solution I was able to find was removing the item and adding the replacement item. The issue with this  is that when the first item can't be found in the in the player's inventory, it removes nothing and adds the replacement item anyways. No forums I've looked up have really helped, does anybody have any other ideas or a solution?

Joined Dec 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Create a procedure and use…
Sat, 12/28/2024 - 06:06

Create a procedure and use the global trigger "On player right clicks block"

on player right clicks block
	if >get block at x y z = customBlock AND item in main-hand of event/target entity = ite	m1
	do
		set item in main-hand of event/target entity to 1 item2
		

The "set item in main-hand..." procedure block will remove/replace any item currently in the main hand.

It sounds to me that you are simply missing an "if item in main-hand" procedure.

Debugging Notes:

  1. Make sure your global trigger is right clicks block
  2. Make sure you are using the AND operator and not the OR operator to ensure that both conditions are met before proceeding.
Joined Dec 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
If you are trying to replace…
Sat, 12/28/2024 - 06:14

If you are trying to replace an item from anywhere in the inventory, your procedure will look a little more like this.

on player right clicks block
if: get block x y z = block AND has event/target entity in inventory [item] = true
do:
	remove 1 [item] from event/target entities main inventory
	add 1 [item] to event/target entities main inventory