2017-11-24 07:04:03 +01:00
|
|
|
mcl_spawn = {}
|
|
|
|
|
2019-02-08 22:44:26 +01:00
|
|
|
local mg_name = minetest.get_mapgen_setting("mg_name")
|
|
|
|
|
2019-02-11 15:49:36 +01:00
|
|
|
-- Returns current custom spawn position of player.
|
|
|
|
-- Returns nil if player has no custom spawn position.
|
2017-11-24 07:04:03 +01:00
|
|
|
-- If player is nil or not a player, the default spawn point is returned.
|
2019-02-07 19:54:12 +01:00
|
|
|
-- The second return value is true if spawn point is player-chosen,
|
|
|
|
-- false otherwise.
|
2017-11-24 07:04:03 +01:00
|
|
|
mcl_spawn.get_spawn_pos = function(player)
|
2019-02-11 15:49:36 +01:00
|
|
|
local spawn, custom_spawn = nil, false
|
2017-11-24 07:04:03 +01:00
|
|
|
if player ~= nil and player:is_player() then
|
2019-03-06 05:45:16 +01:00
|
|
|
local attr = player:get_meta():get_string("mcl_beds:spawn")
|
2019-02-11 15:49:36 +01:00
|
|
|
if attr ~= nil and attr ~= "" then
|
|
|
|
spawn = minetest.string_to_pos(attr)
|
|
|
|
custom_spawn = true
|
|
|
|
end
|
2017-11-24 07:04:03 +01:00
|
|
|
end
|
|
|
|
if not spawn or spawn == "" then
|
|
|
|
spawn = minetest.setting_get_pos("static_spawnpoint")
|
2019-02-07 19:54:12 +01:00
|
|
|
custom_spawn = false
|
2017-11-24 07:04:03 +01:00
|
|
|
end
|
2019-03-08 05:12:31 +01:00
|
|
|
-- We are getting desperate ...
|
|
|
|
-- Use the first spawn point of the player
|
2019-02-11 15:49:36 +01:00
|
|
|
if not spawn or spawn == "" then
|
2019-03-06 05:45:16 +01:00
|
|
|
local attr = player:get_meta():get_string("mcl_spawn:first_spawn")
|
2019-02-11 15:49:36 +01:00
|
|
|
if attr ~= nil and attr ~= "" then
|
2019-03-08 05:12:31 +01:00
|
|
|
-- Adjust Y
|
2019-02-11 15:49:36 +01:00
|
|
|
spawn = minetest.string_to_pos(attr)
|
2019-03-08 05:12:31 +01:00
|
|
|
local y = minetest.get_spawn_level(spawn.x, spawn.z)
|
|
|
|
if y then
|
|
|
|
spawn.y = y
|
|
|
|
end
|
2019-02-11 15:49:36 +01:00
|
|
|
custom_spawn = false
|
2017-11-24 07:04:03 +01:00
|
|
|
end
|
|
|
|
end
|
2019-02-07 19:54:12 +01:00
|
|
|
return spawn, custom_spawn
|
2017-11-24 07:04:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Sets the player's spawn position to pos.
|
|
|
|
-- Set pos to nil to clear the spawn position.
|
2019-02-20 19:39:12 +01:00
|
|
|
-- If message is set, informs the player with a chat message when the spawn position
|
|
|
|
-- changed.
|
|
|
|
mcl_spawn.set_spawn_pos = function(player, pos, message)
|
|
|
|
local spawn_changed = false
|
2019-03-06 05:45:16 +01:00
|
|
|
local meta = player:get_meta()
|
2017-11-24 07:04:03 +01:00
|
|
|
if pos == nil then
|
2019-03-06 05:45:16 +01:00
|
|
|
if meta:get_string("mcl_beds:spawn") ~= "" then
|
2019-02-20 19:39:12 +01:00
|
|
|
spawn_changed = true
|
|
|
|
if message then
|
|
|
|
minetest.chat_send_player(player:get_player_name(), "Respawn position cleared!")
|
|
|
|
end
|
|
|
|
end
|
2019-03-06 05:45:16 +01:00
|
|
|
meta:set_string("mcl_beds:spawn", "")
|
2017-11-24 07:04:03 +01:00
|
|
|
else
|
2019-03-06 05:45:16 +01:00
|
|
|
local oldpos = minetest.string_to_pos(meta:get_string("mcl_beds:spawn"))
|
2019-02-20 19:39:12 +01:00
|
|
|
if oldpos then
|
|
|
|
-- We don't bother sending a message if the new spawn pos is basically the same
|
|
|
|
if vector.distance(pos, oldpos) > 0.1 then
|
|
|
|
spawn_changed = true
|
|
|
|
if message then
|
|
|
|
minetest.chat_send_player(player:get_player_name(), "New respawn position set!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-06 05:45:16 +01:00
|
|
|
meta:set_string("mcl_beds:spawn", minetest.pos_to_string(pos))
|
2017-11-24 07:04:03 +01:00
|
|
|
end
|
2019-02-20 19:39:12 +01:00
|
|
|
return spawn_changed
|
2017-11-24 07:04:03 +01:00
|
|
|
end
|
|
|
|
|
2019-02-28 15:35:18 +01:00
|
|
|
local function get_far_node(pos)
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
if node.name ~= "ignore" then
|
|
|
|
return node
|
|
|
|
end
|
|
|
|
minetest.get_voxel_manip():read_from_map(pos, pos)
|
|
|
|
return minetest.get_node(pos)
|
|
|
|
end
|
|
|
|
|
2017-11-24 07:04:03 +01:00
|
|
|
-- Respawn player at specified respawn position
|
|
|
|
minetest.register_on_respawnplayer(function(player)
|
2019-02-07 19:54:12 +01:00
|
|
|
local pos, custom_spawn = mcl_spawn.get_spawn_pos(player)
|
2019-02-11 15:49:36 +01:00
|
|
|
if pos and custom_spawn then
|
2019-02-04 16:53:06 +01:00
|
|
|
-- Check if bed is still there
|
|
|
|
-- and the spawning position is free of solid or damaging blocks.
|
2019-02-28 15:35:18 +01:00
|
|
|
local node_bed = get_far_node(pos)
|
|
|
|
local node_up1 = get_far_node({x=pos.x,y=pos.y+1,z=pos.z})
|
|
|
|
local node_up2 = get_far_node({x=pos.x,y=pos.y+2,z=pos.z})
|
2019-02-04 16:53:06 +01:00
|
|
|
local bgroup = minetest.get_item_group(node_bed.name, "bed")
|
|
|
|
local def1 = minetest.registered_nodes[node_up1.name]
|
|
|
|
local def2 = minetest.registered_nodes[node_up2.name]
|
|
|
|
if (bgroup == 1 or bgroup == 2) and
|
|
|
|
(not def1.walkable) and (not def2.walkable) and
|
|
|
|
(def1.damage_per_second == nil or def2.damage_per_second <= 0) and
|
|
|
|
(def1.damage_per_second == nil or def2.damage_per_second <= 0) then
|
|
|
|
player:set_pos(pos)
|
|
|
|
return true
|
|
|
|
else
|
2019-02-05 00:07:39 +01:00
|
|
|
-- Forget spawn if bed was missing
|
|
|
|
if (bgroup ~= 1 and bgroup ~= 2) then
|
|
|
|
mcl_spawn.set_spawn_pos(player, nil)
|
|
|
|
end
|
2019-02-04 16:53:06 +01:00
|
|
|
minetest.chat_send_player(player:get_player_name(), "Your spawn bed was missing or blocked.")
|
|
|
|
end
|
2017-11-24 07:04:03 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-11-26 15:04:31 +01:00
|
|
|
minetest.register_on_newplayer(function(player)
|
2019-02-11 15:49:36 +01:00
|
|
|
-- Remember where the player spawned first
|
2019-03-06 05:45:16 +01:00
|
|
|
player:get_meta():set_string("mcl_spawn:first_spawn", minetest.pos_to_string(player:get_pos()))
|
2017-11-26 15:04:31 +01:00
|
|
|
end)
|
2019-02-11 15:49:36 +01:00
|
|
|
|