From 51bfb07d48740ec63b133f43c69f9bf32fc2d3fb Mon Sep 17 00:00:00 2001 From: teknomunk Date: Mon, 11 Mar 2024 00:34:26 +0000 Subject: [PATCH] Hopper minecarts pull from containers above rail --- mods/CORE/mcl_util/init.lua | 27 +++++++++++++++++++++++++++ mods/ENTITIES/mcl_minecarts/init.lua | 25 +++++++++++++++++++------ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 6143bfb41..dc56aca55 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -359,6 +359,33 @@ function mcl_util.hopper_push(pos, dst_pos) return ok end +function mcl_util.hopper_pull_to_inventory(hop_inv, hop_list, src_pos) + -- TODO: merge together with hopper_pull after https://git.minetest.land/MineClone2/MineClone2/pulls/4190 is merged + -- Get node pos' for item transfer + local src = minetest.get_node(src_pos) + if not minetest.registered_nodes[src.name] then return end + local src_type = minetest.get_item_group(src.name, "container") + if src_type ~= 2 then return end + local src_def = minetest.registered_nodes[src.name] + + local src_list = 'main' + local src_inv, stack_id + + if src_def._mcl_hoppers_on_try_pull then + src_inv, src_list, stack_id = src_def._mcl_hoppers_on_try_pull(src_pos, pos, hop_inv, hop_list) + else + local src_meta = minetest.get_meta(src_pos) + src_inv = src_meta:get_inventory() + stack_id = mcl_util.select_stack(src_inv, src_list, hop_inv, hop_list) + end + + if stack_id ~= nil then + local ok = mcl_util.move_item(src_inv, src_list, stack_id, hop_inv, hop_list) + if src_def._mcl_hoppers_on_after_pull then + src_def._mcl_hoppers_on_after_pull(src_pos) + end + end +end -- Try pulling from source inventory to hopper inventory ---@param pos Vector ---@param src_pos Vector diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index 33ff7dcfe..1346e502b 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -31,10 +31,10 @@ local function handle_cart_enter(self,pos) end -- Handle above-track behaviors (to ensure hoppers can transfer at least one item) - pos = pos + vector.new(0,1,0) - local node = minetest.get_node(pos) + local above_pos = pos + vector.new(0,1,0) + local node = minetest.get_node(above_pos) if node_def._mcl_minecarts_on_enter_below then - node_def._mcl_minecarts_on_enter_below(pos, cart) + node_def._mcl_minecarts_on_enter_below(above_pos, cart) end -- Handle cart-specific behaviors @@ -116,7 +116,7 @@ local function do_movement_step(self, remaining_distance) staticdata.connected_at = pos -- Enter the new node - handle_cart_enter(self,pos) + handle_cart_enter(self, pos) -- check for hopper under the rail local under_pos = pos - vector.new(0,1,0) @@ -639,6 +639,9 @@ local function register_entity(entity_id, def) staticdata = make_staticdata() self._staticdata = staticdata end + if (staticdata.hopper_delay or 0) > 0 then + staticdata.hopper_delay = staticdata.hopper_delay - dtime + end local pos, rou_pos, node = self.object:get_pos() --local update = {} @@ -835,7 +838,7 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer) le._staticdata = make_staticdata( railtype, railpos, cart_dir ) end - handle_cart_enter( railpos ) + handle_cart_enter(le, railpos) local pname = "" if placer then @@ -1083,7 +1086,17 @@ register_minecart({ on_rightclick = nil, on_activate_by_rail = nil, _mcl_minecarts_on_enter = function(self,pos) - -- TODO: try to pull from containers into our inventory + local staticdata = self._staticdata + if (staticdata.hopper_delay or 0) > 0 then + return + end + + -- try to pull from containers into our inventory + local inv = mcl_entity_invs.load_inv(self,5) + local above_pos = pos + vector.new(0,1,0) + mcl_util.hopper_pull_to_inventory(inv, 'main', above_pos) + + staticdata.hopper_delay = (staticdata.hopper_delay or 0) + (1/20) end, creative = true })