Simplify double inventory inv logic

This commit is contained in:
Mikita Wiśniewski 2024-06-25 09:28:33 +07:00 committed by the-real-herowl
parent 209b24a2fb
commit 70e903b716

View file

@ -379,10 +379,10 @@ local function limit_put_list(stack, list)
return stack return stack
end end
local function limit_put(stack, inv1, inv2) local function limit_put(stack, top_inv, bottom_inv)
local leftover = ItemStack(staick) local leftover = ItemStack(staick)
leftover = limit_put_list(leftover, inv1:get_list("main")) leftover = limit_put_list(leftover, top_inv:get_list("main"))
leftover = limit_put_list(leftover, inv2:get_list("main")) leftover = limit_put_list(leftover, bottom_inv:get_list("main"))
return stack:get_count() - leftover:get_count() return stack:get_count() - leftover:get_count()
end end
@ -396,62 +396,52 @@ local function close_forms(canonical_basename, pos)
end end
end end
local function get_chest_inventories(pos, side)
local node = minetest.get_node(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local pos_other = get_double_container_neighbor_pos(pos, node.param2, side)
local meta_other = minetest.get_meta(pos_other)
local inv_other = meta_other:get_inventory()
local top_inv, bottom_inv
if side == "left" then
top_inv = inv
bottom_inv = inv_other
else
top_inv = inv_other
bottom_inv = inv
end
return top_inv, bottom_inv
end
-- Functions used in double chest registration code -- Functions used in double chest registration code
-- ================================================ ---------------------------------------------------
-- The `return function` wrapping is necessary to avoid stacking up parameters. -- The `return function` wrapping is necessary to avoid stacking up parameters.
-- `side` is either "left" or "right". -- `side` is either "left" or "right".
local function hopper_pull_double(side) return function(pos, hop_pos, hop_inv, hop_list) local function hopper_pull_double(side) return function(pos, hop_pos, hop_inv, hop_list)
local node = minetest.get_node(pos) local top_inv, bottom_inv = get_chest_inventories(pos, side)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local pos_other = get_double_container_neighbor_pos(pos, node.param2, side) local stack_id = mcl_util.select_stack(top_inv, "main", hop_inv, hop_list)
local meta_other = minetest.get_meta(pos_other)
local inv_other = meta_other:get_inventory()
local ret_inv1, ret_inv2
if side == "left" then
ret_inv1 = inv
ret_inv2 = inv_other
else
ret_inv1 = inv_other
ret_inv2 = inv
end
local stack_id = mcl_util.select_stack(ret_inv1, "main", hop_inv, hop_list)
if stack_id ~= nil then if stack_id ~= nil then
return ret_inv1, "main", stack_id return top_inv, "main", stack_id
end end
stack_id = mcl_util.select_stack(ret_inv2, "main", hop_inv, hop_list) stack_id = mcl_util.select_stack(bottom_inv, "main", hop_inv, hop_list)
return ret_inv2, "main", stack_id return bottom_inv, "main", stack_id
end end end end
local function hopper_push_double(side) return function(pos, hop_pos, hop_inv, hop_list) local function hopper_push_double(side) return function(pos, hop_pos, hop_inv, hop_list)
local node = minetest.get_node(pos) local top_inv, bottom_inv = get_chest_inventories(pos, side)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local pos_other = get_double_container_neighbor_pos(pos, node.param2, side) local stack_id = mcl_util.select_stack(hop_inv, hop_list, top_inv, "main", nil, 1)
local meta_other = minetest.get_meta(pos_other)
local inv_other = meta_other:get_inventory()
local ret_inv1, ret_inv2
if side == "left" then
ret_inv1 = inv
ret_inv2 = inv_other
else
ret_inv1 = inv_other
ret_inv2 = inv
end
local stack_id = mcl_util.select_stack(hop_inv, hop_list, ret_inv1, "main", nil, 1)
if stack_id ~= nil then if stack_id ~= nil then
return ret_inv1, "main", stack_id return top_inv, "main", stack_id
end end
stack_id = mcl_util.select_stack(hop_inv, hop_list, ret_inv2, "main", nil, 1) stack_id = mcl_util.select_stack(hop_inv, hop_list, bottom_inv, "main", nil, 1)
return ret_inv2, "main", stack_id return bottom_inv, "main", stack_id
end end end end
local function construct_double_chest(side, names) return function(pos) local function construct_double_chest(side, names) return function(pos)
@ -492,14 +482,8 @@ local function protection_check_put(side) return function(pos, listname, index,
return 0 return 0
-- BEGIN OF LISTRING WORKAROUND -- BEGIN OF LISTRING WORKAROUND
elseif listname == "input" then elseif listname == "input" then
local inv = minetest.get_inventory({ type = "node", pos = pos }) local top_inv, bottom_inv = get_chest_inventories(pos, side)
local other_pos = get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, side) return limit_put(stack, top_inv, bottom_inv)
local other_inv = minetest.get_inventory({ type = "node", pos = other_pos })
if side == "left" then
return limit_put(stack, inv, other_inv)
else
return limit_put(stack, other_inv, inv)
end
-- END OF LISTRING WORKAROUND -- END OF LISTRING WORKAROUND
else else
return stack:get_count() return stack:get_count()
@ -511,17 +495,9 @@ local function log_inventory_put_double(side) return function(pos, listname, ind
" moves stuff to chest at " .. minetest.pos_to_string(pos)) " moves stuff to chest at " .. minetest.pos_to_string(pos))
-- BEGIN OF LISTRING WORKAROUND -- BEGIN OF LISTRING WORKAROUND
if listname == "input" then if listname == "input" then
local inv = minetest.get_inventory({ type = "node", pos = pos }) local top_inv, bottom_inv = get_chest_inventories(pos, side)
local other_pos = get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, side) top_inv:set_stack("input", 1, nil)
local other_inv = minetest.get_inventory({ type = "node", pos = other_pos }) double_chest_add_item(top_inv, bottom_inv, "main", stack)
inv:set_stack("input", 1, nil)
if side == "left" then
double_chest_add_item(inv, other_inv, "main", stack)
else
double_chest_add_item(other_inv, inv, "main", stack)
end
end end
-- END OF LISTRING WORKAROUND -- END OF LISTRING WORKAROUND
end end end end