Skip to content

Commit

Permalink
Fix editing recipes, not saving item data.
Browse files Browse the repository at this point in the history
  • Loading branch information
fatboychummy committed Mar 4, 2024
1 parent 71c4053 commit fcd9c95
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
19 changes: 18 additions & 1 deletion lib/recipe_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions ui/items/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions ui/items/edit.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
6 changes: 4 additions & 2 deletions ui/items/get_item_details.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion ui/items/remove.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ui/util/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<integer,string> 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)
Expand Down

0 comments on commit fcd9c95

Please sign in to comment.