How to Make an Automatic Door that Opens and Closes with an Item in Minecraft
Minecraft Java Edition 1.21+ and 26+ – Item Key Door using Command Blocks
Want to create an automatic key door in Minecraft?
In this tutorial you will learn how to make a door that opens and closes automatically when a player is holding a specific item (like a key), using Command Blocks.
This system works in Minecraft Java Edition 1.21+ and 26+ and includes area detection, door opening/closing commands, and sound effects for a more realistic map.
Let's start with the following command. It must be in a Repeat command block set to Always Active
execute as @a[x=62,y=22,z=-991,dx=-4,dz=2] if items entity @s weapon.mainhand minecraft:gold_ingot run setblock 60 22 -991 minecraft:iron_door[open=true] replace
Any player will be detected at this coordinate, just change it to one of your choice
The dx and dz values are used to increase the detection range on those axes
For example: x=1 and dx=2 will detect from X coordinate 1 to 3
Then we detect the item the player is holding in their main hand
In this case, we chose the gold_ingot
If the player is holding this item at that coordinate, the setblock command will run, making the door open at the chosen location
There is no sound when this command runs successfully, so to fix that...
Place a Chain command block facing the same direction as the Repeat command block and enable Conditional and Always Active
With both command blocks placed and configured, enter this command in the Chain command block
playsound minecraft:block.iron_door.open ambient @a 66 23 -991 1 1 1
We can also do the opposite to close the door, just change if to unless and put the coordinates inside @s. Use the command below in another Repeat command block
execute as @a unless entity @s[x=62,y=22,z=-991,dx=-4,dz=2] if items entity @s weapon.mainhand minecraft:gold_ingot run setblock 60 22 -991 minecraft:iron_door[open=false] replace
Sound when the door closes:
playsound minecraft:block.iron_door.close ambient @a 66 23 -991 1 1 1