-
Notifications
You must be signed in to change notification settings - Fork 6
/
command_mgr.lua
285 lines (246 loc) · 8.16 KB
/
command_mgr.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
local sp = simple_protection
local S = sp.S
local commands = {}
function sp.register_subcommand(name, func)
if commands[name] then
minetest.log("info", "[simple_protection] Overwriting chat command " .. name)
end
assert(#name:split(" ") == 1, "Invalid name")
assert(type(func) == "function")
commands[name] = func
end
minetest.register_chatcommand("area", {
description = S("Manages all of your areas."),
privs = {interact = true},
func = function(name, param)
if param == "" or param == "help" then
local function chat_send(desc, cmd)
minetest.chat_send_player(name, desc .. ": "
.. minetest.colorize("#0FF", cmd))
end
local privs = minetest.get_player_privs(name)
minetest.chat_send_player(name, minetest.colorize("#0F0",
"=> " .. S("Available area commands")))
chat_send(S("Information about this area"), "/area show")
chat_send(S("View of surrounding areas"), "/area radar")
chat_send(S("(Un)share one area"), "/area (un)share <name>")
chat_send(S("(Un)share all areas"), "/area (un)shareall <name>")
if sp.area_list or privs.simple_protection then
chat_send(S("List claimed areas"), "/area list [<name>]")
end
chat_send(S("Unclaim this area"), "/area unclaim")
if privs.server then
chat_send(S("Delete all areas of a player"), "/area delete <name>")
end
return
end
local args = param:split(" ", 2)
local func = commands[args[1]]
if not func then
return false, S("Unknown command parameter: @1. Check '/area' for correct usage.", args[1])
end
return func(name, args[2])
end,
})
sp.register_subcommand("show", function(name, param)
local player = minetest.get_player_by_name(name)
local player_pos = player:get_pos()
local data = sp.get_claim(player_pos)
minetest.add_entity(sp.get_center(player_pos), "simple_protection:marker")
local minp, maxp = sp.get_area_bounds(player_pos)
minetest.chat_send_player(name, S("Vertical from Y @1 to @2",
tostring(minp.y), tostring(maxp.y)))
if not data then
if sp.underground_limit and minp.y < sp.underground_limit then
return true, S("Area status: @1", S("Not claimable"))
end
return true, S("Area status: @1", S("Unowned (!)"))
end
minetest.chat_send_player(name, S("Area status: @1", S("Owned by @1", data.owner)))
local v = {}
for _, player in ipairs(data.shared) do
v[#v + 1] = player
end
local pdata = sp.get_player_data(data.owner)
for _, player in ipairs(pdata.shared) do
v[#v + 1] = player .. "*"
end
if #v > 0 then
return true, S("Players with access: @1", table.concat(v, ", "))
end
end)
local function check_ownership(name)
local player = minetest.get_player_by_name(name)
local data, index = sp.get_claim(player:get_pos())
if not data then
return false, S("This area is not claimed yet.")
end
local priv = minetest.check_player_privs(name, {simple_protection=true})
if name ~= data.owner and not priv then
return false, S("You do not own this area.")
end
return true, data, index
end
local function table_erase(t, e)
if not t or not e then
return false
end
local removed = false
for i, v in ipairs(t) do
if v == e then
table.remove(t, i)
removed = true
end
end
return removed
end
sp.register_subcommand("share", function(name, param)
if not param or name == param then
return false, S("No player name given.")
end
if not minetest.get_auth_handler().get_auth(param) and param ~= "*all" then
return false, S("Unknown player.")
end
local success, data, index = check_ownership(name)
if not success then
return success, data
end
if sp.is_shared(name, param) then
return true, S("@1 already has access to all your areas.", param)
end
if sp.is_shared(data, param) then
return true, S("@1 already has access to this area.", param)
end
table.insert(data.shared, param)
sp.set_claim(data, index)
if minetest.get_player_by_name(param) then
minetest.chat_send_player(param, S("@1 shared an area with you.", name))
end
return true, S("@1 has now access to this area.", param)
end)
sp.register_subcommand("unshare", function(name, param)
if not param or name == param or param == "" then
return false, S("No player name given.")
end
local success, data, index = check_ownership(name)
if not success then
return success, data
end
if not sp.is_shared(data, param) then
return true, S("That player has no access to this area.")
end
table_erase(data.shared, param)
sp.set_claim(data, index)
if minetest.get_player_by_name(param) then
minetest.chat_send_player(param, S("@1 unshared an area with you.", name))
end
return true, S("@1 has no longer access to this area.", param)
end)
sp.register_subcommand("shareall", function(name, param)
if not param or name == param or param == "" then
return false, S("No player name given.")
end
if not minetest.get_auth_handler().get_auth(param) then
if param == "*all" then
return false, S("You can not share all your areas with everybody.")
end
return false, S("Unknown player.")
end
if sp.is_shared(name, param) then
return true, S("@1 already has now access to all your areas.", param)
end
sp.update_share_all(name, { [param] = true })
if minetest.get_player_by_name(param) then
minetest.chat_send_player(param, S("@1 shared all areas with you.", name))
end
return true, S("@1 has now access to all your areas.", param)
end)
sp.register_subcommand("unshareall", function(name, param)
if not param or name == param or param == "" then
return false, S("No player name given.")
end
local removed = sp.update_share_all(name, { [param] = false }) > 0
-- Unshare each single claim
local claims = sp.get_player_claims(name)
for index, data in pairs(claims) do
if table_erase(data.shared, param) then
removed = true
end
end
if not removed then
return false, S("@1 does not have access to any of your areas.", param)
end
sp.update_claims(claims)
if minetest.get_player_by_name(param) then
minetest.chat_send_player(param, S("@1 unshared all areas with you.", name))
end
return true, S("@1 has no longer access to your areas.", param)
end)
sp.register_subcommand("unclaim", function(name)
local success, data, index = check_ownership(name)
if not success then
return success, data
end
if sp.claim_return and name == data.owner then
local player = minetest.get_player_by_name(name)
local inv = player:get_inventory()
if inv:room_for_item("main", "simple_protection:claim") then
inv:add_item("main", "simple_protection:claim")
end
end
sp.set_claim(nil, index)
return true, S("This area is unowned now.")
end)
sp.register_subcommand("delete", function(name, param)
if not param or name == param or param == "" then
return false, S("No player name given.")
end
if not minetest.check_player_privs(name, {server=true}) then
return false, S("Missing privilege: @1", "server")
end
local removed = {}
-- Remove globally shared areas
if sp.update_share_all(param, "erase") > 0 then
table.insert(removed, S("Globally shared areas"))
end
-- Delete all claims
local claims, count = sp.get_player_claims(param)
for index in pairs(claims) do
claims[index] = false
end
sp.update_claims(claims)
if count > 0 then
table.insert(removed, S("@1 claimed area(s)", tostring(count)))
end
if #removed == 0 then
return false, S("@1 does not own any claimed areas.", param)
end
return true, S("Removed")..": "..table.concat(removed, ", ")
end)
sp.register_subcommand("list", function(name, param)
local has_sp_priv = minetest.check_player_privs(name, {simple_protection=true})
if not sp.area_list and not has_sp_priv then
return false, S("This command is not available.")
end
if not param or param == "" then
param = name
end
if not has_sp_priv and param ~= name then
return false, S("Missing privilege: @1", "simple_protection")
end
local list = {
"" -- Header placeholder
}
local claims = sp.get_player_claims(param)
for index, cdata in pairs(claims) do
local minp, maxp = sp.get_area_bounds(index, true)
table.insert(list, string.format("%5i,%5i,%5i | %s",
math.floor((minp.x + maxp.x) / 2),
math.floor((minp.y + maxp.y) / 2),
math.floor((minp.z + maxp.z) / 2),
table.concat(cdata.shared or {}, ", ")
))
end
list[1] = S("Listing all areas of @1. Amount: @2", param, tostring(#list - 1))
return true, table.concat(list, "\n")
end)