icyessentials/ess/warp.lua

119 lines
2.6 KiB
Lua

local warpcache = nil
local function get_warps()
local p = ess.get_world_meta("warps")
if not p then
p = {}
end
for name,pos in pairs(p) do
if type(pos) == "string" then
p[name] = minetest.string_to_pos(pos)
end
end
warpcache = p
return p
end
local function save_warps()
if not warpcache then return end
local e = table.copy(warpcache)
for name,pos in pairs(e) do
e[name] = minetest.pos_to_string(pos)
end
ess.set_world_meta("warps", e)
end
local function warp_exists(name)
if not warpcache then get_warps() end
if not warpcache[name:lower()] then return nil end
return warpcache[name:lower()]
end
local function set_warp(name, pos)
name = name:lower()
if not warpcache then get_warps() end
warpcache[name] = pos
save_warps()
end
local function cmd_warp(name,params,splitparams)
if params == "" then
if not warpcache then get_warps() end
local warps = {}
for name in pairs(warpcache) do
table.insert(warps, name)
end
return true, "Warps: " .. table.concat(warps, ", ")
end
local warpee = name
local location = params
if #splitparams > 1 then
if splitparams[1] ~= name and not ess.priv_match(name, "ess.warp.warp.other") then
return false, "You do not have permission to warp other players!"
else
warpee = splitparams[1]
location = splitparams[2]
end
end
local exists = warp_exists(location)
if not exists then
return false, "Warp \""..location.."\" not found."
end
local user = minetest.get_player_by_name(warpee)
if not user then
return false, "No such user."
end
user:set_pos(exists)
return true, "Warped to "..location.."."
end
local function cmd_setwarp(name,params,splitparams)
local user = minetest.get_player_by_name(name)
local pos = user:get_pos()
set_warp(splitparams[1], pos)
return true, "Successfully set the warp point."
end
local function cmd_delwarp(name,params,splitparams)
local location = splitparams[1]
local exists = warp_exists(location:lower())
if not exists then
return false, "Warp \""..location.."\" not found."
end
warpcache[location] = nil
save_warps()
return true, "Successfully removed the warp point."
end
local commands = {
["warp"] = {
description = "Warp to a location.",
params = "[<player>] <location>",
aliases = {"warps"},
privs = {
["ess.warp.warp"] = true,
["ess.warp.warp.other"] = true,
},
save_player_pos = true,
func = cmd_warp,
},
["setwarp"] = {
description = "Set a warp to the current location.",
params = "<location>",
privs = true,
func = cmd_setwarp,
},
["delwarp"] = {
description = "Remove a warp point.",
params = "<location>",
privs = true,
func = cmd_delwarp,
}
}
ess.autoregister(commands, "warp")