Copying Default Config

Discussion in 'Plugin Development' started by James0897, Sep 9, 2013.

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


    I'm really struggling to work out how to copy the default config file in the plugin.jar to the data folder with all its default values in.

    I don't make the config file within the plugin because there is so much unique values within it. I've seen it said that saveDefaultConfig() wont work if you make your config file this way.

    please help.
  2. Offline


    can you explain what you mean by this? saveDefaultConfig() works just fine if you have a config file included in your jar, it will copy it to the data folder (if it doesn't already exist) this sounds like exactly what you want.
  3. Offline


    how could I reset the config file in the data folder to be identical to the default file in the plugin.jar
  4. Offline


    by deleting it manually and calling saveDefaultConfig() again.

    2. File file = new File(plugin.getDataFolder() + File.Seperator + "config.yml");
    3. if (file.exists())
    4. file.delete();
  5. Offline


    In the main:
    1. @Override
    2. public void onEnable() {
    3. copy(getResource("config.yml"), new File(getDataFolder(), "config.yml");
    4. }
    6. private void copy(InputStream in, File file) {
    7. try {
    8. byte[] buf = new byte[1024];
    9. int len;
    10. while ((len = > 0) {
    11. out.write(buf, 0, len);
    12. }
    13. out.close();
    14. in.close();
    15. } catch (Exception e) {
    16. e.printStackTrace();
    17. }
    18. }

  6. Offline


    First is overcomplicated, and I don't agree with the second because hard-coded deletion of a configuration file seems like a bad idea. In my opinion, you should just check if it exists, and if not then save a default copy.

    1. private final YamlConfiguration config;
    3. ...
    5. File conf = new File(plugin.getDataFolder() + File.separator + "config.yml");
    6. if (!conf.exists()) {
    7. plugin.saveResource("config.yml", true);
    8. }
    9. config = YamlConfiguration.loadConfiguration(conf);
  7. Offline


    1Rogue he wanted to reset the config file back to defaults. I don't believe there is a method for that other than physical re-creation, which is what my post did.
  8. Offline


    Right, but with my method you can just delete the config.yml and let it regenerate (do it yourself).
Thread Status:
Not open for further replies.

Share This Page