Essentials/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbalancetop.java

177 lines
7.2 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
2013-04-29 21:04:59 +02:00
import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.textreader.TextPager;
2013-06-08 23:31:19 +02:00
import com.earth2me.essentials.utils.NumberUtil;
import com.google.common.collect.Lists;
2015-04-15 06:06:16 +02:00
import org.bukkit.Server;
2013-05-05 05:13:17 +02:00
import java.math.BigDecimal;
2011-11-30 00:48:51 +01:00
import java.text.DateFormat;
2020-10-03 19:46:05 +02:00
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock;
2015-04-15 06:06:16 +02:00
import static com.earth2me.essentials.I18n.tl;
2014-03-07 16:15:30 +01:00
2015-04-15 06:06:16 +02:00
public class Commandbalancetop extends EssentialsCommand {
2020-10-03 19:46:05 +02:00
public static final int MINUSERS = 50;
private static final int CACHETIME = 2 * 60 * 1000;
private static final SimpleTextInput cache = new SimpleTextInput();
private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private static long cacheage = 0;
2015-04-15 06:06:16 +02:00
public Commandbalancetop() {
super("balancetop");
}
2020-10-03 19:46:05 +02:00
private static void outputCache(final CommandSource sender, final int page) {
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cacheage);
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
sender.sendMessage(tl("balanceTop", format.format(cal.getTime())));
new TextPager(cache).showPage(Integer.toString(page), null, "balancetop", sender);
}
2015-04-15 06:06:16 +02:00
@Override
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
int page = 0;
boolean force = false;
if (args.length > 0) {
try {
page = Integer.parseInt(args[0]);
2020-10-03 19:46:05 +02:00
} catch (final NumberFormatException ex) {
2015-04-15 06:06:16 +02:00
if (args[0].equalsIgnoreCase("force") && (!sender.isPlayer() || ess.getUser(sender.getPlayer()).isAuthorized("essentials.balancetop.force"))) {
force = true;
}
}
}
if (!force && lock.readLock().tryLock()) {
try {
if (cacheage > System.currentTimeMillis() - CACHETIME) {
outputCache(sender, page);
2015-04-15 06:06:16 +02:00
return;
}
if (ess.getUserMap().getUniqueUsers() > MINUSERS) {
sender.sendMessage(tl("orderBalances", ess.getUserMap().getUniqueUsers()));
}
} finally {
lock.readLock().unlock();
}
} else {
if (ess.getUserMap().getUniqueUsers() > MINUSERS) {
sender.sendMessage(tl("orderBalances", ess.getUserMap().getUniqueUsers()));
}
}
ess.runTaskAsynchronously(new Viewer(sender, commandLabel, page, force));
2015-04-15 06:06:16 +02:00
}
2020-10-03 19:46:05 +02:00
@Override
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
if (args.length == 1) {
final List<String> options = Lists.newArrayList("1");
if (!sender.isPlayer() || ess.getUser(sender.getPlayer()).isAuthorized("essentials.balancetop.force")) {
options.add("force");
}
return options;
} else {
return Collections.emptyList();
}
2015-04-15 06:06:16 +02:00
}
private class Calculator implements Runnable {
private final transient Viewer viewer;
private final boolean force;
2020-10-03 19:46:05 +02:00
Calculator(final Viewer viewer, final boolean force) {
2015-04-15 06:06:16 +02:00
this.viewer = viewer;
this.force = force;
}
@Override
public void run() {
lock.writeLock().lock();
try {
if (force || cacheage <= System.currentTimeMillis() - CACHETIME) {
cache.getLines().clear();
final Map<String, BigDecimal> balances = new HashMap<>();
2015-04-15 06:06:16 +02:00
BigDecimal totalMoney = BigDecimal.ZERO;
if (ess.getSettings().isEcoDisabled()) {
if (ess.getSettings().isDebug()) {
ess.getLogger().info("Internal economy functions disabled, aborting baltop.");
}
} else {
2020-10-03 19:46:05 +02:00
for (final UUID u : ess.getUserMap().getAllUniqueUsers()) {
2015-04-15 06:06:16 +02:00
final User user = ess.getUserMap().getUser(u);
if (user != null) {
if (!ess.getSettings().isNpcsInBalanceRanking() && user.isNPC()) {
// Don't list NPCs in output
continue;
}
if (!user.isAuthorized("essentials.balancetop.exclude")) {
final BigDecimal userMoney = user.getMoney();
user.updateMoneyCache(userMoney);
totalMoney = totalMoney.add(userMoney);
final String name = user.isHidden() ? user.getName() : user.getDisplayName();
balances.put(name, userMoney);
}
2015-04-15 06:06:16 +02:00
}
}
}
final List<Map.Entry<String, BigDecimal>> sortedEntries = new ArrayList<>(balances.entrySet());
sortedEntries.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));
2015-04-15 06:06:16 +02:00
cache.getLines().add(tl("serverTotal", NumberUtil.displayCurrency(totalMoney, ess)));
int pos = 1;
2020-10-03 19:46:05 +02:00
for (final Map.Entry<String, BigDecimal> entry : sortedEntries) {
cache.getLines().add(tl("balanceTopLine", pos, entry.getKey(), NumberUtil.displayCurrency(entry.getValue(), ess)));
2015-04-15 06:06:16 +02:00
pos++;
}
cacheage = System.currentTimeMillis();
}
} finally {
lock.writeLock().unlock();
}
ess.runTaskAsynchronously(viewer);
}
}
private class Viewer implements Runnable {
private final transient CommandSource sender;
private final transient int page;
private final transient boolean force;
private final transient String commandLabel;
2020-10-03 19:46:05 +02:00
Viewer(final CommandSource sender, final String commandLabel, final int page, final boolean force) {
2015-04-15 06:06:16 +02:00
this.sender = sender;
this.page = page;
this.force = force;
this.commandLabel = commandLabel;
}
@Override
public void run() {
lock.readLock().lock();
try {
if (!force && cacheage > System.currentTimeMillis() - CACHETIME) {
outputCache(sender, page);
2015-04-15 06:06:16 +02:00
return;
}
} finally {
lock.readLock().unlock();
}
ess.runTaskAsynchronously(new Calculator(new Viewer(sender, commandLabel, page, false), force));
}
}
}