Minecraft Java Edition, Commands – How to Count Total Items on the Ground

minecraft-commandsminecraft-java-edition

Items on the ground clump together in stacks, so getting the number of entities doesn't give me the number of items there are. Is there a way to count the number of items instead of the number of item entities?

Best Answer

First you need a scoreboard to store the amount of items per entity into:

/scoreboard objectives add count dummy

Then you can easily transfer the stack size of every item entity into their "count" scoreboard like this:

/execute as @e[type=item] store result score @s count run data get entity @s Item.Count

If you want to do this only for a certain type of item, you can filter that as well:

/execute as @e[type=item,nbt={Item:{id:"minecraft:stone"}}] store result score @s count run data get entity @s Item.Count

Then you just need to add all those scores up to a shared score, for example yours:

/scoreboard players operation @s count += @e[type=item] count

Of course if you want to do this multiple times, you have to reset your score before the next repetition:

/scoreboard players reset @s count