Add Spawn API

This commit is contained in:
Wuzzy 2017-11-24 07:04:03 +01:00
parent 124e1aac94
commit 351f1834a3
9 changed files with 51 additions and 27 deletions

View file

@ -215,3 +215,5 @@ function mcl_beds.register_bed(name, def)
doc.add_entry_alias("nodes", name.."_bottom", "nodes", name.."_top")
end

View file

@ -4,3 +4,4 @@ mcl_wool?
mcl_dye?
mcl_tnt?
mcl_weather?
mcl_spawn?

View file

@ -1,10 +1,6 @@
local pi = math.pi
local player_in_bed = 0
local is_sp = minetest.is_singleplayer()
local enable_respawn = minetest.settings:get_bool("enable_bed_respawn")
if enable_respawn == nil then
enable_respawn = true
end
local weather_mod = minetest.get_modpath("mcl_weather") ~= nil
-- Helper functions
@ -189,7 +185,9 @@ function mcl_beds.on_rightclick(pos, player)
-- move to bed
if not mcl_beds.player[name] then
lay_down(player, ppos, pos)
player:set_attribute("mcl_beds:spawn", minetest.pos_to_string(player:getpos())) -- save respawn position when entering bed
if minetest.get_modpath("mcl_spawn") then
mcl_spawn.set_spawn_pos(player:get_pos()) -- save respawn position when entering bed
end
else
lay_down(player, nil, nil, false)
end
@ -211,17 +209,6 @@ end
-- Callbacks
-- Only register respawn callback if respawn enabled
if enable_respawn then
-- respawn player at bed if enabled and valid position is found
minetest.register_on_respawnplayer(function(player)
local pos = minetest.string_to_pos(player:get_attribute("mcl_beds:spawn"))
if pos then
player:setpos(pos)
return true
end
end)
end
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()

View file

@ -4,5 +4,6 @@ mcl_core
mcl_nether
mcl_end
mcl_particles
mcl_spawn
awards?
doc?

View file

@ -217,18 +217,8 @@ minetest.register_abm({
if dim == "end" then
-- End portal in the End:
-- Teleport back to the player's spawn in the Overworld.
-- TODO: Implement better spawn point detection
target = minetest.string_to_pos(obj:get_attribute("mcl_beds:spawn"))
if not target then
target = minetest.setting_get_pos("static_spawnpoint")
end
if not target then
target = { x=0, y=0, z=0 }
if mg_name == "flat" then
target.y = mcl_vars.mg_bedrock_overworld_max + 5
end
end
target = mcl_spawn.get_spawn_pos(obj)
else
-- End portal in any other dimension:
-- Teleport to the End at a fixed position and generate a

View file

@ -0,0 +1 @@
mcl_init

View file

@ -0,0 +1 @@
Set and get the player's respawn position

View file

@ -0,0 +1,40 @@
mcl_spawn = {}
-- Returns current spawn position of player.
-- If player is nil or not a player, the default spawn point is returned.
mcl_spawn.get_spawn_pos = function(player)
local spawn
if player ~= nil and player:is_player() then
spawn = minetest.string_to_pos(player:get_attribute("mcl_beds:spawn"))
end
if not spawn or spawn == "" then
spawn = minetest.setting_get_pos("static_spawnpoint")
end
if not spawn then
spawn = { x=0, y=0, z=0 }
if mg_name == "flat" then
spawn.y = mcl_vars.mg_bedrock_overworld_max + 5
end
end
return spawn
end
-- Sets the player's spawn position to pos.
-- Set pos to nil to clear the spawn position.
mcl_spawn.set_spawn_pos = function(player, pos)
if pos == nil then
player:set_attribute("mcl_beds:spawn", "")
else
player:set_attribute("mcl_beds:spawn", minetest.pos_to_string(pos))
end
end
-- Respawn player at specified respawn position
minetest.register_on_respawnplayer(function(player)
local pos = mcl_spawn.get_spawn_pos(player)
if pos then
player:set_pos(pos)
return true
end
end)

View file

@ -0,0 +1 @@
name = mcl_spawn