diff --git a/areas.lua b/areas.lua new file mode 100644 index 0000000..ec35689 --- /dev/null +++ b/areas.lua @@ -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 = " ", + 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 = "", + 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 "") + end, +}) diff --git a/depends.txt b/depends.txt index 1aab81e..cdb1888 100644 --- a/depends.txt +++ b/depends.txt @@ -1,4 +1,5 @@ default protector? doors? -mesecons_mvps? \ No newline at end of file +mesecons_mvps? +areas? diff --git a/init.lua b/init.lua index 3e6281d..8a6c74f 100644 --- a/init.lua +++ b/init.lua @@ -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") diff --git a/readme.md b/readme.md index 0ccac1f..357564b 100644 --- a/readme.md +++ b/readme.md @@ -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 ` for this to work) +* `area_priv_get` Returns the privilege, if any, required for that area