icyessentials/ess/teleport.lua

197 lines
5.0 KiB
Lua
Raw Normal View History

2019-11-11 16:52:54 +00:00
local ess_tpa_timeout = tonumber(minetest.settings:get("ess_tpa_timeout")) or 120
local tpask = {
requests = {},
muted = {},
}
local function cmd_top(name)
local curr_pos = minetest.get_player_by_name(name):getpos()
curr_pos.y = math.ceil(curr_pos.y) + 0.5
while minetest.get_node(curr_pos).name ~= "ignore" do
curr_pos.y = curr_pos.y + 1
end
curr_pos.y = curr_pos.y - 0.5
while minetest.get_node(curr_pos).name == "air" do
curr_pos.y = curr_pos.y - 1
end
curr_pos.y = curr_pos.y + 0.5
minetest.get_player_by_name(name):set_pos(curr_pos)
return true, "Teleported to top."
end
local function cmd_back(name)
local pos = minetest.string_to_pos(ess.get_player_meta(name, "position"))
if not pos then
return false, "Could not return to previous position."
end
2019-11-11 16:52:54 +00:00
local p = minetest.get_player_by_name(name)
ess.save_player_pos(p)
p:set_pos(pos)
return true, "Teleported back."
end
local function cmd_tpcommon(name,tname,direction)
if name == tname then
return false, "Cannot teleport to self."
end
local target = minetest.get_player_by_name(tname)
if not target then
return false, "Could not find player."
end
2019-11-11 16:52:54 +00:00
if tpask.requests[tname] and tpask.requests[tname].when > minetest.get_us_time() - ess_tpa_timeout * 100000 then
return false, "There are currently pending requests regarding this player. Please wait."
end
if not tpask.muted[tname] then
local message = "to teleport to you"
if direction == 2 then
message = "you to teleport to them"
end
minetest.chat_send_player(tname, name .. " has requested "..message..".")
minetest.chat_send_player(tname, "Run /tpaccept to accept or /tpadeny to deny.")
2019-11-11 16:52:54 +00:00
if ess_tpa_timeout > 0 then
minetest.chat_send_player(tname, "You have "..ess_tpa_timeout.." seconds to respond.")
end
2019-11-11 16:52:54 +00:00
tpask.requests[tname] = {when = minetest.get_gametime()}
if direction == 1 then
tpask.requests[tname].who = name
else
tpask.requests[tname].to = name
end
end
return true, "Teleport request sent to " .. tname .. ".."
end
local function cmd_tpask(name,tname)
return cmd_tpcommon(name,tname,1)
end
local function cmd_tpaskhere(name,tname)
return cmd_tpcommon(name,tname,2)
end
local function cmd_tpaconfirm(name)
local reqs = tpask.requests[name]
local me = minetest.get_player_by_name(name)
2019-11-11 16:52:54 +00:00
if not reqs or (tpask.timeout > 0 and tpask.requests[name].when < minetest.get_gametime() - ess_tpa_timeout) then
return false, "You have no pending teleport requests."
end
local who = reqs.to or reqs.who
local whoplayer = minetest.get_player_by_name(who)
if not whoplayer then
tpask.requests[name] = nil
return false, "You have no pending teleport requests."
end
if reqs.to then
minetest.chat_send_player(name, "Teleporting..")
2019-11-11 16:52:54 +00:00
ess.save_player_pos(me)
me:set_pos(whoplayer:get_pos())
else
minetest.chat_send_player(who, "Teleporting..")
2019-11-11 16:52:54 +00:00
ess.save_player_pos(whoplayer)
whoplayer:set_pos(me:get_pos())
end
tpask.requests[name] = nil
return true
end
local function cmd_tpadeny(name)
local reqs = tpask.requests[name]
2019-11-11 16:52:54 +00:00
if not reqs or tpask.requests[name].when < minetest.get_gametime() - tpask.timeout then
return false, "You have no pending teleport requests."
end
tpask.requests[name] = nil
return true, "Declined teleport request successfully. Use /tpmute to silence teleport requests."
end
local function cmd_tpamute(name)
if tpask.muted[name] then
tpask.muted[name] = nil
return true, "Unmuted teleport requests successfully."
end
tpask.muted[name] = "*"
return true, "Muted teleport requests successfully."
end
-- Use builtin tele
local cmd_teleport = minetest.registered_chatcommands["teleport"].func
local commands = {
["tpask"] = {
description = "Request to teleport to the specified player.",
params = "<player>",
aliases = {"tpa"},
privs = {
["ess.teleport.tpa"] = true,
},
func = cmd_tpask,
},
["tpaskhere"] = {
description = "Request to teleport the specified player to you.",
params = "<player>",
aliases = {"tpahere"},
privs = {
["ess.teleport.tpahere"] = true,
},
func = cmd_tpaskhere,
},
["tpaccept"] = {
description = "Accept a teleport request.",
aliases = {"tpayes"},
func = cmd_tpaconfirm,
},
["tpdeny"] = {
description = "Reject a teleport request.",
aliases = {"tpno"},
func = cmd_tpadeny,
},
["tpmute"] = {
description = "Silence/unsilence teleport requests.",
func = cmd_tpamute,
},
["back"] = {
description = "Teleports you to your location prior to tp/spawn/warp.",
aliases = {"return"},
privs = true,
func = cmd_back,
},
["top"] = {
description = "Teleport to the highest node at your current position.",
privs = true,
save_player_pos = true,
func = cmd_top,
},
["teleport"] = {
description = "Teleport to position or player",
aliases = {"tp", "tele", "tp2p"},
params = "<X>,<Y>,<Z> | <to_name> | (<name> <X>,<Y>,<Z>) | (<name> <to_name>)",
privs = {
["teleport"] = true,
["ess.teleport"] = true,
},
override = true,
save_player_pos = true,
func = cmd_teleport,
}
}
ess.autoregister(commands, "teleport")