TeleportResult refactor, use record and bundle resolved information, separate TeleportState enum, deprecate relevant API

This commit is contained in:
William committed 2022-10-19 01:12:19 +01:00
1 parent 82d47f0878
commit 4164f07841
12 files changed
+285 -151

No files matched your search

@@ -9,8 +9,8 @@ import net.william278.huskhomes.player.UserData;
import net.william278.huskhomes.position.*;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TeleportBuilder;
import net.william278.huskhomes.teleport.TeleportResult;
import net.william278.huskhomes.random.RandomTeleportEngine;
import net.william278.huskhomes.teleport.TeleportResult;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -343,17 +343,19 @@ 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} indicating the result of
* completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
* @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}.
* @since 3.0
* @deprecated Use {@link #teleportBuilder(OnlineUser)} to construct a teleport
*/
public final CompletableFuture<TeleportResult> teleportPlayer(@NotNull OnlineUser user,
@NotNull Position position,
final boolean timedTeleport) {
@Deprecated(since = "3.1")
public final CompletableFuture<TeleportResult.ResultState> 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())
: builder.toTeleport().thenApplyAsync(teleport -> teleport.execute().join());
? builder.toTimedTeleport().thenApplyAsync(teleport -> teleport.execute().join().getState())
: builder.toTeleport().thenApplyAsync(teleport -> teleport.execute().join().getState());
}
/**
@@ -361,11 +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} indicating the result of
* completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
* @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}.
* @since 3.0
* @deprecated Use {@link #teleportBuilder(OnlineUser)} to construct a teleport
*/
public final CompletableFuture<TeleportResult> teleportPlayer(@NotNull OnlineUser user, @NotNull Position position) {
@Deprecated(since = "3.1")
public final CompletableFuture<TeleportResult.ResultState> teleportPlayer(@NotNull OnlineUser user, @NotNull Position position) {
return teleportPlayer(user, position, false);
}
@@ -377,20 +381,27 @@ 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} indicating the result of
* completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
* @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}.
* @since 3.0
*/
public final CompletableFuture<TeleportResult> randomlyTeleportPlayer(@NotNull OnlineUser user,
final boolean timedTeleport,
@NotNull String... rtpArgs) {
public final CompletableFuture<TeleportResult.ResultState> randomlyTeleportPlayer(@NotNull OnlineUser user,
final boolean timedTeleport,
@NotNull String... rtpArgs) {
return CompletableFuture.supplyAsync(() -> plugin.getRandomTeleportEngine()
.getRandomPosition(user, rtpArgs)
.thenApply(position -> {
if (position.isPresent()) {
return teleportPlayer(user, position.get(), timedTeleport).join();
final TeleportBuilder builder = Teleport.builder(plugin, user)
.setTarget(position.get());
return timedTeleport
? builder.toTimedTeleport()
.thenApplyAsync(teleport -> teleport.execute().join().getState()).join()
: builder.toTeleport()
.thenApplyAsync(teleport -> teleport.execute().join().getState()).join();
} else {
return TeleportResult.CANCELLED;
return TeleportResult.ResultState.CANCELLED;
}
}).join());
}
@@ -400,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} indicating the result of
* completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
* @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}.
* @since 3.0
*/
public final CompletableFuture<TeleportResult> randomlyTeleportPlayer(@NotNull OnlineUser user) {
public final CompletableFuture<TeleportResult.ResultState> randomlyTeleportPlayer(@NotNull OnlineUser user) {
return randomlyTeleportPlayer(user, false);
}
@@ -86,7 +86,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
teleportFuture.thenAccept(teleport -> teleport.execute()
.thenAccept(result -> {
if (isExecutorTeleporting &&
result.successful && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) {
result.successful() && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) {
plugin.getDatabase().updateUserData(new UserData(onlineUser,
userData.get().homeSlots(), userData.get().ignoringTeleports(),
Instant.now().plus(plugin.getSettings().rtpCooldownLength, ChronoUnit.MINUTES)));
@@ -118,7 +118,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
.setTarget(position.get())
.toTeleport()
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
if (result.successful) {
if (result.successful()) {
plugin.getLoggingAdapter().log(Level.INFO, "Teleported " + foundUser.get().username + " to a random position.");
} else {
plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport" + foundUser.get().username + " to a random position.");
@@ -69,9 +69,10 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
.setTarget(targetPlayer.playerName)
.toTeleport()
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
if (result.successful) {
plugin.getLocales().getLocale("teleporting_other_complete",
playerToTeleport, targetPlayer.playerName)
if (result.successful()) {
result.getTeleporter()
.flatMap(teleporter -> plugin.getLocales().getLocale("teleporting_other_complete",
teleporter.username, targetPlayer.playerName))
.ifPresent(onlineUser::sendMessage);
} else {
plugin.getLocales().getLocale("error_player_not_found", targetPlayer.playerName)
@@ -91,14 +92,16 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
.setTarget(targetPosition.position)
.toTeleport()
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
if (!result.successful || playerToTeleport.equalsIgnoreCase(onlineUser.username)) {
if (!result.successful() || playerToTeleport.equalsIgnoreCase(onlineUser.username) || result.getDestination().isEmpty()) {
return;
}
plugin.getLocales().getLocale("teleporting_other_complete_position",
playerToTeleport,
Integer.toString((int) targetPosition.position.x),
Integer.toString((int) targetPosition.position.y),
Integer.toString((int) targetPosition.position.z))
final Position destination = result.getDestination().get();
result.getTeleporter()
.flatMap(teleporter -> plugin.getLocales()
.getLocale("teleporting_other_complete", teleporter.username,
Integer.toString((int) destination.x),
Integer.toString((int) destination.y),
Integer.toString((int) destination.z)))
.ifPresent(onlineUser::sendMessage);
}));
}
@@ -187,7 +190,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
builder.setTarget(((TargetPosition) teleportTarget).position);
}
builder.toTeleport().thenAccept(teleport -> teleport.execute().thenAccept(result -> {
if (result.successful) {
if (result.successful()) {
plugin.getLoggingAdapter().log(Level.INFO, "Successfully teleported " + playerToTeleport.username);
} else {
plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport " + playerToTeleport.username + " to " + teleportTarget);
@@ -32,9 +32,10 @@ public class TpHereCommand extends CommandBase implements TabCompletable {
.setTarget(onlineUser.getPosition())
.toTeleport()
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
if (result.successful) {
plugin.getLocales().getLocale("teleporting_other_complete",
targetPlayerName, onlineUser.username)
if (result.successful()) {
result.getTeleporter()
.flatMap(teleporter -> plugin.getLocales().getLocale("teleporting_other_complete",
teleporter.username, onlineUser.username))
.ifPresent(onlineUser::sendMessage);
}
}));
@@ -43,7 +43,7 @@ public class TpOfflineCommand extends CommandBase implements TabCompletable {
.setTarget(offlinePosition.get())
.toTeleport()
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
if (result.successful) {
if (result.successful()) {
plugin.getLocales().getLocale("teleporting_offline_complete", targetUser)
.ifPresent(onlineUser::sendMessage);
}
@@ -23,7 +23,7 @@ public class MessagePayload {
*/
@Nullable
@SerializedName("teleport_result")
public TeleportResult teleportResult;
public TeleportResult.ResultState resultState;
/**
* A teleport request field
@@ -56,15 +56,15 @@ public class MessagePayload {
}
/**
* Returns a payload containing a {@link TeleportResult}
* Returns a payload containing a {@link TeleportResult.ResultState}
*
* @param teleportResult the teleport to send
* @param resultState the teleport to send
* @return a payload containing the teleport result
*/
@NotNull
public static MessagePayload withTeleportResult(@NotNull TeleportResult teleportResult) {
public static MessagePayload withTeleportResult(@NotNull TeleportResult.ResultState resultState) {
final MessagePayload payload = new MessagePayload();
payload.teleportResult = teleportResult;
payload.resultState = resultState;
return payload;
}
@@ -152,7 +152,7 @@ public abstract class NetworkMessenger {
if (message.payload.position != null) {
message.payload = MessagePayload.withTeleportResult(Teleport.builder(plugin, receiver)
.setTarget(message.payload.position)
.toTeleport().join().execute().join());
.toTeleport().join().execute().join().getState());
} else {
message.payload = MessagePayload.empty();
}
@@ -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<TeleportResult> teleportLocally(@NotNull Location location, boolean asynchronous);
public abstract CompletableFuture<TeleportResult.ResultState> teleportLocally(@NotNull Location location, boolean asynchronous);
/**
* Returns if a player is moving (i.e. they have momentum)
@@ -95,28 +95,33 @@ public class Teleport {
public CompletableFuture<TeleportResult> execute() {
// Validate the teleporter
if (teleporter == null) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED);
return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_TELEPORTER_NOT_RESOLVED)
.thenApply(resultState -> TeleportResult.from(resultState, this));
}
// Validate the target
if (target == null) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_TARGET_NOT_RESOLVED);
return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_TARGET_NOT_RESOLVED)
.thenApply(resultState -> TeleportResult.from(resultState, this));
}
// Check economy actions
for (Settings.EconomyAction economyAction : economyActions) {
if (!plugin.validateEconomyCheck(executor, economyAction)) {
return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY);
return CompletableFuture.completedFuture(TeleportResult.ResultState.CANCELLED_ECONOMY)
.thenApply(resultState -> TeleportResult.from(resultState, this));
}
}
// Run the teleport and apply economy actions
return run(teleporter).thenApply(result -> {
if (teleporter instanceof OnlineUser user) {
finish(user, result);
}
return result;
});
return run(teleporter)
.thenApply(resultState -> TeleportResult.from(resultState, this))
.thenApply(result -> {
if (teleporter instanceof OnlineUser user) {
finish(user, result);
}
return result;
});
}
/**
@@ -125,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<TeleportResult> run(@NotNull User teleporter) {
private CompletableFuture<TeleportResult.ResultState> run(@NotNull User teleporter) {
return teleporter instanceof OnlineUser onlineUser
? teleportLocalUser(onlineUser)
: teleportNetworkedUser();
@@ -135,10 +140,10 @@ 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} of the teleport
* @param result The {@link TeleportResult.ResultState} of the teleport
*/
private void finish(@NotNull OnlineUser teleporter, @NotNull TeleportResult result) {
if (result.successful) {
if (result.successful()) {
// Complete economy transactions
economyActions.forEach(economyAction -> plugin.performEconomyTransaction(executor, economyAction));
@@ -148,7 +153,7 @@ public class Teleport {
}
// Send result message
result.sendMessage(plugin.getLocales(), teleporter);
result.sendResultMessage(plugin.getLocales(), teleporter);
}
/**
@@ -157,7 +162,7 @@ public class Teleport {
* @param teleporter The teleporter
* @return A {@link CompletableFuture} that completes when the teleport is finished
*/
private CompletableFuture<TeleportResult> teleportLocalUser(@NotNull OnlineUser teleporter) {
private CompletableFuture<TeleportResult.ResultState> teleportLocalUser(@NotNull OnlineUser teleporter) {
assert target != null;
// Dispatch the teleport event and update the player's last position
@@ -172,20 +177,20 @@ public class Teleport {
}
// If the target position is on another server, execute a cross-server teleport
final CompletableFuture<TeleportResult> teleportFuture = new CompletableFuture<>();
final CompletableFuture<TeleportResult.ResultState> teleportFuture = new CompletableFuture<>();
plugin.getDatabase()
.setCurrentTeleport(teleporter, this)
.thenApply(ignored -> plugin
.getNetworkMessenger()
.sendPlayer(teleporter, target.server)
.thenApply(completed -> completed
? TeleportResult.COMPLETED_CROSS_SERVER
: TeleportResult.FAILED_INVALID_SERVER))
? TeleportResult.ResultState.COMPLETED_CROSS_SERVER
: TeleportResult.ResultState.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.FAILED_INVALID_SERVER);
return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_INVALID_SERVER);
})
.thenAccept(result -> result.thenAcceptAsync(teleportFuture::complete));
return teleportFuture;
@@ -195,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#COMPLETED_CROSS_SERVER}.
* @implNote Cross-server teleports will return with a {@link TeleportResult#FAILED_INVALID_SERVER} result if the
* 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
* target server is not online, or connection timed out
*/
private CompletableFuture<TeleportResult> teleportNetworkedUser() {
private CompletableFuture<TeleportResult.ResultState> teleportNetworkedUser() {
assert target != null;
assert teleporter != null;
@@ -215,10 +220,10 @@ public class Teleport {
.orTimeout(3, TimeUnit.SECONDS)
.exceptionally(throwable -> null)
.thenApply(result -> {
if (result == null || result.payload.teleportResult == null) {
return TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED;
if (result == null || result.payload.resultState == null) {
return TeleportResult.ResultState.FAILED_TELEPORTER_NOT_RESOLVED;
}
return result.payload.teleportResult;
return result.payload.resultState;
});
}
@@ -2,6 +2,7 @@ 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;
@@ -10,95 +11,204 @@ import org.jetbrains.annotations.Nullable;
import java.util.Optional;
/**
* Represents the result of a completed {@link Teleport}
* Represents the result of executing a {@link Teleport} or {@link TimedTeleport}
*
* @since 3.1
*/
public enum TeleportResult {
public class TeleportResult {
/**
* Returns if the {@link Teleport} was completed locally, successfully
* The state of the {@link Teleport}
*/
COMPLETED_LOCALLY(true, "teleporting_complete"),
@NotNull
private final ResultState state;
/**
* Returns if the {@link Teleport} was completed cross-server
* The {@link User} who is doing the teleporting
*/
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;
@Nullable
private final String messageId;
private final User teleporter;
TeleportResult(boolean successful, @NotNull String messageId) {
this.successful = successful;
this.messageId = messageId;
}
/**
* The {@link Position} the teleporter should be teleported to
*/
@Nullable
private final Position destination;
TeleportResult(boolean successful) {
this.successful = successful;
this.messageId = null;
/**
* <b>Internal</b> - 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;
}
/**
* Send an {@link OnlineUser} the corresponding message for this {@link TeleportResult}, if there is one
* 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<User> 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<Position> 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 sendMessage(@NotNull Locales locales, @NotNull OnlineUser user) {
Optional.ofNullable(messageId).flatMap(locales::getLocale).ifPresent(user::sendMessage);
public void sendResultMessage(@NotNull Locales locales, @NotNull OnlineUser user) {
this.state.sendResultMessage(locales, user);
}
}
/**
* 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);
}
}
}
@@ -43,12 +43,14 @@ public class TimedTeleport extends Teleport {
public CompletableFuture<TeleportResult> execute() {
// If the target has not been resolved, fail the teleport
if (target == null) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_TARGET_NOT_RESOLVED);
return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_TARGET_NOT_RESOLVED)
.thenApply(resultState -> TeleportResult.from(resultState, this));
}
// Check if the teleporter is already warming up to teleport
if (plugin.getCache().currentlyOnWarmup.contains(teleporter.uuid)) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_ALREADY_TELEPORTING);
return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_ALREADY_TELEPORTING)
.thenApply(resultState -> TeleportResult.from(resultState, this));
}
// Check if the teleporter can bypass warmup
@@ -59,19 +61,21 @@ public class TimedTeleport extends Teleport {
// Check economy actions
for (Settings.EconomyAction economyAction : economyActions) {
if (!plugin.validateEconomyCheck(executor, economyAction)) {
return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY);
return CompletableFuture.completedFuture(TeleportResult.ResultState.CANCELLED_ECONOMY)
.thenApply(resultState -> TeleportResult.from(resultState, this));
}
}
// Check if they are moving at the start of the teleport
if (teleporter.isMoving()) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_MOVING);
return CompletableFuture.completedFuture(TeleportResult.ResultState.FAILED_MOVING)
.thenApply(resultState -> TeleportResult.from(resultState, this));
}
// Process the warmup and execute the teleport
return process().thenApplyAsync(ignored -> {
if (cancelled) {
return TeleportResult.CANCELLED;
return TeleportResult.from(TeleportResult.ResultState.CANCELLED, this);
}
return super.execute().join();
});