2014-11-16 16:55:13 +01:00
|
|
|
local homes_file = minetest.get_worldpath() .. "/homes"
|
|
|
|
local homepos = {}
|
2014-10-28 18:01:32 +01:00
|
|
|
|
|
|
|
local function loadhomes()
|
2014-11-16 16:55:13 +01:00
|
|
|
local input = io.open(homes_file, "r")
|
2014-10-28 18:01:32 +01:00
|
|
|
if input then
|
|
|
|
repeat
|
|
|
|
local x = input:read("*n")
|
|
|
|
if x == nil then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
local y = input:read("*n")
|
|
|
|
local z = input:read("*n")
|
|
|
|
local name = input:read("*l")
|
2014-11-16 16:55:13 +01:00
|
|
|
homepos[name:sub(2)] = {x = x, y = y, z = z}
|
2014-10-28 18:01:32 +01:00
|
|
|
until input:read(0) == nil
|
|
|
|
io.close(input)
|
|
|
|
else
|
2014-11-16 16:55:13 +01:00
|
|
|
homepos = {}
|
2014-10-28 18:01:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
loadhomes()
|
|
|
|
|
|
|
|
minetest.register_privilege("home", "Can use /sethome and /home")
|
|
|
|
|
|
|
|
local changed = false
|
|
|
|
|
|
|
|
minetest.register_chatcommand("home", {
|
|
|
|
description = "Teleport you to your home point",
|
|
|
|
privs = {home=true},
|
|
|
|
func = function(name)
|
2014-11-16 16:55:13 +01:00
|
|
|
local player = minetest.env:get_player_by_name(name)
|
2014-10-28 18:01:32 +01:00
|
|
|
if player == nil then
|
|
|
|
-- just a check to prevent the server crashing
|
|
|
|
return false
|
|
|
|
end
|
2014-11-16 16:55:13 +01:00
|
|
|
if homepos[player:get_player_name()] then
|
|
|
|
player:setpos(homepos[player:get_player_name()])
|
|
|
|
minetest.chat_send_player(name, "Teleported to home!")
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(name, "Set a home using /sethome")
|
2014-10-28 18:01:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("sethome", {
|
|
|
|
description = "Set your home point",
|
|
|
|
privs = {home=true},
|
|
|
|
func = function(name)
|
2014-11-16 16:55:13 +01:00
|
|
|
local player = minetest.env:get_player_by_name(name)
|
|
|
|
local pos = player:getpos()
|
|
|
|
homepos[player:get_player_name()] = pos
|
|
|
|
minetest.chat_send_player(name, "Home set!")
|
|
|
|
changed = true
|
2014-10-28 18:01:32 +01:00
|
|
|
if changed then
|
2014-11-16 16:55:13 +01:00
|
|
|
local output = io.open(homes_file, "w")
|
|
|
|
for i, v in pairs(homepos) do
|
|
|
|
output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n")
|
|
|
|
end
|
2014-10-28 18:01:32 +01:00
|
|
|
io.close(output)
|
|
|
|
changed = false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|