· 6 years ago · Mar 06, 2019, 09:18 PM
1--Create a table of chests
2chests = {}
3
4--Create a table of commands to their functions
5local commands = {
6 --The get items function
7 ["get_items"] = function(payload)
8 --Create a table of items
9 local items = {}
10 --Iterate over the chests
11 for _, chest in ipairs(chests) do
12 --Iterate over the items
13 --in the chest
14 for itemID, amount in
15 ipairs(chest.items) do
16 --If the entry doesn't exist
17 --in the items table
18 if not items[itemID] then
19 --Then add it with a 0
20 --amount
21 items[itemID] = 0
22 end
23 --Add the item to the table
24 items[itemID] =
25 items[itemID] + amount
26 end
27 end
28
29 --Return success and send the
30 --item table
31 return true, items
32 end,
33
34 ["get_item"] = function(payload)
35 --Create a table of chests with
36 --the item in them
37 local chestsWithItem = {}
38 --Iterate over the chests
39 for chestName, chest in
40 ipairs(chests) do
41 --Try to get the amount of the
42 --type of item in the chest
43 local amount = chest.items[payload]
44 --If the item is in the chest
45 if amount then
46 --Add the item amount to the
47 --table for the chest
48 chestsWithItem[chestName] =
49 amount
50 end
51 end
52
53 --If the table is empty
54 if next(chestsWithItem) == nil then
55 --Return an error
56 return false, [[Item ']]..payload.. [[
57 ' doesn't exist in the system]]
58 else
59 --Return success and send the
60 --chests with item table
61 return true, chestsWithItem
62 end
63 end,
64
65 ["get_chest"] = function(payload)
66 --Try to get the chest
67 local chest = chests[payload]
68 --If it exists
69 if chest then
70 return true, {
71 ["name"] = payload,
72 ["location"] = chest.location,
73 ["buffer_location"] =
74 chest.bufferLocation
75 }
76 else
77 return false, [[Chest ']]..payload..[[
78 ' doesn't exist in the system]]
79 end
80 end
81}
82
83--If the "database" entry exists and
84--isn't a directory
85if fs.exists("database") and
86 not fs.isDir("database") then
87 --Delete the file
88 fs.delete("database")
89end
90
91--If the "database" entry doesn't exist
92if not fs.exists("database") then
93 --Create a directory called "database"
94 fs.makeDir("database")
95end
96
97--If the "ender_chest" entry doesn't exist
98if not fs.exists("database/ender_chest.txt") then
99 print("Error: database/ender_chest.txt doesn't exist")
100end
101
102--If the "ender_chest" entry is a directory
103if fs.isDir("database/ender_chest.txt") then
104 print("Error: database/ender_chest.txt is a directory")
105end
106
107--Set the channel to listen on
108local listenChannel = 128
109
110--Get the modem
111local modem = peripheral.wrap("top")
112--Open the channel
113modem.open(listenChannel)
114
115--Start an infinite loop
116while true do
117 --Wait for a message
118 local _,_,_, replyChannel, message =
119 os.pullEvent("modem_message")
120
121 --Get the function for the command
122 local commandFunc = commands[message.cmd]
123
124 --If the command is invalid
125 if not commandFunc then
126 --Send an error message back
127 modem.transmit(replyChannel, listenChannel,
128 {
129 ["success"] = false,
130 ["payload"] = [[Unknown command '
131 ]]..message.cmd .. [[']]
132 }
133 )
134 else
135 --Call the command function
136 --with the payload
137 local success, payload =
138 commandFunc(message.payload)
139 --Send a message back
140 modem.transmit(replyChannel, listenChannel,
141 {
142 ["success"] = success,
143 ["payload"] = payload
144 }
145 )
146 end
147end