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

179 lines
3.9 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Util;
2011-11-18 17:42:26 +00:00
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.locks.ReentrantReadWriteLock;
2011-11-18 17:42:26 +00:00
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
public class Commandbalancetop extends EssentialsCommand
{
public Commandbalancetop()
{
super("balancetop");
}
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();
@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]) < 19)
{
max = Integer.parseInt(args[0]);
}
}
catch (NumberFormatException ex)
{
//catch it because they tried to enter a string not number.
}
}
if (lock.readLock().tryLock())
{
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()));
}
}
finally
{
lock.readLock().unlock();
}
ess.scheduleAsyncDelayedTask(new Viewer(sender, max));
}
else
2011-07-15 17:29:06 +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
}
ess.scheduleAsyncDelayedTask(new Viewer(sender, max));
}
}
private static void outputCache(final CommandSender sender, int max)
{
sender.sendMessage(_("balanceTop", max));
for (String line : cache)
{
if (max == 0)
{
break;
}
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())
{
final User user = ess.getUserMap().getUser(u);
if (user != null)
{
balances.put(u, user.getMoney());
}
}
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)));
}
}
}