Skip to content

Config Handling

Jerom van der Sar edited this page Jan 8, 2014 · 7 revisions

Note: Like all BukkitLib implementations, using this is completely optional and up to you.

Config handling is incredibly easy with BukkitLib and is is one of its key features. Nearly everything is automatic and thus this is beneficial in plugin environments.

YAML configs are managed with instances of the class YamlConfig. This is a sub-implementation of the YamlConfiguration class present in Bukkit itself and therefore features all its methods such as getString(path) and getStringList(path).

YamlConfig Features:

  • Easy loading and saving
  • (Optional) Defaults to config in plugin jar
  • (Optional) Automatically copies defaults to plugin folder
  • Comments in config files are preserved (As long as the config isn't written to).
  • Saving and loading Serializable Objects

Here's a quick example:

public Plugin plugin;
public YamlConfig config;
public YamlConfig notchConfig;

@Override
public void onEnable() {
    plugin = this;
    
    // Creates a new configuration, loading defaults from "config.yml" in the java archive.
    config = new YamlConfig(plugin, "config.yml");
    
    // Copies the config if nonexistant, loads it for use.
    config.load();
    String donalds = config.getString("mac");

    // This won't copy defaults from the jar file.
    // The location is relative to the plugin-specific folder.
    notchConfig = new YamlConfig(plugin, "players/notch.yml", false);
    
    if (!notchConfig.exists()) { // Validates if the config exists
        notchConfig.setInt("level", 9001);
        notchConfig.save(); // Saves the config
    } else {
        notchConfig.load(); // Loads the config
        notchConfig.setInt("level", notchConfig.getInt("level") + 1);
        notchConfig.save();
    }
}

Note that no exceptions are ever thrown in config handling. If an error does occur, the config will always print the stack-trace to the console using a built-in logger.

Building on what we had from last tutorial, here's a recap of what you should be able to recreate right now.

package net.pravian.bukkitlibtest;

import net.pravian.bukkitlib.BukkitLib;
import net.pravian.bukkitlib.config.YamlConfig;
import net.pravian.bukkitlib.implementation.BukkitLogger;
import net.pravian.bukkitlib.implementation.BukkitPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

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();
        
        logger.setDebugMode(config.getBoolean("debug"));
        logger.debug("Debug mode enabled!"); // Smart huh?
        
        logger.info(plugin.getName() + " v" + plugin.getVersion() + " is enabled"); 
    }


    @Override
    public void onDisable() {
        logger.info(plugin.getName() + " is disabled");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        // Todo: Command Handling
        return false;
    }
}

And config.yml In the default package.

#################################
# BukkitLibTest 1.0 Config file #
#################################

debug: true

Previous: Logging with BukkitLogger, Next: Handling Commands

Clone this wiki locally