Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --M1cr0S Door Locker 1.6
- os.loadAPI("/m1")
- m1.set(true, 1, 1)
- local settings, menu, input, name, result = {}, 0, {}, "", {}
- local x, y = term.getSize()
- function firstrun()
- print "Getting server ID"
- settings[1] = m1.getperipheral("modem")
- rednet.open(settings[1])
- repeat
- rednet.broadcast("srvidreq")
- settings[2], request = rednet.receive(3)
- until type(settings[2]) == "number"
- rednet.close(settings[1])
- print "Valid User/Required Rank(and above)"
- settings[3] = read()
- if tonumber(settings[3]) ~= nil then settings[3] = tonumber(settings[3]) end
- print "Side to send redstone signal/ID of door control"
- settings[4] = read()
- if tonumber(settings[4]) ~= nil then
- settings[4] = tonumber(settings[4])
- else
- print "How long to send signal for"
- settings[5] = tonumber(read())
- print "Door closed redstone state (on/off)"
- if read() == "on" then settings[6] = true else settings[6] = false end
- end
- m1.tablesave("/settings", settings)
- print("Done")
- m1.wait(2)
- os.reboot()
- end
- function redraw()
- term.clear()
- local x, y = term.getSize()
- local origx = x
- while x > 0 do
- m1.printex(1, "-", x, "noclr")
- m1.printex(y, "-", x, "noclr")
- x = x-1
- end
- while y > 0 do
- m1.printex(y, "|", 1, "noclr")
- m1.printex(y, "|", origx, "noclr")
- y = y-1
- end
- m1.printex(1, "Password Protected Door", "center", "noclr")
- m1.printex(3, "Input Username:", 2, "noclr")
- end
- function pwdchk(event, line)
- if event == "disk" then input = m1.tableload("disk/login") else input = {[1] = "pwdchk", [2] = name, [3] = input} end
- if type(settings[3]) == "string" and input[2] ~= settings[3] then return({"disallowed"}) end
- m1.tablesend(input, settings[2])
- local id, request = m1.tablereceive(settings[2], 3)
- if request == nil then return({"No Connection"})
- elseif type(settings[3]) == "number" and request[1] == "true" and request[2] < settings[3] then return({"disallowed"})
- else return(request) end
- request, name = {}, ""
- end
- function getinput(key)
- local line = 4
- if menu == 1 then line = 6 end
- if key == 14 then table.remove(input)
- elseif key ~= 1 and key ~= 28 and key ~= nil and key ~= " " then table.insert(input, key)
- elseif key == 28 and menu == 0 then
- name = ""
- for i,data in ipairs(input) do name = name..data end
- input = {}
- return(name)
- end
- m1.printex(line, "|")
- m1.printex(line, "|", x, "noclr")
- for i,data in ipairs(input) do
- if menu == 1 then data = "*" end
- m1.printex(line, data, i+1, "noclr")
- end
- end
- function startup()
- if fs.exists("/settings") == false then firstrun() end
- settings = m1.tableload("/settings")
- settings[1] = m1.getperipheral("modem")
- redraw()
- rednet.open(settings[1])
- repeat
- if type(settings[4]) == "string" and settings[6] == true then redstone.setOutput(settings[4], settings[6]) elseif type(settings[4]) == "string" then redstone.setOutput(settings[4], false) end
- local event, p1, p2 = os.pullEvent()
- if event == "rednet_message" then
- local request = m1.deserialize(p2)
- if request ~= nil and request[2] ~= nil and request[3] ~= nil and ((type(settings[3]) == "string" and request[2] == settings[3]) or (type(settings[3]) == "number" and request[3] >= settings[3])) then
- if settings[6] == true then redstone.setOutput(settings[4], false) else redstone.setOutput(settings[4], true) end
- m1.printex(7, "Remotely Opened", 2, "noclr")
- m1.wait(settings[5])
- else
- m1.printex(7, "Access Denied", 2, "noclr")
- m1.wait(2)
- end
- menu, input, name, result, requset = 0, {}, "", {}, {}
- redraw()
- elseif (event == "disk") or (event == "key" and p1 == 28 and menu == 1) then --End events, which check password
- result = pwdchk(event, 7)
- if event == "disk" then disk.eject(m1.getperipheral("drive")) end
- if result[1] == "true" then
- m1.printex(7, "Password Correct", 2, "noclr")
- if type(settings[4]) == "number" then
- m1.tablesend({[1] = "remopn", [2] = settings[4], [3] = input[2]}, settings[2])
- elseif type(settings[4]) == "string" and settings[6] == true then
- redstone.setOutput(settings[4], false)
- elseif type(settings[4]) == "string" then
- redstone.setOutput(settings[4], true)
- end
- m1.wait(settings[5] or 3)
- elseif result[1] == "disallowed" then
- m1.printex(7, "Access Denied", 2, "noclr")
- m1.wait(2)
- else
- m1.printex(7, result[1], 2, "noclr")
- m1.wait(2)
- end
- menu, input, name, result = 0, {}, "", {}
- redraw()
- elseif event == "key" and p1 == 28 and menu == 0 then
- name = getinput(p1)
- menu = 1
- m1.printex(5, "Enter Password:", 2, "noclr")
- elseif event == "key" and p1 == 14 then getinput(p1)
- elseif event == "char" and menu == 0 then getinput(p1)
- elseif event == "key" and menu == 1 then getinput(p1) end
- until false
- rednet.close(settings[1])
- end
- startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement