Looping through config

Discussion in 'Plugin Development' started by benzimmer123, May 5, 2015.

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

    benzimmer123

    I'm currently working on a KOTH plugin, I want to disable all the KOTHs on a reload or stop command using the onDisable() method. I need to loop through the config and set all the ".ACTIVE" values to false without knowing what the user of the plugin actually called the KOTH in the first place. Not really sure how to do this any ideas?

    The config looks like this

    Code:
    KOTH:
      god:
        taskID: 1
        WORLD: world
        '1':
          X: 2.0
          Y: 3.0
          Z: -26.0
        '2':
          X: -2.0
          Y: 7.0
          Z: -30.0
        ACTIVE: false
      mushroom:
        taskID: 2
        WORLD: world
        '1':
          X: 190.0
          Y: 10.0
          Z: -175.0
        '2':
          X: 184.0
          Y: 2.0
          Z: -181.0
        ACTIVE: true
     
  2. Loop through the ConfigurationSection:
    Code:
    for (String key : getConfig().getConfigurationSection("KOTH").getKeys(false)) {
    
    }
    Then do stuff with the key (key will be god and mushroom in your case)
     
    benzimmer123 likes this.
  3. Offline

    benzimmer123

    @FisheyLP
    Thanks! That's what I needed. :)

    @FisheyLP
    I'm not very good with config Booleans, values, etc.
    This is the code I have created but doesn't seem to work?
    Code:
            for (String key : settings.getConfig().getConfigurationSection("KOTH").getKeys(false)) {
                settings.getConfig().set(key + ".ACTIVE", false);
                settings.saveConfig();
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  4. Nope.
    key is just the string under "KOTH" (for example god or mushroom).
    You need to get/set the value with:
    "KOTH." + key + ".ACTIVE"
     
  5. Offline

    benzimmer123

    @FisheyLP
    Thanks. :)

    @FisheyLP
    No clue what just happend to the config but yeh...

    Code:
      god:
        taskID:
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
        WORLD:
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
        '1':
          X:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Y:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Z:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
        '2':
          X:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Y:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Z:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
        ACTIVE: false
      mushroom:
        taskID:
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
        WORLD:
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
        '1':
          X:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Y:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Z:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
        '2':
          X:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Y:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          Z:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE:
                    ACTIVE: false
          ACTIVE:
            ACTIVE:
              ACTIVE:
                ACTIVE:
                  ACTIVE: false
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. .getKeys(false) not .getKeys(true)
     
  7. Offline

    benzimmer123

    @FisheyLP
    Everything is working! Thanks.

    Quick question, how would you convert an integer argument to a double? I want the user to enter an integer but cast it as a double so it works in the rest of my code.
     
  8. Code:
    int i = 3;
    double d = (double) i;
    Or if you just want to get the input (from args or something) as a double, use:
    Code:
    double d;
    try {
    d = Double.parseDouble(string);
    } catch(Exception e) {
    //Handle exception and return from method
    }
     
    benzimmer123 likes this.
  9. Offline

    benzimmer123

    @FisheyLP
    Didn't know it was that simple:eek:. Thanks for all your help! :)
     
    FisheyLP likes this.
  10. Offline

    benzimmer123

    @FisheyLP
    Sorry for bringing this up again but when I try and edit the config manually it just resets itself straight after? It works fine if I remove this code though

    Code:
            if (settings.getConfig().getConfigurationSection("KOTH") != null) {
                for (String key : settings.getConfig().getConfigurationSection("KOTH").getKeys(false)) {
                    settings.getConfig().set("KOTH." + key + ".ACTIVE", false);
                    settings.saveConfig();
                }
            }
     
  11. Offline

    Monkey_Swag

    @benzimmer123 if that is on your onEnable, then it'll reset the config every time. Check if the config exists, and if it doesn't, continue with what you're doing above.
     
  12. Offline

    benzimmer123

    @Monkey_Swag
    It's not on my onEnable it's on my onDisable.
     
  13. Youre setting every value under "KOTH.x.ACTIVE" to false on disable...
    No wonder it "resets"
     
  14. Offline

    benzimmer123

    @FisheyLP
    But it resets my whole configuration file not just that path so like I have a path called "USE_REWARDS" (not under "KOTH") but it still resets?

    BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  15. Offline

    benzimmer123

  16. Show the code where you're saving/creating the config
     
  17. Offline

    benzimmer123

    @FisheyLP

    Where I load the config file

    Code:
        FileConfiguration config;
        File cfile;
    
        public void setup(Plugin p) {
            config = p.getConfig();
            config.options().copyDefaults(true);
            cfile = new File(p.getDataFolder(), "config.yml");
            saveConfig();
        }
    
        public FileConfiguration getConfig() {
            return config;
        }
    
        public void saveConfig() {
            try {
                config.save(cfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
            }
        }
    
        public void reloadConfig() {
            config = YamlConfiguration.loadConfiguration(cfile);
        }
    Where I loop through the config

    Code:
        public void onDisable() {
           
            if (settings.getConfig().getConfigurationSection("KOTH") != null) {
                for (String key : settings.getConfig().getConfigurationSection("KOTH").getKeys(false)) {
                    settings.getConfig().set("KOTH." + key + ".ACTIVE", false);
                }
            }
           
            settings.saveConfig();
        }
    
    BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  18. Offline

    benzimmer123

    BUMP

    BUMP (Really need a fix for this)!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page