Config file overwritten each time plugin is loaded..

Discussion in 'Plugin Development' started by DocRedstone, May 27, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    DocRedstone

    It's bizarre, this code is currently being used in 2 other plugins, yet in this plugin it over rides the existing config file...

    If you prefer syntax highlighting then check it here: http://pastebin.com/4dc90ApL
    Code:
    File configFile = new File(this.getDataFolder(), "config.yml");
     
    @Override
    public void onDisable() {
    }
     
    @Override
    public void onEnable() {
    if(!configFile.exists()) {
    createConfiguration();
    }
    getServer().getPluginManager().registerEvents(this, this);
    loadConfig();
    }
     
    public void createConfiguration() {
    getConfig().options().header("SignLink - By DocRedstone");
    String[] listOfStrings = {"10,10,10|http://minecraft.net", "100,100,100|http://bukkit.org"};
    this.getConfig().set("SignsnLinks", Arrays.asList(listOfStrings));
    getConfig().options().copyDefaults(true);
    saveConfig();
    }
     
    public void loadConfig() {
    textplink = this.getConfig().getStringList("SignsnLinks");
    }
    
    Thoughts?
     
  2. Offline

    Kanlaki101

    If you're going to use the built in configuration methods for Bukkit, you do not need to check if the file exists.
    If you call the getConfig() method, and the file doesn't exist, it will make it for you.

    Code:java
    1. public void onEnable() {
    2. createConfiguration();
    3. //register events/other needed methods.
    4. }
    5.  
    6. public void createConfiguration() {
    7. //add defaults here
    8. getConfig().options.copyDefaults(true);
    9. saveConfig();
    10. }
    11.  
     
    DocRedstone likes this.
  3. Also your using set which will overwrite it every time, setDefault instead so that it will only write it if it is empty.
     
  4. Offline

    DocRedstone

    Thank you to both of you!
     
Thread Status:
Not open for further replies.

Share This Page