From d197e1f1e369916af53ec93049f1db72df3899ae Mon Sep 17 00:00:00 2001 From: trans_soup <> Date: Tue, 17 Oct 2023 15:02:39 +0200 Subject: [PATCH] move `item_matches` into api. move function `item_matches` from `bg_crafting` into `bg_api`, since it'll be useful in many other places. --- mods/bg_api/init.lua | 1 + mods/bg_api/util_item.lua | 12 ++++++++++++ mods/bg_crafting/pummel.lua | 18 +++++------------- 3 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 mods/bg_api/util_item.lua diff --git a/mods/bg_api/init.lua b/mods/bg_api/init.lua index 95181ec..2e1e41e 100644 --- a/mods/bg_api/init.lua +++ b/mods/bg_api/init.lua @@ -13,6 +13,7 @@ load_file("util_random") load_file("util_vector") load_file("util_node") +load_file("util_item") load_file("wrappers") diff --git a/mods/bg_api/util_item.lua b/mods/bg_api/util_item.lua new file mode 100644 index 0000000..159e382 --- /dev/null +++ b/mods/bg_api/util_item.lua @@ -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 diff --git a/mods/bg_crafting/pummel.lua b/mods/bg_crafting/pummel.lua index 5409b2d..a245ee4 100644 --- a/mods/bg_crafting/pummel.lua +++ b/mods/bg_crafting/pummel.lua @@ -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