TransWikia.com

How do you take away a certain amount items from a player and then give back an item?

Arqade Asked on January 26, 2021

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?

One Answer

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.

execute
  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

Correct answer by ExpertCoder14 on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP