Welcome to Episode Six of our Minecraft plugin development series.
Click here to see the previous, fifth episode where I show you how to setup permissions (and add/remove them using code and a permissions plugin) when making Minecraft plugins.
NOTE: Comment support requires Minecraft 1.16+. If you code for an older version such as 1.8.8 or 1.12.2, you can use this library to save your files: https://github.com/tchristofferson/Config-Updater
In this episode, we’re going to cover:
- Using YamlConfiguration — Saving and loading keys from .yml files.
- Editing files on the go — How to write and load values to your settings using a command, with tab-completion support.
- Comment support — How to properly save .yml files with retaining support for comments.
Source Codes:
Links:
- Yaml tutorial — Deeper understanding of how .yml files work and what values you can write to them.
- ConfigSerializable — See which classes implement this so that you can save/load them directly to your files.
- Config-Updater — (Not covered in this video) Comment support for saving .yml files under Minecraft older than 1.16.
How Settings Work In Minecraft Plugins
Bukkit stores most of its configuration files in the .yml format. This format is used by the YAML language which lets you write configuration files with relative ease. See the link above for a full yaml tutorial.
Since Minecraft plugins are written in standard Java, you can of course use any other settings system like .json or even databases (we’ll cover them later), but for now, let’s stick to the most common one.
Loading And Saving
You need to first define a File on the disk where the settings are stored, we use the JavaPlugin#getDataFolder() to get the plugin’s folder of your plugin inside plugins/ folder (i.e. for EssentialsX that would be plugins/Essentials).
And then create a new instance of YamlConfiguration that is responsible for the actual content of the file. You can parse comments in it to save them when you update the file (this requires Minecraft 1.16+).
Finally, load the file. Surround the loading with try-catch in case of an error.
Saving the file is the same to loading it, simply call “config.save(file)” on line 10.
File file = new File(CowCannon.getInstance().getDataFolder(), "settings.yml");
if (!file.exists())
CowCannon.getInstance().saveResource("settings.yml", false);
YamlConfiguration config = new YamlConfiguration();
config.options().parseComments(true);
try {
config.load(file);
} catch (Exception ex) {
ex.printStackTrace();
}
Make sure to load your settings when your plugin starts in the onEnable() section!
Loading Your Keys
To load your keys, you can call YamlConfiguration#get(path) methods.
The path represents where in the .yml file the key is located, this can be “Explosion.Entity_Type” if the key Entity_Type is inside the Explosion section. See the above video for graphical explanation or click the above link about Yaml.
Bukkit also provides a bunch of helper methods such as getLocation or getString or even getList to help you get various stuff from your files.
See the video above for a live demostration!
Setting Config Values
Simply call the “config.set(path, value);” to put a value to the file followed by saving it.
The value must be a saveable value, such as a Java primitive type, or Bukkit serializable object. We have not covered serialization in the video, but you can click this link to learn about all known classes implementing ConfigSerializable.
Thanks to this, you will be able to save or load Location or ItemStack objects directly.
Final Words
Creating Minecraft plugins can be confusing, with most YouTube tutorials being painfully outdated, disorganized and offering no live support.
If you want to learn from the best of the pack and create truly customized plugins, check out Project Orion. This is a full fledged training tried and tested by 2,000+ people showing step-by-step on how to make plugins, advanced systems (minigames, custom mobs, antipiracy, dungeons, claims etc.) and much more!
You’ll meet me live 2x/week inside Project Orion where you can unmute yourself, get me to review your code and get your questions answered, live.
It’s just so much better experience than any blog post or video since I can pour my time and heart into it with greater depth and our platform is custom coded with features we need for it specifically. Click here to learn more!