blockgame/mods/bg_crafting/api.lua

46 lines
1.2 KiB
Lua
Raw Normal View History

blockgame.crafting = blockgame.crafting or {}
local api = blockgame.crafting
local recipes = {}
api.registered_recipes = recipes
api.register_stack_recipe = function (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 {}
table.insert(recipes[top_node], {
["type"] = "stack_two_nodes",
index_key = top_node,
top_node = top_node,
bottom_node = bottom_node,
result = result,
consumes_top = consumes_top,
})
end
api.handle_placement = function (pos, new_node, placer, old_node, itemstack, pointed_thing)
if recipes[new_node.name] == nil then return false end
for _, recipe in pairs(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
return false
end
api.handle_stack_recipe = function (recipe, pos, top_node)
if top_node.name ~= recipe.top_node then return false end
local down = vector.add(pos, vector.new(0, -1, 0))
local below = minetest.get_node(down)
if below.name ~= recipe.bottom_node then return false end
minetest.remove_node(pos)
minetest.set_node(down, recipe.result)
return true
end