Minecraft – How to detect if a player has a specific item with a specific datavalue in their inventory

minecraft-commandsminecraft-java-edition

Let's say I want to track a group of players to see which one of them (if any) have a specific block type in their inventory that's of a specific datavalue; for instance Red Wool (minecraft:wool with a data value of 14). I already know how to detect if a player has any wool in their inventory:
/testfor @a {Inventory:[{id:"minecraft:wool"}]}.
However, this doesn't discriminate between the various colours. It'll find anyone with any colour wool, whereas I want to find only players with red wool.

I've tried modifying the data tag to also include a data value, such as
/testfor @a {Inventory:[{id:"minecraft:wool",data:14}]}
but this results in unmatched NBT tags and the error message <Player> did not match the required data structure. Obviously it's because data (or Data, damage, dataID, etc.) isn't the name of the NBT tag, or that the structure is wrong.

I realize I can accomplish this using the /clear command, but that's a bit clunky when running on a 20Hz /fill clock. How can I filter out specific data values when using the /testfor or /scoreboard players commands?

Best Answer

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.)