-
Notifications
You must be signed in to change notification settings - Fork 7
Using PathContainer in Configs
Note: Direct PathContainer methods only work with the YamlConfig class. If you're using the default Bukkit configuration setup, you can always use getPath() to get the path in string form.
Configs are cool for storing stuff but become a pest when you want to change the structure. Usually, doing so requires editing all files which access a particular section of a config followed by heavy bug testing to ensure everything still works.
Additionally, writing code which features reading or writing to a config can become complicated if you're accessing already implemented config sections.
PathContainer is a simple interface which can be seamlessly integrated with YamlConfig. It features a single method: getPath() which returns the config path in string form. The ideal way of implementing this would be using an enum. An enum can store path locations and can implement the PathContainer interface.
Here's an example of how it could be used.
MyConfigPaths.java
package net.pravian.bukkitlibtest;
import net.pravian.bukkitlib.config.PathContainer;
public enum MyConfigPaths implements PathContainer {
MY_CONFIG_PATH("players.notch.level"),
MY_OTHER_CONFIG_PATH("blabla.bla");
private final String path;
private MyConfigPaths(String path) {
this.path = path;
}
public String getPath() {
return path;
}
}BukkitLibTest.java
package net.pravian.bukkitlibtest;
import net.pravian.bukkitlib.BukkitLib;
import net.pravian.bukkitlib.config.YamlConfig;
...
public class BukkitLibTest extends BukkitPlugin {
public BukkitLibTest plugin;
public BukkitLogger logger;
public YamlConfig config;
...
@Override
public void onLoad() {
this.plugin = this;
this.logger = new BukkitLogger(plugin);
this.config = new YamlConfig(plugin, "config.yml");
...
}
@Override
public void onEnable() {
BukkitLib.init(plugin);
config.load(); // Loads the config for use
// Gets an int from the config.
int notchAwesomenessLevel = config.getInt(MyConfigPaths.MY_CONFIG_PATH);
// Stores an int to the config.
config.set(MyConfigPaths.MY_CONFIG_PATH, notchAwesomenessLevel + 1);
// Saves the config as before.
config.save();
}
}config.yml
players:
notch:
level: 9000All general methods in YamlConfig support PathContainer and thus it can be freely used anytime you want to set or get variables in a config. Using it, however, is completely up to you.
Previous: Config Handling, Next: Handling Commands
Aero is not affiliated with Bukkit or Mojang in any way. All rights belong to their respective owners.
- Home
- About
- Tutorial
- Getting Started
- Extending BukkitPlugin
- Logging with BukkitLogger
- Config Handling
- Handling Commands
- Utility Classes
- Implementations
- Using Metrics - mcstats.org
- Concurrency
- Proxy Classes
- Serializable Objects
- The BukkitLib class
- Example projects
- Online Javadocs
- BukkitDev Page
- Changelogs
- Credits