-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIL.lua
More file actions
309 lines (295 loc) · 8.26 KB
/
BIL.lua
File metadata and controls
309 lines (295 loc) · 8.26 KB
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
--[[
Bagi's Inventory Libary (BIL)
Made by: afonya2@github
]]
local BIL = {}
function mysplit (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 BIL.processQuery(query)
local out = {}
local ms1 = mysplit(query, "?")
out.itemId = ms1[1]
out.raw_query = ms1[2]
out.query = {}
local ms2 = mysplit(ms1[2] or "", "&")
for k,v in ipairs(ms2) do
local ms3 = mysplit(v, "=")
if ms3[2] == nil then
ms3[2] = true
end
out.query[ms3[1]] = ms3[2]
end
if ms1[2] == nil then
out.query = nil
end
return out
end
function BIL.isItemMatch(storage, slot, rawDat, query)
local pq = BIL.processQuery(query)
local stwrap = BIL.getStorage(storage).wrap
if pq.query == nil then
if rawDat.name ~= pq.itemId then
return false
end
return true
else
if rawDat.name ~= pq.itemId then
return false
end
local stdetail = stwrap.getItemDetail(slot)
for k,v in pairs(pq.query) do
if stdetail[k] ~= v then
return false
end
end
return true
end
end
function BIL.getStorages(addTurtle)
local peps = peripheral.getNames()
local out = {}
for k,v in ipairs(peps) do
local _,t = peripheral.getType(v)
if t == "inventory" then
table.insert(out, v)
end
end
if addTurtle then
table.insert(out, "turtle")
end
return out
end
local function generateTurtleInvWrap(tid)
local out = {}
out.size = function()
return 16
end
out.list = function()
local llist = {}
for i=1,16 do
llist[i] = turtle.getItemDetail(i)
end
return llist
end
out.getItemDetail = function(slot)
return turtle.getItemDetail(i, true)
end
out.getItemLimit = function(slot)
return turtle.getItemDetail(i, true).maxCount
end
out.pushItems = function(toName, fromSlot, limit, toSlot)
local wrap = peripheral.wrap(toName)
return wrap.pullItems(tid, fromSlot, limit, toSlot)
end
out.pullItems = function(fromName, fromSlot, limit, toSlot)
local wrap = peripheral.wrap(fromName)
return wrap.pushItems(tid, fromSlot, limit, toSlot)
end
return out
end
local function generateCustomInvWrap(pid)
local wrapp = peripheral.wrap(pid)
local out = {}
out.size = function()
return wrapp.size()
end
out.list = function()
return wrapp.list()
end
out.getItemDetail = function(slot)
return wrapp.getItemDetail(slot)
end
out.getItemLimit = function(slot)
return wrapp.getItemLimit(slot)
end
out.pushItems = function(toName, fromSlot, limit, toSlot)
if (toName == "turtle") and (turtle ~= nil) then
local mod = peripheral.find("modem")
return wrapp.pushItems(mod.getNameLocal(), fromSlot, limit, toSlot)
else
return wrapp.pushItems(toName, fromSlot, limit, toSlot)
end
end
out.pullItems = function(fromName, fromSlot, limit, toSlot)
if (fromName == "turtle") and (turtle ~= nil) then
local mod = peripheral.find("modem")
return wrapp.pullItems(mod.getNameLocal(), fromSlot, limit, toSlot)
else
return wrapp.pullItems(fromName, fromSlot, limit, toSlot)
end
end
return out
end
function BIL.translateStorages(storages)
local out = {}
for k,v in ipairs(storages) do
if peripheral.isPresent(v) then
if (v == "turtle") and (turtle ~= nil) then
local mod = peripheral.find("modem")
local wrap = generateTurtleInvWrap(mod.getNameLocal())
table.insert(out, {
id = mod.getNameLocal(),
wrap = wrap
})
else
local wrap = generateCustomInvWrap(v)
table.insert(out, {
id = v,
wrap = wrap
})
end
end
end
return out
end
function BIL.getStorage(lid)
if (lid == "turtle") and (turtle ~= nil) then
local mod = peripheral.find("modem")
local wrap = generateTurtleInvWrap(mod.getNameLocal())
return {
id = mod.getNameLocal(),
wrap = wrap
}
else
if peripheral.isPresent(lid) then
local wrap = generateCustomInvWrap(lid)
return {
id = lid,
wrap = wrap
}
else
return nil, "Peripheral not found"
end
end
end
function BIL.list(storages)
if storages == nil then
storages = BIL.getStorages()
end
local ustorages = BIL.translateStorages(storages)
local out = {}
local iout = {}
for k,v in ipairs(ustorages) do
local llist = v.wrap.list()
for kk,vv in pairs(llist) do
if iout[vv.name..","..(vv.nbt or "")] == nil then
table.insert(out, {
id = vv.name,
count = vv.count,
nbt = vv.nbt
})
iout[vv.name..","..(vv.nbt or "")] = #out
else
out[iout[vv.name..","..(vv.nbt or "")]].count = out[iout[vv.name..","..(vv.nbt or "")]].count + vv.count
end
end
end
return out
end
function BIL.getSize(storages)
if storages == nil then
storages = BIL.getStorages()
end
local ustorages = BIL.translateStorages(storages)
local out = {
total = 0,
used = 0,
free = 0
}
for k,v in ipairs(ustorages) do
out.total = out.total + v.wrap.size()
local llist = v.wrap.list()
for kk,vv in pairs(llist) do
if vv ~= nil then
out.used = out.used + 1
end
end
end
out.free = out.total - out.used
return out
end
function BIL.getItemCount(query, storages)
if storages == nil then
storages = BIL.getStorages()
end
local ustorages = BIL.translateStorages(storages)
local count = 0
for k,v in ipairs(ustorages) do
local llist = v.wrap.list()
for kk,vv in pairs(llist) do
if BIL.isItemMatch(v.id, kk, vv, query) then
count = count + vv.count
end
end
end
return count
end
function BIL.transferItems(to, query, slimit, storages)
if storages == nil then
storages = BIL.getStorages()
end
local ustorages = BIL.translateStorages(storages)
if (not peripheral.isPresent(to)) and ((to ~= "turtle") or (turtle == nil)) then
return nil, "To peripheral is not present"
end
local ic = BIL.getItemCount(query, storages)
if ic < slimit then
slimit = ic
end
local function trans(remm)
for k,v in ipairs(ustorages) do
local llist = v.wrap.list()
for kk,vv in pairs(llist) do
if BIL.isItemMatch(v.id, kk, vv, query) then
return v.wrap.pushItems(to, kk, remm)
end
end
end
end
local ramm = slimit
while ramm > 0 do
local ca = trans(ramm)
ramm = ramm - ca
end
return slimit
end
function BIL.dropItems(query, slimit, storages)
if storages == nil then
storages = BIL.getStorages()
end
local ustorages = BIL.translateStorages(storages)
if turtle == nil then
return nil, "Computer must be a turtle"
end
local ic = BIL.getItemCount(query, storages)
if ic < slimit then
slimit = ic
end
local function trans(remm)
for k,v in ipairs(ustorages) do
local llist = v.wrap.list()
for kk,vv in pairs(llist) do
if BIL.isItemMatch(v.id, kk, vv, query) then
local pushed = v.wrap.pushItems("turtle", kk, remm)
turtle.drop()
return pushed
end
end
end
end
local ramm = slimit
while ramm > 0 do
local ca = trans(ramm)
ramm = ramm - ca
end
return slimit
end
return BIL