summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java
blob: 1d8b2e619ccdc5c0156cef2e2c6d370debeff6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.earth2me.essentials.commands;

import java.util.Map.Entry;
import org.bukkit.Server;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.command.CommandSender;


public class Commandbalancetop extends EssentialsCommand
{
	public Commandbalancetop()
	{
		super("balancetop");
	}

	@Override
	protected void run(Server server, CommandSender sender, String commandLabel, 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.
			}
		}
		final Map<User, Double> balances = new HashMap<User, Double>();
		for (User u : ess.getAllUsers().values())
		{
			balances.put(u, u.getMoney());
		}

		final List<Map.Entry<User, Double>> sortedEntries = new ArrayList<Map.Entry<User, Double>>(balances.entrySet());
		Collections.sort(sortedEntries, new Comparator<Map.Entry<User, Double>>()
		{
			public int compare(final Entry<User, Double> entry1, final Entry<User, Double> entry2)
			{
				return -entry1.getValue().compareTo(entry2.getValue());
			}
		});
		int count = 0;
		sender.sendMessage(Util.format("balanceTop", max));
		for (Map.Entry<User, Double> entry : sortedEntries)
		{
			if (count == max)
			{
				break;
			}
			sender.sendMessage(entry.getKey().getDisplayName() + ", " + Util.formatCurrency(entry.getValue(), ess));
			count++;
		}
	}
}