Minecraft Java Edition – How to Test If a Chest Contains a Specific Item

minecraft-commandsminecraft-java-edition

I've seen a lot of questions for finding specific items in chests using commands using testforblock but how would you test if any block/item has been placed in a chest?

Thanks

Best Answer

You can use

/testforblock x y z minecraft:chest -1 {Items:[{}]}

Note the extra {} in the Items list. {Items:[{}]} checks if the list of item tags contains an item that matches all the tags present in {}. Obviously, {} does not contain any tags, meaning every item in the game matches this1.

You can achieve the opposite behavior using {Items:[]}, which will test for an empty list of items.

1 Technically, everything matches {}, including Creepers, but the Items tag limits the scope for us.