How would I go through a list of players in a config and try to find the player w/ the highest score

Discussion in 'Plugin Development' started by ice374, Aug 23, 2013.

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

    ice374

    Say my config file is called highscore.yml and is setup like this:

    Highscore:
    (Tab) Players:
    (Tab) (Tab) Ice374: 7
    (Tab) (Tab) Notch: 12
    (Tab) (Tab) Grumm: 11
    (Tab) (Tab) Blackice: 3
    (Tab) (Tab) Holly: 15

    How would I cycle through those names looking for the top 3 scores, and corresponding player, then print them in 3 different messages kind of like this:

    Bukkit.broadcastMessage (1stplace + " is coming first with " + score + " points!";

    Bukkit.broadcastMessage (2ndplace + " is coming second with " + score + " points!";

    Bukkit.broadcastMessage (3rdplace + " is coming third with " + score + " points!");

    Thanks so much for reading and I will like and follow anyone who solves my dilemma
     
  2. Offline

    xXMaTTHDXx

    Ok well first I dont know how you are managing the points here but asuming everytime play Notch does x you add him to the hashmap <yourmap>.put(p.getName(), 1++); then loop through the hashmap for(String s : <yourmap>){
    }
    then check who has most points
     
  3. Offline

    Axe2760

    Try this:

    So you have an int array playerscores,
    Code:
    Arrays.sort(playerscores);
    int top = playerscores[0]
    int two = playerscores[1]
    
    Etc. etc.

    Hopefully that will work.
     
    ice374 likes this.
  4. Offline

    blablubbabc

    Here you go for a fast but ugly and fixed on your case solution:
    Code:
    ConfigurationSection section = plugin.getConfig().getConfigurationSection("Highscore.Players");
    String top1 = null;
    String top2 = null;
    String top3 = null;
    int top1Score = 0;
    int top2Score = 0;
    int top3Score = 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 (score > top1) {
            top3 = top2;
            top3Score = top2Score;
            top2 = top1;
            top2Score = top1;
            top1 = player;
            top1Score = score;
          } else if (score > top2) {
           top3 = top2;
           top3Score = top2Score;
            top2 = player;
            top2Score = score;
          } else if (score > top3) {
           top3 = player;
            top3Score = score;
          }
        }
    }
     
    if (top1 != null) {
      Bukkit.broadcastMessage (top1 + " is coming first with " + top1Score + " points!";
      if (top2 != null) {
        Bukkit.broadcastMessage (top2 + " is coming second with " + top2Score + " points!";
        if (top3 != null) {
          Bukkit.broadcastMessage (top3 + " is coming third with " + top3Score + " points!");
        }
      }
    }
    
     
    ice374 likes this.
Thread Status:
Not open for further replies.

Share This Page