Fix /balancetop, cache was not cleared

This commit is contained in:
snowleo 2011-11-30 00:48:51 +01:00
parent e169e954ba
commit f5344b65d3
8 changed files with 30 additions and 18 deletions

View file

@ -3,6 +3,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Util; import com.earth2me.essentials.Util;
import java.text.DateFormat;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -16,7 +17,7 @@ public class Commandbalancetop extends EssentialsCommand
{ {
super("balancetop"); super("balancetop");
} }
private static final int CACHETIME = 5 * 60 * 1000; private static final int CACHETIME = 2 * 60 * 1000;
public static final int MINUSERS = 50; public static final int MINUSERS = 50;
private static List<String> cache = new ArrayList<String>(); private static List<String> cache = new ArrayList<String>();
private static long cacheage = 0; private static long cacheage = 0;
@ -26,6 +27,7 @@ public class Commandbalancetop extends EssentialsCommand
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{ {
int max = 10; int max = 10;
boolean force = false;
if (args.length > 0) if (args.length > 0)
{ {
try try
@ -37,11 +39,13 @@ public class Commandbalancetop extends EssentialsCommand
} }
catch (NumberFormatException ex) catch (NumberFormatException ex)
{ {
//catch it because they tried to enter a string not number. if (args[0].equalsIgnoreCase("force") && sender.isOp()) {
force = true;
}
} }
} }
if (lock.readLock().tryLock()) if (!force && lock.readLock().tryLock())
{ {
try try
{ {
@ -59,7 +63,7 @@ public class Commandbalancetop extends EssentialsCommand
{ {
lock.readLock().unlock(); lock.readLock().unlock();
} }
ess.scheduleAsyncDelayedTask(new Viewer(sender, max)); ess.scheduleAsyncDelayedTask(new Viewer(sender, max, force));
} }
else else
{ {
@ -67,14 +71,17 @@ public class Commandbalancetop extends EssentialsCommand
{ {
sender.sendMessage(_("orderBalances", ess.getUserMap().getUniqueUsers())); sender.sendMessage(_("orderBalances", ess.getUserMap().getUniqueUsers()));
} }
ess.scheduleAsyncDelayedTask(new Viewer(sender, max)); ess.scheduleAsyncDelayedTask(new Viewer(sender, max, force));
} }
} }
private static void outputCache(final CommandSender sender, int max) private static void outputCache(final CommandSender sender, int max)
{ {
sender.sendMessage(_("balanceTop", max)); final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cacheage);
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
sender.sendMessage(_("balanceTop", max, format.format(cal.getTime())));
for (String line : cache) for (String line : cache)
{ {
if (max == 0) if (max == 0)
@ -90,10 +97,12 @@ public class Commandbalancetop extends EssentialsCommand
private class Calculator implements Runnable private class Calculator implements Runnable
{ {
private final transient Viewer viewer; private final transient Viewer viewer;
private final boolean force;
public Calculator(final Viewer viewer) public Calculator(final Viewer viewer, final boolean force)
{ {
this.viewer = viewer; this.viewer = viewer;
this.force = force;
} }
@Override @Override
@ -102,8 +111,9 @@ public class Commandbalancetop extends EssentialsCommand
lock.writeLock().lock(); lock.writeLock().lock();
try try
{ {
if (cacheage < System.currentTimeMillis() - 5 * 60 * 1000) if (force || cacheage <= System.currentTimeMillis() - CACHETIME)
{ {
cache.clear();
final Map<String, Double> balances = new HashMap<String, Double>(); final Map<String, Double> balances = new HashMap<String, Double>();
for (String u : ess.getUserMap().getAllUniqueUsers()) for (String u : ess.getUserMap().getAllUniqueUsers())
{ {
@ -149,11 +159,13 @@ public class Commandbalancetop extends EssentialsCommand
{ {
private final transient CommandSender sender; private final transient CommandSender sender;
private final transient int max; private final transient int max;
private final transient boolean force;
public Viewer(final CommandSender sender, final int max) public Viewer(final CommandSender sender, final int max, final boolean force)
{ {
this.sender = sender; this.sender = sender;
this.max = max; this.max = max;
this.force = force;
} }
@Override @Override
@ -162,7 +174,7 @@ public class Commandbalancetop extends EssentialsCommand
lock.readLock().lock(); lock.readLock().lock();
try try
{ {
if (cacheage > System.currentTimeMillis() - 5 * 60 * 1000) if (!force && cacheage > System.currentTimeMillis() - CACHETIME)
{ {
outputCache(sender, max); outputCache(sender, max);
return; return;
@ -172,7 +184,7 @@ public class Commandbalancetop extends EssentialsCommand
{ {
lock.readLock().unlock(); lock.readLock().unlock();
} }
ess.scheduleAsyncDelayedTask(new Calculator(new Viewer(sender, max))); ess.scheduleAsyncDelayedTask(new Calculator(new Viewer(sender, max, force), force));
} }
} }
} }

View file

@ -15,7 +15,7 @@ backUsageMsg=\u00a77Returning to previous location.
backupFinished=Backup finished backupFinished=Backup finished
backupStarted=Backup started backupStarted=Backup started
balance=\u00a77Balance: {0} balance=\u00a77Balance: {0}
balanceTop=\u00a77Top {0} balances balanceTop=\u00a77Top {0} balances ({1})
banExempt=\u00a7cYou can not ban that player. banExempt=\u00a7cYou can not ban that player.
banIpAddress=\u00a77Banned IP address banIpAddress=\u00a77Banned IP address
bannedIpsFileError=Error reading banned-ips.txt bannedIpsFileError=Error reading banned-ips.txt

View file

@ -15,7 +15,7 @@ backUsageMsg=\u00a77Returnere til tidligere placering.
backupFinished=Backup sluttede backupFinished=Backup sluttede
backupStarted=Backup startede backupStarted=Backup startede
balance=\u00a77Balance: {0} balance=\u00a77Balance: {0}
balanceTop=\u00a77 Top {0} saldi balanceTop=\u00a77 Top {0} saldi ({1})
banExempt=\u00a7cDu kan ikke forbyde den p\u00e5g\u00e6ldende spiller. banExempt=\u00a7cDu kan ikke forbyde den p\u00e5g\u00e6ldende spiller.
banIpAddress=\u00a77Bannede IP addresse banIpAddress=\u00a77Bannede IP addresse
bannedIpsFileError=Fejl i l\u00e6sning af banned-ips.txt bannedIpsFileError=Fejl i l\u00e6sning af banned-ips.txt

View file

@ -15,7 +15,7 @@ backUsageMsg=\u00a77Kehre zur letzten Position zur\u00fcck.
backupFinished=Backup beendet backupFinished=Backup beendet
backupStarted=Backup gestartet backupStarted=Backup gestartet
balance=\u00a77Geldb\u00f6rse: {0} balance=\u00a77Geldb\u00f6rse: {0}
balanceTop=\u00a77 Top {0} Guthaben balanceTop=\u00a77 Top {0} Guthaben ({1})
banExempt=\u00a7cDu kannst diesen Spieler nicht sperren. banExempt=\u00a7cDu kannst diesen Spieler nicht sperren.
banIpAddress=\u00a77IP-Adresse gesperrt. banIpAddress=\u00a77IP-Adresse gesperrt.
bannedIpsFileError=Fehler beim Lesen von banned-ips.txt bannedIpsFileError=Fehler beim Lesen von banned-ips.txt

View file

@ -15,7 +15,7 @@ backUsageMsg=\u00a77Returning to previous location.
backupFinished=Backup finished backupFinished=Backup finished
backupStarted=Backup started backupStarted=Backup started
balance=\u00a77Balance: {0} balance=\u00a77Balance: {0}
balanceTop=\u00a77 Top {0} balances balanceTop=\u00a77 Top {0} balances ({1})
banExempt=\u00a7cYou can not ban that player. banExempt=\u00a7cYou can not ban that player.
banIpAddress=\u00a77Banned IP address banIpAddress=\u00a77Banned IP address
bannedIpsFileError=Error reading banned-ips.txt bannedIpsFileError=Error reading banned-ips.txt

View file

@ -15,7 +15,7 @@ backUsageMsg=\u00a77Volviendo a la localizacion anterior.
backupFinished=Copia de seguridad completada backupFinished=Copia de seguridad completada
backupStarted=Comenzando copia de seguridad backupStarted=Comenzando copia de seguridad
balance=\u00a77Cantidad: {0} balance=\u00a77Cantidad: {0}
balanceTop=\u00a77Top {0} cantidades balanceTop=\u00a77Top {0} cantidades ({1})
banExempt=\u00a7cNo puedes banear a ese jugador banExempt=\u00a7cNo puedes banear a ese jugador
banIpAddress=\u00a77Direccion IP baneada banIpAddress=\u00a77Direccion IP baneada
bannedIpsFileError=Error leyendo banned-ips.txt bannedIpsFileError=Error leyendo banned-ips.txt

View file

@ -15,7 +15,7 @@ backUsageMsg=\u00a77Retour \u00e0 votre emplacement pr\u00e9c\u00c3\u00a8dent.
backupFinished=Sauvegarde termin\u00e9 backupFinished=Sauvegarde termin\u00e9
backupStarted=D\u00e9but de la sauvegarde backupStarted=D\u00e9but de la sauvegarde
balance=\u00a77Solde : {0} balance=\u00a77Solde : {0}
balanceTop=\u00a77 Meilleurs {0} soldes balanceTop=\u00a77 Meilleurs {0} soldes ({1})
banExempt=\u00a77Vous ne pouvez pas bannir ce joueur. banExempt=\u00a77Vous ne pouvez pas bannir ce joueur.
banIpAddress=\u00a77Adresse IP bannie banIpAddress=\u00a77Adresse IP bannie
bannedIpsFileError=Erreur de lecture de banned-ips.txt bannedIpsFileError=Erreur de lecture de banned-ips.txt

View file

@ -15,7 +15,7 @@ backUsageMsg=\u00a77Naar de vorige locatie aan het gaan.
backupFinished=Backup voltooid backupFinished=Backup voltooid
backupStarted=Backup gestart backupStarted=Backup gestart
balance=\u00a77Saldo: {0} balance=\u00a77Saldo: {0}
balanceTop=\u00a77 Top {0} saldi balanceTop=\u00a77 Top {0} saldi ({1})
banExempt=\u00a77Je kunt deze speler niet verbannen. banExempt=\u00a77Je kunt deze speler niet verbannen.
banIpAddress=\u00a77Verbannen IP-adres banIpAddress=\u00a77Verbannen IP-adres
bannedIpsFileError=Fout bij het lezen van banned-ips.txt bannedIpsFileError=Fout bij het lezen van banned-ips.txt