Creating a Config File

Oct 28, 2017
Creating a Config File
  • Using the Configuration API


    In order for players to be able to change the settings of your plugin, a configuration file can be used. This tutorial will show how to create a config file with the Bukkit configuration API.


    Retrieving the configuration file(top)

    In order to retrieve the configuration object, you will need to use the method getConfig() in your main class, like so :
    Code (Java):
    this.getConfig()

    Adding options to the config(top)

    First off, let's create a configuration variable, so we don't have to keep on calling that function!
    Code (Java):
    FileConfiguration config = this.getConfig();
    Now, we are going to add some options to the config. What we're going to do in this tutorial is that if 'youAreAwesome' is true, it will log a message to the console saying you are awesome!

    What we have to add, is the following. Make sure to put it in your onEnable() method:
    Code (Java):
    config.addDefault("youAreAwesome", true);
    What we have achieved (well, almost), is that your plugin will generate a config.yml with the option 'youAreAwesome'. It will be set to true by default. (If you'd use '("youAreAwesome", false)', it'd be set to false, of course.)

    In this case, we used a boolean. 'youAreAwesome' can be either true or false. The following types can be used:
    • boolean
    • int
    • double
    • String
    • String List
    • Many more!

    Checking for values(top)

    Now that we have added the option, we need to check what the value of the option is. Say the user set 'youAreAwesome' as true, we need to check if it's true (or false).

    If it's false, we can execute a block of code, and the same applies if it's true.

    We can check the value of 'youAreAwesome' with the following code:

    Code (Java):
    if (config.getBoolean("youAreAwesome")) {
    // do something
    }
    We used getBoolean() because 'youAreAwesome' is a boolean. If it was a String for example, we would use getString().

    Saving the config file(top)

    Our config file now looks like this:
    Code (YAML):
    youAreAwesome: true
    We still have to save the config file. Put the following after all config.addDefault()'s (in your onEnable() method):
    Code (Java):
    config.options().copyDefaults(true);
    saveConfig();

    The result(top)

    Example code could be:
    Code (Java):
    public final class TestPlugin extends JavaPlugin implements Listener {
    FileConfiguration config = getConfig();

    public void onEnable() {
        config.addDefault("youAreAwesome", true);
        config.options().copyDefaults(true);
        saveConfig();

       // Enable our class to check for new players using onPlayerJoin()
       getServer().getPluginManager().registerEvents(this, this);
    }

    // This method checks for incoming players and sends them a message
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();

        if (config.getBoolean("youAreAwesome")) {
            player.sendMessage("You are awesome!");
        } else {
            player.sendMessage("You are not awesome...");
        }
    }

    Appendix(top)

    For more detailed informations (e.g. how to set up more complex & nested YAML structures or using Lists & Maps and how to create & use new YAML files) see the Bukkit wiki.
  • Loading...
  • Loading...