Skip to content

Commit c328478

Browse files
RobertZenzest31
authored andcommittedOct 22, 2015
Add more ways to pass data to check_player_privs
The callback can now be invoked with either the player object or name as the first parameter, and with either a table or a list of strings, like this: minetest.check_player_privs(player_name, { shout = true, fly = true }) minetest.check_player_privs(player_name, "shout", "fly") minetest.check_player_privs(player, { shout = true, fly = true }) minetest.check_player_privs(player, "shout", "fly")
1 parent 37c1e20 commit c328478

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed
 

Diff for: ‎builtin/game/misc.lua

+24-5
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,37 @@ function core.after(time, func, ...)
7474
}
7575
end
7676

77-
function core.check_player_privs(name, privs)
77+
function core.check_player_privs(player_or_name, ...)
78+
local name = player_or_name
79+
-- Check if we have been provided with a Player object.
80+
if type(name) ~= "string" then
81+
name = name:get_player_name()
82+
end
83+
84+
local requested_privs = {...}
7885
local player_privs = core.get_player_privs(name)
7986
local missing_privileges = {}
80-
for priv, val in pairs(privs) do
81-
if val
82-
and not player_privs[priv] then
83-
table.insert(missing_privileges, priv)
87+
88+
if type(requested_privs[1]) == "table" then
89+
-- We were provided with a table like { privA = true, privB = true }.
90+
for priv, value in pairs(requested_privs[1]) do
91+
if value and not player_privs[priv] then
92+
table.insert(missing_privileges, priv)
93+
end
94+
end
95+
else
96+
-- Only a list, we can process it directly.
97+
for key, priv in pairs(requested_privs) do
98+
if not player_privs[priv] then
99+
table.insert(missing_privileges, priv)
100+
end
84101
end
85102
end
103+
86104
if #missing_privileges > 0 then
87105
return false, missing_privileges
88106
end
107+
89108
return true, ""
90109
end
91110

Diff for: ‎doc/lua_api.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1924,8 +1924,11 @@ Call these functions only at load time!
19241924
* `minetest.set_player_privs(name, {priv1=true,...})`
19251925
* `minetest.get_player_privs(name) -> {priv1=true,...}`
19261926
* `minetest.auth_reload()`
1927-
* `minetest.check_player_privs(name, {priv1=true,...})`: returns `bool, missing_privs`
1928-
* A quickhand for checking privileges
1927+
* `minetest.check_player_privs(player_or_name, ...)`: returns `bool, missing_privs`
1928+
* A quickhand for checking privileges.
1929+
* `player_or_name`: Either a Player object or the name of a player.
1930+
* `...` is either a list of strings, e.g. `"priva", "privb"` or
1931+
a table, e.g. `{ priva = true, privb = true }`.
19291932
* `minetest.get_player_ip(name)`: returns an IP address string
19301933

19311934
`minetest.set_player_password`, `minetest_set_player_privs`, `minetest_get_player_privs`

0 commit comments

Comments
 (0)
Please sign in to comment.