forked from mtcontrib/bitchange
Shop: Avoid unhandled leftovers from ItemStack:add_item
Pre-fix if https://github.com/minetest/minetest/pull/5774 gets merged somewhen Thanks to @nybble41 for providing this code
This commit is contained in:
parent
3814d994db
commit
bf68097a75
24
shop.lua
24
shop.lua
@ -29,29 +29,35 @@ end
|
|||||||
|
|
||||||
-- Tool wear aware replacement for remove_item.
|
-- Tool wear aware replacement for remove_item.
|
||||||
local function list_remove_item(inv, listname, stack)
|
local function list_remove_item(inv, listname, stack)
|
||||||
local count = stack:get_count()
|
local wanted = stack:get_count()
|
||||||
if count == 0 then
|
if wanted == 0 then
|
||||||
return stack
|
return stack
|
||||||
end
|
end
|
||||||
local removed_stack = ItemStack(nil)
|
|
||||||
|
|
||||||
local list = inv:get_list(listname)
|
local list = inv:get_list(listname)
|
||||||
local name = stack:get_name()
|
local name = stack:get_name()
|
||||||
local wear = stack:get_wear()
|
local wear = stack:get_wear()
|
||||||
|
local remaining = wanted
|
||||||
|
local removed_wear = 0
|
||||||
|
|
||||||
for index, list_stack in pairs(list) do
|
for index, list_stack in pairs(list) do
|
||||||
if list_stack:get_name() == name and
|
if list_stack:get_name() == name and
|
||||||
list_stack:get_wear() <= wear then
|
list_stack:get_wear() <= wear then
|
||||||
if list_stack:get_count() >= count then
|
local taken_stack = list_stack:take_item(remaining)
|
||||||
removed_stack:add_item(list_stack:take_item(count))
|
|
||||||
inv:set_stack(listname, index, list_stack)
|
inv:set_stack(listname, index, list_stack)
|
||||||
|
|
||||||
|
removed_wear = math.max(removed_wear, taken_stack:get_wear())
|
||||||
|
remaining = remaining - taken_stack:get_count()
|
||||||
|
if remaining == 0 then
|
||||||
break
|
break
|
||||||
else
|
|
||||||
removed_stack:add_item(list_stack)
|
|
||||||
inv:set_stack(listname, index, ItemStack(nil))
|
|
||||||
count = count - list_stack:get_count()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Todo: Also remove kebab
|
||||||
|
local removed_stack = ItemStack(name)
|
||||||
|
removed_stack:set_count(wanted - remaining)
|
||||||
|
removed_stack:set_wear(removed_wear)
|
||||||
return removed_stack
|
return removed_stack
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user