refined storage initial update - wip

This commit is contained in:
kepler155c
2018-03-26 15:13:12 -04:00
parent f62b760174
commit e532ee60f3
7 changed files with 83 additions and 2464 deletions

View File

@@ -1,35 +1,23 @@
local class = require('class')
local Util = require('util')
local Peripheral = require('peripheral')
local itemDB = require('itemDB')
local Peripheral = require('peripheral')
local Util = require('util')
local RefinedAdapter = class()
local keys = {
'damage',
'displayName',
'maxCount',
'maxDamage',
'name',
'nbtHash',
}
function RefinedAdapter:init(args)
local defaults = {
items = { },
name = 'refinedStorage',
direction = 'up',
wrapSide = 'bottom',
}
Util.merge(self, defaults)
Util.merge(self, args)
local controller
if self.autoDetect then
if not self.side then
controller = Peripheral.getByType('refinedstorage:controller')
else
controller = Peripheral.getBySide(self.wrapSide)
if controller and not controller.listAvailableItems then
controller = Peripheral.getBySide(self.side)
if controller and not controller.getCraftingTasks then
controller = nil
end
end
@@ -43,72 +31,69 @@ function RefinedAdapter:isValid()
return not not self.listAvailableItems
end
function RefinedAdapter:isOnline()
return self.getNetworkEnergyStored() > 0
function RefinedAdapter:getItemDetails(item)
local detail = self.findItems(item)
if detail and #detail > 0 then
return detail[1].getMetadata()
end
end
function RefinedAdapter:getCachedItemDetails(item)
local detail = itemDB:get(item)
if not detail then
detail = self.findItem(item)
if detail then
local meta
pcall(function() meta = detail.getMetadata() end)
if not meta then
return
end
Util.merge(detail, meta)
local t = { }
for _,k in pairs(keys) do
t[k] = detail[k]
end
detail = t
itemDB:add(detail)
end
local cached = itemDB:get(item)
if cached then
return cached
end
local detail = self:getItemDetails(item)
if detail then
return Util.shallowCopy(detail)
return itemDB:add(detail)
end
end
function RefinedAdapter:listItems()
function RefinedAdapter:refresh(throttle)
return self:listItems(throttle)
end
function RefinedAdapter:listItems(throttle)
local items = { }
local list
throttle = throttle or Util.throttle()
pcall(function()
list = self.listAvailableItems()
end)
if list then
local throttle = Util.throttle()
for _,v in pairs(list) do
local item = self:getCachedItemDetails(v)
if item then
item.count = v.count
table.insert(items, item)
end
local s, m = pcall(function()
for _,v in pairs(self.listAvailableItems()) do
--if v.count > 0 then
local item = self:getCachedItemDetails(v)
if item then
item = Util.shallowCopy(item)
item.count = v.count
table.insert(items, item)
end
--end
throttle()
end
itemDB:flush()
end)
if not s and m then
debug(m)
end
return items
itemDB:flush()
if not Util.empty(items) then
return items
end
end
function RefinedAdapter:getItemInfo(fingerprint)
local item = itemDB:get(fingerprint)
if not item then
return self:getCachedItemDetails(fingerprint)
end
function RefinedAdapter:getItemInfo(item)
return self:getItemDetails(item)
end
function RefinedAdapter:isCPUAvailable()
return true
end
function RefinedAdapter:craft(item, qty)
local detail = self.findItem(item)
if detail then
item.count = detail.count
return item
return detail.craft(qty)
end
end
@@ -124,26 +109,29 @@ function RefinedAdapter:isCrafting(item)
return false
end
function RefinedAdapter:craft(item, qty)
local detail = self.findItem(item)
if detail then
return detail.craft(qty)
end
end
function RefinedAdapter:craftItems()
return false
end
function RefinedAdapter:provide()
end
function RefinedAdapter:extract()
-- self.pushItems(self.direction, slot, qty)
function RefinedAdapter:provide(item, qty, slot, direction)
return pcall(function()
for _,stack in pairs(self.listAvailableItems()) do
if stack.name == item.name and
(not item.damage or stack.damage == item.damage) and
(not item.nbtHash or stack.nbtHash == item.nbtHash) then
local amount = math.min(qty, stack.count)
if amount > 0 then
local detail = self.findItem(item)
if detail then
return detail.export(direction or self.direction, amount, slot)
end
end
qty = qty - amount
if qty <= 0 then
break
end
end
end
end)
end
function RefinedAdapter:insert()
-- self.pullItems(self.direction, slot, qty)
end
return RefinedAdapter