From eafea119a666269dec8c697272c42b5d004df757 Mon Sep 17 00:00:00 2001 From: "@ArkhamNetwork" Date: Sat, 17 May 2014 01:12:13 +0100 Subject: Move the teleport timer to an async task. --- .../src/com/earth2me/essentials/Essentials.java | 6 ++ .../src/com/earth2me/essentials/IEssentials.java | 6 +- .../src/com/earth2me/essentials/TimedTeleport.java | 95 +++++++++++++--------- 3 files changed, 65 insertions(+), 42 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 305fee60e..e600f2cec 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -803,6 +803,12 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { return this.getScheduler().runTaskLaterAsynchronously(this, run, delay); } + + @Override + public BukkitTask runTaskTimerAsynchronously(final Runnable run, final long delay, final long period) + { + return this.getScheduler().runTaskTimerAsynchronously(this, run, delay, period); + } @Override public int scheduleSyncDelayedTask(final Runnable run) diff --git a/Essentials/src/com/earth2me/essentials/IEssentials.java b/Essentials/src/com/earth2me/essentials/IEssentials.java index ea18fb329..1e155976e 100644 --- a/Essentials/src/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/IEssentials.java @@ -63,12 +63,14 @@ public interface IEssentials extends Plugin BukkitTask runTaskAsynchronously(Runnable run); BukkitTask runTaskLaterAsynchronously(Runnable run, long delay); + + BukkitTask runTaskTimerAsynchronously(Runnable run, long delay, long period); int scheduleSyncDelayedTask(Runnable run); int scheduleSyncDelayedTask(Runnable run, long delay); - int scheduleSyncRepeatingTask(final Runnable run, long delay, long period); + int scheduleSyncRepeatingTask(Runnable run, long delay, long period); TNTExplodeListener getTNTListener(); @@ -76,7 +78,7 @@ public interface IEssentials extends Plugin AlternativeCommandsHandler getAlternativeCommandsHandler(); - void showError(final CommandSource sender, final Throwable exception, final String commandLabel); + void showError(CommandSource sender, Throwable exception, String commandLabel); IItemDb getItemDb(); diff --git a/Essentials/src/com/earth2me/essentials/TimedTeleport.java b/Essentials/src/com/earth2me/essentials/TimedTeleport.java index edc614235..6b9e88f97 100644 --- a/Essentials/src/com/earth2me/essentials/TimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/TimedTeleport.java @@ -50,7 +50,7 @@ public class TimedTeleport implements Runnable this.timer_respawn = respawn; this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move"); - timer_task = ess.scheduleSyncRepeatingTask(this, 20, 20); + timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); } @Override @@ -63,7 +63,7 @@ public class TimedTeleport implements Runnable return; } - IUser teleportUser = ess.getUser(this.timer_teleportee); + final IUser teleportUser = ess.getUser(this.timer_teleportee); if (teleportUser == null || !teleportUser.getBase().isOnline()) { @@ -89,49 +89,64 @@ public class TimedTeleport implements Runnable return; } - timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured - final long now = System.currentTimeMillis(); - if (now > timer_started + timer_delay) + ess.scheduleSyncDelayedTask(new Runnable() { - try + @Override + public void run() { - teleport.cooldown(false); - } - catch (Exception ex) - { - teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); - if (teleportOwner != teleportUser) - { - teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); - } - } - try - { - cancelTimer(false); - teleportUser.sendMessage(tl("teleportationCommencing")); - if (timer_chargeFor != null) - { - timer_chargeFor.isAffordableFor(teleportOwner); - } - if (timer_respawn) - { - teleport.respawnNow(teleportUser, timer_cause); - } - else - { - teleport.now(teleportUser, timer_teleportTarget, timer_cause); - } - if (timer_chargeFor != null) + + timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured + final long now = System.currentTimeMillis(); + if (now > timer_started + timer_delay) { - timer_chargeFor.charge(teleportOwner); + try + { + teleport.cooldown(false); + } + catch (Exception ex) + { + teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); + if (teleportOwner != teleportUser) + { + teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); + } + } + try + { + cancelTimer(false); + teleportUser.sendMessage(tl("teleportationCommencing")); + + try + { + if (timer_chargeFor != null) + { + timer_chargeFor.isAffordableFor(teleportOwner); + } + if (timer_respawn) + { + teleport.respawnNow(teleportUser, timer_cause); + } + else + { + teleport.now(teleportUser, timer_teleportTarget, timer_cause); + } + if (timer_chargeFor != null) + { + timer_chargeFor.charge(teleportOwner); + } + } + catch (Exception ex) + { + } + + } + catch (Exception ex) + { + ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); + } } } - catch (Exception ex) - { - ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); - } - - } + }); } //If we need to cancelTimer a pending teleportPlayer call this method -- cgit v1.2.3