2 minetest
.register_chatcommand("protect", {
4 description
= "Protect your own area",
5 privs
= {[areas
.config
.self_protection_privilege
]=true},
6 func
= function(name
, param
)
8 return false, "Invalid usage, see /help protect."
10 local pos1
, pos2
= areas
:getPos(name
)
11 if not (pos1
and pos2
) then
12 return false, "You need to select an area first."
15 minetest
.log("action", "/protect invoked, owner="..name
..
17 " StartPos="..minetest
.pos_to_string(pos1
)..
18 " EndPos=" ..minetest
.pos_to_string(pos2
))
20 local canAdd
, errMsg
= areas
:canPlayerAddArea(pos1
, pos2
, name
)
22 return false, "You can't protect that area: "..errMsg
25 local id
= areas
:add(name
, param
, pos1
, pos2
, nil)
28 return true, "Area protected. ID: "..id
33 minetest
.register_chatcommand("set_owner", {
34 params
= "<PlayerName> <AreaName>",
35 description
= "Protect an area beetween two positions and give"
36 .." a player access to it without setting the parent of the"
37 .." area to any existing area",
38 privs
= areas
.adminPrivs
,
39 func
= function(name
, param
)
40 local ownerName
, areaName
= param
:match('^(%S+)%s(.+)$')
43 return false, "Incorrect usage, see /help set_owner."
46 local pos1
, pos2
= areas
:getPos(name
)
47 if not (pos1
and pos2
) then
48 return false, "You need to select an area first."
51 if not areas
:player_exists(ownerName
) then
52 return false, "The player \""
53 ..ownerName
.."\" does not exist."
56 minetest
.log("action", name
.." runs /set_owner. Owner = "..ownerName
..
57 " AreaName = "..areaName
..
58 " StartPos = "..minetest
.pos_to_string(pos1
)..
59 " EndPos = " ..minetest
.pos_to_string(pos2
))
61 local id
= areas
:add(ownerName
, areaName
, pos1
, pos2
, nil)
64 minetest
.chat_send_player(ownerName
,
65 "You have been granted control over area #"..
66 id
..". Type /list_areas to show your areas.")
67 return true, "Area protected. ID: "..id
72 minetest
.register_chatcommand("add_owner", {
73 params
= "<ParentID> <Player> <AreaName>",
74 description
= "Give a player access to a sub-area beetween two"
75 .." positions that have already been protected,"
76 .." Use set_owner if you don't want the parent to be set.",
77 func
= function(name
, param
)
78 local pid
, ownerName
, areaName
79 = param
:match('^(%d+) ([^ ]+) (.+)$')
82 minetest
.chat_send_player(name
, "Incorrect usage, see /help add_owner")
86 local pos1
, pos2
= areas
:getPos(name
)
87 if not (pos1
and pos2
) then
88 return false, "You need to select an area first."
91 if not areas
:player_exists(ownerName
) then
92 return false, "The player \""..ownerName
.."\" does not exist."
95 minetest
.log("action", name
.." runs /add_owner. Owner = "..ownerName
..
96 " AreaName = "..areaName
.." ParentID = "..pid
..
97 " StartPos = "..pos1
.x
..","..pos1
.y
..","..pos1
.z
..
98 " EndPos = " ..pos2
.x
..","..pos2
.y
..","..pos2
.z
)
100 -- Check if this new area is inside an area owned by the player
102 if (not areas
:isAreaOwner(pid
, name
)) or
103 (not areas
:isSubarea(pos1
, pos2
, pid
)) then
104 return false, "You can't protect that area."
107 local id
= areas
:add(ownerName
, areaName
, pos1
, pos2
, pid
)
110 minetest
.chat_send_player(ownerName
,
111 "You have been granted control over area #"..
112 id
..". Type /list_areas to show your areas.")
113 return true, "Area protected. ID: "..id
118 minetest
.register_chatcommand("rename_area", {
119 params
= "<ID> <newName>",
120 description
= "Rename a area that you own",
121 func
= function(name
, param
)
122 local id
, newName
= param
:match("^(%d+)%s(.+)$")
124 return false, "Invalid usage, see /help rename_area."
129 return false, "That area doesn't exist."
132 if not areas
:isAreaOwner(id
, name
) then
133 return true, "You don't own that area."
136 areas
.areas
[id
].name
= newName
138 return true, "Area renamed."
143 minetest
.register_chatcommand("find_areas", {
145 description
= "Find areas using a Lua regular expression",
146 privs
= areas
.adminPrivs
,
147 func
= function(name
, param
)
149 return false, "A regular expression is required."
152 -- Check expression for validity
153 local function testRegExp()
154 ("Test [1]: Player (0,0,0) (0,0,0)"):find(param
)
156 if not pcall(testRegExp
) then
157 return false, "Invalid regular expression."
161 for id
, area
in pairs(areas
.areas
) do
162 local str
= areas
:toString(id
)
163 if str
:find(param
) then
164 table.insert(matches
, str
)
168 return true, table.concat(matches
, "\n")
170 return true, "No matches found."
176 minetest
.register_chatcommand("list_areas", {
177 description
= "List your areas, or all areas if you are an admin.",
178 func
= function(name
, param
)
179 local admin
= minetest
.check_player_privs(name
, areas
.adminPrivs
)
180 local areaStrings
= {}
181 for id
, area
in pairs(areas
.areas
) do
182 if admin
or areas
:isAreaOwner(id
, name
) then
183 table.insert(areaStrings
, areas
:toString(id
))
186 if #areaStrings
== 0 then
187 return true, "No visible areas."
189 return true, table.concat(areaStrings
, "\n")
194 minetest
.register_chatcommand("recursive_remove_areas", {
196 description
= "Recursively remove areas using an id",
197 func
= function(name
, param
)
198 local id
= tonumber(param
)
200 return false, "Invalid usage, see"
201 .." /help recursive_remove_areas"
204 if not areas
:isAreaOwner(id
, name
) then
205 return false, "Area "..id
.." does not exist or is"
206 .." not owned by you."
209 areas
:remove(id
, true)
211 return true, "Removed area "..id
.." and it's sub areas."
216 minetest
.register_chatcommand("remove_area", {
218 description
= "Remove an area using an id",
219 func
= function(name
, param
)
220 local id
= tonumber(param
)
222 return false, "Invalid usage, see /help remove_area"
225 if not areas
:isAreaOwner(id
, name
) then
226 return false, "Area "..id
.." does not exist or"
227 .." is not owned by you."
232 return true, "Removed area "..id
237 minetest
.register_chatcommand("change_owner", {
238 params
= "<ID> <NewOwner>",
239 description
= "Change the owner of an area using it's ID",
240 func
= function(name
, param
)
241 local id
, newOwner
= param
:match("^(%d+)%s(%S+)$")
243 return false, "Invalid usage, see"
244 .." /help change_owner."
247 if not areas
:player_exists(newOwner
) then
248 return false, "The player \""..newOwner
249 .."\" does not exist."
253 if not areas
:isAreaOwner(id
, name
) then
254 return false, "Area "..id
.." does not exist"
255 .." or is not owned by you."
257 areas
.areas
[id
].owner
= newOwner
259 minetest
.chat_send_player(newOwner
,
260 ("%s has given you control over the area %q (ID %d).")
261 :format(name
, areas
.areas
[id
].name
, id
))
262 return true, "Owner changed."
267 minetest
.register_chatcommand("area_open", {
269 description
= "Toggle an area open (anyone can interact) or closed",
270 func
= function(name
, param
)
271 local id
= tonumber(param
)
273 return false, "Invalid usage, see /help area_open."
276 if not areas
:isAreaOwner(id
, name
) then
277 return false, "Area "..id
.." does not exist"
278 .." or is not owned by you."
280 local open
= not areas
.areas
[id
].open
281 -- Save false as nil to avoid inflating the DB.
282 areas
.areas
[id
].open
= open
or nil
284 return true, ("Area %s."):format(open
and "opened" or "closed")
289 minetest
.register_chatcommand("move_area", {
291 description
= "Move (or resize) an area to the current positions.",
292 privs
= areas
.adminPrivs
,
293 func
= function(name
, param
)
294 local id
= tonumber(param
)
296 return false, "Invalid usage, see /help move_area."
299 local area
= areas
.areas
[id
]
301 return false, "Area does not exist."
304 local pos1
, pos2
= areas
:getPos(name
)
306 return false, "You need to select an area first."
312 return true, "Area successfully moved."