Counting items in a chest
I am working on a system that allows a player to build a template building such as a house. Building one of these structures requires the player to meet a certain quota of required items.
(for example 20 planks and 35 stone bricks)
I've been trying different strategies to accomplish this such as draining the chest using a hopper or lazy testforblocking, but I can't seem to find a quick and flexible system for this, either it takes 1 tick per item or the items would have to be arranged in a very specific order so that the testforblock can find it.
I am aware of the AffectedItems stat but I haven't found a way to use it to count items and if it's even possible. I'm not asking for a system that does this, I'm just looking for a concept that I can build on.
Best Answer
Update of the other answers to account for changes in command format for version 1.13:
/execute if block X Y Z minecraft:chest{Items:[{id:"minecraft:oak_planks",Count:20b},{id:"minecraft:stone",Count:35b}]} run <other command>
The caveat about which way the chest is facing no longer applies; this command works regardless of any other properties about the chest.
Pictures about "Counting items in a chest"



How Many Items fit in a Minecraft CHEST?
More answers regarding counting items in a chest
Answer 2
An alternative would be:
/testforblock x y z minecraft:chest m {Items:[{Slot:0b,id:minecraft:fishing_rod,Damage:0s,Count:1b}]}
I've tried the other one and it didn't work FOR ME, but you can try this out.
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Pavel Danilyuk, Pavel Danilyuk, Viktoria Slowikowska, Viktoria Slowikowska
