Copying Default Config

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

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

    James0897

    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

    metalhedd

    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

    James0897

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

    jayfella

    by deleting it manually and calling saveDefaultConfig() again.

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

    KingNyuels

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


    KingNyuels
     
  6. Offline

    1Rogue


    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.

    Code:java
    1. private final YamlConfiguration config;
    2.  
    3. ...
    4.  
    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

    jayfella

    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

    1Rogue


    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