I'm making a map, and I want to have a command block or a command block set in the middle of the map that detects when a player picks up 5 diamonds and then replaces those diamonds with a bow and arrow. I've found the command

/execute if data entity @a {Inventory:[{id:"minecraft:diamond",Count:5b}]}

But the only way I've gotten that to work with anything is to do a /give @p command, but because I want this command to be working with multiple people on the map it won't work, How can I take this output and use it to both clear the diamonds and give 2 items?


You are very close. More magic from /execute can split each player's handling to themselves. Using scoreboard tags, we can then target the players individually.

  as @a                                                             # Tell all players to do the following:
  if data entity @s {Inventory:[{id:"minecraft:diamond",Count:5b}]} # If I have 5 diamonds, continue, otherwise stop.
run tag @s add diamondBow                                           # give myself a recognizable tag.

Then, just run the following in order:

clear @a[tag=diamondBow] minecraft:diamond 5 # Clear the diamonds
give @a[tag=diamondBow] minecraft:bow        # Give the bow
give @a[tag=diamondBow] minecraft:arrow      # Give the arrow
tag @a[tag=diamondBow] remove diamondBow     # Get rid of the tag

Note that in order to work, all 5 diamonds must be stacked in one slot, not spread out through the inventory. If you want to detect 5 diamonds spread out anywhere in the inventory, replace the /execute command with the following two:

/execute as @a store result score @s itemsCleared run clear @s minecraft:diamond 0
/tag @a[scores={itemsCleared=5}] add diamondBow
  • You shouldn't do NBT inventory testing if you're looking to get an accurate count for a particular item, it's incredibly unreliable. Instead you should execute a clear command (clearing 0 items) and save the result to a scoreboard. – MBraedley Nov 7 '20 at 23:53
  • @MBraedley You are technically correct, I just used this method because it was the method the OP was using. – ExpertCoder14 Nov 8 '20 at 0:15

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.