diff --git a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java index 7982e9ff4..12d5eb23b 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java @@ -299,7 +299,7 @@ public class AsyncTeleport implements IAsyncTeleport { cancel(false); warnUser(teleportee, delay); - initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); + initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false, future); } private void teleportOther(final IUser teleporter, final IUser teleportee, final ITarget target, final Trade chargeFor, final TeleportCause cause, final CompletableFuture future) { @@ -345,12 +345,13 @@ public class AsyncTeleport implements IAsyncTeleport { return; } } + future.complete(true); return; } cancel(false); warnUser(teleportee, delay); - initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); + initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false, future); } @Override @@ -381,12 +382,13 @@ public class AsyncTeleport implements IAsyncTeleport { if (chargeFor != null) { chargeFor.charge(teleportOwner, future); } + future.complete(true); return; } cancel(false); warnUser(teleportOwner, delay); - initTimer((long) (delay * 1000.0), teleportOwner, null, chargeFor, cause, true); + initTimer((long) (delay * 1000.0), teleportOwner, null, chargeFor, cause, true, future); } void respawnNow(final IUser teleportee, final TeleportCause cause, final CompletableFuture future) { @@ -466,8 +468,8 @@ public class AsyncTeleport implements IAsyncTeleport { } } - private void initTimer(final long delay, final IUser teleportUser, final ITarget target, final Trade chargeFor, final TeleportCause cause, final boolean respawn) { - timedTeleport = new AsyncTimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn); + private void initTimer(final long delay, final IUser teleportUser, final ITarget target, final Trade chargeFor, final TeleportCause cause, final boolean respawn, CompletableFuture future) { + timedTeleport = new AsyncTimedTeleport(teleportOwner, ess, this, delay, future, teleportUser, target, chargeFor, cause, respawn); } public enum TeleportType { diff --git a/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java b/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java index 260625e86..e38ee1625 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java @@ -18,6 +18,7 @@ public class AsyncTimedTeleport implements Runnable { private final UUID timer_teleportee; private final long timer_started; // time this task was initiated private final long timer_delay; // how long to delay the teleportPlayer + private final CompletableFuture parentFuture; // note that I initially stored a clone of the location for reference, but... // when comparing locations, I got incorrect mismatches (rounding errors, looked like) // so, the X/Y/Z values are stored instead and rounded off @@ -33,6 +34,10 @@ public class AsyncTimedTeleport implements Runnable { private double timer_health; AsyncTimedTeleport(final IUser user, final IEssentials ess, final AsyncTeleport teleport, final long delay, final IUser teleportUser, final ITarget target, final Trade chargeFor, final TeleportCause cause, final boolean respawn) { + this(user, ess, teleport, delay, null, teleportUser, target, chargeFor, cause, respawn); + } + + AsyncTimedTeleport(final IUser user, final IEssentials ess, final AsyncTeleport teleport, final long delay, final CompletableFuture future, final IUser teleportUser, final ITarget target, final Trade chargeFor, final TeleportCause cause, final boolean respawn) { this.teleportOwner = user; this.ess = ess; this.teleport = teleport; @@ -50,6 +55,18 @@ public class AsyncTimedTeleport implements Runnable { this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move"); timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); + + if (future != null) { + this.parentFuture = future; + return; + } + + final CompletableFuture cFuture = new CompletableFuture<>(); + cFuture.exceptionally(e -> { + ess.showError(teleportOwner.getSource(), e, "\\ teleport"); + return false; + }); + this.parentFuture = cFuture; } @Override @@ -98,20 +115,16 @@ public class AsyncTimedTeleport implements Runnable { cancelTimer(false); teleportUser.sendMessage(tl("teleportationCommencing")); - final CompletableFuture future = new CompletableFuture<>(); - future.exceptionally(e -> { - ess.showError(teleportOwner.getSource(), e, "\\ teleport"); - return false; - }); if (timer_chargeFor != null) { timer_chargeFor.isAffordableFor(teleportOwner); } + if (timer_respawn) { - teleport.respawnNow(teleportUser, timer_cause, future); + teleport.respawnNow(teleportUser, timer_cause, parentFuture); } else { - teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future); + teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, parentFuture); } - future.thenAccept(success -> { + parentFuture.thenAccept(success -> { if (timer_chargeFor != null) { try { timer_chargeFor.charge(teleportOwner);