8

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?

9

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

2

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.

  • 1
    I specifically asked about how to do this with the /scoreboard and /testfor commands, and not the /clear command. Additionally, it doesn't let me track which player has the item, only that a player has the item. – MBraedley Dec 16 '14 at 21:00
  • @MBraedley Technically you could track each player separately, by using actual player names, but I do of course realize that hardcoding player names would be BS here. Unless you extract the players' names somehow and create command blocks with these names filled in, which would be really crazy, if possible at all. – Egor Hans Feb 24 '18 at 15:35
  • @EgorHans don't over think it. This question becomes fairly easy in 1.13 (as does a lot of other things). Using clear is totally viable in 1.13 (although probably not the best option), but the extra hoops needed to be jumped through at the time meant out wasn't a viable option when I posted the question. – MBraedley Feb 24 '18 at 15:44
  • @MBraedley I didn't even mean to make you think it over. That were just some thoughts :D – Egor Hans Feb 25 '18 at 17:13
2

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.

-1
/scoreboard players set @a <objective> <score> {Inventory:[{id:block ID,Damage:Data Value s}]}

protected by Community Jan 28 '16 at 17:11

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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