Solved Multiple Config Files?

Discussion in 'Spigot Plugin Development' started by ExWhyZed, Sep 4, 2015.

  1. How do I create multiple config files? How do I access them from another class?
     
  2. Use the File and YamlConfiguration classes.
     
  3. Can you give me some example code to create multiple configs?
     
  4. Code (Java):
    // the file
    File file;

    // the config
    FileConfiguration config;
    to load the config / reload
    Code (Java):
    // load the yml config
    YamlConfigartion.loadConfiguration(file);
    to save the config
    Code (Java):
    // saving the config to the file
    try {
    config.save(file);
    }catch(IOException e) {
    e.printstacktrace();
    }
    to construct file
    Code (Java):
    // creating the file / constructing the file
    file = new File(plugin.getDataFolder(), "file.yml");
    if(!file.exists()) {
      try {
      file.createnewFile();
      } catch(IOException e) {
      e.prinstacktrace();
      }
      // load the config
    }
     
  5. Why not just use a .yml file contained withing the jar file and use this to get it from resources and then save it.
    Code (Text):
    plugin.saveResource("myFile.yml", false);
    Obviously though you would change myFile.yml to the name of your file. The JavaDoc link for .saveResource(resourcePath, replace)
     
  6. How would I reload a custom config?
     
  7.  
  8. Thankyou :) Got it working!
    Would you mind checking out my newest plugin and giving it an honest rating?
     
  9. Because you cant change/edit that later?
     
  10. Wat? So, you're syaing all those plugins that ship with a default config.yml cant have those config.yml files changed/edited? This changes everything! :O

    In all seriousness, saveResource("myfile.yml", false) is the same as calling saveDefaultConfig() except its for a custom resource obviously. Once the file is copied to the disk it can be modified to the server owners's heart's content, same as editing their config.yml files...
     
  11. Could you lead me to the methods that I could do that with?
     
  12. I'm not understanding the question.. The methods are:

    JavaPlugin#saveResource(String, bool)

    JavaPlugin#saveDefaultConfig()
     
  13. Yes, but if I used saveResource(String, boolean) how would I then load and edit the resource (file in this case)?
     
  14. Code (Text):

    @Override
    public void onEnable() {
        saveDefaultConfig();
        saveResource("myfile.yml", false);

        FileConfiguration normalConfig = getConfig();
        FileConfiguration customConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "myfile.yml"));
    }
     
     
    • Like Like x 2