2016-05-20 169 views
0

我越來越雙打從每個文件使用組織數量從最高到最低

if(label.equalsIgnoreCase("baltop")){ 
     if(!(sender instanceof Player)){ 
      CommandUtils.invalidCommandSender(sender); 
      return true; 
     } 
     File[] files = new File(ServerCore.getPlugin().getDataFolder(), File.separator + "PlayerData").listFiles(); 
     for(File file : files){ 
      FileConfiguration playerData = YamlConfiguration.loadConfiguration(file); 
      double bal = playerData.getDouble("Money"); 
      ChatUtils.sendRawMessage(sender, Bukkit.getOfflinePlayer(UUID.fromString(file.getName().replace(".yml", ""))).getName() + ": " + bal); 
     } 
     return true; 
    } 

它說,所有的文件的順序價格的文件夾中,但我想從最高到最低責令其餘額,以及如果兩個玩家的金額相同會發生什麼?

回答

1

應該先閱讀所有內容,並將它們,存儲在一個TreeSet這樣的:

if(label.equalsIgnoreCase("baltop")){ 
    if(!(sender instanceof Player)){ 
     CommandUtils.invalidCommandSender(sender); 
     return true; 
    } 
    TreeSet<Balance> set = new TreeSet<>(); 
    set = set.descendingSet(); 
    File[] files = new File(ServerCore.getPlugin().getDataFolder(), File.separator + "PlayerData").listFiles(); 
    for (File file : files){ 
     FileConfiguration playerData = YamlConfiguration.loadConfiguration(file); 
     double bal = playerData.getDouble("Money"); 
     UUID uuid = UUID.fromString(file.getName().replace(".yml", "")); 
     set.add(new Balance(Bukkit.getOfflinePlayer(uuid).getName(), uuid, bal)); 
    } 

    for (Balance b : set) { 
     ChatUtils.sendRawMessage(sender, b.name + ": " + b.balance); 
    } 
    return true; 
} 

private static class Balance implements Comparable<Balance> { 
    public String name; 
    public UUID uuid; 
    public double balance; 
    public Balance(String n, UUID u, double b) { 
     name = n; 
     uuid = u; 
     balance = b; 
    } 
    @Override 
    public int compareTo(Balance b) { 
     double d = balance - b.balance; 
     if (d<-0.001||d>0.001) 
      return (int) Math.signum(d); 

     int e = -name.compareToIgnoreCase(b.name); 
     if (e != 0) 
      return e; 
     return -uuid.compareTo(b.uuid); 
    } 
} 

此實現將顯示餘額遞減平衡的秩序,而且,如果兩個玩家有相同的平衡,在不管情況如何,他們的名字的升序。 UUID比較僅僅是名稱衝突的最後一個手段,因爲我不確定bukkit是否允許多個玩家使用名稱不同的名稱。

+0

感謝您向我展示這件事,幫助我這麼多!唯一的問題是在Balance類中,「return d」給出了「不能從double轉換爲int」的eclipse錯誤 – Kingbluesapphire

+0

同樣在「TreeSet set = new TreeSet <>()。descendingSet();」 「new TreeSet <>()。descendingSet();」給出的錯誤是「無法從NavigableSet 轉換爲TreeSet 」 – Kingbluesapphire

+0

對不起。這應該修復它... – glee8e