Let's look at your data tag here carefully.
{
Item:
[
id:"minecraft:written_book",
Slot:0b,
Damage:0s,
Count:1b
}
]
}
In this expanded form, you can immediately see that something is odd about your parentheses. There is a closing curly bracket without a partner. You'll have to add {
before id
(which is the first tag inside the item tag). Generally, when something is wrong, expanding the command over multiple lines and indenting properly at every layer is a good idea to catch these type of errors.
Secondly, the tag containing the item tags for chests is called Items
, plural (cf. the wiki).
In total, the data tag needs to be
{
Items:
[
{
id:"minecraft:written_book",
Slot:0b,
Damage:0s,
Count:1b
}
]
}
Or compacted for easy copy and paste:
{Items:[{id:"minecraft:written_book",Slot:0b,Damage:0s,Count:1b}]}
You can use /testforblock
:
/testforblock X Y Z chest -1 <blockdata>
Is something you'll probably understand, depending on your command block experience. If you don't:
X Y Z should be replaced with the coordinates of the chest. "chest" means the command block only returns a success when it's a chest. -1 means it can be any data value, in a chest's case this means any rotation.
Now for the harder part, you should add the blockdata testing:
/testforblock X Y Z chest -1 {Items:[{Slot:*ID*,id:*ID*,Count:1}]}
The slot IDs are numbered from left to right, top to bottom, starting with 0. This means the top left is 0, top right is 8, bottom right is 26.
Item IDs can be found by pressing F3 and H at the same time (which reveals advanced tooltips) and hovering over the item in your inventory. An emerald, for example, would be "minecraft:emerald".
The count part can be anything you like, although I think you'll want it to stay at 1.
And then there's one last thing to do: add a comparator running out of the command block, into a command block that replaces the chest with an empty one, and one that gives the nearest player the frames.
To make filling in the blockdata easier, I recommend this. Just select the /blockdata
command, fill in all the slots, then copy the part between curly brackets into your /testforblock
command.
Best Answer
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:
Where
X
,Y
, andZ
are the coordinates (or relative coordinates) of the chest you want to check. The value ofM
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: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: