-
Notifications
You must be signed in to change notification settings - Fork 8
Plugin parameter
Often your plugin will want to use configurable settings, for example to reuse the same plugin in different locations or to avoid hardcoding passwords/tokens. Angelia allows passing arguments to plugins upon startup by annotating variables.
The easiest way would be:
@ParameterLoad
private int number;
This will make it so that the startup parameter with the identifier number
is automatically parsed as an integer and loaded into the number variable. You can have any amount of parameters, the visibility of the variable does not matter and many different types are supported, for example:
@ParameterLoad
private int x;
@ParameterLoad
private int z;
@ParameterLoad
private boolean usePower;
@ParameterLoad
private Location center;
Could be initialized with the startup command runplugin examplebot x=24 z=18 usePower=false center="0 64 0"
For more detail on usage of startup parameter see here
Alternatively to using the variable name as identifier, you can also explicitly specify an identifier to use instead:
@ParameterLoad(id="center")
private Location superLongVariableNameJustBecause;
Instead of providing arguments manually at startup you can also automatically read them from the plugins YAML config, for example:
@ParameterLoad(configId="locz")
private int z;
If the plugins YAML config were to look like this
locz: 23
the value 23 would automatically be loaded into your variable! You can also use more complex paths like this:
@ParameterLoad(configId="general.thing.z")
private int z;
for the YAML config
general:
thing:
z: 23
If a value is not supplied at startup or found in the config, it will be left at its default, meaning null for objects and 0 for primitives. If desired you can require a value for a variable like this:
@ParameterLoad(isRequired=true)
private int x;
If no value for x were to be given now, startup would be cancelled and an error explaining that x is missing would be thrown.
You can also allow loading an argument from either the config or startup parameters by specifying a LoadPolicy
For example you can have a startup argument and a fallback value in a config:
@ParameterLoad(configId="general.thing.z", policy=LoadPolicy.PRIORIZE_INPUT)
private int z;
In this case, if a startup parameter z
is given, it will be used. If not, the config path will be used and if it doesn't exist either, the value will not be initialized. PRIORIZE_INPUT is the default policy used for parameter if not overriden.