From 12b83258969ca611be712fb3764a293b454b785d Mon Sep 17 00:00:00 2001 From: Ajrat Makhmutov Date: Sun, 21 Jul 2024 04:14:30 +0300 Subject: [PATCH] Add hopper mod support --- init.lua | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mod.conf | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 0a63291..8ed68aa 100644 --- a/init.lua +++ b/init.lua @@ -649,3 +649,59 @@ minetest.register_alias("connected_chests:chest_left_locked", "default:chest_loc minetest.register_alias("connected_chests:chest_right_locked", "default:chest_locked_connected_right") minetest.register_alias("connected_chests:chest_locked_left", "default:chest_locked_connected_left") minetest.register_alias("connected_chests:chest_locked_right", "default:chest_locked_connected_right") + +if minetest.get_modpath("hopper") then + local function get_inventory(chest_right_pos) + local node_right = minetest.get_node(chest_right_pos) + if node_right.param2 > 3 then + -- The right connected chest node has an invalid param2 value + -- Cannot determine the inventory + return + end + local x, z = unpack(param_tab2[node_right.param2]) + local chest_left_pos = {x=chest_right_pos.x+x, y=chest_right_pos.y, z=chest_right_pos.z+z} + local node_left = minetest.get_node(chest_left_pos) + if node_left.name ~= "default:chest_connected_left" and node_left.name ~= "default:chest_connected_left_open" then + minetest.log("error","The left chest is not a chest: " .. node_left.name .. " at " .. vector.to_string(chest_left_pos)) + return + end + if node_left.param2 ~= node_right.param2 then + minetest.log("error","The chests are pointing in different directions: node_left.param2:" + .. node_left.param2 .. ", node_right.param2:" .. node_right.param2) + return + end + return minetest.get_meta(chest_left_pos):get_inventory() + end + hopper:add_container({ + {"top", "default:chest_connected_left", "main"}, + {"bottom", "default:chest_connected_left", "main"}, + {"side", "default:chest_connected_left", "main"}, + {"top", "default:chest_connected_right", "main", get_inventory = get_inventory}, + {"bottom", "default:chest_connected_right", "main", get_inventory = get_inventory}, + {"side", "default:chest_connected_right", "main", get_inventory = get_inventory}, + {"top", "default:chest_connected_left_open", "main"}, + {"bottom", "default:chest_connected_left_open", "main"}, + {"side", "default:chest_connected_left_open", "main"}, + {"top", "default:chest_connected_right_open", "main", get_inventory = get_inventory}, + {"bottom", "default:chest_connected_right_open", "main", get_inventory = get_inventory}, + {"side", "default:chest_connected_right_open", "main", get_inventory = get_inventory}, + }) + local function set_hopper_param2(hopper_pos, chest_left_pos) + local param2_by_offset = { + [vector.new(-1, 0, 0):to_string()] = 0, + [vector.new( 0, 0, 1):to_string()] = 1, + [vector.new( 1, 0, 0):to_string()] = 2, + [vector.new( 0, 0,-1):to_string()] = 3, + } + local hopper_param2 = param2_by_offset[(chest_left_pos - hopper_pos):to_string()] + if hopper_param2 then + return hopper_param2 + end + local x, z = unpack(param_tab2[minetest.get_node(chest_left_pos).param2]) + local chest_right_pos = {x=chest_left_pos.x-x, y=chest_left_pos.y, z=chest_left_pos.z-z} + return param2_by_offset[(chest_right_pos - hopper_pos):to_string()] + end + hopper:set_extra_container_info({ + {"default:chest_connected_left", set_hopper_param2 = set_hopper_param2}, + }) +end diff --git a/mod.conf b/mod.conf index a874c1a..bad5138 100644 --- a/mod.conf +++ b/mod.conf @@ -1,3 +1,3 @@ name = connected_chests depends = default -optional_depends = pipeworks,technic +optional_depends = pipeworks,technic,hopper