diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 95d1420a..8877a42c 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -20,7 +20,6 @@ import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.SavedPositionManager; import net.william278.huskhomes.position.Server; -import net.william278.huskhomes.teleport.BukkitTeleportManager; import net.william278.huskhomes.teleport.TeleportManager; import net.william278.huskhomes.util.*; import org.bstats.bukkit.Metrics; @@ -151,7 +150,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { cache.initialize(database); // Prepare the teleport manager - this.teleportManager = new BukkitTeleportManager(this); + this.teleportManager = new TeleportManager(this); // Prepare the home and warp position manager this.savedPositionManager = new SavedPositionManager(database, cache); diff --git a/bukkit/src/main/java/net/william278/huskhomes/teleport/BukkitTeleportManager.java b/bukkit/src/main/java/net/william278/huskhomes/teleport/BukkitTeleportManager.java deleted file mode 100644 index 67aa2d70..00000000 --- a/bukkit/src/main/java/net/william278/huskhomes/teleport/BukkitTeleportManager.java +++ /dev/null @@ -1,44 +0,0 @@ -package net.william278.huskhomes.teleport; - -import net.william278.huskhomes.HuskHomes; -import net.william278.huskhomes.BukkitHuskHomes; -import org.bukkit.scheduler.BukkitRunnable; -import org.jetbrains.annotations.NotNull; - -import java.util.Optional; -import java.util.concurrent.CompletableFuture; - -/** - * Bukkit implementation of a {@link TeleportManager}, handling timed teleports through a BukkitRunnable - */ -public class BukkitTeleportManager extends TeleportManager { - - public BukkitTeleportManager(@NotNull HuskHomes implementor) { - super(implementor); - } - - @Override - public CompletableFuture processTimedTeleport(@NotNull final TimedTeleport teleport) { - return CompletableFuture.supplyAsync(() -> { - CompletableFuture teleportCompletableFuture = new CompletableFuture<>(); - new BukkitRunnable() { - @Override - public void run() { - // Display countdown action bar message - plugin.getLocales().getLocale("teleporting_action_bar_countdown", Integer.toString(teleport.timeLeft)) - .ifPresent(message -> teleport.getPlayer().sendActionBar(message)); - - // Tick (decrement) the timed teleport timer - final Optional result = tickTimedTeleport(teleport); - if (result.isPresent()) { - teleportCompletableFuture.complete(teleport); - this.cancel(); - } - } - }.runTaskTimerAsynchronously((BukkitHuskHomes) plugin, 0L, 20L); - - return teleportCompletableFuture.join(); - }); - } - -} 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 c8ba4341..bd7154b6 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -14,11 +14,14 @@ import org.jetbrains.annotations.NotNull; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * Cross-platform teleportation manager */ -public abstract class TeleportManager { +public class TeleportManager { /** * Instance of the implementing plugin @@ -114,23 +117,23 @@ public abstract class TeleportManager { * @return future optionally supplying the player's position, if the player could be found */ private CompletableFuture> getPlayerPosition(@NotNull OnlineUser requester, @NotNull String playerName) { - final Optional localPlayer = plugin.getOnlinePlayers().stream().filter(player -> - player.username.equalsIgnoreCase(playerName)).findFirst(); - if (localPlayer.isPresent()) { - return localPlayer.get().getPosition().thenApply(Optional::of); - } - if (plugin.getSettings().crossServer) { - assert plugin.getNetworkMessenger() != null; - return plugin.getNetworkMessenger().sendMessage(requester, - new Message(Message.MessageType.POSITION_REQUEST, - requester.username, - playerName, - new EmptyPayload(), - Message.MessageKind.MESSAGE, - plugin.getSettings().clusterId)) - .thenApply(reply -> Optional.of(Position.fromJson(reply.payload))); - } - return CompletableFuture.supplyAsync(Optional::empty); + final Optional localPlayer = plugin.getOnlinePlayers().stream().filter(player -> + player.username.equalsIgnoreCase(playerName)).findFirst(); + if (localPlayer.isPresent()) { + return localPlayer.get().getPosition().thenApply(Optional::of); + } + if (plugin.getSettings().crossServer) { + assert plugin.getNetworkMessenger() != null; + return plugin.getNetworkMessenger().sendMessage(requester, + new Message(Message.MessageType.POSITION_REQUEST, + requester.username, + playerName, + new EmptyPayload(), + Message.MessageKind.MESSAGE, + plugin.getSettings().clusterId)) + .thenApply(reply -> Optional.of(Position.fromJson(reply.payload))); + } + return CompletableFuture.supplyAsync(Optional::empty); } /** @@ -140,19 +143,19 @@ public abstract class TeleportManager { * @param position the target {@link Position} to teleport to */ public CompletableFuture teleport(@NotNull OnlineUser onlineUser, @NotNull Position position) { - final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime; - if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) { - return processTimedTeleport(new TimedTeleport(onlineUser, position, teleportWarmupTime)) - .thenApply(teleport -> { - if (!teleport.cancelled) { - return teleportNow(onlineUser, position); - } else { - return CompletableFuture.supplyAsync(() -> TeleportResult.CANCELLED); - } - }).join(); - } else { - return teleportNow(onlineUser, position); - } + final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime; + if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) { + return processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime)) + .thenApply(teleport -> { + if (!teleport.cancelled) { + return teleportNow(onlineUser, position); + } else { + return CompletableFuture.supplyAsync(() -> TeleportResult.CANCELLED); + } + }).join(); + } else { + return teleportNow(onlineUser, position); + } } /** @@ -207,12 +210,46 @@ public abstract class TeleportManager { } /** - * Process a timed teleport, ticking it + * Processes a timed teleport + * + * @param teleport the {@link TimedTeleport} to process + * @return a future, returning when the teleport has finished + */ + private CompletableFuture processTeleportWarmup(@NotNull final TimedTeleport teleport) { + // Create a scheduled executor to tick the timed teleport + final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + final CompletableFuture timedTeleportFuture = new CompletableFuture<>(); + executor.scheduleAtFixedRate(() -> { + // Display countdown action bar message + plugin.getLocales().getLocale("teleporting_action_bar_countdown", Integer.toString(teleport.timeLeft)) + .ifPresent(message -> teleport.getPlayer().sendActionBar(message)); + + // Tick (decrement) the timed teleport timer + final Optional result = tickTeleportWarmup(teleport); + if (result.isPresent()) { + timedTeleportFuture.complete(teleport); + executor.shutdown(); + } + }, 0, 1, TimeUnit.SECONDS); + return timedTeleportFuture; + } + + /** + * Ticks a timed teleport, decrementing the time left until the teleport is complete + *

+ * A timed teleport will be cancelled if certain criteria are met: + *

    + *
  • The player has left the server
  • + *
  • The plugin is disabling
  • + *
  • The player has moved beyond the movement threshold from when the warmup started
  • + *
  • The player has taken damage (though they may heal, have status ailments or lose/gain hunger)
  • + *
* * @param teleport the {@link TimedTeleport} being ticked - * @return {@code true} if the implementor should cancel the timedTeleport + * @return Optional containing the {@link TimedTeleport} after it has been ticked, + * or {@link Optional#empty()} if the teleport has been cancelled */ - protected final Optional tickTimedTeleport(@NotNull final TimedTeleport teleport) { + private Optional tickTeleportWarmup(@NotNull final TimedTeleport teleport) { if (teleport.isDone()) { return Optional.of(teleport); } @@ -242,12 +279,4 @@ public abstract class TeleportManager { return Optional.empty(); } - /** - * Process a timed teleport, implemented by platform-specific schedulers - * - * @param teleport the {@link TimedTeleport} to process - * @return a future, returning when the teleport has finished - */ - public abstract CompletableFuture processTimedTeleport(TimedTeleport teleport); - }