Attempt to fix chest minecarts, at least for 5.9 (#4684)

Not using the `RecheckCartHack` on >5.9 seems to help with #4670 - not tested on older minetest; chest minecarts might still be empty there when the block is unloaded in the meantime. For <5.9, maybe it helps to decrease the time interval, 3 seconds seems to fairly long.

This also makes the minecarts random: 40% minecart, 40% chest minecart, 20% tnt minecart.

Reviewed-on: https://git.minetest.land/VoxeLibre/VoxeLibre/pulls/4684
Reviewed-by: the-real-herowl <the-real-herowl@noreply.git.minetest.land>
Co-authored-by: kno10 <erich.schubert@gmail.com>
Co-committed-by: kno10 <erich.schubert@gmail.com>
This commit is contained in:
kno10 2024-11-10 11:41:19 +01:00 committed by the-real-herowl
parent c428fa576b
commit f6f5481f30
2 changed files with 20 additions and 7 deletions

@ -36,7 +36,11 @@ else
end
end
tsm_railcorridors.carts = { "mcl_minecarts:chest_minecart" }
tsm_railcorridors.carts = {
"mcl_minecarts:minecart", "mcl_minecarts:minecart",
"mcl_minecarts:chest_minecart", "mcl_minecarts:chest_minecart",
"mcl_minecarts:tnt_minecart"
}
-- This is called after a spawner has been placed by the game.
-- Use this to properly set up the metadata and stuff.
@ -54,9 +58,11 @@ end
function tsm_railcorridors.on_construct_cart(_, cart, pr_carts)
local l = cart:get_luaentity()
local inv = mcl_entity_invs.load_inv(l,27)
local items = tsm_railcorridors.get_treasures(pr_carts)
mcl_loot.fill_inventory(inv, "main", items, pr_carts)
mcl_entity_invs.save_inv(l)
if inv then -- otherwise probably not a chest minecart
local items = tsm_railcorridors.get_treasures(pr_carts)
mcl_loot.fill_inventory(inv, "main", items, pr_carts)
mcl_entity_invs.save_inv(l)
end
end
-- Fallback function. Returns a random treasure. This function is called for chests

@ -397,7 +397,9 @@ end
-- This is a workaround thanks to the fact that minetest.add_entity is unreliable as fuck
-- See: https://github.com/minetest/minetest/issues/4759
-- FIXME: Kill this horrible hack with fire as soon you can.
local function RecheckCartHack(params)
local RecheckCartHack = nil
if not minetest.features.random_state_restore then -- proxy for minetest > 5.9.0, this feature will not be removed
RecheckCartHack = function(params)
local pos = params[1]
local cart_id = params[2]
-- Find cart
@ -412,6 +414,7 @@ local function RecheckCartHack(params)
end
minetest.log("info", "[tsm_railcorridors] Cart spawn FAILED: "..minetest.pos_to_string(pos))
end
end
-- Try to place a cobweb.
-- pos: Position of cobweb
@ -935,13 +938,17 @@ local function spawn_carts()
-- See <https://github.com/minetest/minetest/issues/4759>
local cart_id = tsm_railcorridors.carts[cart_type]
minetest.log("info", "[tsm_railcorridors] Cart spawn attempt: "..minetest.pos_to_string(cpos))
minetest.add_entity(cpos, cart_id)
local obj = minetest.add_entity(cpos, cart_id)
-- This checks if the cart is actually spawned, it's a giant hack!
-- Note that the callback function is also called there.
-- TODO: Move callback function to this position when the
-- minetest.add_entity bug has been fixed (supposedly in 5.9.0?)
minetest.after(3, RecheckCartHack, {cpos, cart_id})
if RecheckCartHack then
minetest.after(3, RecheckCartHack, {cpos, cart_id})
else
tsm_railcorridors.on_construct_cart(cpos, obj, pr_carts)
end
end
end
carts_table = {}