Question

I am trying to collect the point values from a configuration file for all players and sort them so I can take the top five to put in a leaderboard. I am having trouble figuring out how to select to the "points" key for all of the players in the configuration file.

Here is the configuration structure:

players:
  9e388a50-d930-11e3-9c1a-0800200c9a66:
    points: 66
    dead: false
  63eabd40-d931-11e3-9c1a-0800200c9a66:
    points: 120
    dead: false

Here's the code I have so far:

public void onCommand(Player p, String[] args) {
        ConfigurationSection section = config.getConfig().getConfigurationSection("players." + GET ALL PLAYERS HERE + ".points");
        String top1 = null;
        String top2 = null;
        String top3 = null;
        String top4 = null;
        String top5 = null;
        int top1Score = 0;
        int top2Score = 0;
        int top3Score = 0;
        int top4Score = 0;
        int top5Score = 0;

        if (section != null) {
            for (String player : section.getKeys(false)) {
                int score = section.getInt(player);
                if (top1 == null) {
                    top1 = player;
                    top1Score = score;
                } else if (top2 == null) {
                    top2 = player;
                    top2Score = score;
                } else if (top3 == null) {
                    top3 = player;
                    top3Score = score;
                } else if (top4 == null) {
                    top4 = player;
                    top3Score = score;
                } else if (top5 == null) {
                    top5 = player;
                    top3Score = score;
                } else if (score > top1) {
                    top5 = top4;
                    top5Score = top4Score;
                    top4 = top3;
                    top4Score = top3Score;
                    top3 = top2;
                    top3Score = top2Score;
                    top2 = top1;
                    top2Score = top1;
                    top1 = player;
                    top1Score = score;
                }
                // This takes continues like the above else if (score > top1)
            }
        }

Here is a Bukkit configuration API reference: http://wiki.bukkit.org/Configuration_API_Reference

Was it helpful?

Solution

I did something like this with currency on my server. Here's how I did it:

long top1Points = 0;
long top2Points = 0;
long top3Points = 0;
long top4Points = 0;
long top5Points = 0;
String top1Player = null;
String top2Player = null;
String top3Player = null;
String top4Player = null;
String top5Player = null;

for(OfflinePlayer p : Bukkit.getOfflinePlayers()){//loop threw all players that have ever played
  String uuid = p.getUUID();//get the player's UUID
  long points = plugin.getConfig().getLong("players." + uuid + ".points");//get the player's points

  if(points > top5Points){ //if the player has more points than the top5Points 
    if(points > top4Points){ //if the player has more points than the top4Points
      if(points > top3Points){ //if the player has more points than the top3Points
        if(points > top2Points){ //if the player has more points than the top2Points
          if(points > top1Points){ //if the player has more points than the top1Points
            //store that this player has the #1 points as of now
            top1Points = points;
            top1Player = p.getName();
            continue;
          }
          //store that this player has the #2 points as of now
          top2Points = points;
          top2Player = p.getName();
          continue;
        }
        //store that this player has the #3 points as of now
        top3Points = points;
        top3Player = p.getName();
        continue;
      }
      //store that this player has the #4 points as of now
      top4Points = points;
      top4Player = p.getName();
      continue;
    }
    //store that this player has the #5 points as of now
    top5Points = points;
    top5Player = p.getName();
    continue;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top