register new node schema recipes when nodes, recipes, or shapes are added, instead of after mods load

This commit is contained in:
flux 2022-09-22 12:55:29 -07:00
parent 7414e274af
commit 6e16c54cc3
No known key found for this signature in database
GPG Key ID: 9333B27816848A15
4 changed files with 62 additions and 60 deletions

View File

@ -236,6 +236,12 @@ function api.register_single(node, shape, overrides, meta)
shapes[shape] = true
api.shapes_by_node[node] = shapes
api.node_by_shaped_node[shaped_name] = node
api.shape_by_shaped_node[shaped_name] = shape
api.node_by_shaped_node[node] = node
api.shape_by_shaped_node[node] = "node"
table.insert(api.registered_singles, {node, shaped_name})
for _, func in ipairs(api.registered_on_register_singles) do
@ -315,24 +321,6 @@ function api.get_micronode(node)
return api.get_schema_recipe_item(node, "micro_8")
end
-- create some hashes for quickly looking things up at run-time (i.e. the circular saw)
-- register schema crafts once, after everything has been registered. otherwise, it's not clear when to do this
minetest.register_on_mods_loaded(function()
stairsplus.log("info", "registering schema crafts")
for node, shapes in pairs(api.shapes_by_node) do
for shape in pairs(shapes) do
local shaped_node = api.format_name(node, shape)
api.node_by_shaped_node[shaped_node] = node
api.shape_by_shaped_node[shaped_node] = shape
end
api.node_by_shaped_node[node] = node
api.shape_by_shaped_node[node] = "node"
api.register_schema_crafts_for_node(node)
end
end)
function api.get_node_of_shaped_node(shaped_node)
return api.node_by_shaped_node[shaped_node]
end

View File

@ -4,6 +4,8 @@ local api = stairsplus.api
api.registered_recipe_schemas = {}
api.registered_on_register_craft_schemas = {}
local registered_schemas_by_node = {}
local function is_valid_output(item, shapes)
local item_name = item:match("^([^ ]+)")
@ -57,26 +59,6 @@ local function verify_schema(schema)
end
end
function api.register_on_register_craft_schema(func)
table.insert(api.registered_on_register_craft_schemas, func)
end
function api.register_craft_schema(schema)
local problems = verify_schema(schema)
if problems then
error(problems)
end
stairsplus.log("info", "registering craft schema %s", minetest.write_json(schema))
table.insert(api.registered_recipe_schemas, schema)
for _, func in ipairs(api.registered_on_register_craft_schemas) do
func(schema)
end
end
local function has_the_right_shapes(schema, shapes)
if not is_valid_output(schema.output, shapes) then
return false
@ -113,6 +95,8 @@ local function has_the_right_shapes(schema, shapes)
end
local function register_for_schema(node, schema)
stairsplus.log("verbose", "using schema %s w/ node %s", minetest.write_json(schema), node)
local recipe = table.copy(schema)
recipe.output = api.get_schema_recipe_item(node, recipe.output)
@ -143,17 +127,53 @@ local function register_for_schema(node, schema)
minetest.register_craft(recipe)
end
function api.register_schema_crafts_for_node(node)
stairsplus.log("info", "registering schema crafts for %q", node)
local shapes = api.get_shapes_hash(node)
for _, schema in ipairs(api.registered_recipe_schemas) do
if has_the_right_shapes(schema, shapes) then
stairsplus.log("verbose", "using schema %s", minetest.write_json(schema))
function api.register_on_register_craft_schema(func)
table.insert(api.registered_on_register_craft_schemas, func)
end
function api.register_craft_schema(schema)
local problems = verify_schema(schema)
if problems then
error(problems)
end
stairsplus.log("info", "registering craft schema %s", minetest.write_json(schema))
table.insert(api.registered_recipe_schemas, schema)
for node, shapes in pairs(api.shapes_by_node) do
local registered_schemas = registered_schemas_by_node[node] or {}
if has_the_right_shapes(schema, shapes) and not registered_schemas[schema] then
register_for_schema(node, schema)
registered_schemas[schema] = true
end
registered_schemas_by_node[node] = registered_schemas
end
for _, func in ipairs(api.registered_on_register_craft_schemas) do
func(schema)
end
end
function api.register_schema_crafts_for_node(node)
local registered_schemas = registered_schemas_by_node[node] or {}
local shapes = api.get_shapes_hash(node)
for _, schema in ipairs(api.registered_recipe_schemas) do
if has_the_right_shapes(schema, shapes) and not registered_schemas[schema] then
register_for_schema(node, schema)
registered_schemas[schema] = true
end
end
registered_schemas_by_node[node] = registered_schemas
end
api.register_on_register_single(api.register_schema_crafts_for_node)
local function shapes_match(a, b)
local a_shapes = api.get_shapes(a)
local b_shapes = api.get_shapes(b)

View File

@ -1,8 +1,13 @@
local api = stairsplus.api
api.registered_on_register_shapes = {}
api.registered_shapes = {}
api.shapes_by_group = {}
function api.register_on_register_shape(func)
table.insert(api.registered_on_register_shapes, func)
end
function api.register_shape(name, def)
stairsplus.log("info", "registering shape %q", name)
def.shape_groups = def.shape_groups or {}
@ -13,6 +18,10 @@ function api.register_shape(name, def)
table.insert(shapes, name)
api.shapes_by_group[group] = shapes
end
for _, func in ipairs(api.registered_on_register_shapes) do
func(name, def)
end
end
function api.register_shape_group(shape_group, shapes)

View File

@ -7,7 +7,7 @@ stairsplus.util = {
end,
table_is_empty = function(t)
return not next(t)
return next(t) == nil
end,
table_sort_keys = function(t, sort_function)
@ -54,21 +54,6 @@ stairsplus.util = {
return true
end,
check_call = function(func)
-- wrap a function w/ logic to avoid crashing the game
local f = function(...)
local status, out = pcall(func, ...)
if status then
return out
else
local message = ("Error (func): %s %s"):format(out, dump({...}))
stairsplus.log("error", message)
error(message)
end
end
return f
end,
get_location_string = function(inv)
local location = inv:get_location()
if location.type == "node" then