From 6397d7876711d8210a8160fe2ec4c30e53569155 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Aug 2022 12:51:11 +0100 Subject: [PATCH] Prevent players starting a teleport warmup while moving --- .../net/william278/huskhomes/player/BukkitPlayer.java | 6 ++++++ .../net/william278/huskhomes/player/OnlineUser.java | 7 +++++++ .../william278/huskhomes/teleport/TeleportManager.java | 10 +++++++++- .../william278/huskhomes/teleport/TeleportResult.java | 5 +++++ common/src/main/resources/locales/en-gb.yml | 1 + 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java index 436743f5..493538bc 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java @@ -141,6 +141,12 @@ public class BukkitPlayer extends OnlineUser { }); } + @Override + public boolean isMoving() { + // Get the player momentum and return if they are moving + return player.getVelocity().length() >= 0.1; + } + /** * Send a Bukkit plugin message */ diff --git a/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java b/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java index c6b5e1f0..116bd7aa 100644 --- a/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java +++ b/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java @@ -97,6 +97,13 @@ public abstract class OnlineUser extends User { */ public abstract CompletableFuture teleport(@NotNull Location location, boolean asynchronous); + /** + * Returns if a player is moving (i.e. they have momentum) + * + * @return {@code true} if the player is moving; {@code false} otherwise + */ + public abstract boolean isMoving(); + /** * Get the maximum number of homes this user may set * diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java index e2e6b739..7766b2e6 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -247,7 +247,12 @@ public class TeleportManager { @NotNull Settings.EconomyAction... economyActions) { // Prevent players starting multiple timed teleports if (currentlyOnWarmup.contains(onlineUser.uuid)) { - return CompletableFuture.supplyAsync(() -> TeleportResult.FAILED_ALREADY_TELEPORTING); + return CompletableFuture.completedFuture(TeleportResult.FAILED_ALREADY_TELEPORTING); + } + + // Cancel if the player is already moving + if (onlineUser.isMoving()) { + return CompletableFuture.completedFuture(TeleportResult.FAILED_MOVING); } final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime; @@ -285,6 +290,8 @@ public class TeleportManager { .ifPresent(onlineUser::sendMessage); case FAILED_ALREADY_TELEPORTING -> plugin.getLocales().getLocale("error_already_teleporting") .ifPresent(onlineUser::sendMessage); + case FAILED_MOVING -> plugin.getLocales().getLocale("error_teleport_warmup_stand_still") + .ifPresent(onlineUser::sendMessage); case FAILED_INVALID_WORLD -> plugin.getLocales().getLocale("error_invalid_on_arrival") .ifPresent(onlineUser::sendMessage); case FAILED_ILLEGAL_COORDINATES -> plugin.getLocales().getLocale("error_illegal_target_coordinates") @@ -370,6 +377,7 @@ public class TeleportManager { * @return a future, returning when the teleport has finished */ private CompletableFuture processTeleportWarmup(@NotNull final TimedTeleport teleport) { + // Mark the player as warming up currentlyOnWarmup.add(teleport.getPlayer().uuid); diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java index 96bb47e7..d114f758 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java @@ -23,6 +23,11 @@ public enum TeleportResult { */ FAILED_ALREADY_TELEPORTING(false), + /** + * Returns if the {@link Teleport} warmup could not be started because they were moving + */ + FAILED_MOVING(false), + /** * Returns if the {@link Teleport} failed because the {@link World} of the * target {@link Position} could not be found diff --git a/common/src/main/resources/locales/en-gb.yml b/common/src/main/resources/locales/en-gb.yml index 7d1b1127..9180706a 100644 --- a/common/src/main/resources/locales/en-gb.yml +++ b/common/src/main/resources/locales/en-gb.yml @@ -140,6 +140,7 @@ error_public_home_invalid: '[Error:](#ff3300) [Could not find a public home owne error_set_home_maximum_homes: '[Error:](#ff3300) [You can only set a maximum of %1% homes!](#ff7e5e)' error_spawn_not_set: '[Error:](#ff3300) [The spawn position has not been set.](#ff7e5e)' error_no_teleport_requests: '[Error:](#ff3300) [You do not have a pending teleport request.](#ff7e5e)' +error_teleport_warmup_stand_still: '[Error:](#ff3300) [You must be standing still before starting a teleport!](#ff7e5e)' error_teleport_request_expired: '[Error:](#ff3300) [Your pending teleport request has expired.](#ff7e5e)' error_teleport_request_sender_not_online: '[Error:](#ff3300) [The sender of your pending teleport request is no longer online.](#ff7e5e)' error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests.](#ff7e5e) [[Stop Ignoring…]](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)'