I am stuck looking for a way to test for an item in a player's inventory so far nothing has worked any help would be nice
2 Answers
I did not see the pocket edition tag when I gave this answer. This answer will not work for pocket edition. These commands are for Java Edition 1.12.2
Here is a command to check all player's inventories for a diamond:
testfor @a {Inventory:[{id:"minecraft:diamond"}]}
You can change minecraft:diamond to other items/blocks.
If you need to test for a specific amount, you can add count to the command:
testfor @a {Inventory:[{id:"minecraft:diamond",Count:64b}]}
This tests for a full stack of 64.
Those commands will go positive if a player has the item(s) but is not very useful otherwise. You can also add a scoreboard tag to the player who has the item(s). This allows you to target the players that have the item in other commands:
scoreboard players tag @a add Holding {Inventory:[{id:"minecraft:diamond",Count:64b}]}
Then you can use that tag within the target selector of another command. As an example, lets make everyone who has the diamonds say hello:
/execute @a[tag=Holding] ~ ~ ~ say hello
-
Thank's for your reply. I have tried this formula. I should have mentioned I'm playing on Minecraft Windows 10. Any help would be grateful.– spacratCommented Nov 25, 2017 at 14:58
-
I did not see the pocket edition tag. I apologize. Unfortunately, my knowledge is mainly the Java edition of the game. Commented Nov 25, 2017 at 15:52