move `item_matches` into api.

move function `item_matches` from `bg_crafting` into `bg_api`, since
it'll be useful in many other places.
This commit is contained in:
trans_soup 2023-10-17 15:02:39 +02:00
parent 7df5d33d02
commit d197e1f1e3
3 changed files with 18 additions and 13 deletions

View File

@ -13,6 +13,7 @@ load_file("util_random")
load_file("util_vector")
load_file("util_node")
load_file("util_item")
load_file("wrappers")

12
mods/bg_api/util_item.lua Normal file
View File

@ -0,0 +1,12 @@
function blockgame.item_matches (name, options)
return blockgame.any(options, function (option)
if name == option then return true end
if blockgame.starts_with(option, "group:") then
local group = string.sub(option, 7)
return minetest.get_item_group(name, group) > 0
end
return false
end)
end

View File

@ -1,5 +1,9 @@
local api = blockgame.crafting
-- LOCALS
local matches = blockgame.item_matches
-- pummeling is heavily based on nodecore by Warr1024 (both the mechanic itself and the code here implementing it).
local pummel_recipes = {}
@ -21,22 +25,10 @@ function api.register_pummel_recipe (def)
table.insert(api.registered_recipes, def)
end
-- TODO: extract this function into general api
local function item_matches (nodename, options)
return blockgame.any(options, function (option)
if nodename == option then return true end
if blockgame.starts_with(option, "group:") then
local group = string.sub(option, 7)
return minetest.get_item_group(nodename, group) > 0
end
return false
end)
end
-- TODO: add support for pummel recipes using groups instead of just specific node names.
function api.pummel_check (pos, used, target_node)
for _, def in pairs(pummel_recipes) do
if item_matches(used, def.used) and item_matches(target_node.name, def.target) then
if matches(used, def.used) and matches(target_node.name, def.target) then
if type(def.check) == "function" and not def.check(pos, used, target_node) then
return false
end