minor fix & refactors of recipe code.

This commit is contained in:
trans_soup 2023-10-17 14:23:44 +02:00
parent 17636dbf87
commit 5305cc526d
2 changed files with 18 additions and 11 deletions

View File

@ -4,26 +4,31 @@ local api = blockgame.crafting
local recipes = {}
api.registered_recipes = recipes
local stack_recipes = {}
function api.register_stack_recipe (top_node, bottom_node, result, consumes_top)
local consumes_top = consumes_top
if consumes_top == nil then consumes_top = true end
recipes[top_node] = recipes[top_node] or {}
stack_recipes[top_node] = stack_recipes[top_node] or {}
table.insert(recipes[top_node], {
local def = {
["type"] = "stack_two_nodes",
index_key = top_node,
top_node = top_node,
bottom_node = bottom_node,
result = result,
consumes_top = consumes_top,
})
}
table.insert(stack_recipes[top_node], def)
table.insert(api.registered_recipes, def)
end
function api.handle_placement (pos, new_node, placer, old_node, itemstack, pointed_thing)
if recipes[new_node.name] == nil then return false end
if stack_recipes[new_node.name] == nil then return false end
for _, recipe in pairs(recipes[new_node.name]) do
for _, recipe in pairs(stack_recipes[new_node.name]) do
local result = api.handle_stack_recipe(recipe, pos, new_node)
if result then return not recipe.consumes_top end
end
@ -31,6 +36,10 @@ function api.handle_placement (pos, new_node, placer, old_node, itemstack, point
return false
end
blockgame.register_on_placenode(function (...)
return blockgame.crafting.handle_placement(...)
end)
function api.handle_stack_recipe (recipe, pos, top_node)
if top_node.name ~= recipe.top_node then return false end
@ -61,8 +70,12 @@ function api.register_pummel_recipe (def)
local key = def.used_item
def.index_key = key
def["type"] = "pummel"
pummel_recipes[key] = pummel_recipes[key] or {}
table.insert(pummel_recipes[key], def)
table.insert(api.registered_recipes, def)
end
-- TODO: add support for pummel recipes using groups instead of just specific node names.

View File

@ -1,7 +1 @@
local modname = minetest.get_current_modname()
load_file("api")
blockgame.register_on_placenode(function (...)
return blockgame.crafting.handle_placement(...)
end)