diff --git a/apps/Turtles.lua b/apps/Turtles.lua index 5eae7ec..f6212b9 100644 --- a/apps/Turtles.lua +++ b/apps/Turtles.lua @@ -296,8 +296,14 @@ if not Util.getOptions(options, { ... }, true) then return end -if options.turtle.value then - page.turtle = _G.network[options.turtle.value] +if options.turtle.value >= 0 then + for i = 1, 10 do + page.turtle = _G.network[options.turtle.value] + if page.turtle then + break + end + os.sleep(1) + end end UI:setPage(page) diff --git a/apps/refinedManager.lua b/apps/refinedManager.lua index 1d53c6d..2bb773c 100644 --- a/apps/refinedManager.lua +++ b/apps/refinedManager.lua @@ -1,79 +1,18 @@ local injector = requireInjector or load(http.get('http://pastebin.com/raw/c0TWsScv').readAll())() require = injector(getfenv(1)) -local Event = require('event') + local UI = require('ui') +local RefinedProvider = require('refinedProvider') +local Terminal = require('terminal') local Peripheral = require('peripheral') -local controller = Peripheral.getByType('refinedstorage:controller') -if not controller then +local controller = RefinedProvider() +if not controller:isValid() then error('Refined storage controller not found') end multishell.setTitle(multishell.getCurrent(), 'Storage Manager') --- refined storage is slooow -local cache = { } - --- Strip off color prefix -local function safeString(text) - - local val = text:byte(1) - - if val < 32 or val > 128 then - - local newText = {} - for i = 4, #text do - local val = text:byte(i) - newText[i - 3] = (val > 31 and val < 127) and val or 63 - end - return string.char(unpack(newText)) - end - - return text -end - -function getItemDetails(item) - local key = table.concat({ item.name, item.damage, item.nbtHash }, ':') - - local detail = cache[key] - if not detail then - detail = controller.findItem(item) - if detail then - Util.merge(detail, detail.getMetadata()) - if detail.displayName then - detail.displayName = safeString(detail.displayName) - if detail.maxDamage and detail.maxDamage > 0 and detail.damage > 0 then - detail.displayName = detail.displayName .. ' (damaged)' - end - detail.lname = detail.displayName:lower() - cache[key] = detail - end - end - end - return detail -end - -function listItems() - local items = { } - local list - - pcall(function() - list = controller.listAvailableItems() - end) - - if list then - for _,v in pairs(list) do - local item = getItemDetails(v) - if item then - item.count = v.count - table.insert(items, item) - end - end - end - - return items -end - function getItem(items, inItem, ignoreDamage) for _,item in pairs(items) do if item.name == inItem.name then @@ -105,6 +44,9 @@ function mergeResources(t) item.low = tonumber(v.low) item.auto = v.auto item.ignoreDamage = v.ignoreDamage + item.rsControl = v.rsControl + item.rsDevice = v.rsDevice + item.rsSide = v.rsSide else v.count = 0 v.limit = tonumber(v.limit) @@ -131,33 +73,15 @@ function filterItems(t, filter) return r end -function getJobList() - local list = { } - - for _,task in pairs(controller.getCraftingTasks()) do - table.insert(list, task.getPattern().outputs[1]) - end - - return list -end - function craftItems(itemList, allItems) for _,item in pairs(itemList) do - - local alreadyCrafting = false - local jobList = getJobList() - - for _,v in pairs(jobList) do - if v.name == item.name and v.damage == item.damage and v.nbtHash == item.nbtHash then - alreadyCrafting = true - end - end - local cItem = getItem(allItems, item) - if alreadyCrafting then + if controller:isCrafting(item) then item.status = '(crafting)' + elseif item.rsControl then + item.status = 'Activated' elseif not cItem then item.status = '(no recipe)' else @@ -248,9 +172,18 @@ function watchResources(items) count = res.low - item.count, name = item.name, displayName = item.displayName, - status = '' + status = '', + rsControl = res.rsControl, }) end + + if res.rsControl and res.rsDevice and res.rsSide then + if item.count < res.low then + pcall(function() device[res.rsDevice].setOutput(res.rsSide, true) end) + else + pcall(function() device[res.rsDevice].setOutput(res.rsSide, false) end) + end + end end return itemList @@ -268,7 +201,7 @@ itemPage = UI.Page { x = 5, y = 3, width = UI.term.width - 10, height = 3, }, form = UI.Form { - x = 4, y = 6, height = 8, rex = -4, + x = 4, y = 5, height = 10, rex = -4, [1] = UI.TextEntry { width = 7, backgroundColor = colors.gray, @@ -295,6 +228,37 @@ itemPage = UI.Page { }, help = 'Ignore damage of item' }, + [4] = UI.Chooser { + width = 7, + formLabel = 'RS Control', formKey = 'rsControl', + nochoice = 'No', + choices = { + { name = 'Yes', value = true }, + { name = 'No', value = false }, + }, + help = 'Control via redstone' + }, + [5] = UI.Chooser { + width = 25, + formLabel = 'RS Device', formKey = 'rsDevice', + nochoice = 'No', + --choices = devices, + help = 'Redstone Device' + }, + [6] = UI.Chooser { + width = 10, + formLabel = 'RS Side', formKey = 'rsSide', + --nochoice = 'No', + choices = { + { name = 'up', value = 'up' }, + { name = 'down', value = 'down' }, + { name = 'east', value = 'east' }, + { name = 'north', value = 'north' }, + { name = 'west', value = 'west' }, + { name = 'south', value = 'south' }, + }, + help = 'Output side' + }, }, statusBar = UI.StatusBar { } } @@ -316,6 +280,14 @@ function itemPage:enable(item) self.titleBar.title = item.name self.displayName.value = item.displayName + local devices = self.form[5].choices + Util.clear(devices) + for _,device in pairs(device) do + if device.setOutput then + table.insert(devices, { name = device.name, value = device.name }) + end + end + UI.Page.enable(self) self:focusFirst() end @@ -337,7 +309,9 @@ function itemPage:eventHandler(event) break end end - local keys = { 'name', 'displayName', 'auto', 'low', 'damage', 'maxDamage', 'nbtHash', 'limit', 'ignoreDamage' } + local keys = { 'name', 'displayName', 'auto', 'low', 'damage', + 'maxDamage', 'nbtHash', 'limit', 'ignoreDamage', + 'rsControl', 'rsDevice', 'rsSide', } local filtered = { } for _,key in pairs(keys) do filtered[key] = values[key] @@ -434,7 +408,7 @@ end function listingPage:eventHandler(event) if event.type == 'quit' then - Event.exitPullEvents() + UI:exitPullEvents() elseif event.type == 'grid_select' then local selected = event.selected @@ -479,7 +453,7 @@ function listingPage:enable() end function listingPage:refresh() - self.allItems = listItems() + self.allItems = controller:listItems() mergeResources(self.allItems) self:applyFilter() end @@ -492,17 +466,6 @@ end local function jobMonitor(jobList) local mon = Peripheral.getByType('monitor') - local nullDevice = { - setCursorPos = function(...) end, - write = function(...) end, - getSize = function() return 13, 20 end, - isColor = function() return false end, - setBackgroundColor = function(...) end, - setTextColor = function(...) end, - clear = function(...) end, - sync = function(...) end, - blit = function(...) end, - } if mon then mon = UI.Device({ @@ -511,7 +474,7 @@ local function jobMonitor(jobList) }) else mon = UI.Device({ - device = nullDevice + device = Terminal.getNullTerm(term.current()) }) end @@ -545,11 +508,11 @@ function craftingThread() while true do os.sleep(5) - pcall(function() + --pcall(function() - local items = listItems() + local items = controller:listItems() - if controller.getNetworkEnergyStored() == 0 then + if not controller:isOnline() then jobListGrid.parent:clear() jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'Power failure') jobListGrid:sync() @@ -572,11 +535,11 @@ function craftingThread() itemList = getAutocraftItems(items) -- autocrafted items don't show on job monitor craftItems(itemList, items) end - end) + --end) end end -Event.pullEvents(craftingThread) +UI:pullEvents(craftingThread) UI.term:reset() jobListGrid.parent:reset() diff --git a/apps/simpleMiner.lua b/apps/simpleMiner.lua index 8f8457d..01ce262 100644 --- a/apps/simpleMiner.lua +++ b/apps/simpleMiner.lua @@ -369,6 +369,7 @@ function fortuneDig(action, blockName) repeat until not turtle.dig() turtle.selectSlot('minecraft:diamond_pickaxe') turtle.equipRight() + turtle.select(1) return true end end diff --git a/sys/apis/refinedProvider.lua b/sys/apis/refinedProvider.lua index 77b867f..96fa373 100644 --- a/sys/apis/refinedProvider.lua +++ b/sys/apis/refinedProvider.lua @@ -13,11 +13,18 @@ function RefinedProvider:init(args) Util.merge(self, defaults) Util.merge(self, args) - self.controller = Peripheral.getByType('refinedstorage:controller') + local controller = Peripheral.getByType('refinedstorage:controller') + if controller then + Util.merge(self, controller) + end end function RefinedProvider:isValid() - return not not self.controller + return not not self.listAvailableItems +end + +function RefinedProvider:isOnline() + return self.getNetworkEnergyStored() > 0 end function RefinedProvider:getCachedItemDetails(item) @@ -25,7 +32,7 @@ function RefinedProvider:getCachedItemDetails(item) local detail = self.cache[key] if not detail then - detail = self.controller.findItem(item) + detail = self.findItem(item) if detail then Util.merge(detail, detail.getMetadata()) if detail.displayName then @@ -52,7 +59,7 @@ function RefinedProvider:listItems() local list pcall(function() - list = self.controller.listAvailableItems() + list = self.listAvailableItems() end) if list then @@ -78,14 +85,25 @@ function RefinedProvider:getItemInfo(fingerprint) return self:getCachedItemDetails(fingerprint) end - local detail = self.controller.findItem(item) + local detail = self.findItem(item) if detail then item.count = detail.count item.qty = detail.count return item end end - + +function RefinedProvider:isCrafting(item) + for _,task in pairs(self.getCraftingTasks()) do + if task.name == item.name and + task.damage == item.damage and + task.nbtHash == item.nbtHash then + return true + end + end + return false +end + function RefinedProvider:craft(id, dmg, qty) return false end @@ -98,11 +116,11 @@ function RefinedProvider:provide(item, qty, slot) end function RefinedProvider:extract(slot, qty) --- self.controller.pushItems(self.direction, slot, qty) +-- self.pushItems(self.direction, slot, qty) end function RefinedProvider:insert(slot, qty) --- self.controller.pullItems(self.direction, slot, qty) +-- self.pullItems(self.direction, slot, qty) end return RefinedProvider