95 lines
2.2 KiB
Lua
95 lines
2.2 KiB
Lua
|
local component = require "component"
|
||
|
local computer = require "computer"
|
||
|
local sides = require "sides"
|
||
|
local invc = component.transposer
|
||
|
|
||
|
local inv = {}
|
||
|
|
||
|
inv.drawer = sides.top
|
||
|
inv.input=sides.north
|
||
|
inv.output=sides.south
|
||
|
|
||
|
inv.cache = {}
|
||
|
inv.size = 0
|
||
|
inv.cacheTime = -1000
|
||
|
inv.cacheInvalid = false
|
||
|
|
||
|
function inv.index() -- refresh the index of items in the storage system
|
||
|
inv.size = invc.getInventorySize(inv.drawer)
|
||
|
inv.cache = {}
|
||
|
for k,v in pairs(invc.getAllStacks(inv.drawer).getAll()) do
|
||
|
if v.name ~= "minecraft:air" then
|
||
|
inv.cache[#inv.cache+1] = {k,v}
|
||
|
end
|
||
|
end
|
||
|
inv.cacheTime = computer.uptime()
|
||
|
inv.cacheInvalid = false
|
||
|
end
|
||
|
|
||
|
function inv.cacheInvalidation() -- re-index only if necessary
|
||
|
if computer.uptime() > inv.cacheTime + 300 or inv.cacheInvalid then
|
||
|
inv.index()
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function inv.search(str) -- search names and labels for *str*
|
||
|
inv.cacheInvalidation()
|
||
|
str = string.lower(str or "")
|
||
|
local rt = {}
|
||
|
for k,v in pairs(inv.cache) do
|
||
|
item = v[2]
|
||
|
item.label = item.label or ""
|
||
|
if item.name:match(str) or item.label:lower():match(str) then
|
||
|
rt[#rt+1] = v
|
||
|
end
|
||
|
end
|
||
|
return rt,#inv.cache,inv.size
|
||
|
end
|
||
|
|
||
|
function inv.inputItems() -- pull items into the storage system from the input box
|
||
|
local counter = 0
|
||
|
local imap = invc.getAllStacks(inv.input).getAll()
|
||
|
for i = 1, invc.getInventorySize(inv.input) do
|
||
|
local item = imap[i]
|
||
|
if item.name ~= "minecraft:air" then
|
||
|
if item.maxSize < 2 then
|
||
|
invc.transferItem(inv.input,inv.output,64,i)
|
||
|
else
|
||
|
counter=counter+invc.transferItem(inv.input,inv.drawer,64,i)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
if counter > 0 then
|
||
|
inv.cacheInvalid = true
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function inv.extractItem(slot,count,output)
|
||
|
local tcount = 0
|
||
|
local item = invc.getStackInSlot(inv.drawer,slot)
|
||
|
if item.size <= count then
|
||
|
inv.cacheInvalid = true
|
||
|
end
|
||
|
count=math.min(count,item.size)
|
||
|
local transferred = 0
|
||
|
repeat
|
||
|
transferred = invc.transferItem(inv.drawer,output or inv.output,count - tcount,slot)
|
||
|
tcount = tcount + transferred
|
||
|
for k,v in pairs(inv.cache) do
|
||
|
if v[1] == slot then
|
||
|
inv.cache[k][2].size = inv.cache[k][2].size - transferred or 0
|
||
|
if inv.cache[k][2].size < 1 then
|
||
|
inv.cacheInvalid = true
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
io.write(tcount.." ")
|
||
|
until tcount >= count
|
||
|
return tcount
|
||
|
end
|
||
|
|
||
|
inv.index()
|
||
|
|
||
|
return inv
|