This commit is contained in:
John Cole 2017-02-17 05:10:30 +00:00 committed by GitHub
commit 8668c1e41c
1 changed files with 34 additions and 11 deletions

View File

@ -7,25 +7,48 @@ local exchange_shop = {}
-- Tool wear aware replacement for contains_item. -- Tool wear aware replacement for contains_item.
local function list_contains_item(inv, listname, stack) local function list_contains_item(inv, listname, stack)
local count = stack:get_count()
if count == 0 then return true end
local list = inv:get_list(listname) local list = inv:get_list(listname)
for i, list_stack in pairs(list) do local name = stack:get_name()
if list_stack:get_name() == stack:get_name() and local wear = stack:get_wear()
list_stack:get_count() >= stack:get_count() and for _, list_stack in pairs(list) do
list_stack:get_wear() <= stack:get_wear() then if list_stack:get_name() == name and
return i list_stack:get_wear() <= wear then
if list_stack:get_count() >= count then
return true
else
count = count - list_stack:get_count()
end
end end
end end
end 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 index = list_contains_item(inv, listname, stack) local count = stack:get_count()
if index then if count == 0 then return stack end
local list_stack = inv:get_stack(listname, index) local removed_stack = ItemStack(nil)
local removed_stack = list_stack:take_item(stack:get_count())
inv:set_stack(listname, index, list_stack) local list = inv:get_list(listname)
return removed_stack local name = stack:get_name()
local wear = stack:get_wear()
for index, list_stack in pairs(list) do
if list_stack:get_name() == name and
list_stack:get_wear() <= wear then
if list_stack:get_count() >= count then
removed_stack:add_item(list_stack:take_item(count))
inv:set_stack(listname, index, list_stack)
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
return removed_stack
end end
local function get_exchange_shop_formspec(number,pos,title) local function get_exchange_shop_formspec(number,pos,title)