Is there a way to make it so the /clear command takes a certain amount of items with a strict amount?
Matthew Barrera
So I have a map with emeralds, and I want it to take a certain amount. HOWEVER, I want it to take 3 emeralds and only 3 emeralds. Is there a way I can do that? I know I can do /clear @p emerald 0 3, but there's no minimum amount. Is there a command to do that? No more, no less.
1 Answer
You must use CommandStats in order to count the number of items the player has, stored as a score.
Prerequisites
Objective to hold the score for each player.
/scoreboard objectives add ItemCount dummyThe trigger to apply to players. Note that if new players can join in at any time, this may need to run on a clock. This trigger activates whenever the player runs a command that targets items in inventories, and will set their own "ItemCount" score equal to the return value of the AffectedItems trigger.
/stats entity @a set AffectedItems @a[c=1] ItemCountThe target of the trigger must be tracked in the objective prior in order to have their score changed. This may also need to run on a clock.
/scoreboard players add @a ItemCount 0Detection
The following must be run in numerical order, on a clock if necessary.
Cause players to run a
/clearcommand that removes 0 items. While their inventory is untouched, it sends theAffectedItemstrigger the total number of items that matched in their inventory./execute @a ~ ~ ~ /clear @a[c=1] minecraft:emerald 0 0Now players will have an "ItemCount" score equal to the number of emeralds in their inventory. You can target them based on that score to remove 3 emeralds only if the player had at least 3 emeralds:
/clear @a[score_ItemCount_min=3] minecraft:emerald 0 3
Why not NBT?
While you could mark players based on their inventory data with NBT:
/scoreboard players tag @a add hasEmeralds {Inventory:[{id:"minecraft:emerald",Count:3b}]}This requires that the player has a single inventory slot dedicated to exactly 3 emeralds. If the player had a size-4 stack of emeralds in an inventory slot, it fails. If the player had 3 emeralds in their inventory, but they were spread out in size-1 stacks, it also fails.