View difference between Paste ID: kcKTQsz0 and 3pbVFyu6
SHOW: | | - or go back to the newest paste.
1
os.unloadAPI("ocs/apis/sensor")
2
os.loadAPI("ocs/apis/sensor")
3
local prox = sensor.wrap("top")
4
local modem = peripheral.wrap("right")
5
local playerName = ""
6
local playerNames = {}
7
local secretPhrase = ""
8
local portNum = ""
9
local answer = ""
10
local inputting
11
local checking
12
local heldItemCheck
13
local item
14
local heldItem
15
local b = 0
16
--This function checks what item the player is holding
17
function itemCheck(name)
18
local targets = prox.getTargets()
19
--Read data from sensor
20
    for k, v in pairs(targets) do
21
        if ((v.Name) == "Player") then
22
            targetsDetail = prox.getTargetDetails(k)
23
            if not(targetsDetail == nil) then
24
                if name == targetsDetail.Username then
25
                    heldItem = targetsDetail.HeldItem.Name
26
                    return heldItem
27
                end
28
            end
29
        end
30
    end
31
end
32
--This functions checks our array "playerNames" which contains the players
33
--we allowed access to this door
34
--If the for loops finds a match, it will open the door
35
function arrayCheck(playerNames,playerName,secretPhrase,portNum,item,heldItem)
36
local port = tonumber(portNum)
37
    for key, value in pairs(playerNames) do
38
        if value == playerName and item == nil then
39
            modem.transmit(port,524,secretPhrase)
40
        elseif value == playerName and item == heldItem then
41
            modem.transmit(port,524,secretPhrase)
42
        end      
43
    end
44
    modem.transmit(port,524,"not_player")
45
end
46
--This checks the names of the players near the sensor
47
--and then sends this daya to the "arrayCheck" function
48
function nameCheck(secretPhrase,playerNames,portNum,item)
49
local targets = prox.getTargets()
50
--Read data from sensor
51
    for k, v in pairs(targets) do
52
        if ((v.Name) == "Player") then
53
            targetsDetail = prox.getTargetDetails(k)
54
            if not(targetsDetail == nil) then
55
                playerName = targetsDetail.Username
56
                heldItem = targetsDetail.HeldItem.Name
57
            end
58
        end
59
    end
60
    arrayCheck(playerNames,playerName,secretPhrase,portNum,item,heldItem)
61
    targets = nil
62
    targetsDetail = nil
63
    playerName = nil
64
end
65
--This functions checks if there is a file named "proxInfo"
66
--If there is, it will tell the program to just read from that
67
--if there is no file it will tell the program we need to make one
68
function fileCheck(name)
69
local f = fs.open(name, "r")
70
    if f~=nil then
71
        repeat
72
            local line = f.readLine()
73
            if tostring(line) == "END" then
74
                f.close()
75
                sleep(1)
76
                return true
77
            end
78
        until line == nil
79
        f.close()
80
        sleep(1)
81
        return false
82
    else
83
        return false
84
    end
85
end
86
--The main loop of the program on startup
87
while true do
88
    modem.closeAll()
89
    if fileCheck("proxInfo") then
90
        inputting = false
91
        answer = "y"
92
        checking = false
93
    else
94
        inputting = true
95
        checking = true
96
    end
97
    while inputting == true do
98
        term.clear()
99
        term.setCursorPos(1,1)
100
        write("Enter a secret phrase: ")
101
        secretPhrase = read()
102
-----------------------------------
103
        term.clear()
104
        term.setCursorPos(1,1)
105
        write("Enter port number \n(can't be greater than 65535): ")
106
        portNum = read()
107
        while (tonumber(portNum) > 65535) do
108
            term.clear()
109
            term.setCursorPos(1,1)
110
            write("Please enter a vaild port number \n(can't be greater than 65535): ")
111
            portNum = read()
112
        end
113
-----------------------------------
114
        term.clear()
115
        term.setCursorPos(1,1)
116
        write("Do you want to have the\ndoor open when holding a\nspecific item like a key (y/n)?: ")
117
        heldItemCheck = read()
118
        if heldItemCheck == "y" then
119
            term.clear()
120
            term.setCursorPos(1,1)
121
            write("Please enter your name so the\nsensor knows who to look for: ")
122
            local myName = read()
123
            term.clear()
124
            term.setCursorPos(1,1)
125
            write("Please hold the item you wish\nto be a 'key' for the door\nand then push enter...")
126
            sleep(1)
127
            local dummy = read()
128
            item = itemCheck(myName)
129
        end
130
-----------------------------------
131
        term.clear()
132
        term.setCursorPos(1,1)
133
        print("Enter the names seperated by a space\nof the players you want to open this door")
134
        write("Player Names: ")
135
        local userInput = read()
136
        for i in string.gmatch(userInput, "%S+") do
137
            table.insert(playerNames, i)
138
        end
139
        sleep(1)
140
        term.clear()
141
        term.setCursorPos(1,1)
142
        print("Your secret phrase is: "..secretPhrase)
143
        print("Your port number is: "..portNum)
144
        if heldItemCheck == "y" then
145
            print("Your key is: "..item)
146
        end
147
        print("The player(s) allowed is/are:")
148
        for d=1,# playerNames do
149
            print(playerNames[d])
150
        end   
151
        sleep(1)
152
        print("Is this the correct information (y/n)? ")
153
        answer = read()
154
        if answer == "y" then
155
            h = fs.open("proxInfo", "w")
156
            h.writeLine(secretPhrase)
157
            h.writeLine(portNum)
158
            if heldItemCheck == "y" then
159
                h.writeLine("ITEM")
160
                h.writeLine(item)
161
            else
162
                h.writeLine("")
163
            end
164
            for a=1,# playerNames do
165
                h.writeLine(playerNames[a])
166
            end
167
            h.writeLine("END")
168
            h.close()
169
            checking = false
170
            inputting = false
171
        else
172
            for d=1,# playerNames do
173
                table.remove(playerNames,d)
174
            end  
175
            inputting = true
176
            checking = true
177
        end
178
    end
179
-----------------------------------
180
    --We keep looping through this once we have a file
181
    while answer == "y" and checking == false do
182
        print("Starting the program...")
183
        h = fs.open("proxInfo", "r")
184
        secretPhrase = h.readLine()
185
        portNum = h.readLine()
186
        if h.readLine() == "ITEM" then
187
            item = h.readLine()
188
        else
189
            item = nil
190
        end
191
        local x = 0
192
        repeat
193
            x = x + 1
194
            ln = h.readLine()
195
            playerNames[x] = ln
196
        until ln == "END"
197
-----------------------------------
198
        --print(secretPhrase)
199
        --print(portNum)
200
        --print(item)
201
        --for i=1,# playerNames do
202
        --    print(playerNames[i])
203
        --end
204
-----------------------------------
205
        h.close()
206
        sleep(3)
207
        term.clear()
208
        term.setCursorPos(1,1)
209
        print("If you want to change any settings place")
210
        print("a lever in front of this computer and pull it.")
211
        print("You will be prompted if you wish to edit")
212
        print("the settings. Don't forget to remove")
213
        print("the lever or toggle it again when you are done!")
214
        while true do
215
            nameCheck(secretPhrase,playerNames,portNum,item)
216
            if rs.getInput("front") == true then
217
                term.clear()
218
                term.setCursorPos(1,1)
219
                print("Would you like to edit the settings (y/n)? ")
220
                sleep(1)
221
                local editCheck = read()
222
                if editCheck == "y" then
223
                    sleep(1)
224
                    fs.delete("proxInfo")
225
                    os.reboot()
226
                else
227
                    sleep(1)
228
                    term.clear()
229
                    term.setCursorPos(1,1)
230
                    print("If you want to change any settings place")
231
                    print("a lever in front of this computer and pull it.")
232
                    print("You will be prompted if you wish to edit")
233
                    print("the settings. Don't forget to remove")
234
                    print("the lever or toggle it again when you are done!")
235
                end
236
            end
237
            sleep(2)
238
        end
239
    end
240
end
241
--Code created by ThatGamingGnome
242
--Check out my youtube channel @ http://www.youtube.com/user/ThatGamingGnome