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 = {},