2016-05-20 123 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