mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-25 01:31:24 +01:00
Refactor some async attempt handling
This commit is contained in:
parent
94bcca914f
commit
bf8244e587
@ -1,6 +1,6 @@
|
|||||||
package org.mvplugins.multiverse.core.commands;
|
package org.mvplugins.multiverse.core.commands;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.List;
|
||||||
|
|
||||||
import co.aikar.commands.CommandIssuer;
|
import co.aikar.commands.CommandIssuer;
|
||||||
import co.aikar.commands.annotation.CommandAlias;
|
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.permissions.CorePermissionsChecker;
|
||||||
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
|
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
|
||||||
import org.mvplugins.multiverse.core.utils.MVCorei18n;
|
import org.mvplugins.multiverse.core.utils.MVCorei18n;
|
||||||
import org.mvplugins.multiverse.core.utils.result.AsyncAttempt;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@CommandAlias("mv")
|
@CommandAlias("mv")
|
||||||
@ -72,10 +71,8 @@ class TeleportCommand extends MultiverseCommand {
|
|||||||
issuer.sendInfo(MVCorei18n.TELEPORT_SUCCESS,
|
issuer.sendInfo(MVCorei18n.TELEPORT_SUCCESS,
|
||||||
"{player}", playerName, "{destination}", destination.toString());
|
"{player}", playerName, "{destination}", destination.toString());
|
||||||
|
|
||||||
AsyncAttempt.allOf(Arrays.stream(players)
|
safetyTeleporter.teleportSafely(issuer.getIssuer(), List.of(players), destination)
|
||||||
.map(player -> safetyTeleporter.teleportSafely(issuer.getIssuer(), player, destination))
|
.thenAccept(attempts -> Logging.fine("Async teleport completed: %s", attempts))
|
||||||
.toList())
|
|
||||||
.thenRun(() -> Logging.fine("Async teleport result: %s"))
|
|
||||||
.exceptionally(throwable -> {
|
.exceptionally(throwable -> {
|
||||||
Logging.severe("Error while teleporting %s to %s: %s",
|
Logging.severe("Error while teleporting %s to %s: %s",
|
||||||
playerName, destination, throwable.getMessage());
|
playerName, destination, throwable.getMessage());
|
||||||
|
@ -144,16 +144,15 @@ public class AsyncSafetyTeleporter {
|
|||||||
Logging.warning("Failed to teleport %s to %s: %s",
|
Logging.warning("Failed to teleport %s to %s: %s",
|
||||||
teleportee.getName(), location, exception.getMessage());
|
teleportee.getName(), location, exception.getMessage());
|
||||||
return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED_EXCEPTION);
|
return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED_EXCEPTION);
|
||||||
}).mapAsyncAttempt(result -> {
|
}).mapAttempt(result -> {
|
||||||
Logging.finer("Teleported async %s to %s", teleportee.getName(), location);
|
Logging.finer("Teleported async %s to %s", teleportee.getName(), location);
|
||||||
if (result) {
|
if (result) {
|
||||||
if (shouldAddToQueue) {
|
if (shouldAddToQueue) {
|
||||||
teleportQueue.popFromQueue(teleportee.getName());
|
teleportQueue.popFromQueue(teleportee.getName());
|
||||||
}
|
}
|
||||||
return AsyncAttempt.success();
|
return Attempt.success(null);
|
||||||
} else {
|
|
||||||
return AsyncAttempt.failure(TeleportResult.Failure.TELEPORT_FAILED);
|
|
||||||
}
|
}
|
||||||
|
return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,19 +21,15 @@ public final class AsyncAttempt<T, F extends FailureReason> {
|
|||||||
public static <T, F extends FailureReason> AsyncAttempt<T, F> of(
|
public static <T, F extends FailureReason> AsyncAttempt<T, F> of(
|
||||||
CompletableFuture<T> future,
|
CompletableFuture<T> future,
|
||||||
BiFunction<T, Throwable, Attempt<T, F>> completionHandler) {
|
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(
|
public static <T, F extends FailureReason> AsyncAttempt<T, F> of(
|
||||||
CompletableFuture<T> future,
|
CompletableFuture<T> future,
|
||||||
Function<Throwable, Attempt<T, F>> exceptionHandler) {
|
Function<Throwable, Attempt<T, F>> exceptionHandler) {
|
||||||
BiFunction<T, Throwable, Attempt<T, F>> completionHandler = (result, exception) -> {
|
BiFunction<T, Throwable, Attempt<T, F>> completionHandler = (result, exception) -> exception != null
|
||||||
if (exception != null) {
|
? exceptionHandler.apply(exception)
|
||||||
return exceptionHandler.apply(exception);
|
: Attempt.success(result);
|
||||||
} else {
|
|
||||||
return Attempt.success(result);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return of(future, completionHandler);
|
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)));
|
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) {
|
public <U> AsyncAttempt<U, F> mapAsyncAttempt(Function<? super T, AsyncAttempt<U, F>> mapper) {
|
||||||
return new AsyncAttempt<>(future.thenApplyAsync(
|
return new AsyncAttempt<>(future.thenApplyAsync(
|
||||||
attempt -> attempt.mapAttempt(rasult -> mapper.apply(rasult).toAttempt())));
|
attempt -> attempt.mapAttempt(rasult -> mapper.apply(rasult).toAttempt())));
|
||||||
|
Loading…
Reference in New Issue
Block a user