How can I detect if a player has a specific item with a specific datavalue in their inventory?

This answer was written prior to the release of 1.13 and "The Flattening" that came with it. An updated answer can be found here.


You're actually pretty close, and definitely on the right track. Instead of {Inventory:[{id:<block ID>,data:<data value>}]}, use {Inventory:[{id:<block ID>,Damage:<data value>s}]}. The s after the data value is important since it's stored as a short, not as an int. (As an aside, if it were stored as a byte, you would append a b instead.)


The simplest solution is to update to 1.13, which implemented "The Flattening". For every block that had multiple variations based on the data value, a new block has been introduced for each variation with its own block ID (name). This effects the coloured blocks in particular, but there's a number of other block types (wood blocks, for instance) that are also affected. Now all you have to do is:

/tag @a[nbt={Inventory:[{id:red_wool}]}] add CarryingRed

Additionally, 1.13 introduced the idea of tags for groups of blocks or items (or functions) that are related. If you want to tag someone (in the sense of the former scoreboard tag functionality like above), all you have to do is select the wool tag in the selector:

/tag @a[nbt={Inventory:[{id:#wool}]}] add CarryingWool

Note the #wool, which will match any item that is listed in the wool tag, which by default includes all colours of wool.


You can detect item without actually clearing using /clear command.
Syntax for /clear command is following :

/clear [player] [item] [data] [maxCount] [dataTag]

If you set maxCount to 0, command will output success only if player has that specified item.