Fully implement AsyncResult

This commit is contained in:
Ben Woo 2023-09-14 11:03:45 +08:00
parent 3df46bc55c
commit 23b429940c
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
9 changed files with 84 additions and 60 deletions

View File

@ -24,7 +24,7 @@ import org.mvplugins.multiverse.core.commandtools.flags.CommandFlag;
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
import org.mvplugins.multiverse.core.commandtools.queue.QueuedCommand;
import org.mvplugins.multiverse.core.utils.MVCorei18n;
import org.mvplugins.multiverse.core.utils.result.AsyncResult;
import org.mvplugins.multiverse.core.utils.result.Async;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.core.world.helpers.PlayerWorldTeleporter;
@ -83,7 +83,7 @@ class DeleteCommand extends MultiverseCommand {
var future = parsedFlags.hasFlag(REMOVE_PLAYERS_FLAG)
? playerWorldTeleporter.removeFromWorld(world)
: AsyncResult.completedFuture(Collections.emptyList());
: Async.completedFuture(Collections.emptyList());
future.thenRun(() -> doWorldDeleting(issuer, world));
}

View File

@ -26,7 +26,7 @@ import org.mvplugins.multiverse.core.commandtools.flags.CommandValueFlag;
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
import org.mvplugins.multiverse.core.commandtools.queue.QueuedCommand;
import org.mvplugins.multiverse.core.utils.MVCorei18n;
import org.mvplugins.multiverse.core.utils.result.AsyncResult;
import org.mvplugins.multiverse.core.utils.result.Async;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.core.world.helpers.PlayerWorldTeleporter;
@ -101,7 +101,7 @@ class RegenCommand extends MultiverseCommand {
var future = parsedFlags.hasFlag(REMOVE_PLAYERS_FLAG)
? playerWorldTeleporter.removeFromWorld(world)
: AsyncResult.completedFuture(Collections.emptyList());
: Async.completedFuture(Collections.emptyList());
future.thenRun(() -> doWorldRegening(issuer, world, parsedFlags, worldPlayers));
}

View File

@ -22,7 +22,7 @@ import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.commandtools.flags.CommandFlag;
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
import org.mvplugins.multiverse.core.utils.MVCorei18n;
import org.mvplugins.multiverse.core.utils.result.AsyncResult;
import org.mvplugins.multiverse.core.utils.result.Async;
import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.core.world.helpers.PlayerWorldTeleporter;
@ -70,8 +70,8 @@ class RemoveCommand extends MultiverseCommand {
var future = parsedFlags.hasFlag(REMOVE_PLAYERS_FLAG)
? worldManager.getLoadedWorld(worldName)
.map(playerWorldTeleporter::removeFromWorld)
.getOrElse(AsyncResult.completedFuture(Collections.emptyList()))
: AsyncResult.completedFuture(Collections.emptyList());
.getOrElse(Async.completedFuture(Collections.emptyList()))
: Async.completedFuture(Collections.emptyList());
future.thenRun(() -> doWorldRemoving(issuer, worldName));
}

View File

