Minecraft Java Edition – How to Teleport Players with an Item Using Commands

minecraft-commandsminecraft-java-edition

I want to teleport a player when they use a certain item, let's say, a stick, name wand. When the player uses it they'll be teleported to a certain place.

Best Answer

First, you'll need to give the item:

/give @p carrot_on_a_stick 1 0 {display:{Name:"Wand"}}

And add an objective to keep track of when people right click with (any) carrot on a stick:

/scoreboard objectives add UseCarrotStick stat.useItem.minecraft.carrot_on_a_stick

Then, repeating and in this order, have the following commands:

/scoreboard players tag @a remove HoldingWand
/scoreboard players tag @a add HoldingWand {SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{display:{Name:"Wand"}}}}
/tp @a[tag=HoldingWand,score_UseCarrotStick_min=1] X Y Z
/scoreboard players set @a UseCarrotStick 0

This gives anyone holding the wand a "HoldingWand" tag, then teleports anyone who has both a UseCarrotStick score of >=1 and the Holdingwand tag to X Y Z (to be replaced).

The first command is needed so that people who deselect the wand have the tag removed, and the last command is needed so that people are only teleported once per right click.