Skip to content

Commit

Permalink
Core: builtin: crafts: ability to find custom crafts via `core.ge…
Browse files Browse the repository at this point in the history
…t_craft_recipe()`. Closes #1814
  • Loading branch information
alek13 committed Dec 2, 2024
1 parent 6608cbf commit dba80e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions mods/lord/Core/builtin/src/FormSpec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ function FormSpec.image_button(X,Y,W,H,texture_name,name,label,noclip,drawborder
return 'image_button['..
X..','..Y..';' ..W..','..H..';'..
texture_name..';'..name..';'..optional(e(label))..
optional(noclip)..optional(drawborder)..optional(pressed_texture_name)..
optional(bool_str(noclip))..optional(bool_str(drawborder))..optional(pressed_texture_name)..
']'
end

Expand All @@ -574,7 +574,7 @@ end
---
--- @return string
function FormSpec.item_image_button(X,Y,W,H,item_name,name,label)
return 'item_image_button['..X..','..Y..';' ..W..','..H..';'..item_name..';'..name..optional(e(label))..']'
return 'item_image_button['..X..','..Y..';' ..W..','..H..';'..item_name..';'..name..';'..e(label)..']'
end

---
Expand Down
38 changes: 35 additions & 3 deletions mods/lord/Core/builtin/src/craft/method.lua
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,38 @@ function minetest.get_craft_result(input)
return output, input
end

-- to-do
--function minetest.get_craft_recipe(output)
--end

--- @param output string
--- @return minetest.CraftRecipe|nil
local function find_craft_recipe(output)
for method, method_recipes in pairs(method_registered_recipes) do
for type, type_recipes in pairs(method_recipes) do
for recipe_item_name, recipes in pairs(type_recipes) do
if recipe_item_name == output then
return recipes[1]
end
end
end
end
end

local mt_get_craft_recipe = minetest.get_craft_recipe
--- @param output string
--- @return RecipeInput|minetest.CraftRecipe|nil
function minetest.get_craft_recipe(output, method, type)
method = method or minetest.CraftMethod.DEFAULT
type = type or minetest.CraftType.NORMAL

if method == minetest.CraftMethod.DEFAULT then
local found = mt_get_craft_recipe(output)
if found then
return found
end

return find_craft_recipe(output)
end

local r = method_registered_recipes

return r[method] and r[method][type] and r[method][type][output] and r[method][type][output][1]
end

0 comments on commit dba80e6

Please sign in to comment.