How do I lock any doors in minecraft using command blocks?

You can do this by using invisible armor stands that blocks the door so they can't right click it. So here are the steps for it.

  1. Get the cordinates infront of the door.

  2. Then use this command and change the X Y Z /summon armor_stand X Y Z {Invisible:1b,Invulnerable:1b,PersistenceRequired:1b,NoBasePlate:1b,NoGravity:1b,DisabledSlots:31}.

  3. Now you need to use decimals to block the hole door. So now reapet the second step but now write .2 - .4 - .6 - .8 ifront of the X or Z cordinate (depents on were it's facing) so etc:

/summon armor_stand X.2 Y Z {Invisible:1b,Invulnerable:1b,PersistenceRequired:1b,NoBasePlate:1b,NoGravity:1b,DisabledSlots:31}

  1. If there is no roof over the door then you need to the exact same thing but now you need to add 1 to Y.

  2. If you need to unlock the door, then place this command:

    /testfor @a {SelectedItem:{id:"minecraft:ANYITEM",tag:{display:{Name:"KEY NAME"}}}}

In a reapiting always active command block and then place this into a condential chain, always active command block:

/kill @e[type=armor_stand,r=2]

IF YOU NEED DIFFRENT KEYS AND MORE KEYS, THEN NAME THE ARMOR STANDS BY ADDING THIS TO THE ARMOR STAND COMMAND CustomName:"Door1" And then use this command for the key:

/kill @e[r=2,type=armor_stand,name=Door1]

To get the key use this: /give @p minecraft:WHATEVER_ITEM 1 0 {display:{Name:Key Name}}

Example: We say that the cordinates infront of the door is 1460 4 -150. Then we do this.

First we summon the armor stands ifront of the door: /summon armor_stand 1460 4 -150 {Invisible:1b,Invulnerable:1b,PersistenceRequired:1b,NoBasePlate:1b,NoGravity:1b,DisabledSlots:31}

Now we do the this: /summon armor_stand 1460.2 4 -150 {Invisible:1b,Invulnerable:1b,PersistenceRequired:1b,NoBasePlate:1b,NoGravity:1b,DisabledSlots:31}

And reapet until 0.8. Then you add 1 On Y so it will be like this: /summon armor_stand 1460 5 -150 {Invisible:1b,Invulnerable:1b,PersistenceRequired:1b,NoBasePlate:1b,NoGravity:1b,DisabledSlots:31}

And reapet until .8 Now you have to give yourself the key and your done