Create mcl_util.metadata_timer, fix crashes, add checks to prevent hoppers from pulling from carts that are not in the square above it

This commit is contained in:
teknomunk 2024-04-12 13:31:01 +00:00
parent d571f6d8f2
commit 137c0d0c49
2 changed files with 28 additions and 40 deletions

View File

@ -1208,4 +1208,16 @@ function mcl_util.assign_uuid(obj)
return le._uuid return le._uuid
end end
function mcl_util.metadata_timer(meta, name, dtime)
local tick = false
local timer = meta:get_float(name)
if timer < dtime then
tick = true
timer = timer + 1
else
timer = timer - dtime
end
meta:set_float(name, timer)
if not tick then return true end
end

View File

@ -285,7 +285,6 @@ local def_hopper = {
" takes stuff from mcl_hoppers at " .. minetest.pos_to_string(pos)) " takes stuff from mcl_hoppers at " .. minetest.pos_to_string(pos))
end, end,
_mcl_minecarts_on_enter_below = function(pos, cart, next_dir) _mcl_minecarts_on_enter_below = function(pos, cart, next_dir)
print("Cart entered above "..tostring(pos))
-- Only pull to containers -- Only pull to containers
if cart and cart.groups and (cart.groups.container or 0) ~= 0 then if cart and cart.groups and (cart.groups.container or 0) ~= 0 then
cart:add_node_watch(pos) cart:add_node_watch(pos)
@ -293,8 +292,6 @@ local def_hopper = {
end end
end, end,
_mcl_minecarts_on_enter_above = function(pos, cart, next_dir) _mcl_minecarts_on_enter_above = function(pos, cart, next_dir)
print("Cart entered below "..tostring(pos))
-- Only push to containers -- Only push to containers
if cart and cart.groups and (cart.groups.container or 0) ~= 0 then if cart and cart.groups and (cart.groups.container or 0) ~= 0 then
cart:add_node_watch(pos) cart:add_node_watch(pos)
@ -312,25 +309,18 @@ local def_hopper = {
return return
end end
local meta = minetest.get_meta(pos) if not mcl_util.metadata_timer(minetest.get_meta(pos), "minecart_hopper_timer", dtime) then return end
local cart_pos = mcl_minecarts.get_cart_position(cartdata) local cart_pos = mcl_minecarts.get_cart_position(cartdata)
if not cart_pos then return false end
if pos.x ~= cart_pos.x or pos.z ~= cart_pos.z then return end
local timer = meta:get_int("minecart_hopper_timer")
if timer < dtime then
if vector.direction(pos,cart_pos).y > 0 then if vector.direction(pos,cart_pos).y > 0 then
-- The cart is above us, pull from minecart -- The cart is above us, pull from minecart
print("Pulling from cart above "..tostring(pos))
hopper_pull_from_mc(cart, pos, 5) hopper_pull_from_mc(cart, pos, 5)
else else
print("Pushing to cart below "..tostring(pos))
hopper_push_to_mc(cart, pos, 5) hopper_push_to_mc(cart, pos, 5)
end end
timer = timer + 1
else
timer = timer - dtime
end
meta:set_int("minecart_hopper_timer", timer)
return true return true
end, end,
@ -540,11 +530,8 @@ local def_hopper_side = {
sounds = mcl_sounds.node_sound_metal_defaults(), sounds = mcl_sounds.node_sound_metal_defaults(),
_mcl_minecarts_on_enter_below = function(pos, cart, next_dir) _mcl_minecarts_on_enter_below = function(pos, cart, next_dir)
print("Cart entered above "..tostring(pos)..",cart="..tostring(cart))
-- Only push to containers -- Only push to containers
if cart and cart.groups and (cart.groups.container or 0) ~= 0 then if cart and cart.groups and (cart.groups.container or 0) ~= 0 then
print("Pulling from cart above "..tostring(pos))
cart:add_node_watch(pos) cart:add_node_watch(pos)
hopper_pull_from_mc(cart, pos, 5) hopper_pull_from_mc(cart, pos, 5)
end end
@ -583,21 +570,11 @@ local def_hopper_side = {
_mcl_minecarts_node_on_step = function(pos, cart, dtime, cartdata) _mcl_minecarts_node_on_step = function(pos, cart, dtime, cartdata)
if not cart then return end if not cart then return end
local tick = false if not mcl_util.metadata_timer(minetest.get_meta(pos), "minecart_hopper_timer", dtime) then return end
local meta = minetest.get_meta(pos)
local timer = meta:get_int("minecart_hopper_timer")
print("dtime="..dtime..",timer="..timer)
if timer < dtime then
tick = true
timer = timer + 1
else
timer = timer - dtime
end
meta:set_int("minecart_hopper_timer", timer)
if tick then
local cart_pos = mcl_minecarts.get_cart_position(cartdata) local cart_pos = mcl_minecarts.get_cart_position(cartdata)
if not cart_pos then return false end if not cart_pos then return false end
if cart_pos.x ~= pos.x or cart_pos.z ~= pos.x then return end
print("uuid="..cartdata.uuid) print("uuid="..cartdata.uuid)
if cart_pos.y == pos.y then if cart_pos.y == pos.y then
@ -605,7 +582,6 @@ local def_hopper_side = {
elseif cart_pos.y > pos.y then elseif cart_pos.y > pos.y then
hopper_pull_from_mc(cart, pos, 5) hopper_pull_from_mc(cart, pos, 5)
end end
end
return true return true
end, end,