New put function with simplified logic, renamed to better describe purpose, cache removed

This commit is contained in:
Cam B 2020-12-18 02:51:31 +00:00
parent 0fd949b496
commit 9a5ec1a96d
4 changed files with 27 additions and 39 deletions

@ -130,7 +130,7 @@ tubelib.register_node(NODE_NAME,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
meta:set_string("push_dir", wh.Turn180[side]) meta:set_string("push_dir", wh.Turn180[side])
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
item:set_count(num) item:set_count(num)
return tubelib.put_item(meta, "shift", item) return tubelib.put_item(meta, "shift", item)
@ -145,7 +145,7 @@ tubelib.register_node(NODE_NAME,
end, end,
on_unpull_item = function(pos, side, item) on_unpull_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
-- this should never happen, but better safe than sorry -- this should never happen, but better safe than sorry
item:set_count(num) item:set_count(num)

@ -130,7 +130,7 @@ tubelib.register_node(NODE_NAME,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
meta:set_string("push_dir", wh.Turn180[side]) meta:set_string("push_dir", wh.Turn180[side])
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
item:set_count(num) item:set_count(num)
return tubelib.put_item(meta, "shift", item) return tubelib.put_item(meta, "shift", item)
@ -145,7 +145,7 @@ tubelib.register_node(NODE_NAME,
end, end,
on_unpull_item = function(pos, side, item) on_unpull_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
-- this should never happen, but better safe than sorry -- this should never happen, but better safe than sorry
item:set_count(num) item:set_count(num)

@ -130,7 +130,7 @@ tubelib.register_node(NODE_NAME,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
meta:set_string("push_dir", wh.Turn180[side]) meta:set_string("push_dir", wh.Turn180[side])
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
item:set_count(num) item:set_count(num)
return tubelib.put_item(meta, "shift", item) return tubelib.put_item(meta, "shift", item)
@ -145,7 +145,7 @@ tubelib.register_node(NODE_NAME,
end, end,
on_unpull_item = function(pos, side, item) on_unpull_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
-- this should never happen, but better safe than sorry -- this should never happen, but better safe than sorry
item:set_count(num) item:set_count(num)

@ -22,8 +22,6 @@ local COUNTDOWN_TICKS = 2
local CYCLE_TIME = 2 local CYCLE_TIME = 2
local Cache = {}
techpack_warehouse.Box = {} techpack_warehouse.Box = {}
techpack_warehouse.Turn180 = {F="B", L="R", B="F", R="L", U="D", D="U"} techpack_warehouse.Turn180 = {F="B", L="R", B="F", R="L", U="D", D="U"}
@ -167,37 +165,33 @@ function techpack_warehouse.Box:new(attr)
return o return o
end end
function techpack_warehouse.numbers_to_shift(self, meta, item) -- We can't use the standard function "inv:add_item()" because this function
-- check cache -- would not allow to add more than the default 99 items per stack.
local number = meta:get_string("tubelib_number") function techpack_warehouse.inv_add_item(self, meta, item)
local item_name = item:get_name()
if not Cache[number] then
local inv = meta:get_inventory()
Cache[number] = {}
for idx,items in ipairs(inv:get_list("filter")) do
Cache[number][idx] = items:get_name()
end
end
-- determine number to shift
local num_items = item:get_count() local num_items = item:get_count()
local inv_size = meta:get_int("inv_size") local item_name = item:get_name()
local inv = meta:get_inventory() local inv = meta:get_inventory()
local main_list = inv:get_list("main")
for idx, name in ipairs(Cache[number]) do for idx, stack in ipairs(main_list) do
if item_name == name then -- If item configured
local stack_size = inv:get_stack("main", idx):get_count() if item_name == inv:get_stack("filter", idx):get_name() then
if stack_size == self.inv_size then -- full? local stack_size = stack:get_count()
elseif (stack_size + num_items) > self.inv_size then -- limit will be reached? -- If there is some space for further items
inv:set_stack("main", idx, ItemStack({name = item_name, count = self.inv_size})) if stack_size < self.inv_size then
-- search with the rest for further slots local new_stack_size = math.min(self.inv_size, stack_size + num_items)
num_items = num_items - (self.inv_size - stack_size) main_list[idx] = ItemStack({name = item_name, count = new_stack_size})
else -- calc new number of items
inv:set_stack("main", idx, ItemStack({name = item_name, count = stack_size + num_items})) num_items = num_items - (new_stack_size - stack_size)
return 0 -- If everything is distributed
if num_items == 0 then
break
end
end end
end end
end end
inv:set_list("main", main_list)
return num_items return num_items
end end
@ -216,7 +210,6 @@ function techpack_warehouse.allow_metadata_inventory_put(self, pos, listname, in
return math.min(stack:get_count(), self.inv_size - main_stack:get_count()) return math.min(stack:get_count(), self.inv_size - main_stack:get_count())
elseif listname == "filter" and item_name == main_stack:get_name() then elseif listname == "filter" and item_name == main_stack:get_name() then
local number = M(pos):get_string("tubelib_number") local number = M(pos):get_string("tubelib_number")
Cache[number] = nil
return 1 return 1
elseif listname == "shift" then elseif listname == "shift" then
return stack:get_count() return stack:get_count()
@ -227,7 +220,6 @@ end
function techpack_warehouse.on_metadata_inventory_put(pos, listname, index, stack, player) function techpack_warehouse.on_metadata_inventory_put(pos, listname, index, stack, player)
if listname == "input" then if listname == "input" then
local number = M(pos):get_string("tubelib_number") local number = M(pos):get_string("tubelib_number")
Cache[number] = nil
minetest.after(0.5, move_to_main, pos, index) minetest.after(0.5, move_to_main, pos, index)
end end
end end
@ -240,14 +232,11 @@ function techpack_warehouse.allow_metadata_inventory_take(pos, listname, index,
local main_stack = inv:get_stack("main", index) local main_stack = inv:get_stack("main", index)
local number = M(pos):get_string("tubelib_number") local number = M(pos):get_string("tubelib_number")
if listname == "main" then if listname == "main" then
Cache[number] = nil
minetest.after(0.1, move_to_player_inv, player:get_player_name(), pos, index) minetest.after(0.1, move_to_player_inv, player:get_player_name(), pos, index)
return 0 return 0
elseif listname == "filter" and main_stack:is_empty() then elseif listname == "filter" and main_stack:is_empty() then
Cache[number] = nil
return 1 return 1
elseif listname == "shift" then elseif listname == "shift" then
Cache[number] = nil
return stack:get_count() return stack:get_count()
end end
return 0 return 0
@ -262,7 +251,6 @@ function techpack_warehouse.on_receive_fields(self, pos, formname, fields, playe
return return
end end
local number = M(pos):get_string("tubelib_number") local number = M(pos):get_string("tubelib_number")
Cache[number] = nil
self.State:state_button_event(pos, fields) self.State:state_button_event(pos, fields)
end end