Add u_i initialization and craft registration callbacks (#184)

Main purpose: custom recipe sorting provided by cuszom mods.
This commit is contained in:
Oversword 2021-10-14 17:38:07 +01:00 committed by GitHub
parent b1c85a2cb8
commit 8c84751ff8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 3 deletions

41
api.lua
View File

@ -152,6 +152,10 @@ minetest.after(0.01, function()
end end
end end
end end
for _, callback in ipairs(ui.initialized_callbacks) do
callback()
end
end) end)
@ -212,10 +216,15 @@ function ui.register_craft(options)
if options.type == "normal" and options.width == 0 then if options.type == "normal" and options.width == 0 then
options = { type = "shapeless", items = options.items, output = options.output, width = 0 } options = { type = "shapeless", items = options.items, output = options.output, width = 0 }
end end
if not ui.crafts_for.recipe[itemstack:get_name()] then local item_name = itemstack:get_name()
ui.crafts_for.recipe[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 end
table.insert(ui.crafts_for.recipe[itemstack:get_name()],options)
end end
@ -309,6 +318,32 @@ function ui.register_button(name, def)
table.insert(ui.buttons, def) table.insert(ui.buttons, def)
end 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) function ui.is_creative(playername)
return minetest.check_player_privs(playername, {creative=true}) return minetest.check_player_privs(playername, {creative=true})
or minetest.settings:get_bool("creative_mode") or minetest.settings:get_bool("creative_mode")

View File

@ -20,6 +20,64 @@ Grouped by use-case, afterwards sorted alphabetically.
* Checks whether creative is enabled or the player has `creative` * 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 Pages
----- -----

View File

@ -24,6 +24,8 @@ unified_inventory = {
filtered_items_list = {}, filtered_items_list = {},
pages = {}, pages = {},
buttons = {}, buttons = {},
initialized_callbacks = {},
craft_registered_callbacks = {},
-- Homepos stuff -- Homepos stuff
home_pos = {}, home_pos = {},