4

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.

5

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}]}
  • I was a bit hesitant to use this method as it's not very flexible, in that it cannot detect overflow of items nor detect item spread over different slots. But I don't see an alternative so this will have to do. Also, instead of going through the complication of finding the damage value of the chest, one could easly use -1 as the damage and it will detect any value – lennartVH01 Mar 27 '15 at 22:32
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.