@ -22,7 +22,7 @@ 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.AsyncResult;
import org.mvplugins.multiverse.core.utils.result.AsyncAttempt;
@Service
@CommandAlias("mv")
@ -72,7 +72,7 @@ class TeleportCommand extends MultiverseCommand {
issuer.sendInfo(MVCorei18n.TELEPORT_SUCCESS,
"{player}", playerName, "{destination}", destination.toString());
AsyncResult.allOf(Arrays.stream(players)
AsyncAttempt.allOf(Arrays.stream(players)
.map(player -> safetyTeleporter.teleportSafely(issuer.getIssuer(), player, destination))
.toList())
.thenRun(() -> Logging.fine("Async teleport result: %s"))

View File

@ -20,7 +20,7 @@ import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.commandtools.flags.CommandFlag;
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
import org.mvplugins.multiverse.core.utils.MVCorei18n;
import org.mvplugins.multiverse.core.utils.result.AsyncResult;
import org.mvplugins.multiverse.core.utils.result.Async;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.core.world.helpers.PlayerWorldTeleporter;
@ -73,7 +73,7 @@ class UnloadCommand extends MultiverseCommand {
var future = parsedFlags.hasFlag(REMOVE_PLAYERS_FLAG)
? playerWorldTeleporter.removeFromWorld(world)
: AsyncResult.completedFuture(Collections.emptyList());
: Async.completedFuture(Collections.emptyList());
future.thenRun(() -> doWorldUnloading(issuer, world, parsedFlags));
}

View File

@ -4,7 +4,6 @@ import java.util.List;
import com.dumptruckman.minecraft.util.Logging;
import io.papermc.lib.PaperLib;
import io.vavr.control.Try;
import jakarta.inject.Inject;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
@ -16,12 +15,13 @@ 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.Async;
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")
/**
* Teleports entities safely and asynchronously.
*/
@Service
public class AsyncSafetyTeleporter {
private final BlockSafety blockSafety;
@ -41,11 +41,11 @@ public class AsyncSafetyTeleporter {
return teleportSafely(null, teleportee, destination);
}
public <T extends Entity> AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>> teleportSafely(
public <T extends Entity> Async<List<Attempt<Void, TeleportResult.Failure>>> teleportSafely(
@Nullable CommandSender teleporter,
@NotNull List<T> teleportees,
@Nullable ParsedDestination<?> destination) {
return AsyncResult.allOf(teleportees.stream()
return AsyncAttempt.allOf(teleportees.stream()
.map(teleportee -> teleportSafely(teleporter, teleportee, destination))
.toList());
}
@ -82,10 +82,10 @@ public class AsyncSafetyTeleporter {
return teleport(teleporter, teleportee, safeLocation);
}
public <T extends Entity> AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>> teleport(
public <T extends Entity> Async<List<Attempt<Void, TeleportResult.Failure>>> teleport(
@NotNull List<T> teleportees,
@Nullable ParsedDestination<?> destination) {
return AsyncResult.allOf(teleportees.stream()
return AsyncAttempt.allOf(teleportees.stream()
.map(teleportee -> teleport(teleportee, destination))
.toList());
}
@ -106,10 +106,10 @@ public class AsyncSafetyTeleporter {
return teleport(teleporter, teleportee, destination.getLocation(teleportee));
}
public <T extends Entity> AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>> teleport(
public <T extends Entity> Async<List<Attempt<Void, TeleportResult.Failure>>> teleport(
@NotNull List<T> teleportees,
@Nullable Location location) {
return AsyncResult.allOf(teleportees.stream()
return AsyncAttempt.allOf(teleportees.stream()
.map(teleportee -> teleport(teleportee, location))
.toList());
}

View File

@ -11,7 +11,7 @@ import java.util.function.Function;
*
* @param <T> The type of the value.
*/
public final class AsyncResult<T> {
public final class Async<T> {
/**
* Returns a new AsyncResult that is completed when all of the given AsyncResult complete.
@ -19,8 +19,8 @@ public final class AsyncResult<T> {
* @param results The results to wait for.
* @return A new AsyncResult that is completed when all of the given AsyncResult complete.
*/
public static AsyncResult<Void> allOf(AsyncResult<?>... results) {
return new AsyncResult<>(CompletableFuture.allOf(Arrays.stream(results)
public static Async<Void> allOf(Async<?>... results) {
return new Async<>(CompletableFuture.allOf(Arrays.stream(results)
.map(result -> result.future)
.toArray(CompletableFuture[]::new)));
}
@ -32,8 +32,8 @@ public final class AsyncResult<T> {
* @param <T> The type of the AsyncResult.
* @return A new AsyncResult that is completed when all of the given AsyncResult complete.
*/
public static <T> AsyncResult<List<T>> allOf(List<AsyncResult<T>> results) {
return new AsyncResult<>(CompletableFuture.allOf(results.stream()
public static <T> Async<List<T>> allOf(List<Async<T>> results) {
return new Async<>(CompletableFuture.allOf(results.stream()
.map(result -> result.future)
.toArray(CompletableFuture[]::new))
.thenApply(v -> results.stream()
@ -48,8 +48,8 @@ public final class AsyncResult<T> {
* @param <T> The type of the future.
* @return A new AsyncResult that is completed when the given future completes.
*/
public static <T> AsyncResult<T> of(CompletableFuture<T> future) {
return new AsyncResult<>(future);
public static <T> Async<T> of(CompletableFuture<T> future) {
return new Async<>(future);
}
/**
@ -59,8 +59,8 @@ public final class AsyncResult<T> {
* @param <T> The type of the value.
* @return The completed AsyncResult.
*/
public static <T> AsyncResult<T> completedFuture(T value) {
return new AsyncResult<>(CompletableFuture.completedFuture(value));
public static <T> Async<T> completedFuture(T value) {
return new Async<>(CompletableFuture.completedFuture(value));
}
/**
@ -70,8 +70,8 @@ public final class AsyncResult<T> {
* @param <T> The type of the value.
* @return The completed AsyncResult.
*/
public static <T> AsyncResult<T> failedFuture(Throwable throwable) {
return new AsyncResult<>(CompletableFuture.failedFuture(throwable));
public static <T> Async<T> failedFuture(Throwable throwable) {
return new Async<>(CompletableFuture.failedFuture(throwable));
}
private final CompletableFuture<T> future;
@ -79,11 +79,11 @@ public final class AsyncResult<T> {
/**
* Creates a new AsyncResult.
*/
public AsyncResult() {
public Async() {
this(new CompletableFuture<>());
}
private AsyncResult(CompletableFuture<T> future) {
private Async(CompletableFuture<T> future) {
this.future = future;
}
@ -113,8 +113,8 @@ public final class AsyncResult<T> {
* @param consumer The action to perform.
* @return This AsyncResult.
*/
public AsyncResult<Void> thenAccept(Consumer<T> consumer) {
return new AsyncResult<>(future.thenAccept(consumer));
public Async<Void> thenAccept(Consumer<T> consumer) {
return new Async<>(future.thenAccept(consumer));
}
/**
@ -123,8 +123,8 @@ public final class AsyncResult<T> {
* @param runnable The action to perform.
* @return This AsyncResult.
*/
public AsyncResult<Void> thenRun(Runnable runnable) {
return new AsyncResult<>(future.thenRun(runnable));
public Async<Void> thenRun(Runnable runnable) {
return new Async<>(future.thenRun(runnable));
}
/**
@ -134,8 +134,8 @@ public final class AsyncResult<T> {
* @param <U> The type of the new value.
* @return A new AsyncResult with the new value.
*/
public <U> AsyncResult<U> thenApply(Function<T, U> function) {
return new AsyncResult<>(future.thenApply(function));
public <U> Async<U> thenApply(Function<T, U> function) {
return new Async<>(future.thenApply(function));
}
/**
@ -144,8 +144,8 @@ public final class AsyncResult<T> {
* @param consumer The action to perform.
* @return This AsyncResult.
*/
public AsyncResult<T> exceptionally(Consumer<Throwable> consumer) {
return new AsyncResult<>(future.exceptionally(throwable -> {
public Async<T> exceptionally(Consumer<Throwable> consumer) {
return new Async<>(future.exceptionally(throwable -> {
consumer.accept(throwable);
return null;
}));
@ -157,7 +157,7 @@ public final class AsyncResult<T> {
* @param function The action to perform.
* @return A new AsyncResult with the new value.
*/
public AsyncResult<T> exceptionally(Function<Throwable, T> function) {
return new AsyncResult<>(future.exceptionally(function));
public Async<T> exceptionally(Function<Throwable, T> function) {
return new Async<>(future.exceptionally(function));
}
}

View File

@ -1,5 +1,6 @@
package org.mvplugins.multiverse.core.utils.result;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;
@ -8,6 +9,15 @@ import org.mvplugins.multiverse.core.utils.message.MessageReplacement;
public final class AsyncAttempt<T, F extends FailureReason> {
public static <T, F extends FailureReason> Async<List<Attempt<T, F>>> allOf(List<AsyncAttempt<T, F>> attempts) {
return Async.of(CompletableFuture.allOf(attempts.stream()
.map(attempt -> attempt.future)
.toArray(CompletableFuture[]::new))
.thenApply(v -> attempts.stream()
.map(attempt -> attempt.future.join())
.toList()));
}
public static <T, F extends FailureReason> AsyncAttempt<T, F> of(
CompletableFuture<T> future,
BiFunction<T, Throwable, Attempt<T, F>> completionHandler) {
@ -39,18 +49,25 @@ public final class AsyncAttempt<T, F extends FailureReason> {
}
private final CompletableFuture<Attempt<T, F>> future;
`
private AsyncAttempt(CompletableFuture<Attempt<T, F>> future) {
this.future = future;
}
public <U> AsyncAttempt<U, F> map(Function<? super T, ? extends U> mapper) {
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.map(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())));
}
public AsyncAttempt<T, F> onSuccess(Runnable runnable) {
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.onSuccess(runnable)));
}
public Attempt<T, F> toAttempt() {
return future.join();
}
}

View File

@ -12,8 +12,8 @@ import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
import org.mvplugins.multiverse.core.teleportation.TeleportResult;
import org.mvplugins.multiverse.core.utils.result.AsyncResult;
import org.mvplugins.multiverse.core.utils.result.Result;
import org.mvplugins.multiverse.core.utils.result.Async;
import org.mvplugins.multiverse.core.utils.result.Attempt;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
/**
@ -32,9 +32,9 @@ public class PlayerWorldTeleporter {
* Removes all players from the given world.
*
* @param world The world to remove all players from.
* @return A list of async futures that represent the teleportation result of each player.
*/
public AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>>
removeFromWorld(@NotNull LoadedMultiverseWorld world) {
public Async<List<Attempt<Void, TeleportResult.Failure>>> removeFromWorld(@NotNull LoadedMultiverseWorld world) {
// TODO: Better handling of fallback world
World toWorld = Bukkit.getWorlds().get(0);
return transferFromWorldTo(world, toWorld);
@ -45,9 +45,11 @@ public class PlayerWorldTeleporter {
*
* @param from The world to transfer players from.
* @param to The location to transfer players to.
* @return A list of async futures that represent the teleportation result of each player.
*/
public AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>>
transferFromWorldTo(@NotNull LoadedMultiverseWorld from, @NotNull LoadedMultiverseWorld to) {
public Async<List<Attempt<Void, TeleportResult.Failure>>> transferFromWorldTo(
@NotNull LoadedMultiverseWorld from,
@NotNull LoadedMultiverseWorld to) {
return transferAllFromWorldToLocation(from, to.getSpawnLocation());
}
@ -56,9 +58,11 @@ public class PlayerWorldTeleporter {
*
* @param from The world to transfer players from.
* @param to The world to transfer players to.
* @return A list of async futures that represent the teleportation result of each player.
*/
public AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>>
transferFromWorldTo(@NotNull LoadedMultiverseWorld from, @NotNull World to) {
public Async<List<Attempt<Void, TeleportResult.Failure>>> transferFromWorldTo(
@NotNull LoadedMultiverseWorld from,
@NotNull World to) {
return transferAllFromWorldToLocation(from, to.getSpawnLocation());
}
@ -67,13 +71,14 @@ public class PlayerWorldTeleporter {
*
* @param world The world to transfer players from.
* @param location The location to transfer players to.
* @return A list of futures that represent the teleportation of each player.
* @return A list of async futures that represent the teleportation result of each player.
*/
public AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>>
transferAllFromWorldToLocation(@NotNull LoadedMultiverseWorld world, @NotNull Location location) {
public Async<List<Attempt<Void, TeleportResult.Failure>>> transferAllFromWorldToLocation(
@NotNull LoadedMultiverseWorld world,
@NotNull Location location) {
return world.getPlayers()
.map(players -> safetyTeleporter.teleport(players, location))
.getOrElse(() -> AsyncResult.failedFuture(
.getOrElse(() -> Async.failedFuture(
new IllegalStateException("Unable to get players from world" + world.getName())));
}
@ -82,9 +87,11 @@ public class PlayerWorldTeleporter {
*
* @param players The players to teleport.
* @param world The world to teleport players to.
* @return A list of async futures that represent the teleportation result of each player.
*/
public AsyncResult<List<Result<TeleportResult.Success, TeleportResult.Failure>>>
teleportPlayersToWorld(@NotNull List<Player> players, @NotNull LoadedMultiverseWorld world) {
public Async<List<Attempt<Void, TeleportResult.Failure>>> teleportPlayersToWorld(
@NotNull List<Player> players,
@NotNull LoadedMultiverseWorld world) {
Location spawnLocation = world.getSpawnLocation();
return safetyTeleporter.teleport(players, spawnLocation);
}