diff --git a/ess/warp.lua b/ess/warp.lua index 0d45b95..5f3c3a4 100644 --- a/ess/warp.lua +++ b/ess/warp.lua @@ -1,3 +1,118 @@ -local commands = {} +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 = "[] ", + 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 = "", + privs = true, + func = cmd_setwarp, + }, + ["delwarp"] = { + description = "Remove a warp point.", + params = "", + privs = true, + func = cmd_delwarp, + } +} ess.autoregister(commands, "warp") diff --git a/ess_core/init.lua b/ess_core/init.lua index bfd5d80..2084e9e 100644 --- a/ess_core/init.lua +++ b/ess_core/init.lua @@ -74,7 +74,7 @@ local function worlddata_save() end -- Set a player metadata value -function ess.set_world_meta(player, flag, value) +function ess.set_world_meta(flag, value) if not ess.world_meta then worlddata_load() end ess.world_meta[flag] = value worlddata_save() @@ -95,8 +95,20 @@ function ess.reject_permission() return false, "You don't have permission to run this command." end --- Match a single privilege +-- Match a single privilege, but also check ".all" privileges function ess.priv_match(name, priv) + local parts = string.split(priv, ".") + if #parts > 1 then + if minetest.check_player_privs(name, {[parts[1] .. ".all"] = true}) then + return true + end + if #parts > 2 then + if minetest.check_player_privs(name, {[parts[1] .. "." .. parts[2] .. ".all"] = true}) then + return true + end + end + end + return minetest.check_player_privs(name, {[priv] = true}) end