In Minecraft, how can I detect five or more item in players inventory?
I alraedy know: @p[nbt={Inventory:[{id:"minecraft:cod",Count:5b}]}]
But it only works with five item and no more or less.
-
@MBraedley Please refrain from answeing in comments if you can't provide a full answer. It might discourage other users from answering, and it feels almost like "reserving" an answer for you.– WriggleniteCommented Sep 10, 2019 at 14:45
1 Answer
You can tally how many of an item a player has in their inventory, then use /scoreboard to detect if that tally is more or less than 5.
First, create a dummy objective. This will keep track of how many of a certain item you have in your inventory (in this example, cods)
/scoreboard objectives add cod_count dummy
Next, run this command to match your cod_count score with your item count.
/execute as @p store result score @s cod_count run clear @s cod 0
You can now detect if you have 5 or more cods with this command:
/execute as @p[scores={cod_count=5..}] run tellraw @s "You have more than 5 items"
Detecting a range, like in your title, is just as easy:
/execute as @p[scores={cod_count=15..20}] run tellraw @s "You have between 15 and 20 items"
-
If you want to do this for all players on a multiplayer server, it's a simple matter of switching the
@p
's with@a
. Commented Sep 10, 2019 at 17:38 -
…and if you want it for a single stack, you need to type out the command for 15, 16, 17, 18, 19 and 20. Commented Sep 10, 2019 at 18:46