area priv protection

master
Thomas Rudin 2019-12-26 19:13:51 +01:00
parent 18b3b6aed7
commit 4acff2d95d
4 changed files with 96 additions and 1 deletions

85
areas.lua Normal file
View File

@ -0,0 +1,85 @@
-- id -> priv
local priv_areas = {}
-- protection check
local old_is_protected = minetest.is_protected
function minetest.is_protected(pos, name)
local area_list = areas:getAreasAtPos(pos)
for id in pairs(area_list) do
local required_priv = priv_areas[id]
if required_priv and not minetest.check_player_privs(name, required_priv) then
-- required privililege not met, protected
return true
end
end
return old_is_protected(pos, name)
end
-- File writing / reading utilities
local wpath = minetest.get_worldpath()
local filename = wpath.."/priv_areas.dat"
local function load_priv_areas()
local f = io.open(filename, "r")
if f == nil then return {} end
local t = f:read("*all")
f:close()
if t == "" or t == nil then return {} end
return minetest.deserialize(t)
end
local function save_priv_areas()
local f = io.open(filename, "w")
f:write(minetest.serialize(priv_areas))
f:close()
end
load_priv_areas()
-- chat
minetest.register_chatcommand("area_priv_set", {
params = "<ID> <priv>",
description = "Set the required priv for the area",
func = function(playername, param)
local _, _, id_str, priv = string.find(param, "^([^%s]+)%s+([^%s]+)%s*$")
if id_str == nil then
return true, "Invalid syntax!"
end
local id = tonumber(id_str)
if not id then
return true, "area-id is not numeric: " .. id_str
end
if not areas:isAreaOwner(id, playername) and
not minetest.check_player_privs(playername, { protection_bypas=true }) then
return true, "you are not the owner of area: " .. id
end
priv_areas[id] = priv
save_priv_areas()
return true, "Area " .. id .. " required privilege: " .. priv
end,
})
minetest.register_chatcommand("area_priv_get", {
params = "<ID>",
description = "Returns required priv of an area",
func = function(_, param)
if param == nil then
return true, "Invalid syntax!"
end
local id = tonumber(param)
if not id then
return true, "area-id is not numeric: " .. param
end
return true, "Area " .. id .. " required priv: " .. (priv_areas[id] or "<none>")
end,
})

View File

@ -1,4 +1,5 @@
default
protector?
doors?
mesecons_mvps?
mesecons_mvps?
areas?

View File

@ -3,5 +3,8 @@ local MP = minetest.get_modpath("priv_protector")
dofile(MP.."/protector.lua")
if minetest.get_modpath("areas") then
dofile(MP.."/areas.lua")
end
print("[OK] priv protector")

View File

@ -14,3 +14,9 @@ minetest.register_privilege("protect_streets", {
Place the priv_protector block and change the "Privilege" field to `protect_streets` or whatever you want to protect with
# Area integration
Additional `area` mod commands:
* `area_priv_set` Sets a privilege for the area (the area needs to be opened with `/area_open <id>` for this to work)
* `area_priv_get` Returns the privilege, if any, required for that area