From b1c85a2cb808c4effc1a52b519b65d73e4f25fb2 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 29 Aug 2021 07:47:32 +0200 Subject: [PATCH 1/2] Fix waypoint initialization order --- callbacks.lua | 3 +-- internal.lua | 3 +-- waypoints.lua | 5 +++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/callbacks.lua b/callbacks.lua index 1f43e39..317c3c8 100644 --- a/callbacks.lua +++ b/callbacks.lua @@ -24,8 +24,7 @@ minetest.register_on_joinplayer(function(player) unified_inventory.alternate[player_name] = 1 unified_inventory.current_item[player_name] = nil unified_inventory.current_craft_direction[player_name] = "recipe" - unified_inventory.set_inventory_formspec(player, - unified_inventory.default) + unified_inventory.set_inventory_formspec(player, unified_inventory.default) -- Refill slot local refill = minetest.create_detached_inventory(player_name.."refill", { diff --git a/internal.lua b/internal.lua index f83f96b..76a277d 100644 --- a/internal.lua +++ b/internal.lua @@ -375,6 +375,5 @@ function ui.apply_filter(player, filter, search_dir) ui.current_index[player_name] = 1 ui.activefilter[player_name] = filter ui.active_search_direction[player_name] = search_dir - ui.set_inventory_formspec(player, - ui.current_page[player_name]) + ui.set_inventory_formspec(player, ui.current_page[player_name]) end diff --git a/waypoints.lua b/waypoints.lua index 08fce38..1496cdd 100644 --- a/waypoints.lua +++ b/waypoints.lua @@ -330,8 +330,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end end) - -minetest.register_on_joinplayer(function(player) +-- waypoints_temp must be initialized before the general unified_inventory +-- joinplayer callback is run for updating the inventory +table.insert(minetest.registered_on_joinplayers, 1, function(player) local player_name = player:get_player_name() local waypoints = get_waypoint_data(player) From 8c84751ff814ac5da75b1536bdf6961d9172b315 Mon Sep 17 00:00:00 2001 From: Oversword Date: Thu, 14 Oct 2021 17:38:07 +0100 Subject: [PATCH 2/2] Add u_i initialization and craft registration callbacks (#184) Main purpose: custom recipe sorting provided by cuszom mods. --- api.lua | 41 +++++++++++++++++++++++++++++++--- doc/mod_api.txt | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 2 ++ 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/api.lua b/api.lua index 22788ae..8cc5532 100644 --- a/api.lua +++ b/api.lua @@ -152,6 +152,10 @@ minetest.after(0.01, function() end end end + + for _, callback in ipairs(ui.initialized_callbacks) do + callback() + end end) @@ -212,10 +216,15 @@ function ui.register_craft(options) if options.type == "normal" and options.width == 0 then options = { type = "shapeless", items = options.items, output = options.output, width = 0 } end - if not ui.crafts_for.recipe[itemstack:get_name()] then - ui.crafts_for.recipe[itemstack:get_name()] = {} + local item_name = itemstack:get_name() + if not ui.crafts_for.recipe[item_name] then + ui.crafts_for.recipe[item_name] = {} + end + table.insert(ui.crafts_for.recipe[item_name],options) + + for _, callback in ipairs(ui.craft_registered_callbacks) do + callback(item_name, options) end - table.insert(ui.crafts_for.recipe[itemstack:get_name()],options) end @@ -309,6 +318,32 @@ function ui.register_button(name, def) table.insert(ui.buttons, def) end +function ui.register_on_initialized(callback) + if type(callback) ~= "function" then + error(("Initialized callback must be a function, %s given."):format(type(callback))) + end + table.insert(ui.initialized_callbacks, callback) +end + +function ui.register_on_craft_registered(callback) + if type(callback) ~= "function" then + error(("Craft registered callback must be a function, %s given."):format(type(callback))) + end + table.insert(ui.craft_registered_callbacks, callback) +end + +function ui.get_recipe_list(output) + return ui.crafts_for.recipe[output] +end + +function ui.get_registered_outputs() + local outputs = {} + for item_name, _ in pairs(ui.crafts_for.recipe) do + table.insert(outputs, item_name) + end + return outputs +end + function ui.is_creative(playername) return minetest.check_player_privs(playername, {creative=true}) or minetest.settings:get_bool("creative_mode") diff --git a/doc/mod_api.txt b/doc/mod_api.txt index ff52792..ef87697 100644 --- a/doc/mod_api.txt +++ b/doc/mod_api.txt @@ -20,6 +20,64 @@ Grouped by use-case, afterwards sorted alphabetically. * Checks whether creative is enabled or the player has `creative` +Callbacks +--------- + +Register a callback that will be run whenever a craft is registered via unified_inventory.register_craft: + + unified_inventory.register_on_craft_registered( + function (item_name, options) + -- item_name (string): name of the output item, equivalent to `ItemStack:get_name()` + -- options (table): definition table of crafts registered by `unified_inventory.register_craft` + end + ) + +Register a callback that will be run after all mods have loaded and after the unified_inventory mod has initialised all its internal structures: + + unified_inventory.register_on_initialized(callback) + -- The callback is passed no arguments + + +Accessing Data +-------------- + +These methods should be used instead of accessing the unified_inventory data structures directly - this will ensure your code survives any potential restructuring of the mod. + +Get a list of recipes for a particular output item: + + unified_inventory.get_recipe_list(output_item) + + Returns a list of tables, each holding a recipe definition, like: + { + { + type = "normal", + items = { "default:stick", "default:stick", "default:stick", "default:stick" }, + output = "default:wood", + width = 2 + }, + { + type = "shapeless", + items = { "default:tree" }, + output = "default:wood 4", + width = 0 + }, + ... + } + +Get a list of all the output items crafts have been registered for: + + unified_inventory.get_registered_outputs() + + Returns a list of item names, like: + { + "default:stone", + "default:chest", + "default:brick", + "doors:door_wood", + ... + } + + Pages ----- diff --git a/init.lua b/init.lua index 5375903..98ee2e5 100644 --- a/init.lua +++ b/init.lua @@ -24,6 +24,8 @@ unified_inventory = { filtered_items_list = {}, pages = {}, buttons = {}, + initialized_callbacks = {}, + craft_registered_callbacks = {}, -- Homepos stuff home_pos = {},