Check for destination's checkTeleportSafety

This commit is contained in:
Ben Woo 2023-09-12 16:36:43 +08:00
parent 245fac98e5
commit ef2b42d9a0
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
1 changed files with 17 additions and 5 deletions

View File

@ -45,7 +45,9 @@ public class AsyncSafetyTeleporter {
if (destination == null) { if (destination == null) {
return CompletableFuture.completedFuture(Result.failure(TeleportResult.Failure.NULL_DESTINATION)); return CompletableFuture.completedFuture(Result.failure(TeleportResult.Failure.NULL_DESTINATION));
} }
return teleportSafely(teleporter, teleportee, destination.getLocation(teleportee)); return destination.getDestination().checkTeleportSafety()
? teleportSafely(teleporter, teleportee, destination.getLocation(teleportee))
: teleport(teleporter, teleportee, destination.getLocation(teleportee));
} }
public CompletableFuture<Result<TeleportResult.Success, TeleportResult.Failure>> teleportSafely( public CompletableFuture<Result<TeleportResult.Success, TeleportResult.Failure>> teleportSafely(
@ -120,23 +122,33 @@ public class AsyncSafetyTeleporter {
} }
CompletableFuture<Result<TeleportResult.Success, TeleportResult.Failure>> future = new CompletableFuture<>(); CompletableFuture<Result<TeleportResult.Success, TeleportResult.Failure>> future = new CompletableFuture<>();
doAsyncTeleport(teleportee, location, future, shouldAddToQueue);
return future;
}
private void doAsyncTeleport(
@NotNull Entity teleportee,
@NotNull Location location,
CompletableFuture<Result<TeleportResult.Success, TeleportResult.Failure>> future,
boolean shouldAddToQueue) {
Try.run(() -> PaperLib.teleportAsync(teleportee, location).thenAccept(result -> { Try.run(() -> PaperLib.teleportAsync(teleportee, location).thenAccept(result -> {
Logging.fine("Teleported %s to %s", teleportee.getName(), location); Logging.finer("Teleported async %s to %s", teleportee.getName(), location);
future.complete(result future.complete(result
? Result.success(TeleportResult.Success.SUCCESS) ? Result.success(TeleportResult.Success.SUCCESS)
: Result.failure(TeleportResult.Failure.TELEPORT_FAILED)); : Result.failure(TeleportResult.Failure.TELEPORT_FAILED));
}).exceptionally(exception -> { }).exceptionally(exception -> {
Logging.fine("Failed to teleport %s to %s: %s", teleportee.getName(), location, exception.getMessage()); Logging.warning("Failed to teleport %s to %s: %s",
teleportee.getName(), location, exception.getMessage());
future.completeExceptionally(exception); future.completeExceptionally(exception);
return null; return null;
})).onFailure(exception -> { })).onFailure(exception -> {
Logging.fine("Failed to teleport %s to %s: %s", teleportee.getName(), location, exception.getMessage()); Logging.warning("Failed to teleport %s to %s: %s",
teleportee.getName(), location, exception.getMessage());
future.complete(Result.failure(TeleportResult.Failure.TELEPORT_FAILED_EXCEPTION)); future.complete(Result.failure(TeleportResult.Failure.TELEPORT_FAILED_EXCEPTION));
}).andFinally(() -> { }).andFinally(() -> {
if (shouldAddToQueue) { if (shouldAddToQueue) {
teleportQueue.popFromQueue(teleportee.getName()); teleportQueue.popFromQueue(teleportee.getName());
} }
}); });
return future;
} }
} }