From 351f1834a3f4c608c207a8b14184ccf32e79a07c Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 24 Nov 2017 07:04:03 +0100 Subject: [PATCH] Add Spawn API --- mods/ITEMS/mcl_beds/api.lua | 2 ++ mods/ITEMS/mcl_beds/depends.txt | 1 + mods/ITEMS/mcl_beds/functions.lua | 19 ++----------- mods/ITEMS/mcl_portals/depends.txt | 1 + mods/ITEMS/mcl_portals/portal_end.lua | 12 +------- mods/PLAYER/mcl_spawn/depends.txt | 1 + mods/PLAYER/mcl_spawn/description.txt | 1 + mods/PLAYER/mcl_spawn/init.lua | 40 +++++++++++++++++++++++++++ mods/PLAYER/mcl_spawn/mod.conf | 1 + 9 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 mods/PLAYER/mcl_spawn/depends.txt create mode 100644 mods/PLAYER/mcl_spawn/description.txt create mode 100644 mods/PLAYER/mcl_spawn/init.lua create mode 100644 mods/PLAYER/mcl_spawn/mod.conf diff --git a/mods/ITEMS/mcl_beds/api.lua b/mods/ITEMS/mcl_beds/api.lua index ed2869cc5..a8a87c448 100644 --- a/mods/ITEMS/mcl_beds/api.lua +++ b/mods/ITEMS/mcl_beds/api.lua @@ -215,3 +215,5 @@ function mcl_beds.register_bed(name, def) doc.add_entry_alias("nodes", name.."_bottom", "nodes", name.."_top") end + + diff --git a/mods/ITEMS/mcl_beds/depends.txt b/mods/ITEMS/mcl_beds/depends.txt index d58adae72..17d201545 100644 --- a/mods/ITEMS/mcl_beds/depends.txt +++ b/mods/ITEMS/mcl_beds/depends.txt @@ -4,3 +4,4 @@ mcl_wool? mcl_dye? mcl_tnt? mcl_weather? +mcl_spawn? diff --git a/mods/ITEMS/mcl_beds/functions.lua b/mods/ITEMS/mcl_beds/functions.lua index d4e78eaea..365a399f7 100644 --- a/mods/ITEMS/mcl_beds/functions.lua +++ b/mods/ITEMS/mcl_beds/functions.lua @@ -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() diff --git a/mods/ITEMS/mcl_portals/depends.txt b/mods/ITEMS/mcl_portals/depends.txt index 22f14f67b..f794e5f9c 100644 --- a/mods/ITEMS/mcl_portals/depends.txt +++ b/mods/ITEMS/mcl_portals/depends.txt @@ -4,5 +4,6 @@ mcl_core mcl_nether mcl_end mcl_particles +mcl_spawn awards? doc? diff --git a/mods/ITEMS/mcl_portals/portal_end.lua b/mods/ITEMS/mcl_portals/portal_end.lua index 24a58a7d4..c0291ba0d 100644 --- a/mods/ITEMS/mcl_portals/portal_end.lua +++ b/mods/ITEMS/mcl_portals/portal_end.lua @@ -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 diff --git a/mods/PLAYER/mcl_spawn/depends.txt b/mods/PLAYER/mcl_spawn/depends.txt new file mode 100644 index 000000000..3b355984e --- /dev/null +++ b/mods/PLAYER/mcl_spawn/depends.txt @@ -0,0 +1 @@ +mcl_init diff --git a/mods/PLAYER/mcl_spawn/description.txt b/mods/PLAYER/mcl_spawn/description.txt new file mode 100644 index 000000000..6712e4432 --- /dev/null +++ b/mods/PLAYER/mcl_spawn/description.txt @@ -0,0 +1 @@ +Set and get the player's respawn position diff --git a/mods/PLAYER/mcl_spawn/init.lua b/mods/PLAYER/mcl_spawn/init.lua new file mode 100644 index 000000000..8271ed6bd --- /dev/null +++ b/mods/PLAYER/mcl_spawn/init.lua @@ -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) + diff --git a/mods/PLAYER/mcl_spawn/mod.conf b/mods/PLAYER/mcl_spawn/mod.conf new file mode 100644 index 000000000..ff54191b2 --- /dev/null +++ b/mods/PLAYER/mcl_spawn/mod.conf @@ -0,0 +1 @@ +name = mcl_spawn