收集并排序所有玩家的积分

时间:2014-05-11 17:22:11

标签: java configuration minecraft bukkit

我正在尝试从配置文件中为所有玩家收集点值并对它们进行排序,以便我可以将前五名放入排行榜。我无法弄清楚如何为配置文件中的所有玩家选择“点”键。

这是配置结构:

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

这是我到目前为止的代码:

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)
            }
        }

以下是Bukkit配置API参考:http://wiki.bukkit.org/Configuration_API_Reference

1 个答案:

答案 0 :(得分:2)

我在服务器上使用货币做了类似的事情。以下是我的表现方式:

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;
  }