diff --git a/lib/recipe_handler.lua b/lib/recipe_handler.lua index 6b185ac..c98c279 100644 --- a/lib/recipe_handler.lua +++ b/lib/recipe_handler.lua @@ -909,13 +909,30 @@ end ---@param machine integer? The machine used to craft the item. Defaults to "crafting table". ---@param is_fluid boolean? Whether or not the item is a fluid. Defaults to false. ---@param is_preferred boolean? Whether or not the recipe is preferred. Defaults to false. +---@param previous_name string? The previous name of the item, if editing an existing recipe. ---@return Recipe recipe The recipe created. -function RecipeHandler.create_recipe_object(item_name, output_count, ingredients, machine, is_fluid, is_preferred) +function RecipeHandler.create_recipe_object(item_name, output_count, ingredients, machine, is_fluid, is_preferred, previous_name) -- Check items_common for the item id. If it doesn't exist, add it. local item_id = items_common.get_item_id(item_name) + + if previous_name and not item_id then + -- The item id doesn't exist, but we have a previous name. This means the + -- item was renamed, so we need to get the id from the previous name. + item_id = items_common.get_item_id(previous_name) + if not item_id then + -- The previous name doesn't exist, this is an error. + error(("Previous name %s does not exist."):format(previous_name), 2) + end + + -- We need to update the item name in items_common. + items_common.edit_item(item_id, item_name) + elseif not previous_name and not item_id then + -- The item id doesn't exist, and we don't have a previous name. This means + -- the item is new, so we need to add it. item_id = items_common.add_item(item_name) end + ---@cast item_id integer It can no longer be nil after the above. ---@type Recipe return { diff --git a/ui/items/common.lua b/ui/items/common.lua index 0373336..173c3d3 100644 --- a/ui/items/common.lua +++ b/ui/items/common.lua @@ -43,13 +43,24 @@ function common.add_item(name) local item = {name = name, id = id} common.item_lookup[id] = item + + common.save() return id end +--- Edit an item in the list of items. +---@param id integer The unique id of the item. +---@param name string The new name of the item. +function common.edit_item(id, name) + common.item_lookup[id].name = name + common.save() +end + --- Remove an item from the list of items. ---@param id integer The unique id of the item. function common.remove_item(id) common.item_lookup[id] = nil + common.save() end --- Get the list of items. diff --git a/ui/items/edit.lua b/ui/items/edit.lua index e637de4..f8097b2 100644 --- a/ui/items/edit.lua +++ b/ui/items/edit.lua @@ -1,10 +1,10 @@ local recipe_handler = require "recipe_handler" +local items_common = require "ui.items.common" local get_item_details = require "ui.items.get_item_details" local catch_error = require "ui.util.catch_error" local search = require "ui.util.search" local good_response = require "ui.util.good_response" -local items_common = require "ui.items.common" --- Edit item menu -> Search for an item by name, then edit its recipe. ---@param run_menu fun(name: string) The function to run another menu @@ -53,7 +53,7 @@ return function(run_menu) end -- Get the new recipe data - local ok, new_recipe_data = catch_error(get_item_details, recipe_data, recipe_data.result.name) + local ok, new_recipe_data = catch_error(get_item_details, recipe_data, item_name) if not ok or not new_recipe_data then return @@ -62,6 +62,8 @@ return function(run_menu) recipe_handler.edit_recipe(recipe_id, new_recipe_data) recipe_handler.save() - good_response("Item edited", ("Edited item %s (outputs %d)."):format(new_recipe_data.result.name, new_recipe_data.result.amount)) + local new_item_name = items_common.get_item_name(new_recipe_data.result.id) + + good_response("Item edited", ("Edited item %s (outputs %d)."):format(new_item_name, new_recipe_data.result.amount)) end end diff --git a/ui/items/get_item_details.lua b/ui/items/get_item_details.lua index 120339d..f4120ea 100644 --- a/ui/items/get_item_details.lua +++ b/ui/items/get_item_details.lua @@ -42,7 +42,7 @@ local recipe_handler = require "recipe_handler" --- Get information about an item. ---@param item_data Recipe? The item data to edit, if any. ----@param default_item_search_name string? The default item name to search for. +---@param default_item_search_name string? The default item name to search for. If this is supplied, it will be passed as the previous name to create_recipe_object (i.e: This argument assumes that we are editing a recipe, not creating a new one.) ---@return Recipe? recipe The new recipe for the item. return function(item_data, default_item_search_name) local new_data = util.deep_copy(item_data) or recipe_handler.create_recipe_object("", 1, {}, 0, false) @@ -149,6 +149,8 @@ return function(item_data, default_item_search_name) output_count, ingredients, machine, - is_fluid + is_fluid, + false, + default_item_search_name ) end diff --git a/ui/items/remove.lua b/ui/items/remove.lua index 6dc7527..36b7c9f 100644 --- a/ui/items/remove.lua +++ b/ui/items/remove.lua @@ -5,7 +5,7 @@ local good_response = require "ui.util.good_response" local search = require "ui.util.search" local confirm = require "ui.util.confirmation_menu" ---- Remove machine menu -> Search for a machine by name, then remove it, if the user confirms. +--- Remove recipes menu -> Search for a recipe by name, then remove it, if the user confirms. ---@param run_menu fun(name: string) The function to run another menu return function(run_menu) -- Get the list of item names diff --git a/ui/util/search.lua b/ui/util/search.lua index c3283d3..c5b0627 100644 --- a/ui/util/search.lua +++ b/ui/util/search.lua @@ -6,7 +6,7 @@ local fzy = require "fzy_lua" --- Allows you to search for something. ---@param menu_subtitle string The subtitle of the menu. ---@param initial_list table The initial list of results. If nothing has been entered yet, this will be shown. ----@param no_selection_allowed boolean? If true, the user can select the "No results found." item and the current text will instead be returned. +---@param no_selection_allowed boolean? If true, the user can select the "No results found." item and the current text will instead be returned. Also allows the user to press shift+enter to force the current name. ---@param default_search string? The default text to show in the input box. ---@return string? text The text the user entered, or nil if the user cancelled. return function(menu_subtitle, initial_list, no_selection_allowed, default_search)