diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/SpawnCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/SpawnCommand.java index 4824b833..821f337d 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commands/SpawnCommand.java +++ b/src/main/java/org/mvplugins/multiverse/core/commands/SpawnCommand.java @@ -62,7 +62,9 @@ class SpawnCommand extends CoreCommand { // Teleport the player to spawn // TODO: Different message for teleporting self vs others - safetyTeleporter.teleportSafely(issuer.getIssuer(), player, world.getSpawnLocation()) + safetyTeleporter.to(world.getSpawnLocation()) + .by(issuer) + .teleport(player) .onSuccess(() -> player.sendMessage(commandManager.formatMessage( issuer, MessageType.INFO, diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/TeleportCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/TeleportCommand.java index 7d1d862a..6a249c0d 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commands/TeleportCommand.java +++ b/src/main/java/org/mvplugins/multiverse/core/commands/TeleportCommand.java @@ -83,7 +83,7 @@ class TeleportCommand extends CoreCommand { safetyTeleporter.to(destination) .by(issuer) - .checkSafety(parsedFlags.hasFlag(UNSAFE_FLAG) || destination.checkTeleportSafety()) + .checkSafety(!parsedFlags.hasFlag(UNSAFE_FLAG) && destination.checkTeleportSafety()) .teleport(List.of(players)) .thenAccept(attempts -> { //todo: Check for attempt results diff --git a/src/main/java/org/mvplugins/multiverse/core/listeners/MVPlayerListener.java b/src/main/java/org/mvplugins/multiverse/core/listeners/MVPlayerListener.java index 9a570ede..ce2bb18a 100644 --- a/src/main/java/org/mvplugins/multiverse/core/listeners/MVPlayerListener.java +++ b/src/main/java/org/mvplugins/multiverse/core/listeners/MVPlayerListener.java @@ -195,6 +195,7 @@ public class MVPlayerListener implements CoreListener { * Will teleport the player to the destination specified in config * @param player The {@link Player} to teleport */ + //todo: Explore the use of PlayerSpawnLocationEvent private void handleJoinDestination(@NotNull Player player) { if (!config.getEnableJoinDestination()) { Logging.finer("JoinDestination is disabled"); @@ -209,7 +210,7 @@ public class MVPlayerListener implements CoreListener { Logging.finer("JoinDestination is " + config.getJoinDestination()); destinationsProvider.parseDestination(config.getJoinDestination()) - .peek(destination -> safetyTeleporter.teleportSafely(player, player, destination)) + .peek(destination -> safetyTeleporter.to(destination).teleport(player)) .onEmpty(() -> Logging.warning("The destination in JoinDestination in config is invalid")); } @@ -363,7 +364,7 @@ public class MVPlayerListener implements CoreListener { new Runnable() { @Override public void run() { - safetyTeleporter.teleportSafely(player, player, parsedDestination); + safetyTeleporter.to(parsedDestination).teleport(player); } }, 1L); } diff --git a/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java b/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java index 86faa351..a5749191 100644 --- a/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java +++ b/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java @@ -1,15 +1,8 @@ package org.mvplugins.multiverse.core.teleportation; -import java.util.List; - -import com.dumptruckman.minecraft.util.Logging; -import io.papermc.lib.PaperLib; import io.vavr.control.Either; import jakarta.inject.Inject; import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -17,10 +10,6 @@ import org.jvnet.hk2.annotations.Service; import org.mvplugins.multiverse.core.api.BlockSafety; import org.mvplugins.multiverse.core.destination.DestinationInstance; -import org.mvplugins.multiverse.core.event.MVTeleportDestinationEvent; -import org.mvplugins.multiverse.core.utils.result.Async; -import org.mvplugins.multiverse.core.utils.result.AsyncAttempt; -import org.mvplugins.multiverse.core.utils.result.Attempt; /** * Teleports entities safely and asynchronously. @@ -41,153 +30,6 @@ public class AsyncSafetyTeleporter { this.pluginManager = pluginManager; } - public AsyncAttempt teleportSafely( - @NotNull Entity teleportee, - @Nullable DestinationInstance destination) { - return teleportSafely(null, teleportee, destination); - } - - public Async>> teleportSafely( - @Nullable CommandSender teleporter, - @NotNull List teleportees, - @Nullable DestinationInstance destination) { - return AsyncAttempt.allOf(teleportees.stream() - .map(teleportee -> teleportSafely(teleporter, teleportee, destination)) - .toList()); - } - - public AsyncAttempt teleportSafely( - @Nullable CommandSender teleporter, - @NotNull Entity teleportee, - @Nullable DestinationInstance destination) { - if (destination == null) { - return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION); - } - MVTeleportDestinationEvent event = new MVTeleportDestinationEvent(destination, teleportee, teleporter); - this.pluginManager.callEvent(event); - if (event.isCancelled()) { - return AsyncAttempt.failure(TeleportResult.Failure.EVENT_CANCELLED); - } - return destination.getLocation(teleportee) - .map(location -> destination.checkTeleportSafety() - ? teleportSafely(teleporter, teleportee, location) - : teleport(teleporter, teleportee, location)) - .getOrElse(AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION)); - } - - public AsyncAttempt teleportSafely( - @NotNull Entity teleportee, - @Nullable Location location) { - return teleportSafely(null, teleportee, location); - } - - public AsyncAttempt teleportSafely( - @Nullable CommandSender teleporter, - @NotNull Entity teleportee, - @Nullable Location location) { - if (location == null) { - return AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION); - } - Location safeLocation = blockSafety.getSafeLocation(location); - if (safeLocation == null) { - return AsyncAttempt.failure(TeleportResult.Failure.UNSAFE_LOCATION); - } - return teleport(teleporter, teleportee, safeLocation); - } - - public Async>> teleport( - @Nullable CommandSender teleporter, - @NotNull List teleportees, - @Nullable DestinationInstance destination) { - return AsyncAttempt.allOf(teleportees.stream() - .map(teleportee -> teleport(teleporter, teleportee, destination)) - .toList()); - } - - public Async>> teleport( - @NotNull List teleportees, - @Nullable DestinationInstance destination) { - return AsyncAttempt.allOf(teleportees.stream() - .map(teleportee -> teleport(teleportee, destination)) - .toList()); - } - - public AsyncAttempt teleport( - @NotNull Entity teleportee, - @Nullable DestinationInstance destination) { - return teleport(null, teleportee, destination); - } - - public AsyncAttempt teleport( - @Nullable CommandSender teleporter, - @NotNull Entity teleportee, - @Nullable DestinationInstance destination) { - if (destination == null) { - return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION); - } - MVTeleportDestinationEvent event = new MVTeleportDestinationEvent(destination, teleportee, teleporter); - this.pluginManager.callEvent(event); - if (event.isCancelled()) { - return AsyncAttempt.failure(TeleportResult.Failure.EVENT_CANCELLED); - } - return destination.getLocation(teleportee) - .map(location -> teleport(teleporter, teleportee, location)) - .getOrElse(AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION)); - } - - public Async>> teleport( - @NotNull List teleportees, - @Nullable Location location) { - return AsyncAttempt.allOf(teleportees.stream() - .map(teleportee -> teleport(teleportee, location)) - .toList()); - } - - public AsyncAttempt teleport( - @NotNull Entity teleportee, - @Nullable Location location) { - return teleport(null, teleportee, location); - } - - public AsyncAttempt teleport( - @Nullable CommandSender teleporter, - @NotNull Entity teleportee, - @Nullable Location location) { - if (location == null) { - return AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION); - } - - boolean shouldAddToQueue = teleportee instanceof Player; - if (shouldAddToQueue) { - if (teleporter == null) { - teleporter = teleportee; - } - teleportQueue.addToQueue(teleporter.getName(), teleportee.getName()); - } - - return doAsyncTeleport(teleportee, location, shouldAddToQueue); - } - - private AsyncAttempt doAsyncTeleport( - @NotNull Entity teleportee, - @NotNull Location location, - boolean shouldRemoveFromQueue) { - return AsyncAttempt.of(PaperLib.teleportAsync(teleportee, location), exception -> { - Logging.warning("Failed to teleport %s to %s: %s", - teleportee.getName(), location, exception.getMessage()); - return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED_EXCEPTION); - }).mapAttempt(success -> { - if (shouldRemoveFromQueue) { - teleportQueue.popFromQueue(teleportee.getName()); - } - if (success) { - Logging.finer("Teleported async %s to %s", teleportee.getName(), location); - return Attempt.success(null); - } - return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED); - }); - } - public AsyncSafetyTeleporterAction to(@Nullable Location location) { return new AsyncSafetyTeleporterAction( blockSafety, diff --git a/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporterAction.java b/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporterAction.java index bec61a8a..bc32c6ee 100644 --- a/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporterAction.java +++ b/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporterAction.java @@ -132,6 +132,7 @@ public class AsyncSafetyTeleporterAction { Logging.finer("Teleported async %s to %s", teleportee.getName(), location); return Attempt.success(null); } + Logging.warning("Failed to async teleport %s to %s", teleportee.getName(), location); return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED); }); } diff --git a/src/main/java/org/mvplugins/multiverse/core/world/helpers/PlayerWorldTeleporter.java b/src/main/java/org/mvplugins/multiverse/core/world/helpers/PlayerWorldTeleporter.java index 27bc87bf..ea8d7af5 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/helpers/PlayerWorldTeleporter.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/helpers/PlayerWorldTeleporter.java @@ -77,7 +77,7 @@ public class PlayerWorldTeleporter { @NotNull LoadedMultiverseWorld world, @NotNull Location location) { return world.getPlayers() - .map(players -> safetyTeleporter.teleport(players, location)) + .map(players -> safetyTeleporter.to(location).teleport(players)) .getOrElse(() -> Async.failedFuture( new IllegalStateException("Unable to get players from world" + world.getName()))); } @@ -93,6 +93,6 @@ public class PlayerWorldTeleporter { @NotNull List players, @NotNull LoadedMultiverseWorld world) { Location spawnLocation = world.getSpawnLocation(); - return safetyTeleporter.teleport(players, spawnLocation); + return safetyTeleporter.to(spawnLocation).teleport(players); } }