TransWikia.com

How do I make items run commands?

Arqade Asked by PlanetO86 on June 15, 2021

I want to make a carrots on sticks run commands, but I want different carrots on sticks to have different names and run different commands. Is this possible?

2 Answers

You could use something on this lines of /execute at @a[nbt={SelectedItem:{id:"minecraft:<id>",name:"<name>"}}] run <command> in order to run the command, replacing anything in <> with the appropriate option. You can run this as many times as you want, changing those 3 selectors, as long as those are in repeating command blocks, either constantly powered or with "always active" enabled.

Answered by Sayne on June 15, 2021

For running commands if you're holding the item, you can use as or at the player, and use the nbt target selector to check for the SelectedItem tag of the player.

execute as @a[nbt = {SelectedItem: {id: "minecraft:carrot_on_a_stick", tag: {custom_tag: 1b}}}] at @s run say test

Here's a quick rundown:

  • as changes the executor of the command as the entity, meaning you can use @s to target the executor's self just after declaring who to execute it as.
  • at changes the positional context of the command, meaning it will run the command relative to the entity.
  • It's not necessary to use both, but it really depends on what you're doing.
  • @a[...] indicates players, meaning it will only select players.
  • nbt checks for the entity's NBT, which in this case, we're checking for the player's SelectedItem tag
  • {SelectedItem: {id: "minecraft:carrot_on_a_stick", tag: {custom_tag: 1b}}} refers to the item about to be checked. We'll set the id string to "minecraft:carrot_on_a_stick" since we'll be checking for that item. As for tag, it refers to the item's NBT. In this case, we'll be checking for the custom_tag: 1b NBT in the item.

The item in the provided example can be obtained by running this command:

give <player> minecraft:carrot_on_a_stick{custom_tag: 1b}

Now, if you want to check if a player has interacted with that specific carrot on a stick, you would need a scoreboard with the minecraft.used:minecraft.carrot_on_a_stick criterion. As for why, it's a bug that would be useful in our case.

After creating the objective, you would check if a player has a score in the scoreboard objective, using the scores target selector argument. So the whole command would look like this:

execute as @a[nbt = {SelectedItem: {id: "minecraft:carrot_on_a_stick", tag: {custom_tag: 1b}}}, scores = {<objective> = 1..}] at @s run say test
  • <objective> being the name of your newly created scoreboard objective.
  • 1.. is a range that would check if the player has 1 or more value in the said objective

Afterwards, you would reset the score of the player in the objective by running a /scoreboard players command:

scoreboard players reset @a[scores = {<objective> = 1..}] <objective>

Answered by eggohito on June 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP