item db naming fix for items with diff names-nbt combinations

This commit is contained in:
kepler155c
2018-01-04 03:33:09 -05:00
parent 45bbc72fc3
commit c48243eae5
7 changed files with 147 additions and 127 deletions

View File

@@ -5,19 +5,10 @@ local Peripheral = require('peripheral')
local ChestAdapter = class() local ChestAdapter = class()
local keys = Util.transpose({
'damage',
'displayName',
'maxCount',
'maxDamage',
'name',
'nbtHash',
})
function ChestAdapter:init(args) function ChestAdapter:init(args)
local defaults = { local defaults = {
name = 'chest', name = 'chest',
direction = 'up', direction = 'down',
wrapSide = 'bottom', wrapSide = 'bottom',
} }
Util.merge(self, defaults) Util.merge(self, defaults)
@@ -35,13 +26,6 @@ function ChestAdapter:init(args)
if chest then if chest then
Util.merge(self, chest) Util.merge(self, chest)
local sides = {
top = 'down',
bottom = 'up',
}
self.direction = sides[self.side] or self.direction
end end
end end
@@ -50,30 +34,19 @@ function ChestAdapter:isValid()
end end
function ChestAdapter:getCachedItemDetails(item, k) function ChestAdapter:getCachedItemDetails(item, k)
local detail = itemDB:get(item) local cached = itemDB:get(item, true)
if not detail then if cached then
local s, m = pcall(function() detail = self.getItemMeta(k) end) return cached
if not detail then
-- error('Inventory has changed')
return
end
-- NOT SUFFICIENT
if detail.name ~= item.name then
-- error('Inventory has changed')
return
end
for _,k in ipairs(Util.keys(detail)) do
if not keys[k] then
detail[k] = nil
end
end
itemDB:add(detail)
end end
if detail then
return Util.shallowCopy(detail) local s, detail = pcall(self.getItemMeta, k)
if not s or not detail or detail.name ~= item.name then
debug({ s, detail })
-- error('Inventory has changed')
return
end end
return itemDB:add(item, detail)
end end
function ChestAdapter:refresh(throttle) function ChestAdapter:refresh(throttle)
@@ -96,6 +69,7 @@ function ChestAdapter:listItems(throttle)
if not entry then if not entry then
return -- Inventory has changed return -- Inventory has changed
end end
entry = Util.shallowCopy(entry)
entry.count = 0 entry.count = 0
cache[key] = entry cache[key] = entry
table.insert(items, entry) table.insert(items, entry)

View File

@@ -1,6 +1,8 @@
local Util = require('util')
local Adapter = { } local Adapter = { }
function Adapter.wrap(args) function Adapter.wrap(args, computerInfo)
local adapters = { local adapters = {
--'refinedAdapter', --'refinedAdapter',
--'meAdapter', --'meAdapter',
@@ -8,6 +10,37 @@ function Adapter.wrap(args)
'chestAdapter', 'chestAdapter',
} }
if computerInfo then
args = Util.shallowCopy(args)
if not args.direction and computerInfo.facing then
local horz = { top = 'down', bottom = 'up' }
args.direction = horz[args.wrapSide]
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
end
end
for _,adapterType in ipairs(adapters) do for _,adapterType in ipairs(adapters) do
local adapter = require(adapterType)(args) local adapter = require(adapterType)(args)

View File

@@ -5,7 +5,6 @@ local Util = require('util')
local itemDB = TableDB({ fileName = 'usr/config/items.db' }) local itemDB = TableDB({ fileName = 'usr/config/items.db' })
local function safeString(text) local function safeString(text)
local val = text:byte(1) local val = text:byte(1)
if val < 32 or val > 128 then if val < 32 or val > 128 then
@@ -32,7 +31,7 @@ local function safeString(text)
end end
local function makeKey(item) local function makeKey(item)
return { item.name, item.damage or '*', item.nbtHash } return table.concat({ item.name, item.damage or '*', item.nbtHash }, ':')
end end
function itemDB:splitKey(key, item) function itemDB:splitKey(key, item)
@@ -51,8 +50,7 @@ function itemDB:splitKey(key, item)
return item return item
end end
function itemDB:get(key) function itemDB:get(key, enforceNBT)
if type(key) == 'string' then if type(key) == 'string' then
key = self:splitKey(key) key = self:splitKey(key)
end end
@@ -75,7 +73,7 @@ function itemDB:get(key)
end end
end end
if key.nbtHash then if key.nbtHash and not enforceNBT then
item = self:get({ name = key.name, damage = key.damage }) 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.maxDamage > 0 or item.damage == key.damage) then
item = Util.shallowCopy(item) item = Util.shallowCopy(item)
@@ -84,6 +82,7 @@ function itemDB:get(key)
end end
local damage = tonumber(key.damage) local damage = tonumber(key.damage)
debug('scan: ' .. makeKey(key))
for _,item in pairs(self.data) do for _,item in pairs(self.data) do
if item.name == key.name and if item.name == key.name and
((not damage or item.maxDamage > 0) or damage == item.damage) and ((not damage or item.maxDamage > 0) or damage == item.damage) and
@@ -98,31 +97,30 @@ function itemDB:get(key)
end end
end end
end end
--debug('miss: ' .. table.concat(makeKey(key), ':'))
--[[
if not key[3] then
for _,item in pairs(self.data) do
if item.name == key[1] and
item.damage == key[2] and
item.nbtHash then
item = Util.shallowCopy(item)
item.nbtHash = nil
return item
end
end
end
--]]
end end
function itemDB:add(item) --[[
local key = makeKey(item) If the base item contains an NBT hash, then the NBT hash uniquely
if item.maxDamage > 0 then identifies this item.
key = makeKey({ name = item.name, damage = '*', nbtHash = item.nbtHash }) ]]--
function itemDB:add(item, detail)
local nItem = { name = item.name, damage = item.damage, nbtHash = item.nbtHash }
if detail.maxDamage > 0 then
nItem.damage = '*'
end end
item.displayName = safeString(item.displayName)
TableDB.add(self, key, item) 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
end
return nItem
end end
-- Accepts: "minecraft:stick:0" or { name = 'minecraft:stick', damage = 0 } -- Accepts: "minecraft:stick:0" or { name = 'minecraft:stick', damage = 0 }
@@ -168,6 +166,7 @@ function itemDB:flush()
v.name = nil v.name = nil
v.damage = nil v.damage = nil
v.nbtHash = nil v.nbtHash = nil
v.count = nil -- wipe out previously saved counts - temporary
if v.maxDamage == 0 then if v.maxDamage == 0 then
v.maxDamage = nil v.maxDamage = nil
end end

