So I'm making a dungeon based off Legend of Zelda, and I want to make a door that opens when you have a key in your inventory. Say the key is called "Small Key". How can I get it to open when you're 5 blocks away from the door?


First of all, summon an invisible ArmorStand at your door, replacing X, Y and Z with the coordinates of the bottom part of your door:

/summon ArmorStand X Y Z {CustomName:"keyDetector",Invisible:1,Invulnerable:1}

Next, run the following commands in Always Active mode in command blocks, running the first command in a repeating command block (repeating_command_block) and the others in chain command blocks (chain_command_block), following the repeating command block:

/scoreboard players tag @a add hasKey {Inventory:[{tag:{display:{Name:"Small Key"}}}]}

/execute @a[tag=hasKey] ~ ~ ~ /execute @e[name=keyDetector,type=ArmorStand] ~ ~ ~ /setblock ~ ~ ~ wooden_door 4

/execute @a[tag=hasKey] ~ ~ ~ /execute @e[name=keyDetector,type=ArmorStand] ~ ~ ~ /setblock ~ ~1 ~ wooden_door 9

So in the end it should look something like this:

The command blocks, containing the command blocks in order from left to right:

Three Command Blocks in Order: Repeating, Chain, Chain

The door with the ArmorStand (for display purposes you can see the ArmorStand and it's name on this image, but you shouldn't be able to see it on your map):

keyDetector Armor Stand

| improve this answer | |

Instead of using command blocks to detect the key and open the door, you can just use a hopper sorter to get the key from the person (which also doubles as removing the key from their inventory since they don't need it anymore) and then have it trigger command blocks which open the door.

| improve this answer | |
  • Why would you want to do that if it is possible with command blocks? – Jacques Marais Jul 8 '16 at 7:31

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