Config library
Config stores the settings of the server under string keys and shares them with clients.
The server owns the values: Config.set changes one, runs the OnConfigSet hook and sends
the new value to every player who may see it, and Config.get reads one on either side.
Config.save and Config.load keep the values in the 'config' data file, and
Config.import sets them from a YAML file. Config.read reads config definitions (default
values, names, descriptions and editor types) from YAML, out of which the client builds the
categories of the config menu.
Besides hidden (the value is never sent to clients) a definition can carry three flags:
static: the config cannot be changed in game. Its value comes from the YAML files and from the code only, a value saved in the data file is ignored.private: the value is only sent to the players who may edit configs (seeConfig.can_manage) andConfig.display_valuemasks it. The default value is part of the definition and stays public.needs_restart: a change made in game is stored as a pending value and is applied the next time the server starts or changes the map.
configs:
server_password:
name: config.general.server_password.name
description: config.general.server_password.desc
category: general
type: string
private: true
needs_restart: true
default_value: ""
Config.set is the low level setter: it stores whatever it is given and does not look at
the flags. Changes requested by players go through Config.change, which refuses static
configs, checks the value against the definition with Config.validate and holds the
value back when the config needs a restart, and through Config.reset, which puts the
default value back.
These are the settings of the server. The options of an individual player are kept on
the client of that player by the Settings plugin (ClientSettings).
Summary
Functions
Config.read has read for it, with the name, description, category, type, min_value, max_value, default_value and the flags.Functions
Returns the table of all stored configs.
Returns
- Map config key to config data table (value, hidden, added_by, ...)
Returns the value cache that Config.get reads from.
Returns
- Map config key to cached value
Checks whether a player may edit configs, which also lets them see the private ones.
The player needs the permission named by Config.permission, 'manage_configuration'
unless that field has been changed.
Parameters
- actor Player player to check; anything else stands for the server console
Returns
- Boolean on the server true for the console as well, on the client false if the player is not valid
Checks whether a player may be sent the value of a config. Serverside only.
Parameters
- actor Player
- key String config key
Returns
- Boolean false if there is no such config, if it is hidden, or if it is private and the player may not edit configs
Changes the value of a config the way a player is allowed to. Serverside only.
This is what an editor or a command should call with the value it has received, after
checking that the player may edit configs (see Config.can_manage). Unlike Config.set
it refuses unknown and static configs, runs the value through Config.validate and
honors the needs_restart flag: the value of such a config is not applied but stored as
pending, saved to the data file at once and applied by Config.load the next time the
server starts or changes the map.
local success, result, pending = Config.change(key, value)
if !success then
actor:notify(result)
end
Parameters
- key String config key
- value Any new value
Returns
- Boolean whether the value was accepted
- Any the value as it was stored or the language phrase of the error
- Boolean true if the value is pending and takes effect after a restart
Creates a category for the config menu, or returns the existing one with that ID. Clientside only. The returned table has the fields category (name and description), configs, and helper functions that add a config to this category: add_key, add_slider, add_table_editor, add_textbox, add_checkbox and add_dropdown. The helpers are called with a dot and take the same arguments as Config.add_to_menu without the category (and, except for add_key, without the data type).
local category = Config.create_category('general', 'config.general.title',
'config.general.desc')
category.add_slider('walk_speed', 'config.general.walk_speed.name',
'config.general.walk_speed.desc', { min_value = 0, max_value = 1024 })
Parameters
- id String optional, defaults to
'other'category ID - category_name String optional, defaults to
'Other'display name or language phrase - category_description String optional, defaults to
''description or language phrase
Returns
- Map category table
See also
Converts a config value to a string that can be shown to players, for instance when a change is announced. The value of a private config is replaced with asterisks, and a list is turned into its values separated by commas.
Parameters
- key String config key
- value Any optional, defaults to
Config.get(key)value to show
Returns
- String
Returns the stored data table of a config rather than just its value.
Parameters
- id String config key
Returns
- Map config data (value, hidden, added_by, ...), or nil if there is no such config
Returns the value of a config. The result is cached, and that includes the default when the config has no value.
Parameters
- key String config key
- default Any optional, defaults to
nilvalue to return if the config has no value
Returns
- Any config value, or default
Returns a config menu category. Clientside only.
Parameters
- id String category ID
Returns
- Map category table, or nil if there is no such category
Returns the default value of a config, as given by its definition.
Parameters
- key String config key
Returns
- Any a copy of the default value, or nil if the config has no definition or no default
Returns the definition of a config: the entry that Config.read has read for it, with the
name, description, category, type, min_value, max_value, default_value and the flags.
Parameters
- key String config key
Returns
- Map config definition, or nil if the config was not defined with Config.read
Returns the value that a config will take the next time the server starts.
Only configs that need a restart get pending values, see Config.change. Clients only
know the pending values if their player may edit configs.
Parameters
- key String config key
Returns
- Boolean true if a value is pending
- Any the pending value
Imports config values from a YAML file or from a table and sets every one of them. Serverside only. The 'depends' and 'depends_development' keys are skipped, and so is every key for which the ShouldConfigImport hook returns a non-nil value.
Parameters
- path String/Map path to a YAML file, or a table of config key to value
- from_config Number optional, defaults to
CONFIG_FLUXCONFIG_FLUX, CONFIG_SCHEMA or CONFIG_PLUGIN
Returns
- Map the imported table, or nil if the file could not be read or path is neither a string nor a table
Checks whether a config is private. The value of a private config is only sent to the
players who may edit configs and is masked by Config.display_value.
A config that has no definition in this session, for instance because it was loaded from
the data file while its plugin is disabled, is judged by the flag that was saved with it.
Parameters
- key String config key
Returns
- Boolean
Checks whether a config is static, that is cannot be changed in game.
Parameters
- key String config key
Returns
- Boolean
Loads the saved configs from the 'config' data file and puts them over the stored ones.
Serverside only. This is where the pending value of a config that needs a restart takes
effect. A saved value is skipped if the config is static, if it does not pass
Config.validate, or if the config has no definition and was not changed with
Config.change, so that a value set in code is not replaced with a stale copy of
itself. A saved config that is not stored at all, such as one of a plugin that is not
loaded, is added the way it was saved.
Returns
- Map all stored configs
Checks whether a change of a config only takes effect after the server has restarted.
Parameters
- key String config key
Returns
- Boolean
Reads config definitions (categories and configs) from a YAML file or from a table.
On the server every config is set to its default_value and its definition is stored.
On the client the categories and configs are added to the config menu. Both sides keep
the definitions for Config.get_definition. Besides the fields shown below a config can
be marked with hidden, static, private and needs_restart, which are described at
the top of this file.
Config.read('gamemodes/flux/config/config.yml')
Config.read({
categories = {
general = { name = 'config.general.title', description = 'config.general.desc' }
},
configs = {
walk_speed = {
name = 'config.general.walk_speed.name',
description = 'config.general.walk_speed.desc',
category = 'general',
type = 'number',
min_value = 0,
max_value = 1024,
default_value = 100
}
}
})
Parameters
- path String/Map path to a YAML file, or the already parsed definitions
- _from_config Number optional, defaults to
nilignored
Returns
- Map the definitions table, or nil if path is neither a string nor a table or the file could not be read
Asks the server to send the config values again. Clientside only. Private configs and pending values only reach the players who may edit configs, and the server decides that at the moment it sends them. A config editor should call this when it opens to get the values its player is allowed to see by then; they arrive shortly after and run the OnConfigReceived and OnConfigPendingReceived hooks. The server answers one request per second.
Puts the default value of a config back. Serverside only.
Follows the same rules as Config.change: static configs are refused, and the default
of a config that needs a restart becomes its pending value.
Parameters
- key String config key
Returns
- Boolean whether the config was reset
- Any the default value or the language phrase of the error
- Boolean true if the default is pending and takes effect after a restart
See also
Writes all stored configs to the 'config' data file. Serverside only.
Sends the current value of a config to the players who may see it. Serverside only. Nothing is sent for a hidden config, and a private one only reaches the players who may edit configs.
Parameters
- target Player player to send the value to; every player if nil
- key String config key
On the server
Sets the value of a config, creating the config if it does not exist yet.
Serverside variant: runs the OnConfigSet hook and sends the new value to all players
who may see it (see Config.send). Does nothing if key is nil. This is the low level
setter: it does not check the value and ignores the static and needs_restart flags.
Use Config.change for changes that players ask for.
Parameters
- key String config key
- value Any new value
- hidden Boolean optional, defaults to
niltrue to never send this config to clients; nil keeps the current setting - from_config Number optional, defaults to
nilCONFIG_FLUX, CONFIG_SCHEMA or CONFIG_PLUGIN; only used to label where a newly created config came from
On the client
Sets the value of a config, creating the config if it does not exist yet. Clientside variant: runs the OnConfigSet hook and changes the local copy only, nothing is sent to the server. Does nothing if key is nil.
Parameters
- key String config key
- value Any new value
Defined in
- lib/config.lua:499 server
- lib/config.lua:696 client
Checks a value against the definition of a config and converts it to the type the config
expects. Use it on every value that comes from a player before storing it.
Numbers may be given as numeric strings, are rounded to the decimals of the definition
if it has any and are clamped between its min_value and max_value. Booleans may be
given as 1 and 0 or as the strings 'true', 'yes', 'on', '1', 'false', 'no', 'off' and
'0'. Strings may be given as numbers. Tables have to be tables. A config without a
definition expects the type of its current value, and a config whose type is not one of
these takes any string, number, boolean or table.
local valid, value = Config.validate('walk_speed', '5000')
-- true, 1024
local valid, phrase = Config.validate('walk_speed', 'fast')
-- false, 'error.config.not_a_number'
Parameters
- key String config key
- value Any value to check
Returns
- Boolean whether the value is acceptable
- Any the converted value if it is, or the language phrase of the error if it is not