Make mod localizable
[minetest_tutorial_subgame.git] / mods / areas / chatcommands.lua
blob2edc38f1950d75b404dba685843cfe8799ddecbf
2 minetest.register_chatcommand("protect", {
3 params = "<AreaName>",
4 description = "Protect your own area",
5 privs = {[areas.config.self_protection_privilege]=true},
6 func = function(name, param)
7 if param == "" then
8 return false, "Invalid usage, see /help protect."
9 end
10 local pos1, pos2 = areas:getPos(name)
11 if not (pos1 and pos2) then
12 return false, "You need to select an area first."
13 end
15 minetest.log("action", "/protect invoked, owner="..name..
16 " AreaName="..param..
17 " StartPos="..minetest.pos_to_string(pos1)..
18 " EndPos=" ..minetest.pos_to_string(pos2))
20 local canAdd, errMsg = areas:canPlayerAddArea(pos1, pos2, name)
21 if not canAdd then
22 return false, "You can't protect that area: "..errMsg
23 end
25 local id = areas:add(name, param, pos1, pos2, nil)
26 areas:save()
28 return true, "Area protected. ID: "..id
29 end
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(.+)$')
42 if not ownerName then
43 return false, "Incorrect usage, see /help set_owner."
44 end
46 local pos1, pos2 = areas:getPos(name)
47 if not (pos1 and pos2) then
48 return false, "You need to select an area first."
49 end
51 if not areas:player_exists(ownerName) then
52 return false, "The player \""
53 ..ownerName.."\" does not exist."
54 end
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)
62 areas:save()
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
68 end
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+) ([^ ]+) (.+)$')
81 if not pid then
82 minetest.chat_send_player(name, "Incorrect usage, see /help add_owner")
83 return
84 end
86 local pos1, pos2 = areas:getPos(name)
87 if not (pos1 and pos2) then
88 return false, "You need to select an area first."
89 end
91 if not areas:player_exists(ownerName) then
92 return false, "The player \""..ownerName.."\" does not exist."
93 end
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
101 pid = tonumber(pid)
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)
108 areas:save()
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(.+)$")
123 if not id then
124 return false, "Invalid usage, see /help rename_area."
127 id = tonumber(id)
128 if not id then
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
137 areas:save()
138 return true, "Area renamed."
143 minetest.register_chatcommand("find_areas", {
144 params = "<regexp>",
145 description = "Find areas using a Lua regular expression",
146 privs = areas.adminPrivs,
147 func = function(name, param)
148 if param == "" then
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."
160 local matches = {}
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)
167 if #matches > 0 then
168 return true, table.concat(matches, "\n")
169 else
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", {
195 params = "<id>",
196 description = "Recursively remove areas using an id",
197 func = function(name, param)
198 local id = tonumber(param)
199 if not id then
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)
210 areas:save()
211 return true, "Removed area "..id.." and it's sub areas."
216 minetest.register_chatcommand("remove_area", {
217 params = "<id>",
218 description = "Remove an area using an id",
219 func = function(name, param)
220 local id = tonumber(param)
221 if not id then
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."
230 areas:remove(id)
231 areas:save()
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+)$")
242 if not id then
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."
252 id = tonumber(id)
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
258 areas:save()
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", {
268 params = "<ID>",
269 description = "Toggle an area open (anyone can interact) or closed",
270 func = function(name, param)
271 local id = tonumber(param)
272 if not id then
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
283 areas:save()
284 return true, ("Area %s."):format(open and "opened" or "closed")
289 minetest.register_chatcommand("move_area", {
290 params = "<ID>",
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)
295 if not id then
296 return false, "Invalid usage, see /help move_area."
299 local area = areas.areas[id]
300 if not area then
301 return false, "Area does not exist."
304 local pos1, pos2 = areas:getPos(name)
305 if not pos1 then
306 return false, "You need to select an area first."
309 area.pos1 = pos1
310 area.pos2 = pos2
311 areas:save()
312 return true, "Area successfully moved."
313 end,