Appending Variables To YML File?

Discussion in 'Plugin Development' started by kmecpp, Aug 26, 2014.

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

    kmecpp

    Hi, i'm writing to create new variables in a yml file but it keeps overwriting the first one no matter what i try and do... can someone please tell me what i did wrong, heres the code:

    Code:
            YamlConfiguration yml = new YamlConfiguration();
            try {
                yml.load(new File(EventListener.plugin.getDataFolder().getAbsolutePath() + File.separator + "News Data" + File.separator + "News.yml"));
            } catch (Exception e) {
                e.printStackTrace();
            }
           
            yml.options().copyDefaults(true);
           
            yml.addDefault("Players", null);
            yml.addDefault("Players."+player.getName(), null);
            yml.addDefault("Players."+player.getName()+".Up-To-Date", value);
            yml.addDefault("Players."+player.getName()+".Last-Article", currentArticle);
           
            //Save
            yml.options().copyDefaults(true);
            try {
                yml.save(EventListener.plugin.getDataFolder().getAbsolutePath() + File.separator + "News Data" + File.separator + "News.yml");
            } catch (IOException e) {
                e.printStackTrace();
            }
     
  2. Check if the config exists before adding the defaults.
     
  3. Offline

    kmecpp

    XXLuigiMario the config does exist? But how would i check?
     
  4. Offline

    stormneo7


    Code:java
    1. File file = new File(EventListener.plugin.getDataFolder().getAbsolutePath() + File.separator + "News Data" + File.separator + "News.yml");
    2. if(!file.exists()){
    3. file.createNewFile();
    4. }
     
  5. Offline

    kmecpp

    stormneo7 ok thanks but that doesn't really help because the config does exist.
     
  6. Offline

    mythbusterma

    kmecpp

    Create the config? Or just use the one given to you by Bukkit.
     
  7. Offline

    stormneo7

    Code:java
    1. yml.addDefault("Players", null);
    2. yml.addDefault("Players."+player.getName(), null);

    That's for deleting a section.
    What I think you wanted to do was
    Code:
    yml.createSection(path);
    Edit:
    Try this code.
    Code:java
    1. File dataFile = new File(EventListener.plugin.getDataFolder().getAbsolutePath() + File.separator + "News Data" + File.separator + "News.yml");
    2. FileConfiguration yml = new YamlConfiguration();
    3.  
    4. try {
    5. if(!dataFile.exists())
    6. dataFile.createNewFile();
    7. yml.load(dataFile);
    8. } catch (Exception e) {
    9. e.printStackTrace();
    10. }
    11.  
    12. yml.set("Players." + player.getName()+".Up-To-Date", value);
    13. yml.set("Players." + player.getName()+".Last-Article", currentArticle);
    14.  
    15. try {
    16. yml.save(dataFile);
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
     
Thread Status:
Not open for further replies.

Share This Page