mirror of
https://github.com/SmallJoker/bitchange.git
synced 2025-03-20 11:20:23 +01:00
Make suggested changes.
- Simplifies return values by returning only a single value for success and nothing for failure. - Stores the inventory list in a local to prevent redundant retrievals.
This commit is contained in:
parent
a11dab9132
commit
f5303586ba
19
shop.lua
19
shop.lua
@ -6,27 +6,26 @@
|
|||||||
local exchange_shop = {}
|
local exchange_shop = {}
|
||||||
|
|
||||||
-- Tool wear aware replacement for contains_item.
|
-- Tool wear aware replacement for contains_item.
|
||||||
local function list_contains_item(inv, list, stack)
|
local function list_contains_item(inv, listname, stack)
|
||||||
for i, list_stack in pairs(inv:get_list(list)) do
|
local list = inv:get_list(listname)
|
||||||
|
for i, list_stack in pairs(list) do
|
||||||
if list_stack:get_name() == stack:get_name() and
|
if list_stack:get_name() == stack:get_name() and
|
||||||
list_stack:get_count() >= stack:get_count() and
|
list_stack:get_count() >= stack:get_count() and
|
||||||
list_stack:get_wear() <= stack:get_wear() then
|
list_stack:get_wear() <= stack:get_wear() then
|
||||||
return true, i
|
return i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return false, nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Tool wear aware replacement for remove_item.
|
-- Tool wear aware replacement for remove_item.
|
||||||
local function list_remove_item(inv, list, stack)
|
local function list_remove_item(inv, listname, stack)
|
||||||
local contains, index = list_contains_item(inv, list, stack)
|
local index = list_contains_item(inv, listname, stack)
|
||||||
if contains then
|
if index then
|
||||||
local list_stack = inv:get_stack(list, index)
|
local list_stack = inv:get_stack(listname, index)
|
||||||
local removed_stack = list_stack:take_item(stack:get_count())
|
local removed_stack = list_stack:take_item(stack:get_count())
|
||||||
inv:set_stack(list, index, list_stack)
|
inv:set_stack(listname, index, list_stack)
|
||||||
return removed_stack
|
return removed_stack
|
||||||
end
|
end
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_exchange_shop_formspec(number,pos,title)
|
local function get_exchange_shop_formspec(number,pos,title)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user