diff options
author | KHobbits <rob@khobbits.co.uk> | 2012-06-10 21:55:48 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2012-06-10 21:55:48 +0100 |
commit | ecfa7454843cb9f4af61127c1a59838c83bf94fd (patch) | |
tree | 656c435cf044282f999b24604c4e0af001d16eb8 | |
parent | 20d439578beb8776290565c493fbd7985fbf959e (diff) | |
download | Essentials-ecfa7454843cb9f4af61127c1a59838c83bf94fd.tar Essentials-ecfa7454843cb9f4af61127c1a59838c83bf94fd.tar.gz Essentials-ecfa7454843cb9f4af61127c1a59838c83bf94fd.tar.lz Essentials-ecfa7454843cb9f4af61127c1a59838c83bf94fd.tar.xz Essentials-ecfa7454843cb9f4af61127c1a59838c83bf94fd.zip |
Try to be a little more sensible with stored cooldowns.
-rw-r--r-- | Essentials/src/com/earth2me/essentials/Kit.java | 37 | ||||
-rw-r--r-- | Essentials/src/com/earth2me/essentials/Teleport.java | 34 |
2 files changed, 48 insertions, 23 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index 5edd40776..5a907beb3 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -39,26 +39,35 @@ public class Kit public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws NoChargeException { - final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L; - final Calendar c = new GregorianCalendar(); - c.add(Calendar.SECOND, -(int)delay); - c.add(Calendar.MILLISECOND, -(int)((delay * 1000.0) % 1000.0)); + final Calendar time = new GregorianCalendar(); - final long mintime = c.getTimeInMillis(); + // Take the current time, and remove the delay from it. + final double delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0L; + final Calendar earliestTime = new GregorianCalendar(); + earliestTime.add(Calendar.SECOND, -(int)delay); + 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 earliestLong = earliestTime.getTimeInMillis(); + // When was the last kit used? final Long lastTime = user.getKitTimestamp(kitName); - if (lastTime == null || lastTime < mintime) + + if (lastTime == null || lastTime < earliestLong) + { + user.setKitTimestamp(kitName, time.getTimeInMillis()); + } + else if (lastTime > time.getTimeInMillis()) { - final Calendar now = new GregorianCalendar(); - user.setKitTimestamp(kitName, now.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 { - final Calendar future = new GregorianCalendar(); - future.setTimeInMillis(lastTime); - future.add(Calendar.SECOND, (int)delay); - future.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0)); - user.sendMessage(_("kitTimed", Util.formatDateDiff(future.getTimeInMillis()))); + time.setTimeInMillis(lastTime); + time.add(Calendar.SECOND, (int)delay); + time.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0)); + user.sendMessage(_("kitTimed", Util.formatDateDiff(time.getTimeInMillis()))); throw new NoChargeException(); } } @@ -77,7 +86,7 @@ public class Kit catch (Exception e) { user.sendMessage(_("kitError2")); - throw new Exception(_("kitErrorHelp"),e); + throw new Exception(_("kitErrorHelp"), e); } } diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 6569a1689..a920544cb 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -145,23 +145,39 @@ public class Teleport implements Runnable, ITeleport public void cooldown(boolean check) throws Exception { - Calendar now = new GregorianCalendar(); + final Calendar time = new GregorianCalendar(); if (user.getLastTeleportTimestamp() > 0) { - double cooldown = ess.getSettings().getTeleportCooldown(); - Calendar cooldownTime = new GregorianCalendar(); - cooldownTime.setTimeInMillis(user.getLastTeleportTimestamp()); - cooldownTime.add(Calendar.SECOND, (int)cooldown); - cooldownTime.add(Calendar.MILLISECOND, (int)((cooldown * 1000.0) % 1000.0)); - if (cooldownTime.after(now) && !user.isAuthorized("essentials.teleport.cooldown.bypass")) + // Take the current time, and remove the delay from it. + final double cooldown = ess.getSettings().getTeleportCooldown(); + final Calendar earliestTime = new GregorianCalendar(); + earliestTime.add(Calendar.SECOND, -(int)cooldown); + earliestTime.add(Calendar.MILLISECOND, -(int)((cooldown * 1000.0) % 1000.0)); + // 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 (!check) { - user.setLastTeleportTimestamp(now.getTimeInMillis()); + user.setLastTeleportTimestamp(time.getTimeInMillis()); } } |