Privileges allow server owners to grant or revoke the right to do certain actions.
A privilege should give a player the right to do something. They are not for indicating class or status.
The main admin of a server (the name set by the name
setting) has all privileges
given to them.
Good:
Bad:
minetest.register_privilege("vote", {
description = "Can vote on issues",
give_to_singleplayer = true
})
If give_to_singleplayer
is true, then you can remove it as that’s the default
value when not specified:
minetest.register_privilege("vote", {
description = "Can vote on issues"
})
There is a quicker way of checking that a player has all the required privileges:
local has, missing = minetest.check_player_privs(player_or_name, {
interact = true,
vote = true })
has
is true if the player has all the privileges needed.
If has
is false, then missing
will contain a dictionary
of missing privileges[checking needed].
if minetest.check_player_privs(name, {interact=true, vote=true}) then
print("Player has all privs!")
else
print("Player is missing some privs!")
end
local has, missing = minetest.check_player_privs(name, {
interact = true,
vote = true })
if has then
print("Player has all privs!")
else
print("Player is missing privs: " .. dump(missing))
end
You can get a table containing a player’s privileges using minetest.get_player_privs
:
local privs = minetest.get_player_privs(name)
print(dump(privs))
This works whether or not a player is logged in.
Running that example may give the following:
{
fly = true,
interact = true,
shout = true
}
To set a player’s privs, you use minetest.set_player_privs
:
minetest.set_player_privs(name, {
interact = true,
shout = true })
To grant a player some privs, you would use a mixture of those two:
local privs = minetest.get_player_privs(name)
privs.vote = true
minetest.set_player_privs(name, privs)
To allow people with basic_privs to grant and revoke your priv, you’ll need to edit builtin/game/chatcommands.lua:
In both grant and revoke, change the following if statement:
if priv ~= "interact" and priv ~= "shout" and
not core.check_player_privs(name, {privs=true}) then
return false, "Your privileges are insufficient."
end
For example, to add vote:
if priv ~= "interact" and priv ~= "shout" and priv ~= "vote" and
not core.check_player_privs(name, {privs=true}) then
return false, "Your privileges are insufficient."
end