Solved Entering Keys with Multiple Values (help with yml)

Discussion in 'Plugin Development' started by Cosban, Jul 12, 2011.

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

    Cosban

    So I'm writing my first plugin.... It's going pretty well except for the writing to configuration files part. Basically I'm trying to use the bukkit config libraries to automatically write the default. The problem isn't writing to the file, I can do that perfectly... it's when I try to make keys with multiple values.
    Here is the config file I am currently writing:
    Code:
    clans:
        default: human
        list: zombie
    advise:
        message: You do not have the permissions to advise other clans!
        restrict: false
    friendlyfire:
        message: Friendly fire is not allowed!
        reverse: false
        allow: true
    It's obvious that I'm trying to use the .put(string,object) in where the list area is... but I don't know what else to do. This is what I would like the file to look like:
    Code:
    clans:
        default: human
        list:
            - human
            - zombie
    advise:
        message: You do not have the permissions to advise other clans!
        restrict: false
    friendlyfire:
        message: Friendly fire is not allowed!
        reverse: false
        allow: true
    I know you will ask me to post what my code looks like in order to help... So here are excerpts of the problem:

    Code:
    private static final Map<String, Object> CONFIG_DEFAULTS = new HashMap<String, Object>();
    private Configuration config = getConfiguration();
    static String mainDirectory = "plugins/PhantomClans";
    static File PhantomClans = new File(mainDirectory + File.separator + "config.yml");
    static {
        CONFIG_DEFAULTS.put("friendlyfire.allow", true);
        CONFIG_DEFAULTS.put("friendlyfire.reverse", false);
        CONFIG_DEFAULTS.put("friendlyfire.message", "Friendly fire is not allowed!");
        CONFIG_DEFAULTS.put("advise.restrict", false);
        CONFIG_DEFAULTS.put("advise.message", "You do not have the permissions to advise other clans!");
        CONFIG_DEFAULTS.put("clans.default", "human"); // this is the bad section
        CONFIG_DEFAULTS.put("clans.default", "zombie"); // this is the bad section
    }
    // On enable I call a method called load configuration
    
    private void loadConfiguration(){
        PluginDescriptionFile pdf = getDescription();
        if(PhantomClans.exists()){
            log.info("[" + pdf.getName() +"] loading configuration files");
            config = new Configuration(PhantomClans);
               config.load();
               config.setHeader("# Configuration file for PhantomClans\r\n# Please see http://phantomcraft.net/wiki/PhantomClans for configuration details.");
               for (String prop : CONFIG_DEFAULTS.keySet()) {
                   if (config.getProperty(prop) == null) {
                       config.setProperty(prop, CONFIG_DEFAULTS.get(prop));
                   }
               }
        } else {
            log.info("[" + pdf.getName() +"] creating default configuration structures");
               try {
                   this.getDataFolder().mkdir();
                   PhantomClans.createNewFile();
                   config = new Configuration(PhantomClans);
                   // default values
                   config.setHeader("# Configuration file for PhantomClans\r\n# Please see http://phantomcraft.net/wiki/PhantomClans for configuration details.");
                   for (String prop : CONFIG_DEFAULTS.keySet()) {
                       config.setProperty(prop, CONFIG_DEFAULTS.get(prop));
                   }
                   config.save();
               } catch (IOException e) {
                   log.warning(e.toString());
               }
        }
    }
     
  2. Offline

    Bone008

    getConfiguration() always gives you a valid Configuration instance that points to the respective config.yml.
    If it or its directory doesn't exist, it will automatically be created.

    So you can simplify that code a lot (it's too late for me to work out a working code for you).
    However, I don't really see your problem. What's wrong? After skimming the code it looks working to me (but rather complicated; see above).

    Nested nodes should work exactly like you tried (setProperty("parent.child", whatever)).
     
  3. Offline

    Hretsam

    I want to revert you to this:
    https://github.com/Hretsam/IPGet/blob/master/src/org/yoharnu/IPGet/FileHandler.java

    This is something i have written to log ip's, it logs multiple for each user so it should be what you need.

    Otherwise if that doesn't work, try to get the values as a List<String> and put them back as a List<String>.

    The code i am referring to:
    Code:
    /**
    * Adds the ip to the user's IP list
    * @param username
    * @param ip
    * @param datelong
    */
        public void addIp(String username, String ip, long datelong) {
            ip = formatIP(ip);
    
            // Makes sure it wont mistake the ip for nodes in the yml file
            ip = ip.replaceAll("\\.", "_");
    
            // Gets the old date value
            long oldDateLong = Long.valueOf(userlog.getString("users." + username + ".ip." + ip, "0"));
    
            // Check if new date is actually more new then the old value
            if (datelong > oldDateLong) {
                // Sets the ip (use the ip node for future adding of other nodes
                userlog.setProperty("users." + username + ".ip." + ip, datelong);
                // Saves file
                userlog.save();
            }
    
        }
    
     
  4. Offline

    Bone008

    Ah now I got the problematic part. So what you want to save is a list.
    To have one, you need to pass a List<?> into setProperty.
    In your case it would be List<String>.
    So you'd probably create an ArrayList and add your 2 values to it.
     
Thread Status:
Not open for further replies.

Share This Page