peripheral overhaul

This commit is contained in:
kepler155c
2018-01-06 06:08:39 -05:00
parent c48243eae5
commit f2b9efc80f
8 changed files with 176 additions and 108 deletions

View File

@@ -7,18 +7,16 @@ local ChestAdapter = class()
function ChestAdapter:init(args)
local defaults = {
name = 'chest',
direction = 'down',
wrapSide = 'bottom',
name = 'chest',
}
Util.merge(self, defaults)
Util.merge(self, args)
local chest
if self.autoDetect then
if not self.side then
chest = Peripheral.getByMethod('list')
else
chest = Peripheral.getBySide(self.wrapSide)
chest = Peripheral.getBySide(self.side)
if chest and not chest.list then
chest = nil
end
@@ -104,7 +102,7 @@ function ChestAdapter:craftItems()
end
function ChestAdapter:getPercentUsed()
if self.cache then
if self.cache and self.getDrawerCount then
return math.floor(Util.size(self.cache) / self.getDrawerCount() * 100)
end
return 0

View File

@@ -2,7 +2,7 @@ local Util = require('util')
local Adapter = { }
function Adapter.wrap(args, computerInfo)
function Adapter.wrap(args)
local adapters = {
--'refinedAdapter',
--'meAdapter',
@@ -10,34 +10,32 @@ function Adapter.wrap(args, computerInfo)
'chestAdapter',
}
if computerInfo then
if args and args.side and args.facing and not args.direction then
args = Util.shallowCopy(args)
if not args.direction and computerInfo.facing then
local horz = { top = 'down', bottom = 'up' }
args.direction = horz[args.wrapSide]
local horz = { top = 'down', bottom = 'up' }
args.direction = horz[args.side]
if not args.direction then
local sides = {
front = 0,
back = 2,
right = 1,
left = 3,
}
if not args.direction then
local sides = {
front = 0,
back = 2,
right = 1,
left = 3,
}
-- pretty sure computer/turtle have sides reversed
local cards = {
east = 0,
south = 1,
west = 2,
north = 3,
}
local icards = {
[ 0 ] = 'west',
[ 1 ] = 'north',
[ 2 ] = 'east',
[ 3 ] = 'south',
}
args.direction = icards[(cards[computerInfo.facing] + sides[args.wrapSide]) % 4]
end
local cards = {
east = 0,
south = 1,
west = 2,
north = 3,
}
local icards = {
[ 0 ] = 'west',
[ 1 ] = 'north',
[ 2 ] = 'east',
[ 3 ] = 'south',
}
args.direction = icards[(cards[args.facing] + sides[args.side]) % 4]
end
end

View File

@@ -73,29 +73,15 @@ function itemDB:get(key, enforceNBT)
end
end
if key.nbtHash and not enforceNBT then
if key.nbtHash then
item = self:get({ name = key.name, damage = key.damage })
if item and (item.maxDamage > 0 or item.damage == key.damage) then
if item and item.ignoreNBT then
item = Util.shallowCopy(item)
item.nbtHash = key.nbtHash
item.damage = key.damage
return item
end
local damage = tonumber(key.damage)
debug('scan: ' .. makeKey(key))
for _,item in pairs(self.data) do
if item.name == key.name and
((not damage or item.maxDamage > 0) or damage == item.damage) and
item.nbtHash then
item = Util.shallowCopy(item)
item.damage = damage or item.damage
if item.maxDamage > 0 and item.damage and item.damage > 0 then
item.displayName = string.format('%s (damage: %s)', item.displayName, item.damage)
end
item.nbtHash = key.nbtHash
return item
end
end
end
end
@@ -103,23 +89,55 @@ end
If the base item contains an NBT hash, then the NBT hash uniquely
identifies this item.
]]--
function itemDB:add(item, detail)
local nItem = { name = item.name, damage = item.damage, nbtHash = item.nbtHash }
if detail.maxDamage > 0 then
nItem.damage = '*'
end
function itemDB:add(baseItem, detail)
local nItem = {
name = baseItem.name,
damage = baseItem.damage,
nbtHash = baseItem.nbtHash,
}
-- if detail.maxDamage > 0 then
-- nItem.damage = '*'
-- end
debug('--')
debug('adding ' .. makeKey(nItem))
nItem.displayName = safeString(detail.displayName)
nItem.maxCount = detail.maxCount
nItem.maxDamage = detail.maxDamage
TableDB.add(self, makeKey(nItem), nItem)
if detail.maxDamage > 0 then
nItem = Util.shallowCopy(nItem)
nItem.damage = item.damage
for k,item in pairs(self.data) do
if nItem.name == item.name and
nItem.displayName == item.displayName then
debug('found: ' .. makeKey(item))
if nItem.nbtHash ~= item.nbtHash and nItem.damage ~= item.damage then
nItem.damage = '*'
nItem.nbtHash = nil
nItem.ignoreNBT = true
self.data[k] = nil
debug('removing all ' .. makeKey(nItem))
break
elseif nItem.damage ~= item.damage then
nItem.damage = '*'
self.data[k] = nil
debug('removing damage ' .. makeKey(nItem))
break
elseif nItem.nbtHash ~= item.nbtHash then
nItem.nbtHash = nil
nItem.ignoreNBT = true
debug('removing nbt ' .. makeKey(nItem))
self.data[k] = nil
break
end
end
end
debug('final ' .. makeKey(nItem))
TableDB.add(self, makeKey(nItem), nItem)
nItem = Util.shallowCopy(nItem)
nItem.damage = baseItem.damage
nItem.nbtHash = baseItem.nbtHash
return nItem
end