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 00c52fe4..5d76ebb5 100644 --- a/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java +++ b/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java @@ -16,7 +16,9 @@ import org.jvnet.hk2.annotations.Service; import org.mvplugins.multiverse.core.api.BlockSafety; import org.mvplugins.multiverse.core.destination.ParsedDestination; +import org.mvplugins.multiverse.core.utils.result.AsyncAttempt; import org.mvplugins.multiverse.core.utils.result.AsyncResult; +import org.mvplugins.multiverse.core.utils.result.Attempt; import org.mvplugins.multiverse.core.utils.result.Result; @SuppressWarnings("unchecked") @@ -33,7 +35,7 @@ public class AsyncSafetyTeleporter { this.teleportQueue = teleportQueue; } - public AsyncResult> teleportSafely( + public AsyncAttempt teleportSafely( @NotNull Entity teleportee, @Nullable ParsedDestination destination) { return teleportSafely(null, teleportee, destination); @@ -48,34 +50,34 @@ public class AsyncSafetyTeleporter { .toList()); } - public AsyncResult> teleportSafely( + public AsyncAttempt teleportSafely( @Nullable CommandSender teleporter, @NotNull Entity teleportee, @Nullable ParsedDestination destination) { if (destination == null) { - return AsyncResult.completedFuture(Result.failure(TeleportResult.Failure.NULL_DESTINATION)); + return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION); } return destination.getDestination().checkTeleportSafety() ? teleportSafely(teleporter, teleportee, destination.getLocation(teleportee)) : teleport(teleporter, teleportee, destination.getLocation(teleportee)); } - public AsyncResult> teleportSafely( + public AsyncAttempt teleportSafely( @NotNull Entity teleportee, @Nullable Location location) { return teleportSafely(null, teleportee, location); } - public AsyncResult> teleportSafely( + public AsyncAttempt teleportSafely( @Nullable CommandSender teleporter, @NotNull Entity teleportee, @Nullable Location location) { if (location == null) { - return AsyncResult.completedFuture(Result.failure(TeleportResult.Failure.NULL_LOCATION)); + return AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION); } Location safeLocation = blockSafety.getSafeLocation(location); if (safeLocation == null) { - return AsyncResult.completedFuture(Result.failure(TeleportResult.Failure.UNSAFE_LOCATION)); + return AsyncAttempt.failure(TeleportResult.Failure.UNSAFE_LOCATION); } return teleport(teleporter, teleportee, safeLocation); } @@ -88,18 +90,18 @@ public class AsyncSafetyTeleporter { .toList()); } - public AsyncResult> teleport( + public AsyncAttempt teleport( @NotNull Entity teleportee, @Nullable ParsedDestination destination) { return teleport(null, teleportee, destination); } - public AsyncResult> teleport( + public AsyncAttempt teleport( @Nullable CommandSender teleporter, @NotNull Entity teleportee, @Nullable ParsedDestination destination) { if (destination == null) { - return AsyncResult.completedFuture(Result.failure(TeleportResult.Failure.NULL_DESTINATION)); + return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION); } return teleport(teleporter, teleportee, destination.getLocation(teleportee)); } @@ -112,18 +114,18 @@ public class AsyncSafetyTeleporter { .toList()); } - public AsyncResult> teleport( + public AsyncAttempt teleport( @NotNull Entity teleportee, @Nullable Location location) { return teleport(null, teleportee, location); } - public AsyncResult> teleport( + public AsyncAttempt teleport( @Nullable CommandSender teleporter, @NotNull Entity teleportee, @Nullable Location location) { if (location == null) { - return AsyncResult.completedFuture(Result.failure(TeleportResult.Failure.NULL_LOCATION)); + return AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION); } boolean shouldAddToQueue = teleporter != null && teleportee instanceof Player; @@ -131,33 +133,26 @@ public class AsyncSafetyTeleporter { teleportQueue.addToQueue(teleporter.getName(), teleportee.getName()); } - AsyncResult> future = new AsyncResult<>(); - doAsyncTeleport(teleportee, location, future, shouldAddToQueue); - return future; + return doAsyncTeleport(teleportee, location, shouldAddToQueue); } - private void doAsyncTeleport( + private AsyncAttempt doAsyncTeleport( @NotNull Entity teleportee, @NotNull Location location, - AsyncResult> future, boolean shouldAddToQueue) { - Try.run(() -> PaperLib.teleportAsync(teleportee, location).thenAccept(result -> { + 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); + }).mapAsyncAttempt(result -> { Logging.finer("Teleported async %s to %s", teleportee.getName(), location); - future.complete(result - ? Result.success(TeleportResult.Success.SUCCESS) - : Result.failure(TeleportResult.Failure.TELEPORT_FAILED)); - }).exceptionally(exception -> { - Logging.warning("Failed to teleport %s to %s: %s", - teleportee.getName(), location, exception.getMessage()); - future.completeExceptionally(exception); - return null; - })).onFailure(exception -> { - Logging.warning("Failed to teleport %s to %s: %s", - teleportee.getName(), location, exception.getMessage()); - future.complete(Result.failure(TeleportResult.Failure.TELEPORT_FAILED_EXCEPTION)); - }).andFinally(() -> { - if (shouldAddToQueue) { - teleportQueue.popFromQueue(teleportee.getName()); + if (result) { + if (shouldAddToQueue) { + teleportQueue.popFromQueue(teleportee.getName()); + } + return AsyncAttempt.success(); + } else { + return AsyncAttempt.failure(TeleportResult.Failure.TELEPORT_FAILED); } }); } diff --git a/src/main/java/org/mvplugins/multiverse/core/utils/result/AsyncAttempt.java b/src/main/java/org/mvplugins/multiverse/core/utils/result/AsyncAttempt.java new file mode 100644 index 00000000..3ffd6013 --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/utils/result/AsyncAttempt.java @@ -0,0 +1,56 @@ +package org.mvplugins.multiverse.core.utils.result; + +import java.util.concurrent.CompletableFuture; +import java.util.function.BiFunction; +import java.util.function.Function; + +import org.mvplugins.multiverse.core.utils.message.MessageReplacement; + +public final class AsyncAttempt { + + public static AsyncAttempt of( + CompletableFuture future, + BiFunction> completionHandler) { + return new AsyncAttempt<>(future.handleAsync(completionHandler)); + } + + public static AsyncAttempt of( + CompletableFuture future, + Function> exceptionHandler) { + BiFunction> completionHandler = (result, exception) -> { + if (exception != null) { + return exceptionHandler.apply(exception); + } else { + return Attempt.success(result); + } + }; + return of(future, completionHandler); + } + + public static AsyncAttempt success() { + return new AsyncAttempt<>(CompletableFuture.completedFuture(null)); + } + + public static AsyncAttempt failure( + F failureReason, + MessageReplacement... messageReplacements) { + return new AsyncAttempt<>(CompletableFuture.completedFuture( + Attempt.failure(failureReason, messageReplacements))); + } + + private final CompletableFuture> future; +` + private AsyncAttempt(CompletableFuture> future) { + this.future = future; + } + + public AsyncAttempt map(Function mapper) { + + } + + public AsyncAttempt mapAsyncAttempt(Function> mapper) { + } + + public AsyncAttempt onSuccess(Runnable runnable) { + } +}