From b7ae99e72e0f25347e2f76c980e4f58ac4075b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikita=20Wi=C5=9Bniewski?= Date: Tue, 25 Jun 2024 09:28:33 +0700 Subject: [PATCH] Simplify double inventory inv logic --- mods/ITEMS/mcl_chests/api.lua | 102 +++++++++++++--------------------- 1 file changed, 39 insertions(+), 63 deletions(-) diff --git a/mods/ITEMS/mcl_chests/api.lua b/mods/ITEMS/mcl_chests/api.lua index 0dd9b39bd..dbdb1f185 100644 --- a/mods/ITEMS/mcl_chests/api.lua +++ b/mods/ITEMS/mcl_chests/api.lua @@ -379,10 +379,10 @@ local function limit_put_list(stack, list) return stack end -local function limit_put(stack, inv1, inv2) +local function limit_put(stack, top_inv, bottom_inv) local leftover = ItemStack(staick) - leftover = limit_put_list(leftover, inv1:get_list("main")) - leftover = limit_put_list(leftover, inv2:get_list("main")) + leftover = limit_put_list(leftover, top_inv:get_list("main")) + leftover = limit_put_list(leftover, bottom_inv:get_list("main")) return stack:get_count() - leftover:get_count() end @@ -396,62 +396,52 @@ local function close_forms(canonical_basename, pos) 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 --- ================================================ +--------------------------------------------------- -- The `return function` wrapping is necessary to avoid stacking up parameters. -- `side` is either "left" or "right". local function hopper_pull_double(side) return function(pos, hop_pos, hop_inv, hop_list) - local node = minetest.get_node(pos) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() + local top_inv, bottom_inv = get_chest_inventories(pos, side) - 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 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) + local stack_id = mcl_util.select_stack(top_inv, "main", hop_inv, hop_list) if stack_id ~= nil then - return ret_inv1, "main", stack_id + return top_inv, "main", stack_id end - stack_id = mcl_util.select_stack(ret_inv2, "main", hop_inv, hop_list) - return ret_inv2, "main", stack_id + stack_id = mcl_util.select_stack(bottom_inv, "main", hop_inv, hop_list) + return bottom_inv, "main", stack_id end end local function hopper_push_double(side) return function(pos, hop_pos, hop_inv, hop_list) - local node = minetest.get_node(pos) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() + local top_inv, bottom_inv = get_chest_inventories(pos, side) - 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 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) + local stack_id = mcl_util.select_stack(hop_inv, hop_list, top_inv, "main", nil, 1) if stack_id ~= nil then - return ret_inv1, "main", stack_id + return top_inv, "main", stack_id end - stack_id = mcl_util.select_stack(hop_inv, hop_list, ret_inv2, "main", nil, 1) - return ret_inv2, "main", stack_id + stack_id = mcl_util.select_stack(hop_inv, hop_list, bottom_inv, "main", nil, 1) + return bottom_inv, "main", stack_id end end 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 -- BEGIN OF LISTRING WORKAROUND elseif listname == "input" then - local inv = minetest.get_inventory({ type = "node", pos = pos }) - local other_pos = get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, side) - 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 + local top_inv, bottom_inv = get_chest_inventories(pos, side) + return limit_put(stack, top_inv, bottom_inv) -- END OF LISTRING WORKAROUND else 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)) -- BEGIN OF LISTRING WORKAROUND if listname == "input" then - local inv = minetest.get_inventory({ type = "node", pos = pos }) - local other_pos = get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, side) - local other_inv = minetest.get_inventory({ type = "node", pos = other_pos }) - - 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 + local top_inv, bottom_inv = get_chest_inventories(pos, side) + top_inv:set_stack("input", 1, nil) + double_chest_add_item(top_inv, bottom_inv, "main", stack) end -- END OF LISTRING WORKAROUND end end