-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdoor_ctrl_toggle.lua
103 lines (94 loc) · 3.13 KB
/
door_ctrl_toggle.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
local cb = peripheral.find("chatBox")
local config = {}
if fs.exists("cfg_door_ctrl.txt") then
local cfg_file = io.open("cfg_door_ctrl.txt","r")
config = textutils.unserialise(cfg_file:read("*a"))
cfg_file:close()
else
print("Starting Config!")
print("Redstone Side?")
config.side_open = read()
print("Password Player?")
config.admin = read()
local cfg_file = io.open("cfg_door_ctrl.txt","w")
cfg_file:write(textutils.serialise(config))
cfg_file:close()
end
local message_queue = {}
local function sendMessage(text, player, whisper)
message_queue[#message_queue+1] = {
type="plain",
text=text,
player=player,
whisper=whisper
}
end
local function sendFormattedMessage(json_text, player, whisper)
message_queue[#message_queue+1] = {
type="formatted",
text=json_text,
player=player,
whisper=whisper
}
end
local messageThread = function()
while true do
local sleep_time = 0
for k,v in ipairs(message_queue) do
if v.type == "plain" then
if v.whisper then
cb.sendMessageToPlayer(v.text, v.player, "\xA7bDoor-CTRL\xA7r")
else
cb.sendMessage(v.text, "\xA7bDoor-CTRL\xA7r")
end
elseif v.type == "formatted" then
if v.whisper then
cb.sendFormattedMessageToPlayer(v.text, v.player, "\xA7bDoor-CTRL\xA7r")
else
cb.sendFormattedMessage(v.text, "\xA7bDoor-CTRL\xA7r")
end
end
table.remove(message_queue, k)
sleep_time = 1.1
break
end
sleep(sleep_time)
end
end
local function waitMsg(name)
while true do
local event, username, message = os.pullEvent("chat")
if name and name == username then
return message
elseif not name then
return message
end
end
end
local mainThread = function()
while true do
local event, username, message = os.pullEvent("chat")
if message:lower():sub(1,8) == "jjs door" then
local choice = message:lower():sub(10,message:len())
os.sleep(1)
if choice == "toggle" then
config.pass = tostring(math.random(1000,9999))
sendMessage("Password? \xA77(use $ prefix to hide it from other players)", username, true)
sendMessage("Password: \xA7b"..config.pass, config.admin, true)
local pass = waitMsg(username)
if pass == config.pass then
sendMessage("\xA7aPassword Accepted!", username, true)
os.sleep(1)
rs.setOutput(config.side_open, true)
os.sleep(0.5)
rs.setOutput(config.side_open, false)
else
sendMessage("\xA7cPassword Denied!", username, true)
end
else
sendMessage("\xA7cIncorrect Action.", username, true)
end
end
end
end
parallel.waitForAny(mainThread, messageThread)