Solved How to print config entries as a sorted list?

Discussion in 'Plugin Development' started by recon88, Feb 6, 2013.

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

    recon88

    I got 2 "lists" in my yaml config and I want to print them (player message) as a sorted list (Top10):

    Code:
    wins:
      - recon_: 2
      - testperson1: 12
      - testperson2: 5
      - Akakak: 8
    kills:
      - recon_: 2
      - testperson2: 5
      - testperson1: 1
      - Akakak: 8
    There are two commands and it should look like this:
    Code:
    --- Wins ---
    testperson1: 12
    Akakak: 8
    testperson2: 5
    recon_: 2
    Code:
    --- Kills ---
    Akakak: 8
    testperson2: 5
    recon_: 2
    testperson1: 1
     
  2. Offline

    caseif

    I would iterate through the List, then inside the loop, iterate through the other values in the List and find how many are greater than the first value, and assign it to the appropriate key in a new List.
     
  3. Offline

    recon88

    Sounds like pain for me...
    Any other solutions? Or maybe an example?
     
  4. Offline

    recon88

    Anyone? Still stucking at this.
     
  5. Offline

    recon88

    Oh I know about TreeMap and I used that in some other plugins already but I'm not sure what I should use to get both the string and the integer from yml config to store it in the Map.
     
  6. getConfigurationSection("wins") with getKeys(false)... or maybe getMapList().

    Just look around a config's methods and test stuff.
     
  7. Offline

    recon88

    Now I'm confused. I tried to iterate through the configsection but I'm getting a NPE:
    Code:
    for(String playerName : score.getConfigurationSection("wins").getKeys(false))
    Still the same config template as already posted.
     
  8. Offline

    teunie75

    Changed link: to this
    Just do what it says here.
    And, what was I reported for?
     
  9. Offline

    recon88

    reported
     
  10. Offline

    Lolmewn

    What is the exact NPE? Could you show us some code? What have you tried?
     
  11. Offline

    recon88

    NPE is caused by
    Code:
    score.getConfigurationSection("wins")
    Config Part:
    Code:
        public void loadCustomConfig() {
            scorefile = new File(this.getDataFolder(), "scores.yml");
            score = YamlConfiguration.loadConfiguration(scorefile);
        }
    Config contents:
    Code:
    wins:
      - recon_: 2
      - asdasf: 12
      - 3eseh: 5
      - rejr: 4
    kills:
      - recon_: 2
      - asdasf: 5
      - 3eseh: 1
      - rejr: 8
     
  12. Offline

    teunie75

    Try what I said before (and got reported for, sorry for that).
    Read this(a Wikipedia link), it explains how to sort your data easy, then you can just send the sorted strings to a player.
     
  13. Offline

    recon88

    That's not what I'm asking for... Reported again for spam.
     
  14. Offline

    Lolmewn

    Try adding a check if the file exists.
     
  15. Offline

    recon88

    Nevermind. I think I just broke the yml file because I edited it once (maybe too much spaces, didn't check).
    Just removed it and generated a new one. Works now.

    Thanks for that little hint with getConfigurationSection()! Got it now.


    If anyone is interested (sorted by integers):
    Get it:
    Code:
        public TreeMap<Integer, String> getTop() {
     
            TreeMap<Integer, String> scoreMap = new TreeMap<Integer, String>();
            for(String playerName : score.getConfigurationSection("wins").getKeys(false)) {
                int wins = score.getInt("wins." + playerName);
                scoreMap.put(wins, playerName);
            }
            return scoreMap;
        }
    Print it:
    Code:
                    for(Entry<Integer, String> ent: getTop().entrySet()) {
                        player.sendMessage(ent.getValue() + ": " + ent.getKey());
                    }
     
Thread Status:
Not open for further replies.

Share This Page