diff --git a/apis/chestAdapter.lua b/apis/chestAdapter.lua index 26241b1..7511980 100644 --- a/apis/chestAdapter.lua +++ b/apis/chestAdapter.lua @@ -3,6 +3,8 @@ local itemDB = require('itemDB') local Peripheral = require('peripheral') local Util = require('util') +local os = _G.os + local ChestAdapter = class() local convertNames = { @@ -13,14 +15,6 @@ local convertNames = { displayName = 'display_name', maxDamage = 'max_dmg', } -local keys = { - 'damage', - 'displayName', - 'maxCount', - 'maxDamage', - 'name', - 'nbtHash', -} -- Strip off color prefix local function safeString(text) @@ -50,19 +44,27 @@ end function ChestAdapter:init(args) local defaults = { - name = 'chest', - direction = 'up', - wrapSide = 'bottom', + name = 'chest', } Util.merge(self, defaults) Util.merge(self, args) - local chest = Peripheral.getBySide(self.wrapSide) - if not chest then + local chest + if not self.side then chest = Peripheral.getByMethod('getAllStacks') + else + chest = Peripheral.getBySide(self.side) + if chest and not chest.getAllStacks then + chest = nil + end end + if chest then Util.merge(self, chest) + + if chest.listAvailableItems then + self.list = chest.listAvailableItems + end end end @@ -75,30 +77,31 @@ function ChestAdapter:refresh(throttle) end -- provide a consolidated list of items -function ChestAdapter:listItems(throttle) - self.cache = { } +function ChestAdapter:listItems() + local cache = { } + local items = { } for _,v in pairs(self.getAllStacks(false)) do convertItem(v) local key = table.concat({ v.name, v.damage, v.nbtHash }, ':') - local entry = self.cache[key] + local entry = cache[key] if not entry then - self.cache[key] = v + cache[key] = v if not itemDB:get(v) then - local t = { } - for _,k in pairs(keys) do - t[k] = v[k] - end - itemDB:add(t) + itemDB:add(v) end + table.insert(items, v) else entry.count = entry.count + v.count end end itemDB:flush() - return self.cache + if not Util.empty(items) then + self.cache = cache + return items + end end function ChestAdapter:getItemInfo(item) @@ -139,11 +142,12 @@ function ChestAdapter:extract(slot, qty, toSlot) end end -function ChestAdapter:insert(slot, qty) - local s, m = pcall(function() self.pullItem(self.direction, slot, qty) end) +function ChestAdapter:insert(slot, qty, toSlot) + -- toSlot not tested ... + local s, m = pcall(self.pullItem, self.direction, slot, qty, toSlot) if not s and m then os.sleep(1) - pcall(function() self.pullItem(self.direction, slot, qty) end) + pcall(self.pullItem, self.direction, slot, qty, toSlot) end end diff --git a/apps/chestManager.lua b/apps/chestManager.lua index cc8ed4a..52060b9 100644 --- a/apps/chestManager.lua +++ b/apps/chestManager.lua @@ -67,7 +67,7 @@ local RECIPES_FILE = 'usr/config/recipes.db' local craftingPaused = false local canCraft = not not (turtle and turtle.craft) -local canLearn = not not duckAntenna or not not turtleChestAdapter +local canLearn = not not (canCraft and (duckAntenna or turtleChestAdapter)) local userRecipes = Util.readTable(RECIPES_FILE) or { } local jobList local resources diff --git a/apps/persist.lua b/apps/persist.lua index 13eb2f1..3df928c 100644 --- a/apps/persist.lua +++ b/apps/persist.lua @@ -4,12 +4,19 @@ _G.requireInjector(_ENV) local Util = require('util') +local fs = _G.fs local shell = _ENV.shell local args = { ... } -local fileName = args[1] or error('Syntax: persist ') +local fileName = args[1] and + shell.resolve(args[1]) or + error('Syntax: persist ') -local c = Util.readFile(shell.resolve(fileName)) or error('Unable to open file') -Util.writeFile(shell.resolve(fileName), c) +local c = Util.readFile() or error('Unable to read file') + +-- ensure it is writable - if not an error is thrown +Util.writeFile(fileName, '') +fs.delete(fileName) +Util.writeFile(fileName, c) print('Saved')