2015-02-06 14:46:53 +01:00
|
|
|
home = {}
|
|
|
|
|
|
|
|
home.homes_file = {["real"] = minetest.get_worldpath() .. "/realhomes",
|
2015-07-13 23:22:02 +02:00
|
|
|
["nether"] = minetest.get_worldpath() .. "/netherhomes"}
|
2015-02-06 14:46:53 +01:00
|
|
|
home.homepos = {["real"] = {}, ["nether"] = {}}
|
2015-07-13 23:22:02 +02:00
|
|
|
home.time = 20 * 60
|
2015-02-06 14:46:53 +01:00
|
|
|
|
|
|
|
home.sethome = function(name)
|
2015-02-18 19:12:36 +01:00
|
|
|
local player = minetest.get_player_by_name(name)
|
2015-02-12 17:19:53 +01:00
|
|
|
local pos = player:getpos()
|
2015-08-24 01:42:31 +02:00
|
|
|
pos.y = pos.y+1
|
2015-02-06 14:46:53 +01:00
|
|
|
local p_status = "real"
|
|
|
|
if pos.y < -19600 then
|
|
|
|
p_status = "nether"
|
|
|
|
end
|
2015-07-13 23:22:02 +02:00
|
|
|
|
|
|
|
local function assign_home()
|
|
|
|
home.homepos[p_status][name] = pos
|
|
|
|
minetest.chat_send_player(name, "Home set!")
|
|
|
|
local output = io.open(home.homes_file[p_status], "w")
|
|
|
|
output:write(minetest.serialize(home.homepos[p_status]))
|
|
|
|
io.close(output)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2015-11-29 23:36:33 +01:00
|
|
|
return action_timers.wrapper(name, "sethome", "sethome_" .. name, home.time, assign_home, {})
|
2015-02-06 14:46:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
home.tohome = function(name)
|
2015-02-18 19:12:36 +01:00
|
|
|
local player = minetest.get_player_by_name(name)
|
2015-02-06 14:46:53 +01:00
|
|
|
if player == nil then
|
|
|
|
-- just a check to prevent the server crashing
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local p_status = "real"
|
|
|
|
if player:getpos().y < -19600 then
|
|
|
|
p_status = "nether"
|
|
|
|
end
|
2015-02-12 17:19:53 +01:00
|
|
|
if home.homepos[p_status][name] then
|
2015-07-13 23:22:02 +02:00
|
|
|
|
|
|
|
local function go_to_home()
|
|
|
|
player:setpos(home.homepos[p_status][player:get_player_name()])
|
|
|
|
minetest.chat_send_player(name, "Teleported to home!")
|
|
|
|
minetest.log("action","Player ".. name .." teleported to home. Next teleportation allowed in ".. home.time .." seconds.")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2015-11-29 23:36:33 +01:00
|
|
|
return action_timers.wrapper(name, "home", "home_" .. name, home.time, go_to_home, {})
|
2015-02-06 14:46:53 +01:00
|
|
|
else
|
|
|
|
minetest.chat_send_player(name, "Set a home using /sethome")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2014-10-28 18:01:32 +01:00
|
|
|
|
|
|
|
local function loadhomes()
|
2015-02-06 14:46:53 +01:00
|
|
|
for key,_ in pairs(home.homes_file) do
|
|
|
|
local input = io.open(home.homes_file[key], "r")
|
2014-11-17 19:31:06 +01:00
|
|
|
if input then
|
2015-02-14 15:20:00 +01:00
|
|
|
-- Old format handler
|
|
|
|
local line = input:read()
|
|
|
|
input:seek("set",0)
|
|
|
|
if line == nil then return end
|
|
|
|
if not line:match("return {") 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")
|
|
|
|
home.homepos[key][name:sub(2)] = {x = x, y = y, z = z}
|
|
|
|
until input:read(0) == nil
|
|
|
|
else
|
|
|
|
home.homepos[key] = minetest.deserialize(input:read())
|
|
|
|
end
|
2014-11-17 19:31:06 +01:00
|
|
|
io.close(input)
|
|
|
|
else
|
2015-02-06 14:46:53 +01:00
|
|
|
home.homepos[key] = {}
|
2014-11-17 19:31:06 +01:00
|
|
|
end
|
|
|
|
end
|
2014-10-28 18:01:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
loadhomes()
|
|
|
|
|
|
|
|
minetest.register_privilege("home", "Can use /sethome and /home")
|
|
|
|
|
|
|
|
minetest.register_chatcommand("home", {
|
|
|
|
description = "Teleport you to your home point",
|
|
|
|
privs = {home=true},
|
2015-02-06 14:46:53 +01:00
|
|
|
func = home.tohome,
|
2014-10-28 18:01:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("sethome", {
|
|
|
|
description = "Set your home point",
|
|
|
|
privs = {home=true},
|
2015-02-06 14:46:53 +01:00
|
|
|
func = home.sethome,
|
2014-10-28 18:01:32 +01:00
|
|
|
})
|
2015-02-14 15:20:00 +01:00
|
|
|
|
|
|
|
minetest.log("action","[sethome] Loaded.")
|