http://www.minecraftwiki.net/wiki/Chest#Bugs
See the list of items here where you can still open the chest.
- Liquids: Water and Lava.
- Plants: Leaves, Cactus (With a water source block on top), and Farmland.
- Translucent Blocks: Glass, Slabs, Stairs, Monster Spawner, Snow, Ice, Fence, Cake, Beds, Glowstone, and Chests.
- Non-Blocks: Torch, Redstone Torch, Redstone Wire, Redstone Repeater, Pressure Plates, Lever, Ladders, Rails, and Sign.
I personally like Fences because you can make a nice checkerboard pattern of chests and fences
The other solution is to place your chests in the ceiling with another block empty above them; nothing can get on top to sit on them.
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}]}
Best Answer
Main answer for 1.12 players
You need to use
/clear
to remove the items from inventory, and detect whether it succeeds. When in Survival Mode,/clear
only affects the cursor item of players in Survival Mode, not Creative. This will meant that you will need to switch to Survival whenever you want to test it. A bit annoying, but it's what has to be done!To be able to replenish the item, you will need to check if the
/clear
command actually did anything. You can accomplish this through the 1.12 version of/execute store
, which is/stats
.Setup
The first thing you need to do is create a scoreboard objective for the value to be stored in. Here is a sample command:
This command will create a scoreboard objective to hold the values in.
Next: Use a fake player to keep track of the score:
Next: Give yourself the item and modify it with custom NBT tags or
CustomName
tag to make it identifiable.Next: Place a 5-block long repeating command chain. (Starting with 1 repeating command block with 4 chain command blocks after. If you don't know how to create a command chain, see this post.) Here are the commands that are supposed to go into each command block:
Now, the fake player's score in objective
tamperedChest
is going to be updated with the number of brown panes. We just need to detect for a1
in the score and we're home free!In the 4th command block, insert this command:
Test for a
1
inchest1
's score.The 5th command block needs to be set to conditional mode. The command though, is up to you on how you want to do it. Here you'll insert a command that will activate more commands to replenish the glass panes, or other placeholder items. Here are some ways I think of accomplishing this task:
/replaceitem
commands to put those glass panes back.Functions do require a little extra setup, but it's worth it, compared to having 54 command blocks (you need 1 command for each slot of the chest). See how to setup functions using the links below.
As for running the function, use the
/function
command to run it./replaceitem
command, and you need 1/replaceitem
command for each slot of the chest. But, if you do want to take that road, you most certainly can! Just have it set a redstone block next to a command chain that runs all the/replaceitem
commands!Learn more about these mechanics on the Minecraft Wiki:
/clear
command/stats
command