-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory_loader.lua
146 lines (131 loc) · 4.16 KB
/
inventory_loader.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
tu = require("common")
default = require("default")
inventories = {peripheral.find("inventory")}
itemStorages = {peripheral.find("item_storage")}
for k,v in pairs(itemStorages) do
table.insert(inventories, v)
end
defaults = default.load()
inputTypes = {}
outputTypes = {}
for itemType, mode in pairs(defaults) do
if mode == "ii" then
table.insert(inputTypes, itemType)
elseif mode == "oo" then
table.insert(outputTypes, itemType)
end
end
inputs = {}
outputs = {}
if #inventories >= 2 then
print("Select inventories...")
for _,i in pairs(inventories) do
inventoryType = peripheral.getType(i)
if tu.contains(inputTypes, inventoryType) then
table.insert(inputs, i)
print(" Using as input: " .. inventoryType)
elseif tu.contains(outputTypes, inventoryType) then
table.insert(outputs, i)
print(" Using as output: " .. inventoryType)
else
print(" " .. peripheral.getName(i) .. "?")
print(" [I]nput, [O]utput, [II] All inputs, [OO] All outputs")
response = string.sub(read(), 1, 2)
response = string.upper(response)
if response == "I" then
table.insert(inputs, i)
print(" Using as input.")
elseif response == "O" then
table.insert(outputs, i)
print(" Using as output.")
elseif response == "II" then
table.insert(inputs, i)
table.insert(inputTypes, inventoryType)
print(" Using " .. inventoryType .. " as inputs.")
elseif response == "OO" then
table.insert(outputs, i)
table.insert(outputTypes, inventoryType)
print(" Using " .. inventoryType .. " as outputs.")
end
end
end
print("Done selecting inventories. Starting now.")
elseif #inventories == 1 then
error("You must connect at least 2 inventory-capable blocks.")
return
else
error("No compatible inventories found. Please connect at least 2 inventory-capable blocks to this computer.")
return
end
function isItemStorage(inv)
return tu.contains(peripheral.getMethods(peripheral.getName(inv)), "pushItem")
end
function countInventories(side)
count = {}
for _,inv in pairs(side) do
if isItemStorage(inv) then
items = inv.items()
else
items = inv.list()
end
for __,item in pairs(items) do
if not tu.containsKey(count, item.name) then
count[item.name] = item.count
else
count[item.name] = count[item.name] + item.count
end
end
end
return count
end
function compare(compareTo, itemType, count)
for k,v in pairs(compareTo) do
if k == itemType then
return count - v
end
end
return count
end
function tryMoveItems(ins, outs)
for itemType,count in pairs(ins) do
numToTransfer = compare(outs, itemType, count)
if numToTransfer > 0 then
pcall(function() moveItems(itemType, numToTransfer) end)
end
end
end
function moveItems(itemType, num)
for _,i in pairs(inputs) do
if not isItemStorage(i) then
for slot,item in pairs(i.list()) do
if item.name == itemType then
for __,o in pairs(outputs) do
num = num - i.pushItems(peripheral.getName(o), slot, num)
end
end
if num <= 0 then
return
end
end
if num <= 0 then
return
end
else
for __,o in pairs(outputs) do
num = num - i.pushItem(peripheral.getName(o), {name=itemType}, num)
if num <= 0 then
return
end
end
end
end
if num <= 0 then
return
end
end
while true do
inputItems = countInventories(inputs)
outputItems = countInventories(outputs)
tryMoveItems(inputItems, outputItems)
sleep(2)
end