2017-03-08 12:33:22 -08:00
|
|
|
-- spawnpoint/init.lua
|
2017-03-08 12:34:22 -08:00
|
|
|
|
|
|
|
spawnpoint = {}
|
|
|
|
|
|
|
|
local path = minetest.get_worldpath().."/spawnpoint.conf"
|
|
|
|
|
|
|
|
-- [function] Log
|
|
|
|
function spawnpoint.log(content, log_type)
|
|
|
|
if not content then return false end
|
|
|
|
if log_type == nil then log_type = "action" end
|
2017-03-08 12:55:34 -08:00
|
|
|
minetest.log(log_type, "[SpawnPoint] "..content)
|
2017-03-08 12:34:22 -08:00
|
|
|
end
|
2017-03-08 12:48:08 -08:00
|
|
|
|
|
|
|
----------------------
|
|
|
|
-- HELPER FUNCTIONS --
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
-- [function] Load
|
|
|
|
function spawnpoint.load()
|
2017-03-08 12:55:34 -08:00
|
|
|
local res = io.open(path, "r"):read("*all")
|
|
|
|
if res ~= "" then
|
2017-03-08 12:48:08 -08:00
|
|
|
spawnpoint.pos = minetest.string_to_pos(res)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] Save
|
|
|
|
function spawnpoint.save()
|
|
|
|
io.open(path, "w"):write(minetest.pos_to_string(spawnpoint.pos))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] Set
|
|
|
|
function spawnpoint.set(pos)
|
|
|
|
if type(pos) == "string" then
|
|
|
|
pos = minetest.string_to_pos(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(pos) == "table" then
|
|
|
|
spawnpoint.pos = pos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] Bring
|
|
|
|
function spawnpoint.bring(player)
|
|
|
|
if type(player) == "string" then
|
|
|
|
player = minetest.get_player_by_name(player)
|
|
|
|
end
|
|
|
|
|
2017-03-08 12:55:34 -08:00
|
|
|
if player and spawnpoint.pos then
|
2017-03-08 12:48:08 -08:00
|
|
|
local pos = spawnpoint.pos
|
|
|
|
player:setpos({x=pos.x, y=pos.y+0.5, z=pos.z})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-------------------
|
|
|
|
---- CALLBACKS ----
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
spawnpoint.load()
|
|
|
|
|
|
|
|
-- [register] On Shutdown
|
|
|
|
minetest.register_on_shutdown(spawnpoint.save)
|
|
|
|
|
|
|
|
-- [register] On Respawn Player
|
|
|
|
minetest.register_on_respawnplayer(function(player)
|
|
|
|
spawnpoint.bring(player)
|
|
|
|
end)
|
2017-03-08 12:55:34 -08:00
|
|
|
|
|
|
|
-- [register] On New Player
|
|
|
|
minetest.register_on_newplayer(function(player)
|
|
|
|
spawnpoint.bring(player)
|
|
|
|
end)
|