-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.lua
339 lines (299 loc) · 8.4 KB
/
storage.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
-- TODO
-- [x] help
-- [x] commands structure
-- [x] request exact
-- [x] group blocks with amount
-- [x] cache items for faster delivery
-- [x] item sort: show items sorted by amount
-- smart sorting (put new items where old items already are)
-- mark storage for only output and no input
-- [x] filled percentage
-- [x] free slots
-- boost performance by only updating the moved items instead of reloading the storage
require("github")
require("history")
req_chest = peripheral.wrap("minecraft:chest_0")
items = {}
last_searched_items = {}
last_compiled = 0
keyset = {}
function upgrade(self)
download("updater")
shell.run("updater")
end
function max_slots()
local slots = 0
for i = 1, #peripheral.getNames(), 1 do
local peri = peripheral.getNames()[i]
if peri ~= "left" and peri ~= peripheral.getName(req_chest) and peripheral.hasType(peri, "inventory") then
slots = slots + peripheral.call(peri, "size")
end
end
return slots
end
function available_slots()
local slots = 0
for i = 1, #peripheral.getNames(), 1 do
local peri = peripheral.getNames()[i]
if peri ~= "left" and peri ~= peripheral.getName(req_chest) and peripheral.hasType(peri, "inventory") then
slots = slots + #peripheral.call(peri, "list")
end
end
return slots
end
function set_loading_indicator(enabled)
redstone.setOutput("bottom", enabled)
end
function compile_items()
history_print("Recompiling items...")
set_loading_indicator(true)
items = {}
for i = 1, #peripheral.getNames(), 1 do
local peri = peripheral.getNames()[i]
if peri ~= "left" and peri ~= peripheral.getName(req_chest) and peripheral.hasType(peri, "inventory") then
local list = peripheral.call(peri, "list")
for k, v in pairs(list) do
local detail = peripheral.call(peri, "getItemDetail", k)
if items[detail.name] == nil then
items[detail.name] = {}
items[detail.name].displayName = detail.displayName
items[detail.name].total = detail.count
local location = {}
location.peripheral = peri
location.slot = k
location.count = detail.count
items[detail.name].locations = {}
table.insert(items[detail.name].locations, location)
else
items[detail.name].total = items[detail.name].total + detail.count
local location = {}
location.peripheral = peri
location.slot = k
location.count = detail.count
table.insert(items[detail.name].locations, location)
end
end
end
end
sort()
draw_header()
last_compiled = os.epoch("local")
history_print("Recompiling done.")
set_loading_indicator(false)
end
function flush()
-- TODO: rather iterate through the request chest list instead of all other chests
history_print("Flushing request chest...")
set_loading_indicator(true)
for i = 1, #peripheral.getNames(), 1 do
local peri = peripheral.getNames()[i]
local type = peripheral.getType(peri)
if peri ~= "left" and peri ~= peripheral.getName(req_chest) and peripheral.hasType(peri, "inventory") then
list = peripheral.call(peri, "list")
size = peripheral.call(peri, "size")
for k, v in pairs(req_chest.list()) do
req_chest.pushItems(peri, k)
end
end
end
set_loading_indicator(false)
history_print("Flushing done.")
end
function search(name)
results = {}
for k in pairs(items) do
if string.find(string.lower(items[k].displayName), string.lower(name)) then
table.insert(results, { displayName = items[k].displayName, total = items[k].total })
end
end
return results
end
function request(name, amount)
set_loading_indicator(true)
local item = nil
local name = string.lower(name)
if string.find(name, "_") then
name = string.gsub(name, "_", " ")
end
history_print(name)
for k in pairs(items) do
if string.lower(items[k].displayName) == name then
item = items[k]
break
end
end
if not item then
history_print(string.format("Could not find item with name %s", name))
set_loading_indicator(false)
return
end
local amount_to_pull = math.min(item.total, amount)
for i = 1, #item.locations, 1 do
local loc = item.locations[i]
if amount_to_pull > 0 and #req_chest.list() < req_chest.size() then
local pull_amount = math.min(loc.count, amount_to_pull)
req_chest.pullItems(loc.peripheral, loc.slot, pull_amount)
amount_to_pull = amount_to_pull - pull_amount
else
break
end
end
set_loading_indicator(false)
compile_items()
end
function clear()
term.clear()
term.setCursorPos(1, 1)
end
function string_split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
function sort()
keyset = {}
for k, v in pairs(items) do
table.insert(keyset, { key = k, total = v.total })
end
table.sort(keyset, function(t1, t2)
return t1.total < t2.total
end)
end
local commands = {}
commands.flush = {
description = "Sends all item from the request chest to the storage",
usage = "flush",
func = function()
flush()
compile_items()
end
}
commands.search = {
description = "searched lazily for an item name.",
usage = "search <name>",
func = function(args)
local res = search(args[2])
if #res == 0 then
history_print("No items found with name " .. args[2])
else
for _, v in pairs(res) do
history_print(string.format("%s: %s", v.displayName, v.total))
end
last_searched_items = res
end
end
}
commands.help = {
description = "shows all commands",
usage = "help",
func = function()
for k, v in pairs(commands) do
history_print(k .. " - " .. v.description .. "\nUsage: " .. v.usage .. "\n")
end
end
}
commands.upgrade = {
description = "upgrades the program",
usage = "upgrade",
func = function()
upgrade()
end
}
commands.clear = {
description = "clears the terminal",
usage = "clear",
func = function()
clear()
end
}
commands.request = {
description = "request an item lazily",
usage = "clear",
func = function(args)
request(args[2], tonumber(args[3]))
end
}
commands.print = {
description = "prints a message",
usage = "print",
func = function(args)
history_print(args[2])
end
}
commands.compile = {
description = "compiles the current storage system",
usage = "compile",
func = function(args)
compile_items()
end
}
commands.list = {
description = "Lists all items",
usage = "list",
func = function()
for _, v in pairs(keyset) do
history_print(string.format("%s: %s", items[v.key].displayName, items[v.key].total))
end
end
}
commands.slots = {
description = "shows available slots and max slots",
usage = "slots",
func = function(args)
local max = max_slots()
local avail = available_slots()
history_print(string.format("%s / %s (%.2f %%)", avail, max, (avail / max) * 100))
end
}
function handle_input(input)
local args = string_split(input)
history_print("> " .. input)
local found = false
for k, v in pairs(commands) do
if args[1] == k then
-- TODO: remove first element
v.func(args)
found = true
end
end
if not found then
commands.help.func()
end
end
draw_header()
local input = ""
print_input("")
if #items == 0 then
compile_items()
end
while true do
local eventData = { os.pullEvent() }
local event = eventData[1]
if event == "char" then
input = input .. eventData[2]
print_input(input)
elseif event == "key" then
if keys.getName(eventData[2]) == "enter" then
local a = input
input = ""
print_input(input)
handle_input(a)
elseif keys.getName(eventData[2]) == "backspace" then
input = input:sub(1, -2)
print_input(input)
elseif keys.getName(eventData[2]) == "up" then
scroll(-1)
elseif keys.getName(eventData[2]) == "down" then
scroll(1)
elseif keys.getName(eventData[2]) == "end" then
term.exit()
end
elseif event == "mouse_scroll" then
scroll(eventData[2])
end
end