Contents
What Configuration Files Are For(top)
As you develop, you will come across many instances where you will have to store data. A lot of the time, you can store this data in an object such as a HashMap or an ArrayList. So why use configuration files? The reason for this is that when the server is closed or restarts, it recreates everything and all the data you stored would have been lost. This is because objects such as those are stored in virtual memory. In other words, just as easily as they were created, they are also destroyed.Now, this is where files come in handy. Files are a way of saving data in a form that won't get deleted after your program is terminated. This is because files are no longer stored in virtual memory, but rather, on your actual storage disk. You can use this for storing information such as player balances, nicknames, and all sorts of data that you want to keep even after the server restarts.
Let's get started.
Using Default Configuration Files(top)
The creation of the configuration file:Code (Java):
plugin.saveDefaultConfig();
This is essentially all you need to create a quick configuration file, but remember to call it in your onEnable() method to make sure that you have a configuration ready before anything else happens.
Using the Configuration File(top)
To read and write to the config using the above method, we call the getConfig() method. However, if you will be calling getConfig() outside of your main class, you should use the main class's instance. If you do not know how to get instances from other classes, I suggest you check some of the other short guides on how to use constructors (the most commonly used). Once you have an instance, simply use that instance and call getConfig() from it.Now that you have the config, you can read and write to it using its different getter and setter methods. An example would be:
Code (Java):
// Reading from the config
String name = plugin.getConfig().getString("player-name");
// Writing to the config
plugin.getConfig().set("player-name", name);
String name = plugin.getConfig().getString("player-name");
// Writing to the config
plugin.getConfig().set("player-name", name);
Code (YAML):
player-name: Steve
player:
time:
join: 6:00pm
player:
time:
join: 6:00pm
Code (Java):
// Reading from the config
String time = plugin.getConfig().getString("player.time.join");
// Writing to the config
plugin.getConfig().set("player.time.join", time);
String time = plugin.getConfig().getString("player.time.join");
// Writing to the config
plugin.getConfig().set("player.time.join", time);
Note that although I used a String in the getting and setting examples, you can use all sorts of different types as well. For the getters, there exists getInt(), getBoolean(), getList(), and many more. For the setters, it simply takes in the path as the first parameter and an Object as the second. This means that you can set basically any Object.
An important thing to note, however, is that after you write to a configuration file, you should always remember to call saveConfig() if you want the data to be saved to the file. (Warning: saveConfig() will remove all your comments that aren't at the top before the first key)
Code (Java):
plugin.saveConfig();
Using Custom Configurations(top)
Creating the File(top)
First of all, you'll want to be able to make your File and FileConfiguration objects available to other classes in your plugin so that you can readily read and write to your different configuration files. How do you do this?In your main class, create field variables. These are variables that are not contained within a method so that they can be accessed externally.
Code (Java):
public class YourPlugin extends JavaPlugin {
private File customConfigFile;
private FileConfiguration customConfig;
@Override
public void onEnable(){
createCustomConfig();
}
public FileConfiguration getCustomConfig() {
return this.customConfig;
}
private void createCustomConfig() {
customConfigFile = new File(getDataFolder(), "custom.yml");
if (!customConfigFile.exists()) {
customConfigFile.getParentFile().mkdirs();
saveResource("custom.yml", false);
}
customConfig= new YamlConfiguration();
try {
customConfig.load(customConfigFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
}
private File customConfigFile;
private FileConfiguration customConfig;
@Override
public void onEnable(){
createCustomConfig();
}
public FileConfiguration getCustomConfig() {
return this.customConfig;
}
private void createCustomConfig() {
customConfigFile = new File(getDataFolder(), "custom.yml");
if (!customConfigFile.exists()) {
customConfigFile.getParentFile().mkdirs();
saveResource("custom.yml", false);
}
customConfig= new YamlConfiguration();
try {
customConfig.load(customConfigFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
}
What about the saveResource(String, boolean) part? You can actually store files inside your jar aside from just classes. To do this, depending on your IDE, you should create a new file in your 'src' folder (names may vary depending on editor). Now in your main class, you can call saveResource("name of file in jar here", replaceIfAlreadyExists). This will save the file stored in your jar to the file <data folder>/<name of file in jar> if it doesn't already exist (or if the boolean was true). Now, you've created the custom configuration files!
Reading and Writing to Custom Files(top)
We can do this:Code (Java):
plugin.getCustomConfig().getString("some-path");