From 681a74213ca2f48778e7ff90b5f357d3c10b0410 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 19 Oct 2022 01:27:06 +0100 Subject: [PATCH] Fix API back-compat; rename new result class to CompletedTeleport and move enum TeleportResult back to upper --- .../huskhomes/player/BukkitPlayer.java | 12 +- .../huskhomes/api/BaseHuskHomesAPI.java | 34 +-- .../huskhomes/messenger/MessagePayload.java | 6 +- .../huskhomes/player/OnlineUser.java | 2 +- .../huskhomes/teleport/CompletedTeleport.java | 117 ++++++++ .../huskhomes/teleport/Teleport.java | 40 +-- .../huskhomes/teleport/TeleportResult.java | 255 +++++------------- .../huskhomes/teleport/TimedTeleport.java | 20 +- 8 files changed, 248 insertions(+), 238 deletions(-) create mode 100644 common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java 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 d4ed3c48..c7f05b62 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java @@ -101,24 +101,24 @@ public class BukkitPlayer extends OnlineUser { } @Override - public CompletableFuture teleportLocally(@NotNull Location location, boolean asynchronous) { + public CompletableFuture teleportLocally(@NotNull Location location, boolean asynchronous) { final Optional bukkitLocation = BukkitAdapter.adaptLocation(location); if (bukkitLocation.isEmpty()) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_INVALID_WORLD); + return CompletableFuture.completedFuture(TeleportResult.FAILED_INVALID_WORLD); } assert bukkitLocation.get().getWorld() != null; if (!bukkitLocation.get().getWorld().getWorldBorder().isInside(bukkitLocation.get())) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_ILLEGAL_COORDINATES); + return CompletableFuture.completedFuture(TeleportResult.FAILED_ILLEGAL_COORDINATES); } - final CompletableFuture resultCompletableFuture = new CompletableFuture<>(); + final CompletableFuture resultCompletableFuture = new CompletableFuture<>(); Bukkit.getScheduler().runTask(plugin, () -> { if (asynchronous) { PaperLib.teleportAsync(player, bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN) .thenAccept(result -> resultCompletableFuture.complete( - result ? TeleportResult.ResultState.COMPLETED_LOCALLY : TeleportResult.ResultState.FAILED_INVALID_WORLD)); + result ? TeleportResult.COMPLETED_LOCALLY : TeleportResult.FAILED_INVALID_WORLD)); } else { player.teleport(bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN); - resultCompletableFuture.complete(TeleportResult.ResultState.COMPLETED_LOCALLY); + resultCompletableFuture.complete(TeleportResult.COMPLETED_LOCALLY); } }); return resultCompletableFuture; diff --git a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java index a1a95aac..27633174 100644 --- a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java +++ b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java @@ -343,15 +343,15 @@ public abstract class BaseHuskHomesAPI { * @param position The {@link Position} to teleport the user to * @param timedTeleport Whether the teleport should be timed or not (requiring a warmup where they must stand still * for a period of time) - * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult.ResultState} indicating the result of - * completing the teleport. If the teleport was successful, the {@link TeleportResult.ResultState#successful} will be {@code true}. + * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of + * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}. * @since 3.0 * @deprecated Use {@link #teleportBuilder(OnlineUser)} to construct a teleport */ @Deprecated(since = "3.1") - public final CompletableFuture teleportPlayer(@NotNull OnlineUser user, - @NotNull Position position, - final boolean timedTeleport) { + public final CompletableFuture teleportPlayer(@NotNull OnlineUser user, + @NotNull Position position, + final boolean timedTeleport) { final TeleportBuilder builder = teleportBuilder(user).setTarget(position); return timedTeleport ? builder.toTimedTeleport().thenApplyAsync(teleport -> teleport.execute().join().getState()) @@ -363,13 +363,13 @@ public abstract class BaseHuskHomesAPI { * * @param user The {@link OnlineUser} to teleport * @param position The {@link Position} to teleport the user to - * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult.ResultState} indicating the result of - * completing the teleport. If the teleport was successful, the {@link TeleportResult.ResultState#successful} will be {@code true}. + * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of + * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}. * @since 3.0 * @deprecated Use {@link #teleportBuilder(OnlineUser)} to construct a teleport */ @Deprecated(since = "3.1") - public final CompletableFuture teleportPlayer(@NotNull OnlineUser user, @NotNull Position position) { + public final CompletableFuture teleportPlayer(@NotNull OnlineUser user, @NotNull Position position) { return teleportPlayer(user, position, false); } @@ -381,13 +381,13 @@ public abstract class BaseHuskHomesAPI { * @param timedTeleport Whether the teleport should be timed or not (requiring a warmup where they must stand still * for a period of time) * @param rtpArgs Arguments that will be passed to the implementing {@link RandomTeleportEngine} - * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult.ResultState} indicating the result of - * completing the teleport. If the teleport was successful, the {@link TeleportResult.ResultState#successful} will be {@code true}. + * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of + * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}. * @since 3.0 */ - public final CompletableFuture randomlyTeleportPlayer(@NotNull OnlineUser user, - final boolean timedTeleport, - @NotNull String... rtpArgs) { + public final CompletableFuture randomlyTeleportPlayer(@NotNull OnlineUser user, + final boolean timedTeleport, + @NotNull String... rtpArgs) { return CompletableFuture.supplyAsync(() -> plugin.getRandomTeleportEngine() .getRandomPosition(user, rtpArgs) .thenApply(position -> { @@ -401,7 +401,7 @@ public abstract class BaseHuskHomesAPI { : builder.toTeleport() .thenApplyAsync(teleport -> teleport.execute().join().getState()).join(); } else { - return TeleportResult.ResultState.CANCELLED; + return TeleportResult.CANCELLED; } }).join()); } @@ -411,11 +411,11 @@ public abstract class BaseHuskHomesAPI { * generated by the current {@link RandomTeleportEngine} * * @param user The {@link OnlineUser} to teleport - * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult.ResultState} indicating the result of - * completing the teleport. If the teleport was successful, the {@link TeleportResult.ResultState#successful} will be {@code true}. + * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of + * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}. * @since 3.0 */ - public final CompletableFuture randomlyTeleportPlayer(@NotNull OnlineUser user) { + public final CompletableFuture randomlyTeleportPlayer(@NotNull OnlineUser user) { return randomlyTeleportPlayer(user, false); } diff --git a/common/src/main/java/net/william278/huskhomes/messenger/MessagePayload.java b/common/src/main/java/net/william278/huskhomes/messenger/MessagePayload.java index c0703d93..9a1d54dd 100644 --- a/common/src/main/java/net/william278/huskhomes/messenger/MessagePayload.java +++ b/common/src/main/java/net/william278/huskhomes/messenger/MessagePayload.java @@ -23,7 +23,7 @@ public class MessagePayload { */ @Nullable @SerializedName("teleport_result") - public TeleportResult.ResultState resultState; + public TeleportResult resultState; /** * A teleport request field @@ -56,13 +56,13 @@ public class MessagePayload { } /** - * Returns a payload containing a {@link TeleportResult.ResultState} + * Returns a payload containing a {@link TeleportResult} * * @param resultState the teleport to send * @return a payload containing the teleport result */ @NotNull - public static MessagePayload withTeleportResult(@NotNull TeleportResult.ResultState resultState) { + public static MessagePayload withTeleportResult(@NotNull TeleportResult resultState) { final MessagePayload payload = new MessagePayload(); payload.resultState = resultState; return payload; 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 9e68e8df..76c8c111 100644 --- a/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java +++ b/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java @@ -144,7 +144,7 @@ public abstract class OnlineUser extends User { * @param location the {@link Location} to teleport the player to * @param asynchronous if the teleport should be asynchronous */ - public abstract CompletableFuture teleportLocally(@NotNull Location location, boolean asynchronous); + public abstract CompletableFuture teleportLocally(@NotNull Location location, boolean asynchronous); /** * Returns if a player is moving (i.e. they have momentum) diff --git a/common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java b/common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java new file mode 100644 index 00000000..551acf32 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java @@ -0,0 +1,117 @@ +package net.william278.huskhomes.teleport; + +import net.william278.huskhomes.config.Locales; +import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.player.User; +import net.william278.huskhomes.position.Position; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; + +/** + * Represents the result of executing a {@link Teleport} or {@link TimedTeleport} + * + * @since 3.1 + */ +public class CompletedTeleport { + + /** + * The state of the {@link Teleport} + */ + @NotNull + private final TeleportResult state; + + /** + * The {@link User} who is doing the teleporting + */ + @Nullable + private final User teleporter; + + /** + * The {@link Position} the teleporter should be teleported to + */ + @Nullable + private final Position destination; + + /** + * Internal - Create a new {@link CompletedTeleport} (use {@link #from(TeleportResult, Teleport)}) + * + * @param state The {@link TeleportResult} of completing the teleport + * @param teleporter The {@link User} who is doing the teleporting + * @param destination The {@link Position} the teleporter should be teleported to + * @since 3.1 + */ + private CompletedTeleport(@NotNull TeleportResult state, @Nullable User teleporter, @Nullable Position destination) { + this.state = state; + this.teleporter = teleporter; + this.destination = destination; + } + + /** + * Create a new {@link CompletedTeleport} for a {@link Teleport} with an outcome {@link TeleportResult} + * + * @param state The outcome {@link TeleportResult} + * @param teleport The {@link Teleport} that was executed + * @return The {@link CompletedTeleport} of executing the supplied {@link Teleport} + * @since 3.1 + */ + public static CompletedTeleport from(@NotNull TeleportResult state, @NotNull Teleport teleport) { + return new CompletedTeleport(state, teleport.teleporter, teleport.target); + } + + /** + * Get the state of the {@link Teleport} after execution has finished + * + * @return The {@link TeleportResult} of the teleport + * @since 3.1 + */ + @NotNull + public TeleportResult getState() { + return state; + } + + /** + * Returns if the teleport was successful + * + * @return {@code true} if the teleport was successful, {@code false} otherwise + * @since 3.1 + */ + public boolean successful() { + return state.successful; + } + + /** + * Get the {@link User} who is doing the teleporting + * + * @return The teleporter, if they were resolved + * @implNote The optional will return empty if the state is {@link TeleportResult#FAILED_TELEPORTER_NOT_RESOLVED} + * @since 3.1 + */ + public Optional getTeleporter() { + return Optional.ofNullable(teleporter); + } + + /** + * Get the target {@link Position} the teleporter should have been teleported to + * + * @return The target position, if it was resolved + * @implNote The optional will return empty if the state is {@link TeleportResult#FAILED_TARGET_NOT_RESOLVED} + * @since 3.1 + */ + public Optional getDestination() { + return Optional.ofNullable(destination); + } + + /** + * Send an {@link OnlineUser} the corresponding message indicating the state of the teleport once it has executred + * + * @param locales the {@link Locales} instance to use + * @param user the {@link OnlineUser} to send the message to + * @since 3.1 + */ + public void sendResultMessage(@NotNull Locales locales, @NotNull OnlineUser user) { + this.state.sendResultMessage(locales, user); + } + +} \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java index c8d7bfcf..8555e557 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -92,30 +92,30 @@ public class Teleport { * * @return A {@link CompletableFuture} that completes when the teleport is finished */ - public CompletableFuture execute() { + public CompletableFuture execute() { // Validate the teleporter if (teleporter == null) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_TELEPORTER_NOT_RESOLVED) - .thenApply(resultState -> TeleportResult.from(resultState, this)); + return CompletableFuture.completedFuture(TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)); } // Validate the target if (target == null) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_TARGET_NOT_RESOLVED) - .thenApply(resultState -> TeleportResult.from(resultState, this)); + return CompletableFuture.completedFuture(TeleportResult.FAILED_TARGET_NOT_RESOLVED) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)); } // Check economy actions for (Settings.EconomyAction economyAction : economyActions) { if (!plugin.validateEconomyCheck(executor, economyAction)) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.CANCELLED_ECONOMY) - .thenApply(resultState -> TeleportResult.from(resultState, this)); + return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)); } } // Run the teleport and apply economy actions return run(teleporter) - .thenApply(resultState -> TeleportResult.from(resultState, this)) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)) .thenApply(result -> { if (teleporter instanceof OnlineUser user) { finish(user, result); @@ -130,7 +130,7 @@ public class Teleport { * @param teleporter The {@link User} who is doing the teleporting * @return A {@link CompletableFuture} that completes when the teleport is finished */ - private CompletableFuture run(@NotNull User teleporter) { + private CompletableFuture run(@NotNull User teleporter) { return teleporter instanceof OnlineUser onlineUser ? teleportLocalUser(onlineUser) : teleportNetworkedUser(); @@ -140,9 +140,9 @@ public class Teleport { * Finish the teleport, finalize economy transactions if successful and send message * * @param teleporter The {@link OnlineUser} who was teleported - * @param result The {@link TeleportResult.ResultState} of the teleport + * @param result The {@link TeleportResult} of the teleport */ - private void finish(@NotNull OnlineUser teleporter, @NotNull TeleportResult result) { + private void finish(@NotNull OnlineUser teleporter, @NotNull CompletedTeleport result) { if (result.successful()) { // Complete economy transactions economyActions.forEach(economyAction -> plugin.performEconomyTransaction(executor, economyAction)); @@ -162,7 +162,7 @@ public class Teleport { * @param teleporter The teleporter * @return A {@link CompletableFuture} that completes when the teleport is finished */ - private CompletableFuture teleportLocalUser(@NotNull OnlineUser teleporter) { + private CompletableFuture teleportLocalUser(@NotNull OnlineUser teleporter) { assert target != null; // Dispatch the teleport event and update the player's last position @@ -177,20 +177,20 @@ public class Teleport { } // If the target position is on another server, execute a cross-server teleport - final CompletableFuture teleportFuture = new CompletableFuture<>(); + final CompletableFuture teleportFuture = new CompletableFuture<>(); plugin.getDatabase() .setCurrentTeleport(teleporter, this) .thenApply(ignored -> plugin .getNetworkMessenger() .sendPlayer(teleporter, target.server) .thenApply(completed -> completed - ? TeleportResult.ResultState.COMPLETED_CROSS_SERVER - : TeleportResult.ResultState.FAILED_INVALID_SERVER)) + ? TeleportResult.COMPLETED_CROSS_SERVER + : TeleportResult.FAILED_INVALID_SERVER)) .orTimeout(10, TimeUnit.SECONDS) .exceptionally(throwable -> { plugin.getLoggingAdapter().log(Level.WARNING, "Cross-server teleport timed out for " + teleporter.username); plugin.getDatabase().setCurrentTeleport(teleporter, null); - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_INVALID_SERVER); + return CompletableFuture.completedFuture(TeleportResult.FAILED_INVALID_SERVER); }) .thenAccept(result -> result.thenAcceptAsync(teleportFuture::complete)); return teleportFuture; @@ -200,11 +200,11 @@ public class Teleport { * Teleport a user not on this server but on the proxy network * * @return A {@link CompletableFuture} that completes when the teleport is finished. - * Successful cross-server teleports will return {@link TeleportResult.ResultState#COMPLETED_CROSS_SERVER}. - * @implNote Cross-server teleports will return with a {@link TeleportResult.ResultState#FAILED_INVALID_SERVER} result if the + * Successful cross-server teleports will return {@link TeleportResult#COMPLETED_CROSS_SERVER}. + * @implNote Cross-server teleports will return with a {@link TeleportResult#FAILED_INVALID_SERVER} result if the * target server is not online, or connection timed out */ - private CompletableFuture teleportNetworkedUser() { + private CompletableFuture teleportNetworkedUser() { assert target != null; assert teleporter != null; @@ -221,7 +221,7 @@ public class Teleport { .exceptionally(throwable -> null) .thenApply(result -> { if (result == null || result.payload.resultState == null) { - return TeleportResult.ResultState.FAILED_TELEPORTER_NOT_RESOLVED; + return TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED; } return result.payload.resultState; }); 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 0cad85ab..530fd302 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java @@ -2,7 +2,6 @@ package net.william278.huskhomes.teleport; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.player.OnlineUser; -import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.World; import org.jetbrains.annotations.NotNull; @@ -11,204 +10,98 @@ import org.jetbrains.annotations.Nullable; import java.util.Optional; /** - * Represents the result of executing a {@link Teleport} or {@link TimedTeleport} - * - * @since 3.1 + * Represents the state of a {@link Teleport} after it has finished executing */ -public class TeleportResult { +public enum TeleportResult { /** - * The state of the {@link Teleport} + * Returns if the {@link Teleport} was completed locally, successfully */ - @NotNull - private final ResultState state; + COMPLETED_LOCALLY(true, "teleporting_complete"), /** - * The {@link User} who is doing the teleporting + * Returns if the {@link Teleport} was completed cross-server + */ + COMPLETED_CROSS_SERVER(true, "teleporting_complete"), + + /** + * Returns if the {@link Teleport} failed because the user is already teleporting + */ + FAILED_ALREADY_TELEPORTING(false, "error_already_teleporting"), + + /** + * Returns if the {@link TimedTeleport} warmup could not be started because they were moving + */ + FAILED_MOVING(false, "error_teleport_warmup_stand_still"), + + /** + * Returns if the {@link Teleport} failed because the {@link World} of the + * target {@link Position} could not be found + */ + FAILED_INVALID_WORLD(false, "error_invalid_world"), + + /** + * Returns if the {@link Teleport} failed because the server of the target + * {@link Position} was invalid or not online + */ + FAILED_INVALID_SERVER(false, "error_invalid_server"), + + /** + * Returns if the {@link Teleport} failed because the coordinates of the + * target {@link Position} were outside the world border limits + */ + FAILED_ILLEGAL_COORDINATES(false, "error_illegal_target_coordinates"), + + /** + * Returns if the {@link Teleport} failed because the teleporter could not be resolved or was offline + */ + FAILED_TELEPORTER_NOT_RESOLVED(false, "error_teleport_target_not_resolved"), + + /** + * Returns if the {@link Teleport} failed because the target {@link Position} could not be resolved + */ + FAILED_TARGET_NOT_RESOLVED(false, "error_teleport_teleporter_not_resolved"), + + /** + * Returns if the teleport was cancelled, for example if a {@link TimedTeleport} in progress is cancelled + */ + CANCELLED(false), + + /** + * Returns if the {@link Teleport} was cancelled due to failing economy checks + */ + CANCELLED_ECONOMY(false); + + /** + * Is {@code true} if the teleport was a success + */ + public final boolean successful; + + /** + * The {@link Locales} key to use when sending a message to the teleporter */ @Nullable - private final User teleporter; + private final String messageId; - /** - * The {@link Position} the teleporter should be teleported to - */ - @Nullable - private final Position destination; + TeleportResult(boolean successful, @NotNull String messageId) { + this.successful = successful; + this.messageId = messageId; + } - /** - * Internal - Create a new {@link TeleportResult} (use {@link #from(ResultState, Teleport)}) - * - * @param state The {@link ResultState} of completing the teleport - * @param teleporter The {@link User} who is doing the teleporting - * @param destination The {@link Position} the teleporter should be teleported to - * @since 3.1 - */ - private TeleportResult(@NotNull ResultState state, @Nullable User teleporter, @Nullable Position destination) { - this.state = state; - this.teleporter = teleporter; - this.destination = destination; + TeleportResult(boolean successful) { + this.successful = successful; + this.messageId = null; } /** - * Create a new {@link TeleportResult} for a {@link Teleport} with an outcome {@link ResultState} - * - * @param state The outcome {@link ResultState} - * @param teleport The {@link Teleport} that was executed - * @return The {@link TeleportResult} of executing the supplied {@link Teleport} - * @since 3.1 - */ - public static TeleportResult from(@NotNull ResultState state, @NotNull Teleport teleport) { - return new TeleportResult(state, teleport.teleporter, teleport.target); - } - - /** - * Get the state of the {@link Teleport} after execution has finished - * - * @return The {@link ResultState} of the teleport - * @since 3.1 - */ - @NotNull - public ResultState getState() { - return state; - } - - /** - * Returns if the teleport was successful - * - * @return {@code true} if the teleport was successful, {@code false} otherwise - * @since 3.1 - */ - public boolean successful() { - return state.successful; - } - - /** - * Get the {@link User} who is doing the teleporting - * - * @return The teleporter, if they were resolved - * @implNote The optional will return empty if the state is {@link ResultState#FAILED_TELEPORTER_NOT_RESOLVED} - * @since 3.1 - */ - public Optional getTeleporter() { - return Optional.ofNullable(teleporter); - } - - /** - * Get the target {@link Position} the teleporter should have been teleported to - * - * @return The target position, if it was resolved - * @implNote The optional will return empty if the state is {@link ResultState#FAILED_TARGET_NOT_RESOLVED} - * @since 3.1 - */ - public Optional getDestination() { - return Optional.ofNullable(destination); - } - - /** - * Send an {@link OnlineUser} the corresponding message indicating the state of the teleport once it has executred + * Send an {@link OnlineUser} the corresponding message for this {@link TeleportResult}, if there is one * * @param locales the {@link Locales} instance to use * @param user the {@link OnlineUser} to send the message to * @since 3.1 */ public void sendResultMessage(@NotNull Locales locales, @NotNull OnlineUser user) { - this.state.sendResultMessage(locales, user); + Optional.ofNullable(messageId).flatMap(locales::getLocale).ifPresent(user::sendMessage); } - /** - * Represents the state of a {@link Teleport} after it has finished executing - */ - public enum ResultState { - - /** - * Returns if the {@link Teleport} was completed locally, successfully - */ - COMPLETED_LOCALLY(true, "teleporting_complete"), - - /** - * Returns if the {@link Teleport} was completed cross-server - */ - COMPLETED_CROSS_SERVER(true, "teleporting_complete"), - - /** - * Returns if the {@link Teleport} failed because the user is already teleporting - */ - FAILED_ALREADY_TELEPORTING(false, "error_already_teleporting"), - - /** - * Returns if the {@link TimedTeleport} warmup could not be started because they were moving - */ - FAILED_MOVING(false, "error_teleport_warmup_stand_still"), - - /** - * Returns if the {@link Teleport} failed because the {@link World} of the - * target {@link Position} could not be found - */ - FAILED_INVALID_WORLD(false, "error_invalid_world"), - - /** - * Returns if the {@link Teleport} failed because the server of the target - * {@link Position} was invalid or not online - */ - FAILED_INVALID_SERVER(false, "error_invalid_server"), - - /** - * Returns if the {@link Teleport} failed because the coordinates of the - * target {@link Position} were outside the world border limits - */ - FAILED_ILLEGAL_COORDINATES(false, "error_illegal_target_coordinates"), - - /** - * Returns if the {@link Teleport} failed because the teleporter could not be resolved or was offline - */ - FAILED_TELEPORTER_NOT_RESOLVED(false, "error_teleport_target_not_resolved"), - - /** - * Returns if the {@link Teleport} failed because the target {@link Position} could not be resolved - */ - FAILED_TARGET_NOT_RESOLVED(false, "error_teleport_teleporter_not_resolved"), - - /** - * Returns if the teleport was cancelled, for example if a {@link TimedTeleport} in progress is cancelled - */ - CANCELLED(false), - - /** - * Returns if the {@link Teleport} was cancelled due to failing economy checks - */ - CANCELLED_ECONOMY(false); - - /** - * Is {@code true} if the teleport was a success - */ - public final boolean successful; - - /** - * The {@link Locales} key to use when sending a message to the teleporter - */ - @Nullable - private final String messageId; - - ResultState(boolean successful, @NotNull String messageId) { - this.successful = successful; - this.messageId = messageId; - } - - ResultState(boolean successful) { - this.successful = successful; - this.messageId = null; - } - - /** - * Send an {@link OnlineUser} the corresponding message for this {@link ResultState}, if there is one - * - * @param locales the {@link Locales} instance to use - * @param user the {@link OnlineUser} to send the message to - * @since 3.1 - */ - public void sendResultMessage(@NotNull Locales locales, @NotNull OnlineUser user) { - Optional.ofNullable(messageId).flatMap(locales::getLocale).ifPresent(user::sendMessage); - } - - } -} \ No newline at end of file +} diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java index c0f078a2..c6e52d9a 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java @@ -40,17 +40,17 @@ public class TimedTeleport extends Teleport { * @return a {@link CompletableFuture} that completes when the teleport is complete or has been cancelled */ @Override - public CompletableFuture execute() { + public CompletableFuture execute() { // If the target has not been resolved, fail the teleport if (target == null) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_TARGET_NOT_RESOLVED) - .thenApply(resultState -> TeleportResult.from(resultState, this)); + return CompletableFuture.completedFuture(TeleportResult.FAILED_TARGET_NOT_RESOLVED) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)); } // Check if the teleporter is already warming up to teleport if (plugin.getCache().currentlyOnWarmup.contains(teleporter.uuid)) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_ALREADY_TELEPORTING) - .thenApply(resultState -> TeleportResult.from(resultState, this)); + return CompletableFuture.completedFuture(TeleportResult.FAILED_ALREADY_TELEPORTING) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)); } // Check if the teleporter can bypass warmup @@ -61,21 +61,21 @@ public class TimedTeleport extends Teleport { // Check economy actions for (Settings.EconomyAction economyAction : economyActions) { if (!plugin.validateEconomyCheck(executor, economyAction)) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.CANCELLED_ECONOMY) - .thenApply(resultState -> TeleportResult.from(resultState, this)); + return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)); } } // Check if they are moving at the start of the teleport if (teleporter.isMoving()) { - return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_MOVING) - .thenApply(resultState -> TeleportResult.from(resultState, this)); + return CompletableFuture.completedFuture(TeleportResult.FAILED_MOVING) + .thenApply(resultState -> CompletedTeleport.from(resultState, this)); } // Process the warmup and execute the teleport return process().thenApplyAsync(ignored -> { if (cancelled) { - return TeleportResult.from(TeleportResult.ResultState.CANCELLED, this); + return CompletedTeleport.from(TeleportResult.CANCELLED, this); } return super.execute().join(); });