TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java

65 lines
1.6 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
2011-11-18 17:42:26 +00:00
import java.util.*;
import java.util.Map.Entry;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
public class Commandbalancetop extends EssentialsCommand
{
public Commandbalancetop()
{
super("balancetop");
}
@Override
2011-11-18 12:06:59 +00:00
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
int max = 10;
if (args.length > 0)
{
try
{
if (Integer.parseInt(args[0]) < 10)
{
max = Integer.parseInt(args[0]);
}
}
catch (NumberFormatException ex)
{
//catch it because they tried to enter a string not number.
}
}
2011-07-15 17:29:06 +00:00
final Map<User, Double> balances = new HashMap<User, Double>();
for (User u : ess.getUserMap().getAllUsers())
{
2011-07-15 17:29:06 +00:00
balances.put(u, u.getMoney());
}
2011-07-15 17:29:06 +00:00
final List<Map.Entry<User, Double>> sortedEntries = new ArrayList<Map.Entry<User, Double>>(balances.entrySet());
Collections.sort(sortedEntries, new Comparator<Map.Entry<User, Double>>()
{
2011-11-18 18:07:28 +00:00
@Override
2011-07-15 17:29:06 +00:00
public int compare(final Entry<User, Double> entry1, final Entry<User, Double> entry2)
{
return -entry1.getValue().compareTo(entry2.getValue());
}
});
int count = 0;
sender.sendMessage(_("balanceTop", max));
2011-07-15 17:29:06 +00:00
for (Map.Entry<User, Double> entry : sortedEntries)
{
if (count == max)
{
break;
}
sender.sendMessage(entry.getKey().getDisplayName() + ", " + Util.formatCurrency(entry.getValue(), ess));
count++;
}
}
}