How can I write a command that checks for a named item?

I'm trying to set up a contraption that will scan for a Ghast Tear called "essence of chuck norris", but I can't seem to get the commands right.

How can I do this?


You need to use the /testfor command:

/testfor <target> [NBT]

And in your case, it will look like this:

/testfor @e[type=Item] {Item:{id:minecraft:ghast_tear,tag:{display:{Name:"essence of chuck norris"}}}}

But watch out! The commands are case-senitive, watch out especially for capital letters.


Here is a solution that will tag any player with the named ghast tear. This tag can then be used to target those players for other commands. Commands are for 1.12.2.


Use 2 command blocks in a chain.

The first command block is RepeatUnconditionalAlways Active with command:

scoreboard players tag @a remove Holding

The second command block is ChainUnconditionalAlways Active with command:

scoreboard players tag @a add Holding {Inventory:[{id:"minecraft:ghast_tear",tag:{display:{Name:"essence of chuck norris"}}}]}

Now you can use this tag to target the players who currently have the item in their inventory. An example command to kill them all would be:

kill @a[tag=Holding]