Creating config like config.yml?

Discussion in 'Plugin Development' started by DerFreakey, May 11, 2015.

Thread Status:
Not open for further replies.
  1. Hey Guys,
    at first I have to say "Sorry for my bad english."

    But thats not the problem, today I tried to create a config like the config.yml so that I can add the defaults with the Text Editor of Eclipse, but I can't figure out how this works, because I don't know how to save the file (it's called "messages.yml") from the src package to the plugins/MyPluginName folder.

    Has anyone got an idea how I could do this? And no, I don't want to use the YamlConfiguration, because I want to add the defaults in the Text Editor and not in the class.

    Thanks for your help,
    Best Regards
    ~Freakey
     
  2. 1. You need a File and a FileConfiguration:
    Code:
    public File f;
    public FileConfiguration config;
    2. In a method where you load it, get the file. getDataFolder() represents plugins/YourPluginName.
    Code:
    f = new File(getDataFolder(), "messages.yml");
    3. In the same method, load the config:
    Code:
    config = YamlConfiguration.loadConfiguration(f);
    4. In the same method, save the defaults:
    Code:
    saveDefaults();
    5. Create the method to save the defaults:
    Code:
    public void saveDefaults() {
    InputStream defConfigInputStream = getResource("messages.yml");
    if (defConfigInputStream != null) {
    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigInputStream);
    config.setDefaults(defConfig);
    config.options.copyDefaults(true);
    }
    saveMessages();
    }
    6. make a saveMessages method:
    Code:
    public void saveMessages() {
    try {
    config.save(f);
    } catch(Exception e) {
    //Handle exception
    }
    }
     
    DerFreakey likes this.
  3. Offline

    bohafr

    Have you file configuration and file? When yes: fileconfiguration.save(file); when no, what is your code?

    Other things, write to file, read from file is also fileconfiguration.whatYouNeed, config is fileconfiguration, too, but bukkit added method saveConfig().

    I dont saw the post before me, i no refreshed the page, but I dont going to remove this post, it maybe help, but I think post before me answered all.
     

  4. Thank you very much! That's what i needed! :)
     
Thread Status:
Not open for further replies.

Share This Page