1
0
mirror of https://github.com/kepler155c/opus synced 2024-06-18 11:20:01 +00:00
opus/sys/apis/chestProvider18.lua

147 lines
3.1 KiB
Lua
Raw Normal View History

2017-04-01 23:21:49 +00:00
local class = require('class')
local itemDB = require('itemDB')
2017-06-23 06:04:56 +00:00
local Peripheral = require('peripheral')
2017-04-01 23:21:49 +00:00
local ChestProvider = class()
2017-06-23 06:04:56 +00:00
local keys = Util.transpose({
'damage',
'displayName',
'maxCount',
'maxDamage',
'name',
'nbtHash',
})
2017-04-01 23:21:49 +00:00
function ChestProvider:init(args)
2017-06-23 06:04:56 +00:00
local defaults = {
items = { },
name = 'chest',
direction = 'up',
wrapSide = 'bottom',
}
Util.merge(self, defaults)
Util.merge(self, args)
local chest = Peripheral.getBySide(self.wrapSide)
2017-08-09 14:19:00 +00:00
if not chest then
chest = Peripheral.getByMethod('list')
end
2017-06-23 06:04:56 +00:00
if chest then
Util.merge(self, chest)
end
2017-04-01 23:21:49 +00:00
end
2017-04-20 11:33:36 +00:00
2017-04-01 23:21:49 +00:00
function ChestProvider:isValid()
2017-06-23 06:04:56 +00:00
return not not self.list
2017-04-01 23:21:49 +00:00
end
2017-04-20 11:33:36 +00:00
2017-06-23 06:04:56 +00:00
function ChestProvider:getCachedItemDetails(item, k)
local key = { item.name, item.damage, item.nbtHash }
2017-06-23 06:04:56 +00:00
local detail = itemDB:get(key)
2017-06-23 06:04:56 +00:00
if not detail then
pcall(function() detail = self.getItemMeta(k) end)
if not detail then
return
end
2017-07-24 02:37:07 +00:00
-- NOT SUFFICIENT
2017-06-23 06:04:56 +00:00
if detail.name ~= item.name then
return
end
for _,k in ipairs(Util.keys(detail)) do
if not keys[k] then
detail[k] = nil
2017-06-15 07:25:55 +00:00
end
2017-04-01 23:21:49 +00:00
end
2017-06-23 06:04:56 +00:00
itemDB:add(key, detail)
2017-06-23 06:04:56 +00:00
end
if detail then
return Util.shallowCopy(detail)
2017-04-01 23:21:49 +00:00
end
end
2017-06-23 06:04:56 +00:00
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
2017-04-20 11:33:36 +00:00
2017-06-23 06:04:56 +00:00
if entry then
entry.count = entry.count + v.count
entry.qty = entry.count
2017-04-01 23:21:49 +00:00
end
2017-06-23 06:04:56 +00:00
throttle()
end
itemDB:flush()
2017-06-23 06:04:56 +00:00
return items
end
function ChestProvider:getItemInfo(id, dmg, nbtHash)
if not self.cache then
self:listItems()
2017-04-01 23:21:49 +00:00
end
2017-06-23 06:04:56 +00:00
local key = table.concat({ id, dmg, nbtHash }, ':')
return self.cache[key]
2017-04-01 23:21:49 +00:00
end
2017-04-20 11:33:36 +00:00
2017-04-01 23:21:49 +00:00
function ChestProvider:craft(id, dmg, qty)
end
function ChestProvider:craftItems(items)
end
function ChestProvider:provide(item, qty, slot, direction)
2017-06-23 06:04:56 +00:00
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)
if amount > 0 then
self.pushItems(direction or self.direction, key, amount, slot)
end
2017-06-23 06:04:56 +00:00
qty = qty - amount
if qty <= 0 then
break
2017-04-01 23:21:49 +00:00
end
end
end
end
2017-04-20 11:33:36 +00:00
2017-07-24 02:37:07 +00:00
function ChestProvider:extract(slot, qty, toSlot)
self.pushItems(self.direction, slot, qty, toSlot)
2017-04-01 23:21:49 +00:00
end
function ChestProvider:insert(slot, qty)
2017-06-23 06:04:56 +00:00
self.pullItems(self.direction, slot, qty)
2017-04-01 23:21:49 +00:00
end
return ChestProvider