From c48243eae5a6371893acdad3a0bc5a4a4cf0f115 Mon Sep 17 00:00:00 2001 From: kepler155c Date: Thu, 4 Jan 2018 03:33:09 -0500 Subject: [PATCH] item db naming fix for items with diff names-nbt combinations --- apis/chestAdapter18.lua | 52 +++++++++-------------------------- apis/inventoryAdapter.lua | 35 +++++++++++++++++++++++- apis/itemDB.lua | 53 ++++++++++++++++++------------------ apps/chestManager.lua | 36 ++++++++++++++---------- apps/levelEmitter.lua | 12 +++++--- etc/recipes/computercraft.db | 44 ++++++++++++++++++++++++++++++ etc/recipes/minecraft.db | 42 ---------------------------- 7 files changed, 147 insertions(+), 127 deletions(-) create mode 100644 etc/recipes/computercraft.db diff --git a/apis/chestAdapter18.lua b/apis/chestAdapter18.lua index 6bda5e3..64200dc 100644 --- a/apis/chestAdapter18.lua +++ b/apis/chestAdapter18.lua @@ -5,19 +5,10 @@ local Peripheral = require('peripheral') local ChestAdapter = class() -local keys = Util.transpose({ - 'damage', - 'displayName', - 'maxCount', - 'maxDamage', - 'name', - 'nbtHash', -}) - function ChestAdapter:init(args) local defaults = { name = 'chest', - direction = 'up', + direction = 'down', wrapSide = 'bottom', } Util.merge(self, defaults) @@ -35,13 +26,6 @@ function ChestAdapter:init(args) if chest then Util.merge(self, chest) - - local sides = { - top = 'down', - bottom = 'up', - } - - self.direction = sides[self.side] or self.direction end end @@ -50,30 +34,19 @@ function ChestAdapter:isValid() end function ChestAdapter:getCachedItemDetails(item, k) - local detail = itemDB:get(item) - if not detail then - local s, m = pcall(function() detail = self.getItemMeta(k) end) - 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) + local cached = itemDB:get(item, true) + if cached then + return cached 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 + + return itemDB:add(item, detail) end function ChestAdapter:refresh(throttle) @@ -96,6 +69,7 @@ function ChestAdapter:listItems(throttle) if not entry then return -- Inventory has changed end + entry = Util.shallowCopy(entry) entry.count = 0 cache[key] = entry table.insert(items, entry) diff --git a/apis/inventoryAdapter.lua b/apis/inventoryAdapter.lua index 4e29686..b2a1d59 100644 --- a/apis/inventoryAdapter.lua +++ b/apis/inventoryAdapter.lua @@ -1,6 +1,8 @@ +local Util = require('util') + local Adapter = { } -function Adapter.wrap(args) +function Adapter.wrap(args, computerInfo) local adapters = { --'refinedAdapter', --'meAdapter', @@ -8,6 +10,37 @@ function Adapter.wrap(args) '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 local adapter = require(adapterType)(args) diff --git a/apis/itemDB.lua b/apis/itemDB.lua index a95ded2..40259e2 100644 --- a/apis/itemDB.lua +++ b/apis/itemDB.lua @@ -5,7 +5,6 @@ local Util = require('util') local itemDB = TableDB({ fileName = 'usr/config/items.db' }) local function safeString(text) - local val = text:byte(1) if val < 32 or val > 128 then @@ -32,7 +31,7 @@ local function safeString(text) end local function makeKey(item) - return { item.name, item.damage or '*', item.nbtHash } + return table.concat({ item.name, item.damage or '*', item.nbtHash }, ':') end function itemDB:splitKey(key, item) @@ -51,8 +50,7 @@ function itemDB:splitKey(key, item) return item end -function itemDB:get(key) - +function itemDB:get(key, enforceNBT) if type(key) == 'string' then key = self:splitKey(key) end @@ -75,7 +73,7 @@ function itemDB:get(key) end end - if key.nbtHash then + if key.nbtHash and not enforceNBT then item = self:get({ name = key.name, damage = key.damage }) if item and (item.maxDamage > 0 or item.damage == key.damage) then item = Util.shallowCopy(item) @@ -84,6 +82,7 @@ function itemDB:get(key) 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 @@ -98,31 +97,30 @@ function itemDB:get(key) 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 -function itemDB:add(item) - local key = makeKey(item) - if item.maxDamage > 0 then - key = makeKey({ name = item.name, damage = '*', nbtHash = item.nbtHash }) +--[[ + 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 - 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 -- Accepts: "minecraft:stick:0" or { name = 'minecraft:stick', damage = 0 } @@ -168,6 +166,7 @@ function itemDB:flush() v.name = nil v.damage = nil v.nbtHash = nil +v.count = nil -- wipe out previously saved counts - temporary if v.maxDamage == 0 then v.maxDamage = nil end diff --git a/apps/chestManager.lua b/apps/chestManager.lua index 9b083a0..41494fa 100644 --- a/apps/chestManager.lua +++ b/apps/chestManager.lua @@ -13,31 +13,40 @@ local Util = require('util') local ControllerAdapter = require('controllerAdapter') local InventoryAdapter = require('inventoryAdapter') +local colors = _G.colors local device = _G.device local multishell = _ENV.multishell local peripheral = _G.peripheral local term = _G.term -local colors = _G.colors local turtle = _G.turtle multishell.setTitle(multishell.getCurrent(), 'Resource Manager') --- Config location is /sys/config/chestManager --- adjust directions in that file if needed local config = { - trashDirection = 'up', -- trash /chest in relation to chest - inventoryDirection = { direction = 'north', wrapSide = 'back' }, - chestDirection = { direction = 'down', wrapSide = 'top' }, - controllerDirection = { direction = 'south', wrapSide = 'right' }, + trashDirection = 'up', -- trash /chest in relation to chest + computer = { + facing = 'north', + 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 turtleChestAdapter = InventoryAdapter.wrap(config.chestDirection) -local controllerAdapter = ControllerAdapter.wrap(config.controllerDirection) +local inventoryAdapter = InventoryAdapter.wrap(config.inventory, config.computer) +local turtleChestAdapter = InventoryAdapter.wrap(config.craftingChest, config.computer) +local controllerAdapter = ControllerAdapter.wrap(config.controller, config.computer) local duckAntenna +if not inventoryAdapter then + error('Invalid inventory configuration') +end + if device.workbench then local oppositeSide = { [ 'left' ] = 'right', @@ -55,7 +64,7 @@ local RESOURCE_FILE = 'usr/config/resources.db' local RECIPES_FILE = 'usr/config/recipes.db' 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 jobList local resources @@ -501,8 +510,7 @@ end local function loadResources() resources = Util.readTable(RESOURCE_FILE) or { } - for _,k in pairs(Util.keys(resources)) do - local v = resources[k] + for k,v in pairs(resources) do Util.merge(v, itemDB:splitKey(k)) end end diff --git a/apps/levelEmitter.lua b/apps/levelEmitter.lua index 7fd4cb2..424a6bd 100644 --- a/apps/levelEmitter.lua +++ b/apps/levelEmitter.lua @@ -1,6 +1,6 @@ _G.requireInjector() -local ChestAdapter = require('chestAdapter18') +local InventoryAdapter = require('inventoryAdapter') local Config = require('config') local Event = require('event') local itemDB = require('itemDB') @@ -16,11 +16,15 @@ local RESOURCE_FILE = 'usr/config/levelEmitter.db' multishell.setTitle(multishell.getCurrent(), 'Level Emitter') 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 function getItem(items, inItem, ignoreDamage, ignoreNbtHash) diff --git a/etc/recipes/computercraft.db b/etc/recipes/computercraft.db new file mode 100644 index 0000000..f792d85 --- /dev/null +++ b/etc/recipes/computercraft.db @@ -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, + }, +} \ No newline at end of file diff --git a/etc/recipes/minecraft.db b/etc/recipes/minecraft.db index 0297ba1..5952192 100644 --- a/etc/recipes/minecraft.db +++ b/etc/recipes/minecraft.db @@ -1736,20 +1736,6 @@ [ 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" ] = { count = 1, ingredients = { @@ -2012,20 +1998,6 @@ [ 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" ] = { count = 3, ingredients = { @@ -2309,20 +2281,6 @@ [ 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" ] = { count = 1, ingredients = {