1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-05 16:36:16 +00:00
opus/sys/apis/chestProvider18.lua

95 lines
2.0 KiB
Lua
Raw Normal View History

2017-04-01 23:21:49 +00:00
local class = require('class')
local ChestProvider = class()
function ChestProvider:init(args)
args = args or { }
self.items = { } -- consolidated item info
2017-04-20 11:33:36 +00:00
self.cache = { }
2017-04-01 23:21:49 +00:00
self.name = 'chest'
self.direction = args.direction or 'up'
self.wrapSide = args.wrapSide or 'bottom'
self.p = peripheral.wrap(self.wrapSide)
end
2017-04-20 11:33:36 +00:00
2017-04-01 23:21:49 +00:00
function ChestProvider:isValid()
return self.p and self.p.list
end
2017-04-20 11:33:36 +00:00
2017-04-01 23:21:49 +00:00
function ChestProvider:refresh()
if self.p then
self.items = { }
2017-04-20 11:33:36 +00:00
for k,s in pairs(self.p.list()) do
local key = s.name .. ':' .. s.damage
local entry = self.items[key]
if not entry then
2017-04-20 11:33:36 +00:00
entry = self.cache[key]
if not entry then
local meta = self.p.getItemMeta(k) -- slow method.. cache for speed
entry = {
id = s.name,
dmg = s.damage,
name = meta.displayName,
max_size = meta.maxCount,
}
self.cache[key] = entry
end
entry = Util.shallowCopy(entry)
self.items[key] = entry
2017-04-20 11:33:36 +00:00
entry.qty = 0
2017-04-01 23:21:49 +00:00
end
2017-04-20 11:33:36 +00:00
entry.qty = entry.qty + s.count
2017-04-01 23:21:49 +00:00
end
end
return self.items
2017-04-01 23:21:49 +00:00
end
2017-04-01 23:21:49 +00:00
function ChestProvider:getItemInfo(id, dmg)
2017-04-20 11:33:36 +00:00
for key,item in pairs(self.items) do
if item.id == id and item.dmg == dmg then
return item
2017-04-01 23:21:49 +00:00
end
end
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)
if self.p then
2017-04-20 11:33:36 +00:00
local stacks = self.p.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)
2017-04-01 23:21:49 +00:00
self.p.pushItems(self.direction, key, amount, slot)
qty = qty - amount
if qty <= 0 then
break
end
end
end
end
end
2017-04-20 11:33:36 +00:00
2017-04-01 23:21:49 +00:00
function ChestProvider:extract(slot, qty)
if self.p then
self.p.pushItems(self.direction, slot, qty)
end
end
function ChestProvider:insert(slot, qty)
if self.p then
self.p.pullItems(self.direction, slot, qty)
end
end
return ChestProvider