/testfor at least 10 specific items in the Inventory

This is the command I entered in the command block:
testfor @p {Inventory:[{id:minecraft:diamond,Count:10}]}

But it doesn't seem to work. What am I doing wrong?


Solution 1:

So two things. First is that the data tag will never match, because the integer type for count needs to be specified. Using the following command will fix that problem:

testfor @p {Inventory:[{id:minecraft:diamond,Count:10b}]}

Notice the b after the 10. This says the integer type is a byte. You should assume that the type specifier is necessary when trying to match integer values in data tags, even though there are some instances where it's not.

Your second, and probably bigger problem is that you're only checking to see if there is one specific inventory slot that holds exactly 10 diamonds. Have ten slots with one diamond each? Fail. Have a whole stack of diamonds? Fail. This is a problem with /testfor, and in reality, all data tag matching, but since /testfor is used primarily for data tag matching, it ends up being a big problem. Don't use /testfor! Especially in pre-1.9 versions of the game. I cannot stress this enough. /testfor is almost completely useless in pre-1.9, and only gains a small amount of utility in 1.9 with the introduction of conditional execution.

Perhaps counter-intuitively, you want to be using the /clear command to do this. You'll also need to use the /stats command to save the result to the scoreboard. You can generally see how that's done in this answer I wrote not too long ago.