Minecraft – TestForBlock command

minecraft-commandsminecraft-java-edition

I am trying to make a custom crafting table using a dropper. I use this command to detect a shovel that is placed in the dropper:

testforblock 193 58 -314 minecraft:dropper 0 {Items:[0:{id:minecraft:stone_shovel,Count:1b,Damage:0s,Slot:4b}]}

And it works, it detects the shovel in the middle as it is supposed to.
So the shovel is in the middle right now, and the comparator activates. If I keep the shovel there, and put another item into a different slot, the redstone comparator still activates. I want it to only activate when there is 1 item in the table.

My idea is to detect air for the other 8 slots, but it is not working, is minecraft:air invalid?
How do I make /testforblock detect air (blank slot) inside the container (dropper)?

Best Answer

"air" is indeed not a valid inventory item. Slots without items will simply not exist whatsoever, so you cannot directly detect them through NBT.

What you'll need to do instead is use multiple /testforblock commands: one to detect the shovel and others to detect slots having any items in them. You'd only allow a signal to pass if the first succeeds while the others fail:

Must succeed:

/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{id:"minecraft:stone_shovel",Count:1b,Damage:0s,Slot:4b}]}

Must all fail:

/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:0b}]}
/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:1b}]}
/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:2b}]}
/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:3b}]}
/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:5b}]}
/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:6b}]}
/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:7b}]}
/testforblock 193 58 -314 minecraft:dropper -1 {Items:[{Slot:8b}]}

What you cannot do is combine all of the failure commands into one, because then it's looking for an item in every one of those slots rather than any one.