Hopper minecarts pull from containers above rail

This commit is contained in:
teknomunk 2024-03-11 00:34:26 +00:00
parent 0b5e742a9f
commit 2e3de195b2
2 changed files with 46 additions and 6 deletions

View File

@ -335,6 +335,33 @@ function mcl_util.hopper_push(pos, dst_pos)
return false
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

View File

@ -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
})