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.

  • Any useful strategies for finding a Nether Fortress?
  • Can you connect to a LAN server without being on the same internet connection
  • How do you safely remove TNT?
  • No land to start on Agrarian Skies?
  • Find a Minecraft SMP server seed based on known locations
  • How can I keep monsters out of my nether regions?
  • 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.

  • Can't find my maps in Minecraft
  • How do I make a 1x1x2 piston door that opens when I step on a plate and not the other way around?
  • Is it possible to script Minecraft?
  • Can I use dispensers or droppers to auto-replant crops?
  • After migrating account with Mojang, where are my new/current saves?
  • How do I make Endermen spawn more frequently?
  • 2 Solutions collect form web for “Counting items in a chest”

    A /testforblock will actually work perfectly here. /testforblock allows for partial NBT matches. This means that you can simply not specify a slot, and it will work. There are a couple of problems (if someone puts too many materials in a stack it won’t detect properly, for example), but overall, it’s not that difficult to do.

    Let’s go with the example you pose, 20 planks and 35 stone bricks. We can check to see if there is a chest with 20 planks and 35 stone bricks inside as follows:

    /testforblock X Y Z minecraft:chest M {Items:[{id:minecraft:planks,Count:20b,Damage:0s},{id:minecraft:stonebrick,Count:35b,Damage:0s}]}
    

    Where X, Y, and Z are the coordinates (or relative coordinates) of the chest you want to check. The value of M depends on the direction your chest is facing. You can test this by trying 0 first, and then looking in the command block error output, below the command input. If you got it wrong, it will say something to this effect:

    [18:26:20] The block at -1320,57,-662 had the data value of 3 (expected: 0).
    

    and you can adjust accordingly. The value of Damage in each of the item compound tags will vary depending on the particular variety of block you want to check for, or, it can be omitted if it does not matter.

    For some reason, you do need to include the letters after the numbers in the NBT tag, which indicate what type of number it is (Count is a byte, Damage is a short). I’m not sure why this is exactly, to be perfectly honest.

    If you want to check for more than 64 of an item, you can do that, you’ll just need to check for a stack of 64 and the remainder as separate item entries. In other words, checking for exactly 74 cobblestone would be done as follows:

    /testforblock X Y Z minecraft:chest M {Items:[{id:minecraft:cobblestone,Count:64b},{id:minecraft:cobblestone,Count:10b}]}
    

    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.

    We love Playing Games, especially Video Games.