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

185 lines
4.7 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;
import com.earth2me.essentials.textreader.ArrayListInput;
import com.earth2me.essentials.textreader.TextPager;
2011-11-29 23:48:51 +00:00
import java.text.DateFormat;
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");
}
2011-11-29 23:48:51 +00:00
private static final int CACHETIME = 2 * 60 * 1000;
public static final int MINUSERS = 50;
private static ArrayListInput cache = new ArrayListInput();
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 page = 0;
2011-11-29 23:48:51 +00:00
boolean force = false;
if (args.length > 0)
{
try
{
page = Integer.parseInt(args[0]);
}
catch (NumberFormatException ex)
{
if (args[0].equalsIgnoreCase("force") && sender.isOp())
{
2011-11-29 23:48:51 +00:00
force = true;
}
}
}
2011-11-29 23:48:51 +00:00
if (!force && lock.readLock().tryLock())
{
try
{
if (cacheage > System.currentTimeMillis() - CACHETIME)
{
outputCache(sender, page);
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, page, force));
}
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, page, force));
}
}
private static void outputCache(final CommandSender sender, int page)
{
2011-11-29 23:48:51 +00:00
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cacheage);
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
sender.sendMessage(_("balanceTop", format.format(cal.getTime())));
new TextPager(cache).showPage(Integer.toString(page), "", "balancetop", sender);
}
private class Calculator implements Runnable
{
private final transient Viewer viewer;
2011-11-29 23:48:51 +00:00
private final boolean force;
2011-11-29 23:48:51 +00:00
public Calculator(final Viewer viewer, final boolean force)
{
this.viewer = viewer;
2011-11-29 23:48:51 +00:00
this.force = force;
}
@Override
public void run()
{
lock.writeLock().lock();
try
{
2011-11-29 23:48:51 +00:00
if (force || cacheage <= System.currentTimeMillis() - CACHETIME)
{
cache.getLines().clear();
final Map<String, Double> balances = new HashMap<String, Double>();
double totalMoney = 0d;
for (String u : ess.getUserMap().getAllUniqueUsers())
{
final User user = ess.getUserMap().getUser(u);
if (user != null)
{
final double userMoney = user.getMoney();
user.updateMoneyCache(userMoney);
totalMoney += userMoney;
balances.put(user.getDisplayName(), userMoney);
}
}
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());
}
});
cache.getLines().add(_("serverTotal", Util.displayCurrency(totalMoney, ess)));
int pos = 1;
for (Map.Entry<String, Double> entry : sortedEntries)
{
cache.getLines().add(pos + ". " + entry.getKey() + ", " + Util.displayCurrency(entry.getValue(), ess));
pos++;
}
cacheage = System.currentTimeMillis();
}
}
finally
{
lock.writeLock().unlock();
}
ess.scheduleAsyncDelayedTask(viewer);
}
}
private class Viewer implements Runnable
{
private final transient CommandSender sender;
private final transient int page;
2011-11-29 23:48:51 +00:00
private final transient boolean force;
public Viewer(final CommandSender sender, final int page, final boolean force)
{
this.sender = sender;
this.page = page;
2011-11-29 23:48:51 +00:00
this.force = force;
}
@Override
public void run()
{
lock.readLock().lock();
try
{
2011-11-29 23:48:51 +00:00
if (!force && cacheage > System.currentTimeMillis() - CACHETIME)
{
outputCache(sender, page);
return;
}
}
finally
{
lock.readLock().unlock();
}
ess.scheduleAsyncDelayedTask(new Calculator(new Viewer(sender, page, force), force));
}
}
}