(Moderation Edit) ** PLEASE NOTE: **
This guide is no longer updated and therefore lacking new information plus may contain (now) false information. Please head over to the SpongeDocs to get the latest information including updates.
How to create a plugin configuration file:
Using zml2008’s “Configurate” library, you can easily create a default configuration file for your plugin.
You can view the Configurate library code on GitHub here.
Step 1: Add the following imports to the top of your main plugin class
import java.io.File;
import org.spongepowered.api.service.config.DefaultConfig;
import com.google.inject.Inject;
import org.slf4j.Logger;
import java.io.IOException;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
Step 2: Add the following code inside your plugin’s main class, above the ServerStartingEvent
@Inject
@DefaultConfig(sharedRoot = true)
private ConfigurationLoader configManager;
@Inject
@DefaultConfig(sharedRoot = true)
private File defaultConfig;
Step 3: Place the following code inside your ServerStartingEvent - This will create the config file pluginname.conf
in /config/
if it doesn’t already exist, and add the config defaults
ConfigurationNode config = null;
try {
if (!defaultConfig.exists()) {
defaultConfig.createNewFile();
config = configManager.load();
config.getNode("version").setValue(1);
config.getNode("doStuff").setValue(true);
config.getNode("doMoreStuff").setValue(false);
configManager.save(config);
}
config = configManager.load();
} catch (IOException exception) {
getLogger().error("The default configuration could not be loaded or created!");
}
Step 4: To get information out of the configuration, write the following - You may use any of the following: getString(), getInt(), getBoolean(), getLong(), getDouble(), getFloat()
String myString = config.getNode("MyString").getString(); // myString would equal "Testing 123!" given the example above
Step 5: To edit config values later on, use the following
config.getNode("version").setValue(2);
configManager.save(config);
Shout out to @Minecrell for the assistance in getting this working & @zml for the library.