2011-07-15 14:43:39 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2011-11-28 23:36:36 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-07-15 14:43:39 +00:00
|
|
|
import com.earth2me.essentials.Util;
|
2011-11-18 17:42:26 +00:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.Map.Entry;
|
2011-11-28 17:53:38 +00:00
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.Server;
|
2011-07-15 14:43:39 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
|
|
|
|
|
|
|
|
public class Commandbalancetop extends EssentialsCommand
|
|
|
|
{
|
|
|
|
public Commandbalancetop()
|
|
|
|
{
|
|
|
|
super("balancetop");
|
|
|
|
}
|
2011-11-28 17:53:38 +00:00
|
|
|
private static final int CACHETIME = 5 * 60 * 1000;
|
|
|
|
public static final int MINUSERS = 50;
|
|
|
|
private static List<String> cache = new ArrayList<String>();
|
|
|
|
private static long cacheage = 0;
|
|
|
|
private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
2011-07-15 14:43:39 +00:00
|
|
|
|
|
|
|
@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
|
2011-07-15 14:43:39 +00:00
|
|
|
{
|
|
|
|
int max = 10;
|
|
|
|
if (args.length > 0)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-11-28 17:53:38 +00:00
|
|
|
if (Integer.parseInt(args[0]) < 19)
|
2011-07-15 14:43:39 +00:00
|
|
|
{
|
|
|
|
max = Integer.parseInt(args[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
{
|
|
|
|
//catch it because they tried to enter a string not number.
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 17:53:38 +00:00
|
|
|
|
|
|
|
if (lock.readLock().tryLock())
|
2011-07-15 14:43:39 +00:00
|
|
|
{
|
2011-11-28 17:53:38 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (cacheage > System.currentTimeMillis() - CACHETIME)
|
|
|
|
{
|
|
|
|
outputCache(sender, max);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ess.getUserMap().getUniqueUsers() > MINUSERS)
|
|
|
|
{
|
2011-11-28 17:59:33 +00:00
|
|
|
sender.sendMessage(_("orderBalances", ess.getUserMap().getUniqueUsers()));
|
2011-11-28 17:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
lock.readLock().unlock();
|
|
|
|
}
|
|
|
|
ess.scheduleAsyncDelayedTask(new Viewer(sender, max));
|
2011-07-15 14:43:39 +00:00
|
|
|
}
|
2011-11-28 17:53:38 +00:00
|
|
|
else
|
2011-07-15 17:29:06 +00:00
|
|
|
{
|
2011-11-28 17:53:38 +00:00
|
|
|
if (ess.getUserMap().getUniqueUsers() > MINUSERS)
|
2011-07-15 17:29:06 +00:00
|
|
|
{
|
2011-11-28 17:59:33 +00:00
|
|
|
sender.sendMessage(_("orderBalances", ess.getUserMap().getUniqueUsers()));
|
2011-07-15 17:29:06 +00:00
|
|
|
}
|
2011-11-28 17:53:38 +00:00
|
|
|
ess.scheduleAsyncDelayedTask(new Viewer(sender, max));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void outputCache(final CommandSender sender, int max)
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
sender.sendMessage(_("balanceTop", max));
|
2011-11-28 17:53:38 +00:00
|
|
|
for (String line : cache)
|
2011-07-15 14:43:39 +00:00
|
|
|
{
|
2011-11-28 17:53:38 +00:00
|
|
|
if (max == 0)
|
2011-07-15 14:43:39 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-11-28 17:53:38 +00:00
|
|
|
max--;
|
|
|
|
sender.sendMessage(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class Calculator implements Runnable
|
|
|
|
{
|
|
|
|
private final transient Viewer viewer;
|
|
|
|
|
|
|
|
public Calculator(final Viewer viewer)
|
|
|
|
{
|
|
|
|
this.viewer = viewer;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
lock.writeLock().lock();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (cacheage < System.currentTimeMillis() - 5 * 60 * 1000)
|
|
|
|
{
|
|
|
|
final Map<String, Double> balances = new HashMap<String, Double>();
|
|
|
|
for (String u : ess.getUserMap().getAllUniqueUsers())
|
|
|
|
{
|
2011-11-28 23:36:36 +00:00
|
|
|
final User user = ess.getUserMap().getUser(u);
|
|
|
|
if (user != null)
|
2011-11-28 17:53:38 +00:00
|
|
|
{
|
2011-11-28 23:36:36 +00:00
|
|
|
balances.put(u, user.getMoney());
|
2011-11-28 17:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<Map.Entry<String, Double>> sortedEntries = new ArrayList<Map.Entry<String, Double>>(balances.entrySet());
|
|
|
|
Collections.sort(sortedEntries, new Comparator<Map.Entry<String, Double>>()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public int compare(final Entry<String, Double> entry1, final Entry<String, Double> entry2)
|
|
|
|
{
|
|
|
|
return -entry1.getValue().compareTo(entry2.getValue());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
int count = 0;
|
|
|
|
for (Map.Entry<String, Double> entry : sortedEntries)
|
|
|
|
{
|
|
|
|
if (count == 20)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cache.add(entry.getKey() + ", " + Util.formatCurrency(entry.getValue(), ess));
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
cacheage = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
}
|
|
|
|
ess.scheduleAsyncDelayedTask(viewer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class Viewer implements Runnable
|
|
|
|
{
|
|
|
|
private final transient CommandSender sender;
|
|
|
|
private final transient int max;
|
|
|
|
|
|
|
|
public Viewer(final CommandSender sender, final int max)
|
|
|
|
{
|
|
|
|
this.sender = sender;
|
|
|
|
this.max = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
lock.readLock().lock();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (cacheage > System.currentTimeMillis() - 5 * 60 * 1000)
|
|
|
|
{
|
|
|
|
outputCache(sender, max);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
lock.readLock().unlock();
|
|
|
|
}
|
|
|
|
ess.scheduleAsyncDelayedTask(new Calculator(new Viewer(sender, max)));
|
2011-07-15 14:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|