Skip to content

Commit 7ad8ca6

Browse files
authoredMar 29, 2021
Clean up various misleading and/or confusing messages and texts related to priv changes (#11126)
1 parent 7c24a9e commit 7ad8ca6

File tree

2 files changed

+85
-25
lines changed

2 files changed

+85
-25
lines changed
 

‎builtin/game/chat.lua

+78-24
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ core.register_chatcommand("admin", {
167167
end,
168168
})
169169

170+
local function privileges_of(name, privs)
171+
if not privs then
172+
privs = core.get_player_privs(name)
173+
end
174+
local privstr = core.privs_to_string(privs, ", ")
175+
if privstr == "" then
176+
return S("@1 does not have any privileges.", name)
177+
else
178+
return S("Privileges of @1: @2", name, privstr)
179+
end
180+
end
181+
170182
core.register_chatcommand("privs", {
171183
params = S("[<name>]"),
172184
description = S("Show privileges of yourself or another player"),
@@ -176,9 +188,7 @@ core.register_chatcommand("privs", {
176188
if not core.player_exists(name) then
177189
return false, S("Player @1 does not exist.", name)
178190
end
179-
return true, S("Privileges of @1: @2", name,
180-
core.privs_to_string(
181-
core.get_player_privs(name), ", "))
191+
return true, privileges_of(name)
182192
end,
183193
})
184194

@@ -227,7 +237,10 @@ local function handle_grant_command(caller, grantname, grantprivstr)
227237
core.string_to_privs(core.settings:get("basic_privs") or "interact,shout")
228238
for priv, _ in pairs(grantprivs) do
229239
if not basic_privs[priv] and not caller_privs.privs then
230-
return false, S("Your privileges are insufficient.")
240+
return false, S("Your privileges are insufficient. "..
241+
"'@1' only allows you to grant: @2",
242+
"basic_privs",
243+
core.privs_to_string(basic_privs, ', '))
231244
end
232245
if not core.registered_privileges[priv] then
233246
privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n"
@@ -246,15 +259,13 @@ local function handle_grant_command(caller, grantname, grantprivstr)
246259
if grantname ~= caller then
247260
core.chat_send_player(grantname,
248261
S("@1 granted you privileges: @2", caller,
249-
core.privs_to_string(grantprivs, ' ')))
262+
core.privs_to_string(grantprivs, ', ')))
250263
end
251-
return true, S("Privileges of @1: @2", grantname,
252-
core.privs_to_string(
253-
core.get_player_privs(grantname), ' '))
264+
return true, privileges_of(grantname)
254265
end
255266

256267
core.register_chatcommand("grant", {
257-
params = S("<name> (<privilege> | all)"),
268+
params = S("<name> (<privilege> [, <privilege2> [<...>]] | all)"),
258269
description = S("Give privileges to player"),
259270
func = function(name, param)
260271
local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
@@ -266,7 +277,7 @@ core.register_chatcommand("grant", {
266277
})
267278

268279
core.register_chatcommand("grantme", {
269-
params = S("<privilege> | all"),
280+
params = S("<privilege> [, <privilege2> [<...>]] | all"),
270281
description = S("Grant privileges to yourself"),
271282
func = function(name, param)
272283
if param == "" then
@@ -286,16 +297,13 @@ local function handle_revoke_command(caller, revokename, revokeprivstr)
286297
return false, S("Player @1 does not exist.", revokename)
287298
end
288299

289-
local revokeprivs = core.string_to_privs(revokeprivstr)
290300
local privs = core.get_player_privs(revokename)
291-
local basic_privs =
292-
core.string_to_privs(core.settings:get("basic_privs") or "interact,shout")
293-
for priv, _ in pairs(revokeprivs) do
294-
if not basic_privs[priv] and not caller_privs.privs then
295-
return false, S("Your privileges are insufficient.")
296-
end
297-
end
298301

302+
local revokeprivs = core.string_to_privs(revokeprivstr)
303+
local is_singleplayer = core.is_singleplayer()
304+
local is_admin = not is_singleplayer
305+
and revokename == core.settings:get("name")
306+
and revokename ~= ""
299307
if revokeprivstr == "all" then
300308
revokeprivs = privs
301309
privs = {}
@@ -305,27 +313,73 @@ local function handle_revoke_command(caller, revokename, revokeprivstr)
305313
end
306314
end
307315

316+
local privs_unknown = ""
317+
local basic_privs =
318+
core.string_to_privs(core.settings:get("basic_privs") or "interact,shout")
319+
local irrevokable = {}
320+
local has_irrevokable_priv = false
321+
for priv, _ in pairs(revokeprivs) do
322+
if not basic_privs[priv] and not caller_privs.privs then
323+
return false, S("Your privileges are insufficient. "..
324+
"'@1' only allows you to revoke: @2",
325+
"basic_privs",
326+
core.privs_to_string(basic_privs, ', '))
327+
end
328+
local def = core.registered_privileges[priv]
329+
if not def then
330+
privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n"
331+
elseif is_singleplayer and def.give_to_singleplayer then
332+
irrevokable[priv] = true
333+
elseif is_admin and def.give_to_admin then
334+
irrevokable[priv] = true
335+
end
336+
end
337+
for priv, _ in pairs(irrevokable) do
338+
revokeprivs[priv] = nil
339+
has_irrevokable_priv = true
340+
end
341+
if privs_unknown ~= "" then
342+
return false, privs_unknown
343+
end
344+
if has_irrevokable_priv then
345+
if is_singleplayer then
346+
core.chat_send_player(caller,
347+
S("Note: Cannot revoke in singleplayer: @1",
348+
core.privs_to_string(irrevokable, ', ')))
349+
elseif is_admin then
350+
core.chat_send_player(caller,
351+
S("Note: Cannot revoke from admin: @1",
352+
core.privs_to_string(irrevokable, ', ')))
353+
end
354+
end
355+
356+
local revokecount = 0
308357
for priv, _ in pairs(revokeprivs) do
309358
-- call the on_revoke callbacks
310359
core.run_priv_callbacks(revokename, priv, caller, "revoke")
360+
revokecount = revokecount + 1
311361
end
312362

313363
core.set_player_privs(revokename, privs)
364+
local new_privs = core.get_player_privs(revokename)
365+
366+
if revokecount == 0 then
367+
return false, S("No privileges were revoked.")
368+
end
369+
314370
core.log("action", caller..' revoked ('
315371
..core.privs_to_string(revokeprivs, ', ')
316372
..') privileges from '..revokename)
317373
if revokename ~= caller then
318374
core.chat_send_player(revokename,
319375
S("@1 revoked privileges from you: @2", caller,
320-
core.privs_to_string(revokeprivs, ' ')))
376+
core.privs_to_string(revokeprivs, ', ')))
321377
end
322-
return true, S("Privileges of @1: @2", revokename,
323-
core.privs_to_string(
324-
core.get_player_privs(revokename), ' '))
378+
return true, privileges_of(revokename, new_privs)
325379
end
326380

327381
core.register_chatcommand("revoke", {
328-
params = S("<name> (<privilege> | all)"),
382+
params = S("<name> (<privilege> [, <privilege2> [<...>]] | all)"),
329383
description = S("Remove privileges from player"),
330384
privs = {},
331385
func = function(name, param)
@@ -338,7 +392,7 @@ core.register_chatcommand("revoke", {
338392
})
339393

340394
core.register_chatcommand("revokeme", {
341-
params = S("<privilege> | all"),
395+
params = S("<privilege> [, <privilege2> [<...>]] | all"),
342396
description = S("Revoke privileges from yourself"),
343397
privs = {},
344398
func = function(name, param)

‎builtin/game/privileges.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ end
3232

3333
core.register_privilege("interact", S("Can interact with things and modify the world"))
3434
core.register_privilege("shout", S("Can speak in chat"))
35-
core.register_privilege("basic_privs", S("Can modify 'shout' and 'interact' privileges"))
35+
36+
local basic_privs =
37+
core.string_to_privs((core.settings:get("basic_privs") or "shout,interact"))
38+
local basic_privs_desc = S("Can modify basic privileges (@1)",
39+
core.privs_to_string(basic_privs, ', '))
40+
core.register_privilege("basic_privs", basic_privs_desc)
41+
3642
core.register_privilege("privs", S("Can modify privileges"))
3743

3844
core.register_privilege("teleport", {

0 commit comments

Comments
 (0)
Please sign in to comment.