1

I am trying to make this command work :

/execute as @a[nbt={SelectedItem:[{id:"minecraft:grass_block",tag:{display:{Name:"{\"text\":\"GRASS3x3\"}"}}}]}] run fill ~-1 ~-1 ~-1 ~1 ~-1 ~1

It works when I leave the selected item part out, but I only want the command to execute when I'm holding a specific block with a specific name.

1
  • 2
    Have you tried doing this without specifying a custom name? Are you sure the names actually match? What else have you tried besides omitting the selected item? What command are you using to get the named grass block? You need to narrow down the possible causes of the issue before we can help you, and answering these question will go a long way towards doing that. You might even stumble upon the solution yourself in the process. – MBraedley Sep 19 '19 at 18:03
2

The main problem with your command is that you have an extra set of square brackets after SelectedItem; that is, ...SelectedItem:[{id:"mi... should really be ...SelectedItem:{id:"mi...

A square bracket [ tells Minecraft that the specified tag is a list (for example ArmorItems:[{},{},{},{}]), where as a curly bracket { tells Minecraft that the specified tag is a single item (with attributes).

I'm assuming that you should also use an at @s to tell Minecraft to run at the player's coordinates and specify the block you want to fill. This turns out as

/execute as @a[nbt={SelectedItem:{id:"minecraft:grass_block",tag:{display:{Name:"{\"text\":\"GRASS3x3\"}"}}}}] at @s run fill ~-1 ~-1 ~-1 ~1 ~-1 ~1 grass_block
1
  • This is a very good answer but I'd just like to point out that testing for item's name is a terrible idea because it is a plain text check on the JSON source, which could be formatted differently but still produce the same results. It would be wise to use a custom NBT tag to detect the item. – ExpertCoder14 Mar 3 at 5:09
0

This solution should work on versions 1.16 and above. It hasn't been tested on any other versions.

Code in impulse command block: give @p blaze_rod{display:{Name:"{\"text\":\"Fire Stuff\"}"}}

Code in the repeating: execute as @a[nbt={SelectedItem:{id:"minecraft:blaze_rod",tag:{display:{Name:"{\"text\":\"Fire Stuff\"}"}}}}] at @s run fill ~-1 ~-1 ~-1 ~1 ~-1 ~1 minecraft:magma_block

Chain On The Repeating: effect give @a[nbt={SelectedItem:{id:"blaze_rod",tag:{display:{Name:"{\"text\":\"Fire Stuff\"}"}}}}] minecraft:fire_resistance 50 100 true

1
  • 1
    This has already been stated in the previous answer - is there something new that this answer provides? – ExpertCoder14 Mar 9 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.