Skip to content

Commit

Permalink
Handle shapeless recipes in redefinitions
Browse files Browse the repository at this point in the history
* 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 minetest-mods#171
* fixes minetest-mods#170
* backported minetest-mods@1a03b04
  • Loading branch information
mckaygerhard committed Feb 19, 2022
1 parent 4e1f71f commit 1410da4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions redefinitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1410da4

Please sign in to comment.