From 1410da4ce091183535838a4000f07ead694c5ffb Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Fri, 18 Feb 2022 21:48:20 -0400 Subject: [PATCH] Handle shapeless recipes in redefinitions * They can be recognized from having width == 0, and don't need the items list to be massaged to be transformed into the recipe field for lua api * backported https://github.com/minetest-mods/moreblocks/pull/171 * fixes https://github.com/minetest-mods/moreblocks/issues/170 * backported https://github.com/minetest-mods/moreblocks/commit/1a03b041dd49cf52e8d5d84e72206718055b1455 --- redefinitions.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/redefinitions.lua b/redefinitions.lua index cd38f0be..f69e85d9 100644 --- a/redefinitions.lua +++ b/redefinitions.lua @@ -99,9 +99,10 @@ local reconstruct_internal_craft = function(recipe) { "", "", "" }, { "", "", "" }, } + local width = recipe.width for idx, item in pairs(recipe.items) do - local row = math.ceil(idx / recipe.width) - local col = idx - (row-1)*recipe.width + local row = math.ceil(idx / width) + local col = idx - (row-1)*width recp[row][col] = item end return recp @@ -111,12 +112,22 @@ end local change_recipe_amount = function(product, recipe, func) local recp = reconstruct_internal_craft(recipe) + -- if width == 0, this is a shapeless recipe, for which the + -- internal and Lua API recipe table is the same. + -- Otherwise we need to reconstruct the table for the shaped recipe. + local shapeless = (recipe.width == 0) + local recp = shapeless and recipe.items or reconstruct_internal_craft(recipe) + local oldamount = tonumber(recipe.output:match(" [0-9]+$") or "1") local newamount = func(oldamount) -- remove old crafting recipe local redo = { recipe = recp } + -- preserve shapelessness + if shapeless then + redo.type = "shapeless" + end minetest.clear_craft(redo) -- new output