2017-04-15 15:38:08 +00:00
|
|
|
local class = require('class')
|
|
|
|
local Peripheral = require('peripheral')
|
2017-07-02 14:05:10 +00:00
|
|
|
local itemDB = require('itemDB')
|
2017-04-15 15:38:08 +00:00
|
|
|
|
|
|
|
local RefinedProvider = class()
|
2017-04-23 14:06:35 +00:00
|
|
|
|
|
|
|
local keys = {
|
|
|
|
'damage',
|
|
|
|
'displayName',
|
|
|
|
'maxCount',
|
|
|
|
'maxDamage',
|
|
|
|
'name',
|
|
|
|
'nbtHash',
|
|
|
|
}
|
|
|
|
|
2017-04-15 15:38:08 +00:00
|
|
|
function RefinedProvider:init(args)
|
|
|
|
local defaults = {
|
|
|
|
items = { },
|
|
|
|
name = 'refinedStorage',
|
|
|
|
}
|
|
|
|
Util.merge(self, defaults)
|
|
|
|
Util.merge(self, args)
|
|
|
|
|
2017-04-17 07:13:00 +00:00
|
|
|
local controller = Peripheral.getByType('refinedstorage:controller')
|
|
|
|
if controller then
|
|
|
|
Util.merge(self, controller)
|
|
|
|
end
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:isValid()
|
2017-04-17 07:13:00 +00:00
|
|
|
return not not self.listAvailableItems
|
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:isOnline()
|
|
|
|
return self.getNetworkEnergyStored() > 0
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:getCachedItemDetails(item)
|
2017-07-02 14:05:10 +00:00
|
|
|
local key = { item.name, item.damage, item.nbtHash }
|
2017-04-15 15:38:08 +00:00
|
|
|
|
2017-07-02 14:05:10 +00:00
|
|
|
local detail = itemDB:get(key)
|
2017-04-15 15:38:08 +00:00
|
|
|
if not detail then
|
2017-04-17 07:13:00 +00:00
|
|
|
detail = self.findItem(item)
|
2017-04-15 15:38:08 +00:00
|
|
|
if detail then
|
2017-04-20 11:33:36 +00:00
|
|
|
local meta
|
|
|
|
pcall(function() meta = detail.getMetadata() end)
|
2017-04-23 14:06:35 +00:00
|
|
|
if not meta then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
Util.merge(detail, meta)
|
|
|
|
detail.lname = detail.displayName:lower()
|
|
|
|
|
|
|
|
local t = { }
|
2017-07-02 14:05:10 +00:00
|
|
|
for _,k in pairs(keys) do
|
|
|
|
t[k] = detail[k]
|
2017-04-23 14:06:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
detail = t
|
2017-07-02 14:05:10 +00:00
|
|
|
itemDB:add(key, detail)
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-22 04:51:27 +00:00
|
|
|
if detail then
|
|
|
|
return Util.shallowCopy(detail)
|
|
|
|
end
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:listItems()
|
|
|
|
local items = { }
|
|
|
|
local list
|
|
|
|
|
|
|
|
pcall(function()
|
2017-04-17 07:13:00 +00:00
|
|
|
list = self.listAvailableItems()
|
2017-04-15 15:38:08 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
if list then
|
2017-05-14 03:45:59 +00:00
|
|
|
|
|
|
|
local throttle = Util.throttle()
|
|
|
|
|
2017-04-15 15:38:08 +00:00
|
|
|
for _,v in pairs(list) do
|
|
|
|
local item = self:getCachedItemDetails(v)
|
|
|
|
if item then
|
2017-04-25 16:40:06 +00:00
|
|
|
item.display_name = item.displayName
|
|
|
|
item.id = v.name
|
2017-04-15 15:38:08 +00:00
|
|
|
item.count = v.count
|
|
|
|
item.qty = v.count
|
|
|
|
table.insert(items, item)
|
|
|
|
end
|
2017-05-14 03:45:59 +00:00
|
|
|
throttle()
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
2017-07-02 14:05:10 +00:00
|
|
|
itemDB:flush()
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return items
|
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:getItemInfo(fingerprint)
|
|
|
|
|
2017-07-02 14:05:10 +00:00
|
|
|
local key = { fingerprint.name, fingerprint.damage, fingerprint.nbtHash }
|
2017-04-15 15:38:08 +00:00
|
|
|
|
2017-07-02 14:05:10 +00:00
|
|
|
local item = itemDB:get(key)
|
2017-04-15 15:38:08 +00:00
|
|
|
if not item then
|
|
|
|
return self:getCachedItemDetails(fingerprint)
|
|
|
|
end
|
|
|
|
|
2017-04-17 07:13:00 +00:00
|
|
|
local detail = self.findItem(item)
|
2017-04-15 15:38:08 +00:00
|
|
|
if detail then
|
|
|
|
item.count = detail.count
|
|
|
|
item.qty = detail.count
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
end
|
2017-04-17 07:13:00 +00:00
|
|
|
|
|
|
|
function RefinedProvider:isCrafting(item)
|
|
|
|
for _,task in pairs(self.getCraftingTasks()) do
|
2017-04-20 11:33:36 +00:00
|
|
|
local output = task.getPattern().outputs[1]
|
|
|
|
if output.name == item.name and
|
|
|
|
output.damage == item.damage and
|
|
|
|
output.nbtHash == item.nbtHash then
|
2017-04-17 07:13:00 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-04-23 14:06:35 +00:00
|
|
|
function RefinedProvider:craft(item, qty)
|
|
|
|
local detail = self.findItem(item)
|
|
|
|
if detail then
|
|
|
|
return detail.craft(qty)
|
|
|
|
end
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:craftItems(items)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:provide(item, qty, slot)
|
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:extract(slot, qty)
|
2017-04-17 07:13:00 +00:00
|
|
|
-- self.pushItems(self.direction, slot, qty)
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function RefinedProvider:insert(slot, qty)
|
2017-04-17 07:13:00 +00:00
|
|
|
-- self.pullItems(self.direction, slot, qty)
|
2017-04-15 15:38:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return RefinedProvider
|