Solved Yaml doesnt like my comments.

Discussion in 'Spigot Plugin Development' started by Cerus, Oct 7, 2018.

  1. Hey! I've got some trouble with my yaml file. I have this configuration:
    Code (YAML):

    #
    #        [ Advanced Messaging ]
    #

    # Sets the prefix.
    prefix
    : "&6&lA&e&lDVM &8» &7"

    # Sets the language.
    language
    : en_US.yml

    # Sets the cost of one sms. Can be bypassed with the permission advm.sms-cost.bypass
    sms-cost
    : 0.09

    # Sets the maximum amount of contacts. Can be bypassed with the permission advm.contact-limit.bypass
    contact-limit
    : 30

    # When someone creates a new number he has to pay the price if this is enabled. Can be bypassed with the permission advm.number-create-cost.bypass
    number-create-cost
    :
      enabled
    : false
      price
    : 10.0
    I'm copying it from my jar to the disk with this class:
    Code (Java):

    /*
    *  Copyright (c) 2018 Cerus
    */

    package de.cerus.cerusapi.utilities;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.io.*;
    import java.util.Map;
    import java.util.logging.Level;

    /**
    * File created by jcdesimp on 4/8/14.
    * Edited by Cerus
    */

    public class CustomConfig {

        private JavaPlugin plugin;

        private String resourceLocation;
        private String destination;

        private FileConfiguration customConfig = null;
        private File customConfigFile = null;

        public CustomConfig(JavaPlugin plugin, String resourceLocation, String destination) {
            this.plugin = plugin;
            this.resourceLocation = resourceLocation;
            this.destination = destination;

            Map<String, Object> oldConfig = get().getValues(true);

            //this.reload();
            //this.save();
            this.saveDefault();

            // checks for missing entries and applies new ones
            for (Map.Entry<String, Object> entry : customConfig.getDefaults().getValues(true).entrySet()) {
                if (oldConfig.containsKey(entry.getKey())) {
                    customConfig.set(entry.getKey(), oldConfig.get(entry.getKey()));
                } else {
                    customConfig.set(entry.getKey(), entry.getValue());
                }

            }

            saveDefault();
        }

        public void reload() {
            if (customConfigFile == null) {
                customConfigFile = new File(plugin.getDataFolder(), destination);
            }
            customConfig = YamlConfiguration.loadConfiguration(customConfigFile);

            // Look for defaults in the jar
            Reader defConfigStream = null;
            try {
                InputStream resource = plugin.getResource(resourceLocation);
                if (resource == null) {
                    plugin.getLogger().severe("Internal resource missing at " + resourceLocation + "!");
                    return;
                }
                defConfigStream = new InputStreamReader(resource, "UTF8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                plugin.getLogger().severe("Error reading internal resource at " + resourceLocation + "!");
            }
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                //todo
                /*try {
                    defConfig.save(new File(plugin.getDataFolder(), destination));
                } catch (IOException e) {
                    e.printStackTrace();
                }*/

                customConfig.setDefaults(defConfig);
            }
        }


        public FileConfiguration get() {
            if (customConfig == null) {
                reload();
            }

            return customConfig;
        }


        public void save() {
            if (customConfig == null || customConfigFile == null) {
                return;
            }
            try {
                get().save(customConfigFile);
            } catch (IOException ex) {
                plugin.getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
            }
        }

        public void saveDefault() {
            if (customConfigFile == null) {
                customConfigFile = new File(plugin.getDataFolder(), destination);
            }
            if (!customConfigFile.exists()) {
                plugin.saveResource(resourceLocation, true);
            }
        }
    }
    I'm never saving stuff to the config, I'm just copying it to the disk. Unfortunately the comments arent saved properly:
    Code (YAML):

    #
    #        [ Advanced Messaging ]
    prefix
    : '&6&lA&e&lDVM &8» &7'
    language
    : en_US.yml
    sms-cost
    : 0.09
    contact-limit
    : 30
    number-create-cost
    :
      enabled
    : false
      price
    : 10.0
     
    Any help would be appreciated!
     
  2. SteelPhoenix

    Moderator

    Why not just use saveResource() method to copy the yml file to outside your jar?
     
    • Winner Winner x 1
  3. Thats a good point... Actually I never thought of that, I'm kinda dumb sometimes. Thank you!