Minecraft – How to use the “SelectedItem” tag to check for a player in this command

minecraft-commandsminecraft-java-edition

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.

Best Answer

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