Refactor some async attempt handling

This commit is contained in:
Ben Woo 2023-09-14 11:30:35 +08:00
parent 94bcca914f
commit bf8244e587
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
3 changed files with 14 additions and 18 deletions

View File

@ -1,6 +1,6 @@
package org.mvplugins.multiverse.core.commands;
import java.util.Arrays;
import java.util.List;
import co.aikar.commands.CommandIssuer;
import co.aikar.commands.annotation.CommandAlias;
@ -22,7 +22,6 @@ import org.mvplugins.multiverse.core.destination.ParsedDestination;
import org.mvplugins.multiverse.core.permissions.CorePermissionsChecker;
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
import org.mvplugins.multiverse.core.utils.MVCorei18n;
import org.mvplugins.multiverse.core.utils.result.AsyncAttempt;
@Service
@CommandAlias("mv")
@ -72,10 +71,8 @@ class TeleportCommand extends MultiverseCommand {
issuer.sendInfo(MVCorei18n.TELEPORT_SUCCESS,
"{player}", playerName, "{destination}", destination.toString());
AsyncAttempt.allOf(Arrays.stream(players)
.map(player -> safetyTeleporter.teleportSafely(issuer.getIssuer(), player, destination))
.toList())
.thenRun(() -> Logging.fine("Async teleport result: %s"))
safetyTeleporter.teleportSafely(issuer.getIssuer(), List.of(players), destination)
.thenAccept(attempts -> Logging.fine("Async teleport completed: %s", attempts))
.exceptionally(throwable -> {
Logging.severe("Error while teleporting %s to %s: %s",
playerName, destination, throwable.getMessage());

View File

@ -144,16 +144,15 @@ public class AsyncSafetyTeleporter {
Logging.warning("Failed to teleport %s to %s: %s",
teleportee.getName(), location, exception.getMessage());
return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED_EXCEPTION);
}).mapAsyncAttempt(result -> {
}).mapAttempt(result -> {
Logging.finer("Teleported async %s to %s", teleportee.getName(), location);
if (result) {
if (shouldAddToQueue) {
teleportQueue.popFromQueue(teleportee.getName());
}
return AsyncAttempt.success();
} else {
return AsyncAttempt.failure(TeleportResult.Failure.TELEPORT_FAILED);
return Attempt.success(null);
}
return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED);
});
}
}

View File

@ -21,19 +21,15 @@ public final class AsyncAttempt<T, F extends FailureReason> {
public static <T, F extends FailureReason> AsyncAttempt<T, F> of(
CompletableFuture<T> future,
BiFunction<T, Throwable, Attempt<T, F>> completionHandler) {
return new AsyncAttempt<>(future.handleAsync(completionHandler));
return new AsyncAttempt<>(future.handle(completionHandler));
}
public static <T, F extends FailureReason> AsyncAttempt<T, F> of(
CompletableFuture<T> future,
Function<Throwable, Attempt<T, F>> exceptionHandler) {
BiFunction<T, Throwable, Attempt<T, F>> completionHandler = (result, exception) -> {
if (exception != null) {
return exceptionHandler.apply(exception);
} else {
return Attempt.success(result);
}
};
BiFunction<T, Throwable, Attempt<T, F>> completionHandler = (result, exception) -> exception != null
? exceptionHandler.apply(exception)
: Attempt.success(result);
return of(future, completionHandler);
}
@ -58,6 +54,10 @@ public final class AsyncAttempt<T, F extends FailureReason> {
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.map(mapper)));
}
public <U> AsyncAttempt<U, F> mapAttempt(Function<? super T, Attempt<U, F>> mapper) {
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.mapAttempt(mapper)));
}
public <U> AsyncAttempt<U, F> mapAsyncAttempt(Function<? super T, AsyncAttempt<U, F>> mapper) {
return new AsyncAttempt<>(future.thenApplyAsync(
attempt -> attempt.mapAttempt(rasult -> mapper.apply(rasult).toAttempt())));