Fix crash if item dropped in end portal in the end

This commit is contained in:
Wuzzy 2019-03-09 10:04:54 +01:00
parent 4f2baf8506
commit eff91038e8
2 changed files with 40 additions and 26 deletions

View file

@ -239,9 +239,13 @@ minetest.register_abm({
local target local target
if dim == "end" then if dim == "end" then
-- End portal in the End: -- End portal in the End:
-- Teleport back to the player's spawn in the Overworld. -- Teleport back to the player's spawn or world spawn in the Overworld.
target = mcl_spawn.get_spawn_pos(obj) if obj:is_player() then
target = mcl_spawn.get_spawn_pos(obj)
else
target = mcl_spawn.get_world_spawn_pos(obj)
end
else else
-- End portal in any other dimension: -- End portal in any other dimension:
-- Teleport to the End at a fixed position and generate a -- Teleport to the End at a fixed position and generate a

View file

@ -2,10 +2,39 @@ mcl_spawn = {}
local mg_name = minetest.get_mapgen_setting("mg_name") local mg_name = minetest.get_mapgen_setting("mg_name")
-- Returns current custom spawn position of player. local cached_world_spawn
-- Returns nil if player has no custom spawn position.
-- If player is nil or not a player, the default spawn point is returned. mcl_spawn.get_world_spawn_pos = function()
-- The second return value is true if spawn point is player-chosen, local spawn
spawn = minetest.setting_get_pos("static_spawnpoint")
if spawn then
return spawn
end
if cached_world_spawn then
return cached_world_spawn
end
-- 32 attempts to find a suitable spawn point
spawn = { x=math.random(-16, 16), y=8, z=math.random(-16, 16) }
for i=1, 32 do
local y = minetest.get_spawn_level(spawn.x, spawn.z)
if y then
spawn.y = y
cached_world_spawn = spawn
minetest.log("action", "[mcl_spawn] Dynamic world spawn determined to be "..minetest.pos_to_string(spawn))
return spawn
end
-- Random walk
spawn.x = spawn.x + math.random(-64, 64)
spawn.z = spawn.z + math.random(-64, 64)
end
minetest.log("action", "[mcl_spawn] Failed to determine dynamic world spawn!")
-- Use dummy position if nothing found
return { x=math.random(-16, 16), y=8, z=math.random(-16, 16) }
end
-- Returns a spawn position of player.
-- If player is nil or not a player, a world spawn point is returned.
-- The second return value is true if returned spawn point is player-chosen,
-- false otherwise. -- false otherwise.
mcl_spawn.get_spawn_pos = function(player) mcl_spawn.get_spawn_pos = function(player)
local spawn, custom_spawn = nil, false local spawn, custom_spawn = nil, false
@ -17,23 +46,9 @@ mcl_spawn.get_spawn_pos = function(player)
end end
end end
if not spawn or spawn == "" then if not spawn or spawn == "" then
spawn = minetest.setting_get_pos("static_spawnpoint") spawn = mcl_spawn.get_world_spawn_pos()
custom_spawn = false custom_spawn = false
end end
-- We are getting desperate ...
-- Use the first spawn point of the player
if not spawn or spawn == "" then
local attr = player:get_meta():get_string("mcl_spawn:first_spawn")
if attr ~= nil and attr ~= "" then
-- Adjust Y
spawn = minetest.string_to_pos(attr)
local y = minetest.get_spawn_level(spawn.x, spawn.z)
if y then
spawn.y = y
end
custom_spawn = false
end
end
return spawn, custom_spawn return spawn, custom_spawn
end end
@ -105,8 +120,3 @@ minetest.register_on_respawnplayer(function(player)
end end
end) end)
minetest.register_on_newplayer(function(player)
-- Remember where the player spawned first
player:get_meta():set_string("mcl_spawn:first_spawn", minetest.pos_to_string(player:get_pos()))
end)