-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathareas.lua
102 lines (83 loc) · 2.42 KB
/
areas.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- id -> priv
local priv_areas = {}
function priv_protector.get_area_priv(id)
return priv_areas[id]
end
-- protection check
local old_is_protected = minetest.is_protected
function minetest.is_protected(pos, name)
local area_list
if areas.config.use_smallest_area_precedence then
local _, id = areas:getSmallestAreaAtPos(pos)
area_list = id and { [id] = true } or {}
else
area_list = areas:getAreasAtPos(pos)
end
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
priv_areas = 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 matcher = param:gmatch("(%S+)")
local id_str = matcher()
local priv = matcher()
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 or "<none>")
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,
})
areas:registerOnRemove(function(id)
priv_areas[id] = nil
save_priv_areas()
end)