2011-07-15 14:43:39 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-10-16 19:59:39 +00:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2012-09-08 13:55:37 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2013-04-29 19:04:59 +00:00
|
|
|
import com.earth2me.essentials.textreader.SimpleTextInput;
|
2011-12-07 09:53:06 +00:00
|
|
|
import com.earth2me.essentials.textreader.TextPager;
|
2013-06-08 21:31:19 +00:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2017-06-11 00:17:43 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2015-04-15 04:06:16 +00:00
|
|
|
import org.bukkit.Server;
|
|
|
|
|
2013-05-05 03:13:17 +00:00
|
|
|
import java.math.BigDecimal;
|
2011-11-29 23:48:51 +00:00
|
|
|
import java.text.DateFormat;
|
2020-10-03 17:46:05 +00: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;
|
2011-11-28 17:53:38 +00:00
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
2014-03-07 15:15:30 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class Commandbalancetop extends EssentialsCommand {
|
2020-10-03 17:46:05 +00: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 04:06:16 +00:00
|
|
|
public Commandbalancetop() {
|
|
|
|
super("balancetop");
|
|
|
|
}
|
|
|
|
|
2020-10-03 17:46:05 +00: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 04:06:16 +00: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 17:46:05 +00:00
|
|
|
} catch (final NumberFormatException ex) {
|
2015-04-15 04:06:16 +00: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) {
|
2020-08-11 18:09:22 +00:00
|
|
|
outputCache(sender, page);
|
2015-04-15 04:06:16 +00: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()));
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 18:09:22 +00:00
|
|
|
ess.runTaskAsynchronously(new Viewer(sender, commandLabel, page, force));
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-03 17:46:05 +00: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 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class Calculator implements Runnable {
|
|
|
|
private final transient Viewer viewer;
|
|
|
|
private final boolean force;
|
|
|
|
|
2020-10-03 17:46:05 +00:00
|
|
|
Calculator(final Viewer viewer, final boolean force) {
|
2015-04-15 04:06:16 +00:00
|
|
|
this.viewer = viewer;
|
|
|
|
this.force = force;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
lock.writeLock().lock();
|
|
|
|
try {
|
|
|
|
if (force || cacheage <= System.currentTimeMillis() - CACHETIME) {
|
|
|
|
cache.getLines().clear();
|
2020-04-25 12:08:57 +00:00
|
|
|
final Map<String, BigDecimal> balances = new HashMap<>();
|
2015-04-15 04:06:16 +00: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 17:46:05 +00:00
|
|
|
for (final UUID u : ess.getUserMap().getAllUniqueUsers()) {
|
2015-04-15 04:06:16 +00:00
|
|
|
final User user = ess.getUserMap().getUser(u);
|
2016-07-06 18:34:25 +00:00
|
|
|
if (user != null) {
|
|
|
|
if (!ess.getSettings().isNpcsInBalanceRanking() && user.isNPC()) {
|
|
|
|
// Don't list NPCs in output
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-30 14:20:30 +00:00
|
|
|
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 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:08:57 +00:00
|
|
|
final List<Map.Entry<String, BigDecimal>> sortedEntries = new ArrayList<>(balances.entrySet());
|
|
|
|
sortedEntries.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
cache.getLines().add(tl("serverTotal", NumberUtil.displayCurrency(totalMoney, ess)));
|
|
|
|
int pos = 1;
|
2020-10-03 17:46:05 +00:00
|
|
|
for (final Map.Entry<String, BigDecimal> entry : sortedEntries) {
|
2020-09-26 02:16:12 +00:00
|
|
|
cache.getLines().add(tl("balanceTopLine", pos, entry.getKey(), NumberUtil.displayCurrency(entry.getValue(), ess)));
|
2015-04-15 04:06:16 +00: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 17:46:05 +00:00
|
|
|
Viewer(final CommandSource sender, final String commandLabel, final int page, final boolean force) {
|
2015-04-15 04:06:16 +00: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) {
|
2020-08-11 18:09:22 +00:00
|
|
|
outputCache(sender, page);
|
2015-04-15 04:06:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
lock.readLock().unlock();
|
|
|
|
}
|
|
|
|
ess.runTaskAsynchronously(new Calculator(new Viewer(sender, commandLabel, page, false), force));
|
|
|
|
}
|
|
|
|
}
|
2011-07-15 14:43:39 +00:00
|
|
|
}
|