From ad94cca95b5543b1515ab5e54307ec7389201359 Mon Sep 17 00:00:00 2001 From: Ali Moghnieh Date: Tue, 22 Nov 2016 20:39:31 +0000 Subject: [PATCH] Check for timeout in `/tpa` and `/tpaccept`. Fixes #818. This commit adds a method called hasOutstandingTeleportRequest() in IUser - implemented fully in User. --- .../src/com/earth2me/essentials/IUser.java | 7 +++++++ .../src/com/earth2me/essentials/User.java | 18 ++++++++++++++++++ .../essentials/commands/Commandtpa.java | 2 +- .../essentials/commands/Commandtpaccept.java | 3 +-- .../essentials/commands/Commandtpahere.java | 2 +- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 93a79f8ff..12d51615c 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -43,6 +43,13 @@ public interface IUser { void requestTeleport(final User player, final boolean here); + /** + * Returns whether this user has an outstanding teleport request to deal with. + * + * @return whether there is a teleport request + */ + boolean hasOutstandingTeleportRequest(); + ITeleport getTeleport(); BigDecimal getMoney(); diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index fc1fedc9b..f92aabc35 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -241,6 +241,24 @@ public class User extends UserData implements Comparable, IMessageRecipien } } + @Override + public boolean hasOutstandingTeleportRequest() { + if (getTeleportRequest() != null) { // Player has outstanding teleport request. + long timeout = ess.getSettings().getTpaAcceptCancellation(); + if (timeout != 0) { + if ((System.currentTimeMillis() - getTeleportRequestTime()) / 1000 <= timeout) { // Player has outstanding request + return true; + } else { // outstanding request expired. + requestTeleport(null, false); + return false; + } + } else { // outstanding request does not expire + return true; + } + } + return false; + } + public UUID getTeleportRequest() { return teleportRequester; } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java index cb70586ce..cd1d575c9 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java @@ -28,7 +28,7 @@ public class Commandtpa extends EssentialsCommand { throw new Exception(tl("noPerm", "essentials.worlds." + player.getWorld().getName())); } // Don't let sender request teleport twice to the same player. - if (user.getConfigUUID().equals(player.getTeleportRequest()) + if (user.getConfigUUID().equals(player.getTeleportRequest()) && player.hasOutstandingTeleportRequest() // Check timeout && player.isTpRequestHere() == false) { // Make sure the last teleport request was actually tpa and not tpahere throw new Exception(tl("requestSentAlready", player.getDisplayName())); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java index 4a9bd2542..cca6432ed 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java @@ -40,8 +40,7 @@ public class Commandtpaccept extends EssentialsCommand { throw new Exception(tl("noPendingRequest")); } - long timeout = ess.getSettings().getTpaAcceptCancellation(); - if (timeout != 0 && (System.currentTimeMillis() - user.getTeleportRequestTime()) / 1000 > timeout) { + if (user.hasOutstandingTeleportRequest()) { user.requestTeleport(null, false); throw new Exception(tl("requestTimedOut")); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpahere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpahere.java index ac431947d..92765c418 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpahere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpahere.java @@ -28,7 +28,7 @@ public class Commandtpahere extends EssentialsCommand { throw new Exception(tl("noPerm", "essentials.worlds." + user.getWorld().getName())); } // Don't let sender request teleport twice to the same player. - if (user.getConfigUUID().equals(player.getTeleportRequest()) + if (user.getConfigUUID().equals(player.getTeleportRequest()) && player.hasOutstandingTeleportRequest() // Check timeout && player.isTpRequestHere() == true) { // Make sure the last teleport request was actually tpahere and not tpa throw new Exception(tl("requestSentAlready", player.getDisplayName())); }