2016-04-09 16:27:06 +03:00
|
|
|
---
|
|
|
|
title: Privileges
|
|
|
|
layout: default
|
2018-07-15 21:36:35 +03:00
|
|
|
root: ../..
|
2018-07-15 17:28:10 +03:00
|
|
|
idx: 4.1
|
|
|
|
description: Registering privs.
|
2018-07-15 21:13:16 +03:00
|
|
|
redirect_from: /en/chapters/privileges.html
|
2016-04-09 16:27:06 +03:00
|
|
|
---
|
|
|
|
|
2019-05-31 20:32:40 +03:00
|
|
|
## Introduction <!-- omit in toc -->
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
Privileges, often called privs for short, give players the ability to perform
|
|
|
|
certain actions. Server owners can grant and revoke privileges to control
|
|
|
|
which abilities each player has.
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2019-05-31 20:32:40 +03:00
|
|
|
- [When to use Privileges](#when-to-use-privileges)
|
|
|
|
- [Declaring Privileges](#declaring-privileges)
|
|
|
|
- [Checking for Privileges](#checking-for-privileges)
|
|
|
|
- [Getting and Setting Privileges](#getting-and-setting-privileges)
|
|
|
|
- [Adding Privileges to basic_privs](#adding-privileges-to-basicprivs)
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
## When to use Privileges
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-10-04 22:38:52 +03:00
|
|
|
A privilege should give a player the ability to do something.
|
|
|
|
Privileges are **not** for indicating class or status.
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
**Good Privileges:**
|
2016-04-09 16:27:06 +03:00
|
|
|
|
|
|
|
* interact
|
|
|
|
* shout
|
|
|
|
* noclip
|
|
|
|
* fly
|
|
|
|
* kick
|
|
|
|
* ban
|
|
|
|
* vote
|
|
|
|
* worldedit
|
|
|
|
* area_admin - admin functions of one mod is ok
|
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
**Bad Privileges:**
|
2016-04-09 16:27:06 +03:00
|
|
|
|
|
|
|
* moderator
|
|
|
|
* admin
|
|
|
|
* elf
|
|
|
|
* dwarf
|
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
## Declaring Privileges
|
|
|
|
|
|
|
|
Use `register_privilege` to declare a new privilege:
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-09-19 14:04:51 +03:00
|
|
|
```lua
|
2024-10-23 02:39:22 +03:00
|
|
|
core.register_privilege("vote", {
|
2017-08-26 21:01:51 +03:00
|
|
|
description = "Can vote on issues",
|
|
|
|
give_to_singleplayer = true
|
2016-04-09 16:27:06 +03:00
|
|
|
})
|
2018-09-19 14:04:51 +03:00
|
|
|
```
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-10-04 22:38:52 +03:00
|
|
|
`give_to_singleplayer` defaults to true when not specified, so it isn't
|
|
|
|
actually needed in the above definition.
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
## Checking for Privileges
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
To quickly check whether a player has all the required privileges:
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-09-19 14:04:51 +03:00
|
|
|
```lua
|
2024-10-23 02:39:22 +03:00
|
|
|
local has, missing = core.check_player_privs(player_or_name, {
|
2017-08-26 21:01:51 +03:00
|
|
|
interact = true,
|
|
|
|
vote = true })
|
2018-09-19 14:04:51 +03:00
|
|
|
```
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-10-04 22:38:52 +03:00
|
|
|
In this example, `has` is true if the player has all the privileges needed.
|
|
|
|
If `has` is false, then `missing` will contain a key-value table
|
|
|
|
of the missing privileges.
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-09-19 14:04:51 +03:00
|
|
|
```lua
|
2024-10-23 02:39:22 +03:00
|
|
|
local has, missing = core.check_player_privs(name, {
|
2018-10-04 22:38:52 +03:00
|
|
|
interact = true,
|
|
|
|
vote = true })
|
|
|
|
|
2016-04-09 16:27:06 +03:00
|
|
|
if has then
|
2017-08-26 21:01:51 +03:00
|
|
|
print("Player has all privs!")
|
2016-04-09 16:27:06 +03:00
|
|
|
else
|
2017-08-26 21:01:51 +03:00
|
|
|
print("Player is missing privs: " .. dump(missing))
|
2016-04-09 16:27:06 +03:00
|
|
|
end
|
2018-09-19 14:04:51 +03:00
|
|
|
```
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-10-04 22:38:52 +03:00
|
|
|
If you don't need to check the missing privileges, you can put
|
|
|
|
`check_player_privs` directly into the if statement.
|
|
|
|
|
|
|
|
```lua
|
2024-10-23 02:39:22 +03:00
|
|
|
if not core.check_player_privs(name, { interact=true }) then
|
2018-10-04 22:38:52 +03:00
|
|
|
return false, "You need interact for this!"
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
## Getting and Setting Privileges
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-10-04 22:38:52 +03:00
|
|
|
Player privileges can be accessed or modified regardless of the player
|
|
|
|
being online.
|
|
|
|
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-09-19 14:04:51 +03:00
|
|
|
```lua
|
2024-10-23 02:39:22 +03:00
|
|
|
local privs = core.get_player_privs(name)
|
2016-04-09 16:27:06 +03:00
|
|
|
print(dump(privs))
|
2018-10-04 22:38:52 +03:00
|
|
|
|
|
|
|
privs.vote = true
|
2024-10-23 02:39:22 +03:00
|
|
|
core.set_player_privs(name, privs)
|
2018-09-19 14:04:51 +03:00
|
|
|
```
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-10-04 22:38:52 +03:00
|
|
|
Privileges are always specified as a key-value table with the key being
|
|
|
|
the privilege name and the value being a boolean.
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-09-19 14:04:51 +03:00
|
|
|
```lua
|
2016-04-09 16:27:06 +03:00
|
|
|
{
|
2017-08-26 21:01:51 +03:00
|
|
|
fly = true,
|
|
|
|
interact = true,
|
|
|
|
shout = true
|
2016-04-09 16:27:06 +03:00
|
|
|
}
|
2018-09-19 14:04:51 +03:00
|
|
|
```
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
## Adding Privileges to basic_privs
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
Players with the `basic_privs` privilege are able to grant and revoke a limited
|
2019-05-31 20:32:40 +03:00
|
|
|
set of privileges. It's common to give this privilege to moderators so that
|
2018-07-16 01:04:55 +03:00
|
|
|
they can grant and revoke `interact` and `shout`, but can't grant themselves or other
|
2019-05-31 20:32:40 +03:00
|
|
|
players privileges with greater potential for abuse such as `give` and `server`.
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2019-05-31 20:32:40 +03:00
|
|
|
To add a privilege to `basic_privs`, and adjust which privileges your moderators can
|
2017-08-30 00:36:56 +03:00
|
|
|
grant and revoke from other players, you must change the `basic_privs` setting.
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2018-07-16 01:04:55 +03:00
|
|
|
By default, `basic_privs` has the following value:
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-29 03:29:04 +03:00
|
|
|
basic_privs = interact, shout
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-30 00:36:56 +03:00
|
|
|
To add `vote`, update this to:
|
2016-04-09 16:27:06 +03:00
|
|
|
|
2017-08-29 03:29:04 +03:00
|
|
|
basic_privs = interact, shout, vote
|
2017-08-30 00:36:56 +03:00
|
|
|
|
|
|
|
This will allow players with `basic_privs` to grant and revoke the `vote` privilege.
|