3

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?

3

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.

  • I'm playing in a 1.8 world (1.8.8) and maybe I can elude the problem putting next to the shop a safe-deposit box and a sign that says "please, deposit excess diamonds here". How 1.9 can help me in this case? – hiru Sep 19 '15 at 13:49
  • There's no difference in the set of commands you need for this problem between 1.8 and 1.9, because you don't want to be using /testfor to solve this problem. The only real difference, at least for this problem, is that you'd use, for instance, a fill clock in 1.8, but in 1.9, you'd use a repeating command block followed by a chain of command blocks. If you want, I can give a more complete solution that will work in 1.8 or 1.9. – MBraedley Sep 19 '15 at 13:54
  • /clear command removes diamonds in the inventory even if they aren't precisely 10 (9 or less). example: /clear @p[x=?,y=?,z=?,r=0,lm=10] diamond 0 10. How can I solve this problem? – hiru Sep 19 '15 at 14:00
  • Did you not look at the other answer I linked to? You don't actually have to remove the diamonds from a players inventory in order to get a count, which is what you really want to do. Get a count of the diamonds in a player's inventory (do this for all players), save it to a scoreboard objective, and then decide, based on the scoreboard objective, if they have enough diamonds to do something, and clear that many diamonds if necessary. – MBraedley Sep 19 '15 at 14:03
  • Yep sorry, missed the link at the end of your first post. I've never used scoreboards in minecraft so I basically don't know what do first – hiru Sep 19 '15 at 14:18
0

I have solved the problem of the not stacked testfor amount thing, (only 1.12 i guess)

as mebreadly said you can to use the clear command


to do it:

make a tower of commandblocks

the height is the amount of items you need to "testfor"

for example: i need to testfor 3 diamonds so i need 4 commandblocks

so 1 more than the amount


the first commandblock is a repeating needsredstone commandblock pointing to the air

the second (and more) are chain alwaysactive commandblocks also pointing to the air

the highest commandblock is the output


for example how it should be done:

i testfor 3 diamonds and if it tested it say hi

first cmb: /clear @p minecraft:diamond 0 1

second cmb: /clear @p minecraft:diamond 0 1

third cmb: /clear @p minecraft:diamond 0 1

fourth cmb: /say hi


some problems could be:

if you have more it also gives the output

the items are gonna be cleared so you dont have them anymore


i hope you can do something with this

have more questions? leave a comment

btw srry for spelling mistakes

(i also needed this for a captain toad minecraft map)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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