From f6f5481f30e685446539c40ed71294e5e7dda8fc Mon Sep 17 00:00:00 2001 From: kno10 Date: Sun, 10 Nov 2024 11:41:19 +0100 Subject: [PATCH] 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 Co-authored-by: kno10 Co-committed-by: kno10 --- mods/MAPGEN/tsm_railcorridors/gameconfig.lua | 14 ++++++++++---- mods/MAPGEN/tsm_railcorridors/init.lua | 13 ++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/mods/MAPGEN/tsm_railcorridors/gameconfig.lua b/mods/MAPGEN/tsm_railcorridors/gameconfig.lua index 2e80c60c8..9f924f00b 100644 --- a/mods/MAPGEN/tsm_railcorridors/gameconfig.lua +++ b/mods/MAPGEN/tsm_railcorridors/gameconfig.lua @@ -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 diff --git a/mods/MAPGEN/tsm_railcorridors/init.lua b/mods/MAPGEN/tsm_railcorridors/init.lua index 66b4d779b..9895ab44c 100644 --- a/mods/MAPGEN/tsm_railcorridors/init.lua +++ b/mods/MAPGEN/tsm_railcorridors/init.lua @@ -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 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 = {}