Convert all usage of AsyncSafetyTeleporter to new api

This commit is contained in:
Ben Woo 2025-01-09 20:52:16 +08:00
parent 2e309cab0d
commit d8d8607592
6 changed files with 10 additions and 164 deletions

View File

@ -62,7 +62,9 @@ class SpawnCommand extends CoreCommand {
// Teleport the player to spawn
// TODO: Different message for teleporting self vs others
safetyTeleporter.teleportSafely(issuer.getIssuer(), player, world.getSpawnLocation())
safetyTeleporter.to(world.getSpawnLocation())
.by(issuer)
.teleport(player)
.onSuccess(() -> player.sendMessage(commandManager.formatMessage(
issuer,
MessageType.INFO,

View File

@ -83,7 +83,7 @@ class TeleportCommand extends CoreCommand {
safetyTeleporter.to(destination)
.by(issuer)
.checkSafety(parsedFlags.hasFlag(UNSAFE_FLAG) || destination.checkTeleportSafety())
.checkSafety(!parsedFlags.hasFlag(UNSAFE_FLAG) && destination.checkTeleportSafety())
.teleport(List.of(players))
.thenAccept(attempts -> {
//todo: Check for attempt results

View File

@ -195,6 +195,7 @@ public class MVPlayerListener implements CoreListener {
* Will teleport the player to the destination specified in config
* @param player The {@link Player} to teleport
*/
//todo: Explore the use of PlayerSpawnLocationEvent
private void handleJoinDestination(@NotNull Player player) {
if (!config.getEnableJoinDestination()) {
Logging.finer("JoinDestination is disabled");
@ -209,7 +210,7 @@ public class MVPlayerListener implements CoreListener {
Logging.finer("JoinDestination is " + config.getJoinDestination());
destinationsProvider.parseDestination(config.getJoinDestination())
.peek(destination -> safetyTeleporter.teleportSafely(player, player, destination))
.peek(destination -> safetyTeleporter.to(destination).teleport(player))
.onEmpty(() -> Logging.warning("The destination in JoinDestination in config is invalid"));
}
@ -363,7 +364,7 @@ public class MVPlayerListener implements CoreListener {
new Runnable() {
@Override
public void run() {
safetyTeleporter.teleportSafely(player, player, parsedDestination);
safetyTeleporter.to(parsedDestination).teleport(player);
}
}, 1L);
}

View File

@ -1,15 +1,8 @@
package org.mvplugins.multiverse.core.teleportation;
import java.util.List;
import com.dumptruckman.minecraft.util.Logging;
import io.papermc.lib.PaperLib;
import io.vavr.control.Either;
import jakarta.inject.Inject;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -17,10 +10,6 @@ import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.api.BlockSafety;
import org.mvplugins.multiverse.core.destination.DestinationInstance;
import org.mvplugins.multiverse.core.event.MVTeleportDestinationEvent;
import org.mvplugins.multiverse.core.utils.result.Async;
import org.mvplugins.multiverse.core.utils.result.AsyncAttempt;
import org.mvplugins.multiverse.core.utils.result.Attempt;
/**
* Teleports entities safely and asynchronously.
@ -41,153 +30,6 @@ public class AsyncSafetyTeleporter {
this.pluginManager = pluginManager;
}
public AsyncAttempt<Void, TeleportResult.Failure> teleportSafely(
@NotNull Entity teleportee,
@Nullable DestinationInstance<?, ?> destination) {
return teleportSafely(null, teleportee, destination);
}
public <T extends Entity> Async<List<Attempt<Void, TeleportResult.Failure>>> teleportSafely(
@Nullable CommandSender teleporter,
@NotNull List<T> teleportees,
@Nullable DestinationInstance<?, ?> destination) {
return AsyncAttempt.allOf(teleportees.stream()
.map(teleportee -> teleportSafely(teleporter, teleportee, destination))
.toList());
}
public AsyncAttempt<Void, TeleportResult.Failure> teleportSafely(
@Nullable CommandSender teleporter,
@NotNull Entity teleportee,
@Nullable DestinationInstance<?, ?> destination) {
if (destination == null) {
return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION);
}
MVTeleportDestinationEvent event = new MVTeleportDestinationEvent(destination, teleportee, teleporter);
this.pluginManager.callEvent(event);
if (event.isCancelled()) {
return AsyncAttempt.failure(TeleportResult.Failure.EVENT_CANCELLED);
}
return destination.getLocation(teleportee)
.map(location -> destination.checkTeleportSafety()
? teleportSafely(teleporter, teleportee, location)
: teleport(teleporter, teleportee, location))
.getOrElse(AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION));
}
public AsyncAttempt<Void, TeleportResult.Failure> teleportSafely(
@NotNull Entity teleportee,
@Nullable Location location) {
return teleportSafely(null, teleportee, location);
}
public AsyncAttempt<Void, TeleportResult.Failure> teleportSafely(
@Nullable CommandSender teleporter,
@NotNull Entity teleportee,
@Nullable Location location) {
if (location == null) {
return AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION);
}
Location safeLocation = blockSafety.getSafeLocation(location);
if (safeLocation == null) {
return AsyncAttempt.failure(TeleportResult.Failure.UNSAFE_LOCATION);
}
return teleport(teleporter, teleportee, safeLocation);
}
public <T extends Entity> Async<List<Attempt<Void, TeleportResult.Failure>>> teleport(
@Nullable CommandSender teleporter,
@NotNull List<T> teleportees,
@Nullable DestinationInstance<?, ?> destination) {
return AsyncAttempt.allOf(teleportees.stream()
.map(teleportee -> teleport(teleporter, teleportee, destination))
.toList());
}
public <T extends Entity> Async<List<Attempt<Void, TeleportResult.Failure>>> teleport(
@NotNull List<T> teleportees,
@Nullable DestinationInstance<?, ?> destination) {
return AsyncAttempt.allOf(teleportees.stream()
.map(teleportee -> teleport(teleportee, destination))
.toList());
}
public AsyncAttempt<Void, TeleportResult.Failure> teleport(
@NotNull Entity teleportee,
@Nullable DestinationInstance<?, ?> destination) {
return teleport(null, teleportee, destination);
}
public AsyncAttempt<Void, TeleportResult.Failure> teleport(
@Nullable CommandSender teleporter,
@NotNull Entity teleportee,
@Nullable DestinationInstance<?, ?> destination) {
if (destination == null) {
return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION);
}
MVTeleportDestinationEvent event = new MVTeleportDestinationEvent(destination, teleportee, teleporter);
this.pluginManager.callEvent(event);
if (event.isCancelled()) {
return AsyncAttempt.failure(TeleportResult.Failure.EVENT_CANCELLED);
}
return destination.getLocation(teleportee)
.map(location -> teleport(teleporter, teleportee, location))
.getOrElse(AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION));
}
public <T extends Entity> Async<List<Attempt<Void, TeleportResult.Failure>>> teleport(
@NotNull List<T> teleportees,
@Nullable Location location) {
return AsyncAttempt.allOf(teleportees.stream()
.map(teleportee -> teleport(teleportee, location))
.toList());
}
public AsyncAttempt<Void, TeleportResult.Failure> teleport(
@NotNull Entity teleportee,
@Nullable Location location) {
return teleport(null, teleportee, location);
}
public AsyncAttempt<Void, TeleportResult.Failure> teleport(
@Nullable CommandSender teleporter,
@NotNull Entity teleportee,
@Nullable Location location) {
if (location == null) {
return AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION);
}
boolean shouldAddToQueue = teleportee instanceof Player;
if (shouldAddToQueue) {
if (teleporter == null) {
teleporter = teleportee;
}
teleportQueue.addToQueue(teleporter.getName(), teleportee.getName());
}
return doAsyncTeleport(teleportee, location, shouldAddToQueue);
}
private AsyncAttempt<Void, TeleportResult.Failure> doAsyncTeleport(
@NotNull Entity teleportee,
@NotNull Location location,
boolean shouldRemoveFromQueue) {
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);
}).mapAttempt(success -> {
if (shouldRemoveFromQueue) {
teleportQueue.popFromQueue(teleportee.getName());
}
if (success) {
Logging.finer("Teleported async %s to %s", teleportee.getName(), location);
return Attempt.success(null);
}
return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED);
});
}
public AsyncSafetyTeleporterAction to(@Nullable Location location) {
return new AsyncSafetyTeleporterAction(
blockSafety,

View File

@ -132,6 +132,7 @@ public class AsyncSafetyTeleporterAction {
Logging.finer("Teleported async %s to %s", teleportee.getName(), location);
return Attempt.success(null);
}
Logging.warning("Failed to async teleport %s to %s", teleportee.getName(), location);
return Attempt.failure(TeleportResult.Failure.TELEPORT_FAILED);
});
}

View File

@ -77,7 +77,7 @@ public class PlayerWorldTeleporter {
@NotNull LoadedMultiverseWorld world,
@NotNull Location location) {
return world.getPlayers()
.map(players -> safetyTeleporter.teleport(players, location))
.map(players -> safetyTeleporter.to(location).teleport(players))
.getOrElse(() -> Async.failedFuture(
new IllegalStateException("Unable to get players from world" + world.getName())));
}
@ -93,6 +93,6 @@ public class PlayerWorldTeleporter {
@NotNull List<Player> players,
@NotNull LoadedMultiverseWorld world) {
Location spawnLocation = world.getSpawnLocation();
return safetyTeleporter.teleport(players, spawnLocation);
return safetyTeleporter.to(spawnLocation).teleport(players);
}
}