1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-17 06:04:50 +00:00
opus/sys/apis/chestProvider18.lua
kepler155c@gmail.com 7f99c0c69a builder improvements
2017-06-23 02:04:56 -04:00

153 lines
3.2 KiB
Lua

local class = require('class')
local TableDB = require('tableDB')
local Peripheral = require('peripheral')
local ChestProvider = class()
local keys = Util.transpose({
'damage',
'displayName',
'maxCount',
'maxDamage',
'name',
'nbtHash',
})
function ChestProvider:init(args)
local defaults = {
items = { },
name = 'chest',
direction = 'up',
wrapSide = 'bottom',
}
Util.merge(self, defaults)
Util.merge(self, args)
local chest = Peripheral.getBySide(self.wrapSide)
if chest then
Util.merge(self, chest)
end
if not self.itemInfoDB then
self.itemInfoDB = TableDB({
fileName = 'items.db'
})
self.itemInfoDB:load()
end
end
function ChestProvider:isValid()
return not not self.list
end
function ChestProvider:getCachedItemDetails(item, k)
local key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
local detail = self.itemInfoDB:get(key)
if not detail then
pcall(function() detail = self.getItemMeta(k) end)
if not detail then
return
end
if detail.name ~= item.name then
return
end
if detail.maxDamage and detail.maxDamage > 0 and detail.damage > 0 then
detail.displayName = detail.displayName .. ' (damaged)'
end
for _,k in ipairs(Util.keys(detail)) do
if not keys[k] then
detail[k] = nil
end
end
self.itemInfoDB:add(key, detail)
end
if detail then
return Util.shallowCopy(detail)
end
end
function ChestProvider:refresh(throttle)
return self:listItems(throttle)
end
-- provide a consolidated list of items
function ChestProvider:listItems(throttle)
self.cache = { }
local items = { }
throttle = throttle or Util.throttle()
for k,v in pairs(self.list()) do
local key = table.concat({ v.name, v.damage, v.nbtHash }, ':')
local entry = self.cache[key]
if not entry then
entry = self:getCachedItemDetails(v, k)
if entry then
entry.dmg = entry.damage
entry.id = entry.name
entry.count = 0
entry.display_name = entry.displayName
entry.max_size = entry.maxCount
entry.nbt_hash = entry.nbtHash
entry.lname = entry.displayName:lower()
self.cache[key] = entry
table.insert(items, entry)
end
end
if entry then
entry.count = entry.count + v.count
entry.qty = entry.count
end
throttle()
end
self.itemInfoDB:flush()
return items
end
function ChestProvider:getItemInfo(id, dmg, nbtHash)
if not self.cache then
self:listItems()
end
local key = table.concat({ id, dmg, nbtHash }, ':')
return self.cache[key]
end
function ChestProvider:craft(id, dmg, qty)
end
function ChestProvider:craftItems(items)
end
function ChestProvider:provide(item, qty, slot)
local stacks = self.list()
for key,stack in pairs(stacks) do
if stack.name == item.id and stack.damage == item.dmg then
local amount = math.min(qty, stack.count)
self.pushItems(self.direction, key, amount, slot)
qty = qty - amount
if qty <= 0 then
break
end
end
end
end
function ChestProvider:extract(slot, qty)
self.pushItems(self.direction, slot, qty)
end
function ChestProvider:insert(slot, qty)
self.pullItems(self.direction, slot, qty)
end
return ChestProvider