OC-DSS2/OpenOS/server/usr/lib/inv.lua

114 lines
2.6 KiB
Lua
Raw Normal View History

2021-09-03 10:59:21 +10:00
local inv = {}
local transposers = {}
local component = require "component"
local serialization = require "serialization"
local syslog = require "syslog"
function inv.scanTransposers()
transposers = {}
for k,v in pairs(component.list("transposer")) do
for i = 0, 5 do
local iname = component.invoke(k, "getInventoryName", i)
if iname == "storagedrawers:controller" or iname == "storagedrawers:controllerslave" then
transposers[#transposers+1] = component.proxy(k)
transposers[#transposers].drawerside = i
transposers[k] = transposers[#transposers]
break
end
end
end
end
inv.scanTransposers()
function inv.match(specifiers)
local counter = 1
for item in transposers[1].getAllStacks(transposers[1].drawerside) do
local match = true
for k,v in pairs(specifiers) do
if type(v) ~= "table" and item[k] ~= v then
2021-09-03 10:59:21 +10:00
match = false
break
end
end
if match then
return counter, item
end
counter = counter + 1
end
end
function inv.matchAll(specifiers,fuzzy)
local counter = 1
local rt = {}
for item in transposers[1].getAllStacks(transposers[1].drawerside) do
local match = true
if item == nil or item.name == "minecraft:air" then
match = false
end
for k,v in pairs(specifiers) do
if fuzzy and type(item[k]) == "string" and type(v) == "string" then
if not item[k]:lower():find(v:lower()) then
match=false
break
end
else
if item[k] ~= v then
match = false
break
end
end
end
if match then
rt[#rt+1] = item
2021-09-03 10:59:21 +10:00
end
counter = counter + 1
end
return rt, #rt, counter
2021-09-03 10:59:21 +10:00
end
function inv.extract(specifiers,count,addr,side)
if not transposers[addr] then return false end
local transfered = 0
repeat
local slot, stack = inv.match(specifiers)
if not slot or not stack then
break
end
local lt = transposers[addr].transferItem(transposers[addr].drawerside, side, count-transfered, slot)
transfered = transfered + lt
until transfered >= count
local sf = ""
for k,v in pairs(specifiers) do
sf=string.format("%s%s=%s ",sf,tostring(k),tostring(v))
end
syslog(string.format("%s:%i extracted %i items matching %s",addr,side,transfered,sf),nil,"mtdss")
return transfered>0, transfered
end
function inv.getTransposers()
local rt = {}
for k,v in pairs(component.list("transposer")) do
rt[#rt+1] = k
end
return rt
end
function inv.getAliases()
local rt = {}
local f = io.open("/etc/talias","rb")
for line in f:lines() do
local lt = {}
for word in line:gmatch("%S+") do
lt[#lt+1] = word
end
local addr, side = table.remove(lt,1), tonumber(table.remove(lt,1))
for k,v in pairs(lt) do
rt[v] = {addr, side}
end
end
return rt
end
return inv