icyessentials/ess/warp.lua

134 lines
3.1 KiB
Lua

local warps_cache = {}
local warp_privs = minetest.settings:get_bool("ess_privilege_per_warp", false)
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
warps_cache = p
if warp_privs then
for warp in pairs(warps_cache) do
local wp = "ess.warp.warp." .. warp
if not minetest.registered_privileges[wp] then
minetest.register_privilege(wp, {
description = "Warp " .. warp,
give_to_singleplayer = false,
})
end
end
end
return p
end
local function save_warps()
local e = table.copy(warps_cache)
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 warps_cache[name:lower()] then return nil end
return warps_cache[name:lower()]
end
local function set_warp(name, pos)
name = name:lower()
warps_cache[name] = pos
save_warps()
end
local function cmd_warp(name,params,splitparams)
if params == "" then
local warps = {}
for name in pairs(warps_cache) 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.other") then
return false, "You do not have permission to warp other players!"
else
warpee = splitparams[1]
location = splitparams[2]
end
end
if warp_privs and not (ess.priv_match(pname, "ess.warp.warp." .. location) or ess.priv_match(pname, "ess.warp.warp.all")) then
return ess.reject_permission()
end
local exists = warp_exists(location)
if not exists then
return false, string.format("Warp \"%s\" not found.", location)
end
local user = minetest.get_player_by_name(warpee)
if not user then
return false, "No such user."
end
ess.save_player_pos(user)
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, string.format("Warp \"%s\" not found.", location)
end
warps_cache[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.all"] = true,
["ess.warp.other"] = 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")
get_warps()