From 9cd480f1884716176338beba187a372f62344932 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Mon, 31 Dec 2018 11:39:24 -0500 Subject: [PATCH] milo: item rewrite + ability to remove nbts from recipes --- milo/apis/craft2.lua | 39 ++++- milo/apis/milo.lua | 11 ++ milo/plugins/demandCraft.lua | 7 +- milo/plugins/item.lua | 331 ++++++++++++++++------------------- milo/plugins/turtleLearn.lua | 32 +--- 5 files changed, 209 insertions(+), 211 deletions(-) diff --git a/milo/apis/craft2.lua b/milo/apis/craft2.lua index 4c74ce1..096616d 100644 --- a/milo/apis/craft2.lua +++ b/milo/apis/craft2.lua @@ -131,16 +131,28 @@ local function turtleCraft(recipe, storage, request, count) if storage:export(storage.turtleInventory, k, count, item) ~= count then request.status = 'unknown error' request.statusCode = Craft.STATUS_ERROR +_debug(item) return end end turtle.select(1) if turtle.craft() then - request.crafted = request.crafted + count * recipe.count - request.status = 'crafted' - request.statusCode = Craft.STATUS_SUCCESS + local l = storage.turtleInventory.adapter.list() + local crafted = l[1] + if recipe.result ~= itemDB:makeKey(crafted) then + _debug('expected: ' .. recipe.result) + _debug('got: ' .. itemDB:makeKey(crafted)) + request.aborted = true + request.status = 'Failed to craft: ' .. recipe.result + request.statusCode = Craft.STATUS_ERROR + else + request.crafted = request.crafted + count * recipe.count + request.status = 'crafted' + request.statusCode = Craft.STATUS_SUCCESS + end else + _debug('just failed') request.status = 'Failed to craft' request.statusCode = Craft.STATUS_ERROR end @@ -174,20 +186,35 @@ function Craft.craftRecipe(recipe, count, storage, origItem) return Craft.craftRecipeInternal(recipe, count, storage, origItem) end -local function adjustCounts(recipe, count, ingredients) +local function adjustCounts(recipe, count, ingredients, storage) -- decrement ingredients used for key,icount in pairs(Craft.sumIngredients(recipe)) do ingredients[key].count = ingredients[key].count - (icount * count) + if ingredients[key].count == 0 and not storage.cache[key] then + -- + elseif not storage.cache[key] then + _debug({ key, ingredients[key].count, 'nocache' }) + elseif storage.cache[key].count ~= ingredients[key].count then + _debug({ key, ingredients[key].count, storage.cache[key].count }) + + end end -- increment crafted local result = ingredients[recipe.result] result.count = result.count + (count * recipe.count) + + end function Craft.craftRecipeInternal(recipe, count, storage, origItem) local request = origItem.ingredients[recipe.result] + if request.aborted then +_debug('aborted') + return 0 + end + if origItem.pending[recipe.result] then request.status = 'processing' request.statusCode = Craft.STATUS_INFO @@ -234,7 +261,7 @@ function Craft.craftRecipeInternal(recipe, count, storage, origItem) end end end - +_G._p = origItem.ingredients local crafted = 0 while canCraft > 0 do local batch = math.min(canCraft, maxCount) @@ -248,7 +275,7 @@ function Craft.craftRecipeInternal(recipe, count, storage, origItem) break end - adjustCounts(recipe, batch, origItem.ingredients) + adjustCounts(recipe, batch, origItem.ingredients, storage) crafted = crafted + batch canCraft = canCraft - maxCount diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index b69e72c..78fbf8e 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -234,6 +234,17 @@ function Milo:eject(item, count) return count end +function Milo:updateRecipe(result, recipe) + -- save the recipe + if recipe then + recipe = Util.shallowCopy(recipe) + recipe.result = nil + end + self.context.userRecipes[result] = recipe + Util.writeTable(Craft.USER_RECIPES, self.context.userRecipes) + Craft.loadRecipes() +end + function Milo:saveMachineRecipe(recipe, result, machine) local key = Milo:uniqueKey(result) diff --git a/milo/plugins/demandCraft.lua b/milo/plugins/demandCraft.lua index 31831a8..c546f2d 100644 --- a/milo/plugins/demandCraft.lua +++ b/milo/plugins/demandCraft.lua @@ -26,9 +26,14 @@ local craftPage = UI.Page { x = 6, y = 4, value = 'Eject', }, - eject = UI.Checkbox { + eject = UI.Chooser { x = 15, y = 4, width = 7, value = true, + nochoice = 'No', + choices = { + { name = 'Yes', value = true }, + { name = 'No', value = false }, + }, }, }, resources = UI.Window { diff --git a/milo/plugins/item.lua b/milo/plugins/item.lua index a72fb9c..40861e1 100644 --- a/milo/plugins/item.lua +++ b/milo/plugins/item.lua @@ -6,18 +6,14 @@ local UI = require('ui') local Util = require('util') local colors = _G.colors -local device = _G.device local context = Milo:getContext() -local page = UI.Page { - titleBar = UI.TitleBar { - title = 'Limit Resource', - previousPage = true, - event = 'form_cancel', - }, +local manageTab = UI.Window { + tabTitle = 'Manage', form = UI.Form { - x = 1, y = 2, height = 10, ex = -1, + x = 1, ex = -1, ey = -1, + --manualControls = true, [1] = UI.TextEntry { formLabel = 'Name', formKey = 'displayName', help = 'Override display name', shadowText = 'Display name', @@ -42,100 +38,80 @@ local page = UI.Page { formLabel = 'Ignore NBT', formKey = 'ignoreNbtHash', help = 'Ignore NBT of item', }, - machineButton = UI.Button { - x = 2, y = -2, width = 10, - formLabel = 'Machine', - event = 'select_machine', - text = 'Assign', - }, - infoButton = UI.Button { - x = 2, y = -2, - event = 'show_info', - text = 'Info', - }, - resetButton = UI.Button { - x = 9, y = -2, - event = 'reset', - text = 'Reset', - help = 'Clear recipe and all settings', - }, }, - rsControl = UI.SlideOut { - backgroundColor = colors.cyan, - titleBar = UI.TitleBar { - title = "Redstone Control", - }, - form = UI.Form { - y = 2, - [1] = UI.Chooser { - width = 7, - formLabel = 'RS Control', formKey = 'rsControl', - nochoice = 'No', - choices = { - { name = 'Yes', value = true }, - { name = 'No', value = false }, - }, - help = 'Control via redstone' - }, - [2] = UI.Chooser { - width = 25, - formLabel = 'RS Device', formKey = 'rsDevice', - --choices = devices, - help = 'Redstone Device' - }, - [3] = 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' - }, +} + +local machinesTab = UI.Window { + tabTitle = 'Machine', + backgroundColor = colors.cyan, + grid = UI.ScrollingGrid { + x = 2, ex = -2, y = 2, ey = -2, + disableHeader = true, + columns = { + { heading = 'Name', key = 'displayName'}, }, + sortColumn = 'displayName', + help = 'Double-click to set machine', }, - machines = UI.SlideOut { - backgroundColor = colors.cyan, - titleBar = UI.TitleBar { - title = 'Select Machine', - event = 'cancel_machine', +} + +local recipeTab = UI.Window { + tabTitle = 'Recipe', + backgroundColor = colors.cyan, + grid = UI.ScrollingGrid { + x = 2, ex = -2, y = 2, ey = -4, + disableHeader = true, + columns = { + { heading = 'Slot', key = 'slot', width = 2 }, + { heading = 'Key', key = 'key' }, }, - grid = UI.ScrollingGrid { - y = 2, ey = -5, - disableHeader = true, - columns = { - { heading = 'Name', key = 'displayName'}, - }, - sortColumn = 'displayName', - }, - button1 = UI.Button { - x = -14, y = -3, - text = 'Ok', event = 'set_machine', - }, - button2 = UI.Button { - x = -9, y = -3, - text = 'Cancel', event = 'cancel_machine', - }, - statusBar = UI.StatusBar { values = 'Enter or double click to select' }, + sortColumn = 'slot', }, - info = UI.SlideOut { - titleBar = UI.TitleBar { - title = "Information", - }, - textArea = UI.TextArea { - x = 2, ex = -2, y = 3, ey = -4, - backgroundColor = colors.black, - }, - cancel = UI.Button { - ex = -2, y = -2, width = 6, - text = 'Okay', - event = 'hide_info', - }, + ignoreNBT = UI.Button { + x = -13, y = -2, + text = 'Ignore NBT', event = 'ignore_nbt', + }, +} + +local infoTab = UI.Window { + tabTitle = 'Info', + backgroundColor = colors.cyan, + textArea = UI.TextArea { + x = 2, ex = -2, y = 2, + }, +} + +local resetTab = UI.Window { + tabTitle = 'Reset', + backgroundColor = colors.cyan, + textArea = UI.TextArea { + y = 2, ey = 6, + textColor = colors.yellow, + value = [[ Warning! + + This will clear all setting, + recipe, and machine for this item.]] + }, + resetButton = UI.Button { + x = 17, y = 7, + event = 'reset', + text = 'Reset', + help = 'Clear recipe and all settings', + }, +} + +local page = UI.Page { + titleBar = UI.TitleBar { + title = 'Item settings', + previousPage = true, + }, + tabs = UI.Tabs { + y = 2, ey = -2, + [1] = manageTab, + [2] = recipeTab, + [3] = machinesTab, + [4] = infoTab, + [5] = resetTab, }, statusBar = UI.StatusBar { }, notification = UI.Notification { }, @@ -146,17 +122,19 @@ function page:enable(item) self.item = Util.shallowCopy(item) self.res = item.resource or { } self.res.displayName = self.item.displayName - self.form:setValues(self.res) - self.titleBar.title = item.displayName or item.name + manageTab.form:setValues(self.res) local machine = Craft.machineLookup[self.item.key] - self.form.machineButton.inactive = not machine if machine then self:filterMachines(machine) end + self.tabs:selectTab(manageTab) + + self.tabs:setActive(machinesTab, machine) + self.tabs:setActive(recipeTab, Craft.findRecipe(item)) + UI.Page.enable(self) - self:focusFirst() end function page:filterMachines(machine) @@ -165,65 +143,105 @@ function page:filterMachines(machine) return node.adapter and node.adapter.online and node.adapter.pushItems end end) - self.machines.grid:setValues(t) - self.machines.grid:setSelected('name', machine) + machinesTab.grid:setValues(t) + machinesTab.grid:setSelected('name', machine) end -function page.machines.grid:getDisplayValues(row) +function machinesTab.grid:getDisplayValues(row) row = Util.shallowCopy(row) row.displayName = row.displayName or row.name return row end -function page.machines.grid:getRowTextColor(row, selected) +function machinesTab.grid:getRowTextColor(row, selected) if row.name == Craft.machineLookup[page.item.key] then return colors.yellow end return UI.Grid:getRowTextColor(row, selected) end -function page.rsControl:enable() - local devices = self.form[2].choices - Util.clear(devices) - for _,dev in pairs(device) do - if dev.setOutput then - table.insert(devices, { name = dev.name, value = dev.name }) - end - end +function machinesTab:eventHandler(event) + if event.type == 'grid_select' then + Craft.machineLookup[page.item.key] = event.selected.name + Util.writeTable(Craft.MACHINE_LOOKUP, Craft.machineLookup) - if Util.size(devices) == 0 then - table.insert(devices, { name = 'None found', values = '' }) - end + self.grid:draw() + page.notification:info('Machine saved') - UI.SlideOut.enable(self) + return true + end end -function page.rsControl:eventHandler(event) - if event.type == 'form_cancel' then - self:hide() - elseif event.type == 'form_complete' then - self:hide() - else - return UI.SlideOut.eventHandler(self, event) +function recipeTab:enable() + self.recipe = Craft.findRecipe(page.item) + + local t = { } + if self.recipe then + for k, v in pairs(self.recipe.ingredients) do + table.insert(t, { + slot = k, + key = v, + }) + end end - return true + self.grid:setValues(t) + UI.Window.enable(self) +end + +function recipeTab:eventHandler(event) + if event.type == 'ignore_nbt' then + local selected = self.grid:getSelected() + local item = itemDB:splitKey(selected.key) + item.nbtHash = nil + selected.key = itemDB:makeKey(item) + self.grid:draw() + + self.recipe.ingredients = { } + for _, v in pairs(self.grid.values) do + self.recipe.ingredients[v.slot] = v.key + end + + Milo:updateRecipe(self.recipe.result, self.recipe) + page.notification:info('Recipe updated') + + return true + end +end + +function infoTab:draw() + local item = page.item + local value = + string.format('%s%s%s\n%s\n', + Ansi.orange, item.displayName, Ansi.reset, + item.name) + + if item.nbtHash then + value = value .. item.nbtHash .. '\n' + end + + value = value .. string.format('\n%sDamage:%s %s', + Ansi.yellow, Ansi.reset, item.damage) + + if item.maxDamage and item.maxDamage > 0 then + value = value .. string.format(' (max: %s)', item.maxDamage) + end + + if item.maxCount then + value = value .. string.format('\n%sStack Size: %s%s', + Ansi.yellow, Ansi.reset, item.maxCount) + end + + self.textArea.value = value + UI.Window.draw(self) end function page:eventHandler(event) if event.type == 'form_cancel' then UI:setPreviousPage() - elseif event.type == 'show_rs' then - self.rsControl:show() - - elseif event.type == 'select_machine' then - self.machines:show() - elseif event.type == 'reset' then if context.userRecipes[self.item.key] then - context.userRecipes[self.item.key] = nil - Util.writeTable(Craft.USER_RECIPES, context.userRecipes) - Craft.loadRecipes() + Milo:updateRecipe(self.item.key, nil) end if context.resources[self.item.key] then @@ -238,47 +256,8 @@ function page:eventHandler(event) UI:setPreviousPage() - elseif event.type == 'grid_select' then - Craft.machineLookup[self.item.key] = event.selected.name - self.machines.grid:draw() - - elseif event.type == 'set_machine' then - local machine = self.machines.grid:getSelected() - if machine then - Util.writeTable(Craft.MACHINE_LOOKUP, Craft.machineLookup) - end - self.machines:hide() - - elseif event.type == 'cancel_machine' then - self.machines:hide() - - elseif event.type == 'show_info' then - local value = - string.format('%s%s%s\n%s\n', - Ansi.orange, self.item.displayName, Ansi.reset, - self.item.name) - - if self.item.nbtHash then - value = value .. self.item.nbtHash .. '\n' - end - - value = value .. string.format('\n%sDamage:%s %s', - Ansi.yellow, Ansi.reset, self.item.damage) - - if self.item.maxDamage and self.item.maxDamage > 0 then - value = value .. string.format(' (max: %s)', self.item.maxDamage) - end - - if self.item.maxCount then - value = value .. string.format('\n%sStack Size: %s%s', - Ansi.yellow, Ansi.reset, self.item.maxCount) - end - - self.info.textArea.value = value - self.info:show() - - elseif event.type == 'hide_info' then - self.info:hide() + elseif event.type == 'tab_activate' then + event.activated:focusFirst() elseif event.type == 'form_invalid' then self.notification:error(event.message) @@ -290,7 +269,7 @@ function page:eventHandler(event) elseif event.type == 'form_complete' then local item = self.item - if self.form:save() then + if manageTab.form:save() then if self.res.displayName ~= self.origItem.displayName then self.origItem.displayName = self.res.displayName itemDB:add(self.origItem) diff --git a/milo/plugins/turtleLearn.lua b/milo/plugins/turtleLearn.lua index 74476a0..b5c4ba2 100644 --- a/milo/plugins/turtleLearn.lua +++ b/milo/plugins/turtleLearn.lua @@ -9,7 +9,7 @@ local turtle = _G.turtle local context = Milo:getContext() -local function learnRecipe(ignoreNBT) +local function learnRecipe() local ingredients = Milo:getTurtleInventory() if not ingredients then @@ -83,10 +83,6 @@ local function learnRecipe(ignoreNBT) newRecipe.count = recipe.count - if ignoreNBT then - recipe.nbtHash = nil - end - local key = Milo:uniqueKey(recipe) if recipe.maxCount ~= 64 then newRecipe.maxCount = recipe.maxCount @@ -95,16 +91,10 @@ local function learnRecipe(ignoreNBT) if ingredient.maxDamage > 0 then -- ingredient.damage = '*' -- I don't think this is right end - if ignoreNBT then - ingredient.nbtHash = nil - end ingredients[k] = Milo:uniqueKey(ingredient) end - context.userRecipes[key] = newRecipe - Util.writeTable(Craft.USER_RECIPES, context.userRecipes) - Craft.loadRecipes() - + Milo:updateRecipe(key, newRecipe) turtle.emptyInventory() return recipe @@ -115,29 +105,15 @@ local pages = { index = 2, validFor = 'Turtle Crafting', notice = UI.Text { - x = 2, y = 2, + x = 3, y = 2, textColor = colors.yellow, value = 'Place recipe in turtle', }, - ignoreNBT = UI.Checkbox { - x = 3, y = 4, - help = 'Ignore damage of item', - value = true, - }, - text = UI.Text { - x = 7, y = 4, - value = 'Ignore NBT (recommended)', - }, - ignoreInfo = UI.TextArea { - x = 2, ex = -2, y = 6, ey = -2, - textColor = colors.yellow, - value = 'Some items contain unique NBT information. This information can be ignored for most items.', - }, }, } function pages.turtleCraft:validate() - local recipe, msg = learnRecipe(self, self.ignoreNBT.value) + local recipe, msg = learnRecipe() if recipe then local displayName = itemDB:getName(recipe)