Fix autocrafter not taking enough items if number of needed items of a kind > stack max for that item.

This commit is contained in:
Novatux 2013-10-30 08:33:09 +01:00
parent 7887d28e08
commit dc1ef38a58

View File

@ -1,5 +1,13 @@
local autocrafterCache = {} -- caches some recipe data to avoid to call the slow function minetest.get_craft_result() every second local autocrafterCache = {} -- caches some recipe data to avoid to call the slow function minetest.get_craft_result() every second
local function make_inventory_cache(invlist)
local l = {}
for _, stack in ipairs(invlist) do
l[stack:get_name()] = (l[stack:get_name()] or 0) + stack:get_count()
end
return l
end
function autocraft(inventory, pos) function autocraft(inventory, pos)
local recipe = inventory:get_list("recipe") local recipe = inventory:get_list("recipe")
local recipe_last local recipe_last
@ -59,14 +67,14 @@ function autocraft(inventory, pos)
end end
end end
end end
local stack local invcache = make_inventory_cache(inventory:get_list("src"))
for itemname, number in pairs(to_use) do for itemname, number in pairs(to_use) do
stack = ItemStack({name = itemname, count = number}) if (not invcache[itemname]) or invcache[itemname] < number then return end
if not inventory:contains_item("src", stack) then return end
end end
for itemname, number in pairs(to_use) do for itemname, number in pairs(to_use) do
stack = ItemStack({name = itemname, count = number}) for i = 1, number do -- We have to do that since remove_item does not work if count > stack_max
inventory:remove_item("src", stack) inventory:remove_item("src", ItemStack(itemname))
end
end end
inventory:add_item("dst", result) inventory:add_item("dst", result)
for i = 1, 9 do for i = 1, 9 do