⚙️Configuration
Modifying/retrieving values from the configuration file (config.yml).
File-related methods
Config.init();
Config.reload();
Config.save();
init()
- Creates the configuration file if it doesn't exist and then saves the configuration file.reload()
- Reloads (reads) the configuration from the file.save()
- Saves (writes) the current configuration to the file.
Retrieving values
Types
boolean
double
float
int
ItemBuilder
ItemStack
List<...> (ConfigList)
Location
Material
Text
UUID
Object
T (if you provide Class<T>)
Anyways let's retrieve a join message.
Component component = Config.key("join.join_message") // ConfigKey
.val() // ConfigVal
.asText() // Text
.component(); // Component
Modifying values
Updating/Setting
There are 3 methods all named set()
. The one without any parameters will lead you to a builder, and the rest are just shorter methods to do the same thing as the builder.
Types
Object
Text
Location
ItemStack
ItemBuilder
List<?>
int
double
long
float
boolean
Default value will make it so what you set is going to be the default for the config if the value ever disappears, instead of returning null.
We'll use the builder for this.
Config.set()
.key(Config.key("join.join_message"))
.defaultVal(true)
.val(Text.text("<green>A mysterious player has joined..."))
.build();
Deletion
Config
has 2 methods.
delete(ConfigKey)
delete(ConfigSection)
I don't think I need to document those self explanatory methods.
Sections
A ConfigSection
is a key which holds keys inside of it.
In our case, in join.join_message
the join
key is a configuration section.
So we can retrieve a configuration section by doing:
ConfigSection /* ... */ = Config.section("join");
Retrieving Values
If we want to retrieve the join_message
key from the join
configuration section:
Object object = Config.section("join").get("join_message");
String miniMessage = (String) object;
Component component = TextStyle.style(miniMessage);
Modifying Values
Updating/Setting
Use section.set()
Same as setting a normal key to a value. It will return a ConfigSectionSetter.
Deletion
Use section.delete()
This will delete both the section and also the keys inside of it.
Selection
This accepts a ConfigSection
. It lets you iterate over it easily (using a forEach()
method)
Config.selection(Config.section("join")).forEach(keyVal -> {
ConfigKey key = keyVal.key();
ConfigVal val = keyVal.val();
// prints out the name of the key.
System.out.println(key.path);
});
Last updated
Was this helpful?