View File

@@ -13,31 +13,40 @@ local Util = require('util')
local ControllerAdapter = require('controllerAdapter') local ControllerAdapter = require('controllerAdapter')
local InventoryAdapter = require('inventoryAdapter') local InventoryAdapter = require('inventoryAdapter')
local colors = _G.colors
local device = _G.device local device = _G.device
local multishell = _ENV.multishell local multishell = _ENV.multishell
local peripheral = _G.peripheral local peripheral = _G.peripheral
local term = _G.term local term = _G.term
local colors = _G.colors
local turtle = _G.turtle local turtle = _G.turtle
multishell.setTitle(multishell.getCurrent(), 'Resource Manager') multishell.setTitle(multishell.getCurrent(), 'Resource Manager')
-- Config location is /sys/config/chestManager
-- adjust directions in that file if needed
local config = { local config = {
trashDirection = 'up', -- trash /chest in relation to chest trashDirection = 'up', -- trash /chest in relation to chest
inventoryDirection = { direction = 'north', wrapSide = 'back' }, computer = {
chestDirection = { direction = 'down', wrapSide = 'top' }, facing = 'north',
controllerDirection = { direction = 'south', wrapSide = 'right' }, desc = 'Direction computer or turtle is facing',
},
inventory = { wrapSide = 'back',
desc = 'Location of main inventory' },
craftingChest = { wrapSide = 'top',
desc = 'Vanilla chest used for crafting' },
controller = { wrapSide = 'right',
desc = 'optional - AE or refined storage controller - can be same as main inventory' },
} }
Config.load('chestManager', config) Config.loadWithCheck('inventoryManager', config)
local inventoryAdapter = InventoryAdapter.wrap(config.inventoryDirection) local inventoryAdapter = InventoryAdapter.wrap(config.inventory, config.computer)
local turtleChestAdapter = InventoryAdapter.wrap(config.chestDirection) local turtleChestAdapter = InventoryAdapter.wrap(config.craftingChest, config.computer)
local controllerAdapter = ControllerAdapter.wrap(config.controllerDirection) local controllerAdapter = ControllerAdapter.wrap(config.controller, config.computer)
local duckAntenna local duckAntenna
if not inventoryAdapter then
error('Invalid inventory configuration')
end
if device.workbench then if device.workbench then
local oppositeSide = { local oppositeSide = {
[ 'left' ] = 'right', [ 'left' ] = 'right',
@@ -55,7 +64,7 @@ local RESOURCE_FILE = 'usr/config/resources.db'
local RECIPES_FILE = 'usr/config/recipes.db' local RECIPES_FILE = 'usr/config/recipes.db'
local craftingPaused = false local craftingPaused = false
local canCraft = not not duckAntenna or turtleChestAdapter:isValid() local canCraft = not not duckAntenna or turtleChestAdapter
local userRecipes = Util.readTable(RECIPES_FILE) or { } local userRecipes = Util.readTable(RECIPES_FILE) or { }
local jobList local jobList
local resources local resources
@@ -501,8 +510,7 @@ end
local function loadResources() local function loadResources()
resources = Util.readTable(RESOURCE_FILE) or { } resources = Util.readTable(RESOURCE_FILE) or { }
for _,k in pairs(Util.keys(resources)) do for k,v in pairs(resources) do
local v = resources[k]
Util.merge(v, itemDB:splitKey(k)) Util.merge(v, itemDB:splitKey(k))
end end
end end

View File

@@ -1,6 +1,6 @@
_G.requireInjector() _G.requireInjector()
local ChestAdapter = require('chestAdapter18') local InventoryAdapter = require('inventoryAdapter')
local Config = require('config') local Config = require('config')
local Event = require('event') local Event = require('event')
local itemDB = require('itemDB') local itemDB = require('itemDB')
@@ -16,11 +16,15 @@ local RESOURCE_FILE = 'usr/config/levelEmitter.db'
multishell.setTitle(multishell.getCurrent(), 'Level Emitter') multishell.setTitle(multishell.getCurrent(), 'Level Emitter')
local config = { local config = {
inventoryDirection = { direction = 'north', wrapSide = 'back' }, inventorySide = 'bottom',
} }
Config.load('levelEmitter', config) Config.loadWithCheck('levelEmitter', config)
local inventoryAdapter = InventoryAdapter.wrap({ wrapSide = config.inventorySide })
if not inventoryAdapter then
error('No inventory found')
end
local inventoryAdapter = ChestAdapter(config.inventoryDirection)
local resources local resources
local function getItem(items, inItem, ignoreDamage, ignoreNbtHash) local function getItem(items, inItem, ignoreDamage, ignoreNbtHash)

View File

@@ -0,0 +1,44 @@
{
[ "computercraft:turtle_advanced:0" ] = {
ingredients = {
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
[ 9 ] = "minecraft:gold_ingot:0",
[ 10 ] = "minecraft:chest:0",
[ 11 ] = "minecraft:gold_ingot:0",
[ 5 ] = "minecraft:gold_ingot:0",
[ 6 ] = "computercraft:computer:16384",
[ 7 ] = "minecraft:gold_ingot:0",
},
count = 1,
},
[ "computercraft:peripheral:4" ] = {
ingredients = {
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
[ 9 ] = "minecraft:gold_ingot:0",
[ 10 ] = "minecraft:gold_ingot:0",
[ 11 ] = "minecraft:gold_ingot:0",
[ 5 ] = "minecraft:gold_ingot:0",
[ 6 ] = "minecraft:glass_pane:0",
[ 7 ] = "minecraft:gold_ingot:0",
},
count = 4,
},
[ "computercraft:computer:16384" ] = {
ingredients = {
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
[ 9 ] = "minecraft:gold_ingot:0",
[ 10 ] = "minecraft:glass_pane:0",
[ 11 ] = "minecraft:gold_ingot:0",
[ 5 ] = "minecraft:gold_ingot:0",
[ 6 ] = "minecraft:redstone:0",
[ 7 ] = "minecraft:gold_ingot:0",
},
count = 1,
},
}

View File

@@ -1736,20 +1736,6 @@
[ 6 ] = "minecraft:stonebrick:0", [ 6 ] = "minecraft:stonebrick:0",
}, },
}, },
[ "computercraft:peripheral:4" ] = {
ingredients = {
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
[ 9 ] = "minecraft:gold_ingot:0",
[ 10 ] = "minecraft:gold_ingot:0",
[ 11 ] = "minecraft:gold_ingot:0",
[ 5 ] = "minecraft:gold_ingot:0",
[ 6 ] = "minecraft:glass_pane:0",
[ 7 ] = "minecraft:gold_ingot:0",
},
count = 4,
},
[ "minecraft:bone_block:0" ] = { [ "minecraft:bone_block:0" ] = {
count = 1, count = 1,
ingredients = { ingredients = {
@@ -2012,20 +1998,6 @@
[ 6 ] = "minecraft:dye:0", [ 6 ] = "minecraft:dye:0",
}, },
}, },
[ "computercraft:computer:16384" ] = {
ingredients = {
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
[ 9 ] = "minecraft:gold_ingot:0",
[ 10 ] = "minecraft:glass_pane:0",
[ 11 ] = "minecraft:gold_ingot:0",
[ 5 ] = "minecraft:gold_ingot:0",
[ 6 ] = "minecraft:redstone:0",
[ 7 ] = "minecraft:gold_ingot:0",
},
count = 1,
},
[ "minecraft:dark_oak_door:0" ] = { [ "minecraft:dark_oak_door:0" ] = {
count = 3, count = 3,
ingredients = { ingredients = {
@@ -2309,20 +2281,6 @@
[ 7 ] = "minecraft:glass:0", [ 7 ] = "minecraft:glass:0",
}, },
}, },
[ "computercraft:turtle_advanced:0" ] = {
ingredients = {
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
"minecraft:gold_ingot:0",
[ 9 ] = "minecraft:gold_ingot:0",
[ 10 ] = "minecraft:chest:0",
[ 11 ] = "minecraft:gold_ingot:0",
[ 5 ] = "minecraft:gold_ingot:0",
[ 6 ] = "computercraft:computer:16384",
[ 7 ] = "minecraft:gold_ingot:0",
},
count = 1,
},
[ "minecraft:brick_block:0" ] = { [ "minecraft:brick_block:0" ] = {
count = 1, count = 1,
ingredients = { ingredients = {