Rename ConfigurationSection

Discussion in 'Plugin Development' started by Kepler_, Aug 2, 2014.

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

    Kepler_

    Is there an easy way to rename a ConfigurationSection in a YamlConfiguration?

    For example, I have a config like this:
    Code:
    SectionToRename:
      RandomThingThatDoesNothing: true
      AnotherGoshDarnRandomThing:
        Coooolioo: true
        Whoopiee:
        - "This"
        - "Is"
        - "A"
        - "Test"
    How can I change "SectionToRename" to something else? Thanks!
     
  2. Offline

    Freelix2000

    I'm not sure if this is possible, unless configuration sections are actually part of the path, which they may be. If that is the case, this could be done by using a for loop to get and remove all of the values under the configuration section's name and the configuration section itself, then creating a new configuration section with the new name and re-adding all of the values to it by storing them in temporary data before removing them from the first configuration section. If you still need help, I could test if this is possible and give you some code for it.
     
  3. Offline

    MrSparkzz

    Kepler_
    I would suggest copying all the data from the section into code (for example putting the booleans in their own boolean variable and putting the list in an array). Then create a new section, put all the data in the new configuration section and deleting the old one. Which is what I think Freelix2000 said.
     
  4. Offline

    Kepler_

    This is my solution. If anyone can think of a better one, please let me know.
    Code:
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void copyConfigSection(YamlConfiguration config, String fromPath, String toPath){
        Map<String, Object> vals = config.getConfigurationSection(fromPath).getValues(true);
        String toDot = toPath.equals("") ? "" : ".";
        for (String s : vals.keySet()){
            System.out.println(s);
            Object val = vals.get(s);
            if (val instanceof List)
                val = new ArrayList((List)val);
            config.set(toPath + toDot + s, val);
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page