Advertisement
LegoStax

Key Card Protected Door V2

Feb 2nd, 2023
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. -- Tutorial here: https://www.youtube.com/watch?v=vw7o00xvnLQ
  2. -- For the Floppy Disk setup:
  3. -- Insert the floppy into the disk drive adjacent to the computer
  4. -- Go into the computer and type: edit disk/.security/key
  5. -- Type inside the edit program: lemmein
  6. -- do not include quotes
  7. -- Save and exit, then reboot your computer.
  8. -- It should open the door and eject the floppy disk.
  9. -- If it doesn't follow the tutorial again.
  10.  
  11.  
  12. -- feel free to change the following variables
  13.  
  14. -- password. THIS CANNOT CONTAIN SPACES
  15. local PASSWORD = "lemmein"
  16.  
  17. -- which side of the computer a redstone signal should be sent
  18. local RS_SIDE = "bottom"
  19.  
  20. -- default redstone power state
  21. local RS_POWER = true
  22.  
  23. -- (optional) which side of the computer the disk drive is on
  24. local DISK_SIDE = nil
  25.  
  26.  
  27.  
  28. -- auto-locate disk drive
  29. if not DISK_SIDE then
  30.     local peripheralSides = peripheral.getNames()
  31.     for k,v in pairs(peripheral.getNames()) do
  32.         if peripheral.getType(v) == "drive" then
  33.             DISK_SIDE = v
  34.             break
  35.         end
  36.     end
  37. end
  38.  
  39. if not DISK_SIDE then
  40.     print("Please attach a disk drive")
  41.     return
  42. end
  43.  
  44. -- initialize display
  45. rs.setOutput(RS_SIDE, true)
  46. os.pullEvent = os.pullEventRaw
  47. term.clear()
  48. term.setCursorPos(1,1)
  49. term.write("Door Lock Program")
  50. term.setTextColor(colors.lightGray)
  51. term.setCursorPos(1,2)
  52. term.write("Please insert floppy disk")
  53.  
  54. -- main event loop
  55. local timer = -1
  56. while true do
  57.     local e = {os.pullEvent()}
  58.     if e[1] == "disk" and e[2] == DISK_SIDE then
  59.         -- get the mount path in case of multiple disks already inserted
  60.         local mountPath = disk.getMountPath(DISK_SIDE)
  61.         if mountPath then
  62.             local keyPath = mountPath .. "/.security/key"
  63.             if fs.exists(keyPath) then
  64.                 local file = fs.open(keyPath, "r")
  65.                 -- remove whitespace characters from password read from floppy disk
  66.                 local inputPassword = file.readAll():gsub("%s+", "")
  67.                 file.close()
  68.                 disk.eject(DISK_SIDE)
  69.                 if inputPassword == PASSWORD then
  70.                     disk.eject(DISK_SIDE)
  71.                     rs.setOutput(RS_SIDE, not RS_POWER)
  72.                     timer = os.startTimer(3)
  73.                 end
  74.             else
  75.                 disk.eject(DISK_SIDE)
  76.             end
  77.         else
  78.             disk.eject(DISK_SIDE)
  79.         end
  80.     elseif e[1] == "timer" and e[2] == timer then
  81.         rs.setOutput(RS_SIDE, RS_POWER)
  82.     end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement