From b5de18b196c5ae0ba3d679f39af309be3ee64574 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 17 Mar 2024 16:54:21 +0100 Subject: [PATCH] Categories: remove unknown items after start This avoids runtime errors caused by removed items or such that were not registered at all but listed by default in a pre-defined category. --- api.lua | 21 +++++++++++++++++++++ internal.lua | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/api.lua b/api.lua index f2f40c2..7ab5d66 100644 --- a/api.lua +++ b/api.lua @@ -184,6 +184,27 @@ minetest.after(0.01, function() ui.crafts_for.recipe[outputitemname] = new_recipe_list end + -- Remove unknown items from all categories + local total_removed = 0 + for cat_name, cat_def in pairs(ui.registered_category_items) do + for itemname, exists in pairs(cat_def) do + if exists and not minetest.registered_items[itemname] then + total_removed = total_removed + 1 + --[[ + -- For analysis + minetest.log("warning", "[unified_inventory] Removed item '" + .. itemname .. "' from category '" .. cat_name + .. "'. Reason: item not registered") + ]] + cat_def[itemname] = nil + end + end + end + if total_removed > 0 then + minetest.log("info", "[unified_inventory] Removed " .. total_removed .. + " unknown items from the categories.") + end + for _, callback in ipairs(ui.initialized_callbacks) do callback() end diff --git a/internal.lua b/internal.lua index daa3ba1..3caa43a 100644 --- a/internal.lua +++ b/internal.lua @@ -368,6 +368,10 @@ function ui.apply_filter(player, filter, search_dir) local groups = lfilter:sub(7):split(",") ffilter = function(name) local def = registered_items[name] + if not def then + return false + end + for _, group in ipairs(groups) do if not def.groups[group] or def.groups[group] <= 0 then @@ -383,6 +387,10 @@ function ui.apply_filter(player, filter, search_dir) ffilter = function(name) local def = registered_items[name] + if not def then + return false + end + local lname = string.lower(name) local ldesc = string.lower(def.description) local llocaldesc = minetest.get_translated_string