Automatic doors
A shop door that opens by itself: step on the pressure plate in front of it, and it swings open; two seconds after you have passed, it closes again. And before you go to bed, one button tells you which doors, gates and trapdoors of your base are still open.
Functions: GET_PRESSURE_PLATE, GET_OPEN, SET_OPEN, and SLEEP of c0de/time
What you need
- for the door: a timer block, a wooden door and two pressure plates
- for the report: a trigger block with a button on it, and a few more doors, fence gates or trapdoors
- an empty sheet for each program
- for the exercises: a light weighted pressure plate (the gold one)
- no rights for wooden doors, gates and trapdoors. Iron doors and iron trapdoors need
allowBlockChangeson a server, or an operator as owner, like in the guarded door.
The idea
A pressure plate right next to a door opens it by redstone – but then the door shuts the moment you step off, often in front of your nose. A program can do better: it reads the plates, opens the door, and closes it only when nobody has stepped on a plate for two seconds.
GET_PRESSURE_PLATE reads a plate's signal strength from 0 (nobody) to 15. GET_OPEN tells if a door is open, and SET_OPEN opens or closes it. All three also work for trapdoors and fence gates.
The door
PROGRAM autodoor
NOOP
INCLUDE c0de/minecraft
INCLUDE c0de/time
NOOP
FUNCTION near
PARAMETER dx INTEGER
PARAMETER dy INTEGER
PARAMETER dz INTEGER
RESULT spot STRUCT c0de/minecraft/POSITION
BEGIN
ASSIGN spot.x PID.x + dx ;
ASSIGN spot.y PID.y + dy ;
ASSIGN spot.z PID.z + dz ;
END FUNCTION
NOOP
FUNCTION someone
RESULT result BOOLEAN
BEGIN
VARIABLE plate STRUCT c0de/minecraft/POSITION
VARIABLE outside INTEGER
VARIABLE inside INTEGER
CALL near
PARAMETER dx 0 ;
PARAMETER dy 0 ;
PARAMETER dz -2 ;
RESULT spot plate
CALL c0de/minecraft/GET_PRESSURE_PLATE
PARAMETER position plate ;
RESULT result outside
CALL near
PARAMETER dx 0 ;
PARAMETER dy 0 ;
PARAMETER dz -6 ;
RESULT spot plate
CALL c0de/minecraft/GET_PRESSURE_PLATE
PARAMETER position plate ;
RESULT result inside
ASSIGN result outside > 0 OR inside > 0 ;
END FUNCTION
NOOP
FUNCTION watch
BEGIN
VARIABLE door STRUCT c0de/minecraft/POSITION
VARIABLE isOpen BOOLEAN
VARIABLE waiting BOOLEAN
CALL near
PARAMETER dx 0 ;
PARAMETER dy 0 ;
PARAMETER dz -4 ;
RESULT spot door
CALL c0de/minecraft/GET_OPEN
PARAMETER position door ;
RESULT result isOpen
CALL someone
RESULT result waiting
IF waiting AND NOT isOpen ;
BEGIN
CALL c0de/minecraft/SET_OPEN
PARAMETER position door ;
PARAMETER open TRUE ;
END IF
IF isOpen AND NOT waiting ;
BEGIN
CALL c0de/time/SLEEP
PARAMETER ticks 40 ;
CALL someone
RESULT result waiting
IF NOT waiting ;
BEGIN
CALL c0de/minecraft/SET_OPEN
PARAMETER position door ;
PARAMETER open FALSE ;
END IF
END IF
END FUNCTIONBuilding and running it
Stand south of the place for the timer block and look north.
| What | Where, seen from the timer block |
|---|---|
| the wooden door | in a wall 4 blocks north (lower half at the timer's height) |
| the outside plate | 2 blocks north, in front of the door |
| the inside plate | 6 blocks north, behind the door |
Between each plate and the door, one block stays free: a plate that touches the door would open it by redstone itself. Set the timer block to autodoor and watch, with an interval of 10 ticks. Walk over the plate: the door opens before you reach it and closes behind you.

How it works
someonereads both plates and givesTRUEif one of them has a signal. Because the program needs this twice, it is a function of its own.- Opening. If somebody is on a plate and the door is closed, SET_OPEN opens it. GET_OPEN avoids opening a door that is already open – and it tells the program where it is: door open, nobody on a plate means it's time to close.
- The delay. Then the run sleeps 40 ticks, two seconds, and looks at the plates again. Only if they are still empty, the door closes. If somebody stepped on a plate meanwhile, the door stays open, and the next run starts over.
- One run at a time. While a run sleeps, the timer block starts no new run. So there is never a second run that closes the door while the first is waiting.
The report
The second program looks at a list of doors and writes the open ones into the chat. Three arrays describe the doors: a name, and the distance east (dx), up (dy) and south (dz) from the trigger block. Element i of each array belongs to the same door.
PROGRAM doorreport
NOOP
INCLUDE c0de/minecraft
NOOP
VARIABLE names ARRAY STRING
BEGIN
"front door" "garden gate" "cellar trapdoor"
END VARIABLE
VARIABLE dx ARRAY INTEGER
BEGIN
0 8 3
END VARIABLE
VARIABLE dy ARRAY INTEGER
BEGIN
0 0 -1
END VARIABLE
VARIABLE dz ARRAY INTEGER
BEGIN
-4 -4 -8
END VARIABLE
NOOP
FUNCTION report
BEGIN
VARIABLE spot STRUCT c0de/minecraft/POSITION
VARIABLE count INTEGER
VARIABLE isOpen BOOLEAN
VARIABLE openDoors STRING
CALL LEN
PARAMETER value names ;
RESULT result count
FOR i INTEGER 0 ; count - 1 ;
BEGIN
ASSIGN spot.x PID.x + dx[i] ;
ASSIGN spot.y PID.y + dy[i] ;
ASSIGN spot.z PID.z + dz[i] ;
CALL c0de/minecraft/GET_OPEN
PARAMETER position spot ;
RESULT result isOpen
IF isOpen ;
BEGIN
IF openDoors ≠ "" ;
BEGIN
ASSIGN openDoors openDoors ⊕ ", " ;
END IF
ASSIGN openDoors openDoors ⊕ names[i] ;
END IF
END FOR
IF openDoors = "" ;
BEGIN
CALL c0de/minecraft/SAY
PARAMETER message "All doors are closed." ;
END
ELSE
BEGIN
CALL c0de/minecraft/SAY
PARAMETER message "Open: " ⊕ openDoors ;
END IF
END FUNCTIONThe numbers in the arrays are an example; measure your own. Press F3 and look at a door: "Targeted Block" shows its x, y and z. Subtract the x, y and z of the trigger block, and you have dx, dy and dz. For a door, take the lower half. Put the trigger block with its button by your bed, set it to doorreport and report, and press the button. The chat says Open: front door, cellar trapdoor or All doors are closed.
- The position of each door is computed right in the loop:
spot.xbecomesPID.x + dx[i], and so on. - Joining names.
openDoorsstarts empty. Before each further name, the loop adds", ", so the list has commas only between the names.
Try it yourself
- Don't just report the open doors – close them, too, and say which ones you closed.
Solution
PROGRAM doorcloser
NOOP
INCLUDE c0de/minecraft
NOOP
VARIABLE names ARRAY STRING
BEGIN
"front door" "garden gate" "cellar trapdoor"
END VARIABLE
VARIABLE dx ARRAY INTEGER
BEGIN
0 8 3
END VARIABLE
VARIABLE dy ARRAY INTEGER
BEGIN
0 0 -1
END VARIABLE
VARIABLE dz ARRAY INTEGER
BEGIN
-4 -4 -8
END VARIABLE
NOOP
FUNCTION closeAll
BEGIN
VARIABLE spot STRUCT c0de/minecraft/POSITION
VARIABLE count INTEGER
VARIABLE isOpen BOOLEAN
VARIABLE closed STRING
CALL LEN
PARAMETER value names ;
RESULT result count
FOR i INTEGER 0 ; count - 1 ;
BEGIN
ASSIGN spot.x PID.x + dx[i] ;
ASSIGN spot.y PID.y + dy[i] ;
ASSIGN spot.z PID.z + dz[i] ;
CALL c0de/minecraft/GET_OPEN
PARAMETER position spot ;
RESULT result isOpen
IF isOpen ;
BEGIN
CALL c0de/minecraft/SET_OPEN
PARAMETER position spot ;
PARAMETER open FALSE ;
IF closed ≠ "" ;
BEGIN
ASSIGN closed closed ⊕ ", " ;
END IF
ASSIGN closed closed ⊕ names[i] ;
END IF
END FOR
IF closed = "" ;
BEGIN
CALL c0de/minecraft/SAY
PARAMETER message "All doors were closed." ;
END
ELSE
BEGIN
CALL c0de/minecraft/SAY
PARAMETER message "Closed: " ⊕ closed ;
END IF
END FUNCTIONSET_OPEN with open FALSE closes each door that GET_OPEN finds open. Closing a closed door would do no harm, but this way the program knows what it did.
- Use a light weighted pressure plate (the gold one) outside: its signal grows with the number of entities on it. Let the door open only when at least two stand on it, for example you and your horse.
Solution
someone gets ASSIGN result outside ≥ 2 OR inside > 0 ;. A wooden or stone plate always gives 15 or 0, a weighted plate gives more the more there is on it.
Read more
- The functions of this recipe: GET_PRESSURE_PLATE, GET_OPEN, SET_OPEN, SLEEP and LEN
- Project: the guarded door, the iron door with a list of friends
- Changing the world and Owner and permissions in the guide
- The Timer block in the handbook Triggers