-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.lua
413 lines (354 loc) · 10.1 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
-- 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
-- add ability to attach furnaces or other machines
-- make storage chests configurable
-- smelt command
require("github")
require("history")
require("smelter")
req_chest = peripheral.wrap("minecraft:chest_0")
items = {}
last_searched_items = {}
last_compiled = 0
keyset = {}
is_refreshing = false
max_slots = 0
available_slots = 0
input = ""
function update(self)
download("updater")
shell.run("updater")
end
function set_request_chest(name)
req_chest = peripheral.wrap(name)
end
function get_max_slots()
return max_slots
end
function get_available_slots()
return available_slots
end
function set_loading_indicator(enabled)
redstone.setOutput("bottom", enabled)
end
-- Adds an item to the storage, appends the items table
function add_item(chest_name, item)
if items[item.name] == nil then
items[item.name] = {}
items[item.name].name = item.name
items[item.name].displayName = item.displayName
items[item.name].total = item.count
items[item.name].inventories = {}
items[item.name].inventories[chest_name] = item.count
else
items[item.name].total = items[item.name].total + item.count
if items[item.name].inventories[chest_name] then
items[item.name].inventories[chest_name] = items[item.name].inventories[chest_name] + item.count
else
items[item.name].inventories[chest_name] = item.count
end
end
end
function get_item(item)
return items[item.name]
end
function remove_item(item, chest_name, amount)
items[item.name].total = items[item.name].total - amount
if items[item.name].total <= 0 then
history_print(string.format("Remove: Item %s", item.name))
items[item.name] = nil
return
end
items[item.name].inventories[chest_name] = items[item.name].inventories[chest_name] - amount
if items[item.name].inventories[chest_name] <= 0 then
items[item.name].inventories[chest_name] = nil
end
end
-- Check if peripheral is a storage chest, meaning it has chest in the name and is not the request chest
function is_storage_chest(peri)
return peri ~= "left" and peri ~= peripheral.getName(req_chest) and peripheral.hasType(peri, "inventory") and string.find(peri, "chest")
end
-- Initialises the Storage. Fetch all Items in the Network and build the items table. Calculate max slots and available slots
function init()
history_print("Init Storage..")
max_slots = 0
available_slots = 0
items = {}
for i = 1, #peripheral.getNames(), 1 do
local peri = peripheral.getNames()[i]
if is_storage_chest(peri) then
local list = peripheral.call(peri, "list")
for k, v in pairs(list) do
add_item(peri, peripheral.call(peri, "getItemDetail", k))
end
max_slots = max_slots + peripheral.call(peri, "size")
available_slots = available_slots + #peripheral.call(peri, "list")
end
end
draw_header()
Smelter:init()
history_print("Init Done.")
end
-- Clears the request chest and puts all items into the storage
function flush()
history_print("Flushing request chest...")
for i = 1, #peripheral.getNames(), 1 do
local peri = peripheral.getNames()[i]
if is_storage_chest(peri) then
for k, v in pairs(req_chest.list()) do
req_chest.pushItems(peri, k)
if v then
add_item(peri, v)
end
end
end
end
history_print("Flushing done.")
end
-- Lazy search an item
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
-- Request an amount of items by name
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, v in pairs(items) do
if string.lower(v.displayName) == name then
item = v
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)
if item.total < amount then
history_print("Not enough items in storage.")
history_print(string.format("Pull %s instead", amount_to_pull))
end
for k, v in pairs(item.inventories) do
local item_to_pull_from_inv = math.min(v, amount_to_pull)
local foundSlots = {}
for slot, slot_item in pairs(peripheral.call(k, "list")) do
if slot_item.name == item.name then
table.insert(foundSlots, { slot = slot, count = slot_item.count })
end
end
for _, slot in pairs(foundSlots) do
local pull_amount_from_slot = math.min(slot.count, item_to_pull_from_inv)
req_chest.pullItems(k, slot.slot, pull_amount_from_slot)
remove_item(item, k, pull_amount_from_slot)
item_to_pull_from_inv = item_to_pull_from_inv - pull_amount_from_slot
amount_to_pull = amount_to_pull - pull_amount_from_slot
end
end
set_loading_indicator(false)
end
function clear()
term.clear()
term.setCursorPos(1, 1)
draw_header()
input = ""
print_input("")
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()
end
}
commands.search = {
description = "lazily search for an item with 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.update = {
description = "updates the program",
usage = "update",
func = function()
update()
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.reinit = {
description = "compiles the current storage system",
usage = "compile",
func = function(args)
init()
end
}
commands.list = {
description = "Lists all items",
usage = "list",
func = function()
for k, v in pairs(items) do
history_print(string.format("%s: %s", k, v.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
}
commands.request_chest = {
description = "sets a chest as the request chest",
usage = "request_chest",
func = function(args)
set_request_chest(args[2])
history_print(string.format("Set request chest to %s", args[2]))
end
}
commands.smelt = {
description = "smelt amount of item",
usage = "smelt",
func = function(args)
smelt(args[2], tonumber(args[3]))
end
}
commands.debug = {
description = "output item table",
usage = "debug",
func = function(args)
for k, v in pairs(items) do
history_print(string.format("-- %s --", k))
history_print(string.format("Name:%s",v.displayName))
history_print(string.format("Total:%s",v.total))
history_print(string.format("Inventories:%s",#v.inventories))
history_print("--------")
end
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
clear()
if #items == 0 then
init()
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