1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-12 09:35:26 +02:00

Try to be a little more sensible with stored cooldowns.

This commit is contained in:
KHobbits
2012-06-10 21:55:48 +01:00
parent 20d439578b
commit ecfa745484
2 changed files with 48 additions and 23 deletions

View File

@@ -39,26 +39,35 @@ public class Kit
public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws NoChargeException public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws NoChargeException
{ {
final Calendar time = new GregorianCalendar();
// Take the current time, and remove the delay from it.
final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L; final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L;
final Calendar c = new GregorianCalendar(); final Calendar earliestTime = new GregorianCalendar();
c.add(Calendar.SECOND, -(int)delay); earliestTime.add(Calendar.SECOND, -(int)delay);
c.add(Calendar.MILLISECOND, -(int)((delay * 1000.0) % 1000.0)); earliestTime.add(Calendar.MILLISECOND, -(int)((delay * 1000.0) % 1000.0));
// This value contains the most recent time a kit could have been used that would allow another use.
final long mintime = c.getTimeInMillis(); final long earliestLong = earliestTime.getTimeInMillis();
// When was the last kit used?
final Long lastTime = user.getKitTimestamp(kitName); final Long lastTime = user.getKitTimestamp(kitName);
if (lastTime == null || lastTime < mintime)
if (lastTime == null || lastTime < earliestLong)
{ {
final Calendar now = new GregorianCalendar(); user.setKitTimestamp(kitName, time.getTimeInMillis());
user.setKitTimestamp(kitName, now.getTimeInMillis()); }
else if (lastTime > time.getTimeInMillis())
{
// This is to make sure time didn't get messed up on last kit use.
// If this happens, let's give the user the benifit of the doubt.
user.setKitTimestamp(kitName, time.getTimeInMillis());
} }
else else
{ {
final Calendar future = new GregorianCalendar(); time.setTimeInMillis(lastTime);
future.setTimeInMillis(lastTime); time.add(Calendar.SECOND, (int)delay);
future.add(Calendar.SECOND, (int)delay); time.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
future.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0)); user.sendMessage(_("kitTimed", Util.formatDateDiff(time.getTimeInMillis())));
user.sendMessage(_("kitTimed", Util.formatDateDiff(future.getTimeInMillis())));
throw new NoChargeException(); throw new NoChargeException();
} }
} }
@@ -77,7 +86,7 @@ public class Kit
catch (Exception e) catch (Exception e)
{ {
user.sendMessage(_("kitError2")); user.sendMessage(_("kitError2"));
throw new Exception(_("kitErrorHelp"),e); throw new Exception(_("kitErrorHelp"), e);
} }
} }

View File

@@ -145,23 +145,39 @@ public class Teleport implements Runnable, ITeleport
public void cooldown(boolean check) throws Exception public void cooldown(boolean check) throws Exception
{ {
Calendar now = new GregorianCalendar(); final Calendar time = new GregorianCalendar();
if (user.getLastTeleportTimestamp() > 0) if (user.getLastTeleportTimestamp() > 0)
{ {
double cooldown = ess.getSettings().getTeleportCooldown(); // Take the current time, and remove the delay from it.
Calendar cooldownTime = new GregorianCalendar(); final double cooldown = ess.getSettings().getTeleportCooldown();
cooldownTime.setTimeInMillis(user.getLastTeleportTimestamp()); final Calendar earliestTime = new GregorianCalendar();
cooldownTime.add(Calendar.SECOND, (int)cooldown); earliestTime.add(Calendar.SECOND, -(int)cooldown);
cooldownTime.add(Calendar.MILLISECOND, (int)((cooldown * 1000.0) % 1000.0)); earliestTime.add(Calendar.MILLISECOND, -(int)((cooldown * 1000.0) % 1000.0));
if (cooldownTime.after(now) && !user.isAuthorized("essentials.teleport.cooldown.bypass")) // This value contains the most recent time a teleport could have been used that would allow another use.
final long earliestLong = earliestTime.getTimeInMillis();
// When was the last teleport used?
final Long lastTime = user.getLastTeleportTimestamp();
if (lastTime > time.getTimeInMillis())
{ {
throw new Exception(_("timeBeforeTeleport", Util.formatDateDiff(cooldownTime.getTimeInMillis()))); // This is to make sure time didn't get messed up on last kit use.
// If this happens, let's give the user the benifit of the doubt.
user.setLastTeleportTimestamp(time.getTimeInMillis());
return;
}
else if (lastTime > earliestLong && !user.isAuthorized("essentials.teleport.cooldown.bypass"))
{
time.setTimeInMillis(lastTime);
time.add(Calendar.SECOND, (int)delay);
time.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
throw new Exception(_("timeBeforeTeleport", Util.formatDateDiff(time.getTimeInMillis())));
} }
} }
// if justCheck is set, don't update lastTeleport; we're just checking // if justCheck is set, don't update lastTeleport; we're just checking
if (!check) if (!check)
{ {
user.setLastTeleportTimestamp(now.getTimeInMillis()); user.setLastTeleportTimestamp(time.getTimeInMillis());
} }
} }