From f98510b184e63928851168373fade934b38babde Mon Sep 17 00:00:00 2001 From: William Date: Fri, 24 Mar 2023 01:24:49 +0000 Subject: [PATCH] Random teleport: return a future to support x-server pos retrieval --- .../huskhomes/api/BaseHuskHomesAPI.java | 40 ++++++++-------- .../huskhomes/command/RtpCommand.java | 47 ++++++++++--------- .../random/NormalDistributionEngine.java | 24 +++++----- .../random/RandomTeleportEngine.java | 3 +- 4 files changed, 60 insertions(+), 54 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java index 28211ed3..a24f5952 100644 --- a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java +++ b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java @@ -551,26 +551,28 @@ public abstract class BaseHuskHomesAPI { * @since 3.0 */ public final void randomlyTeleportPlayer(@NotNull OnlineUser user, boolean timedTeleport, @NotNull String... rtpArgs) { - plugin.runAsync(() -> { - final Optional position = plugin.getRandomTeleportEngine() - .getRandomPosition(user.getPosition().getWorld(), rtpArgs); - if (position.isEmpty()) { - throw new IllegalStateException("Random teleport engine returned an empty position"); - } + plugin.getRandomTeleportEngine() + .getRandomPosition(user.getPosition().getWorld(), rtpArgs) + .thenAccept(position -> { + if (position.isEmpty()) { + throw new IllegalStateException("Random teleport engine returned an empty position"); + } - final TeleportBuilder builder = Teleport.builder(plugin) - .teleporter(user) - .target(position.get()); - try { - if (timedTeleport) { - builder.toTimedTeleport().execute(); - } else { - builder.toTeleport().execute(); - } - } catch (TeleportationException e) { - e.displayMessage(user, plugin, rtpArgs); - } - }); + final TeleportBuilder builder = Teleport.builder(plugin) + .teleporter(user) + .target(position.get()); + try { + if (timedTeleport) { + builder.toTimedTeleport().execute(); + } else { + builder.toTeleport().execute(); + } + } catch (TeleportationException e) { + e.displayMessage(user, plugin, rtpArgs); + } + }).exceptionally(e -> { + throw new IllegalStateException("Random teleport engine threw an exception", e); + }); } /** diff --git a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java index 716d82c6..22b3360b 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java @@ -2,7 +2,6 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.hook.EconomyHook; -import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.teleport.TeleportBuilder; import net.william278.huskhomes.teleport.TeleportationException; @@ -79,30 +78,32 @@ public class RtpCommand extends Command { // Generate a random position plugin.getLocales().getLocale("teleporting_random_generation") .ifPresent(teleporter::sendMessage); - final Optional position = plugin.getRandomTeleportEngine() - .getRandomPosition(teleporter.getPosition().getWorld(), args.length > 1 ? removeFirstArg(args) : args); - if (position.isEmpty()) { - plugin.getLocales().getLocale("error_rtp_randomization_timeout") - .ifPresent(executor::sendMessage); - return; - } + plugin.getRandomTeleportEngine() + .getRandomPosition(teleporter.getPosition().getWorld(), args.length > 1 ? removeFirstArg(args) : args) + .thenAccept(position -> { + if (position.isEmpty()) { + plugin.getLocales().getLocale("error_rtp_randomization_timeout") + .ifPresent(executor::sendMessage); + return; + } - final TeleportBuilder builder = Teleport.builder(plugin) - .teleporter(teleporter) - .target(position.get()); - try { - if (executor.equals(teleporter)) { - builder.toTimedTeleport().execute(); - } else { - builder.toTeleport().execute(); - } - } catch (TeleportationException e) { - e.displayMessage(executor, plugin, args); - return; - } + final TeleportBuilder builder = Teleport.builder(plugin) + .teleporter(teleporter) + .target(position.get()); + try { + if (executor.equals(teleporter)) { + builder.toTimedTeleport().execute(); + } else { + builder.toTeleport().execute(); + } + } catch (TeleportationException e) { + e.displayMessage(executor, plugin, args); + return; + } - user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES)); - plugin.getDatabase().updateUserData(user); + user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES)); + plugin.getDatabase().updateUserData(user); + }); } } diff --git a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java index 7c3f079b..ab669858 100644 --- a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java @@ -37,7 +37,7 @@ public final class NormalDistributionEngine extends RandomTeleportEngine { */ @NotNull public static Location generateLocation(@NotNull Location origin, float mean, float standardDeviation, - float spawnRadius, float maxRadius) { + float spawnRadius, float maxRadius) { // Generate random values final float radius = getDistributedRadius(mean, standardDeviation, spawnRadius, maxRadius); final float angle = getRandomAngle(); @@ -90,16 +90,18 @@ public final class NormalDistributionEngine extends RandomTeleportEngine { } @Override - public Optional getRandomPosition(@NotNull World world, @NotNull String[] args) { - Optional location = generateSafeLocation(world).join(); - int attempts = 0; - while (location.isEmpty()) { - location = generateSafeLocation(world).join(); - if (attempts >= maxAttempts) { - return Optional.empty(); + public CompletableFuture> getRandomPosition(@NotNull World world, @NotNull String[] args) { + return plugin.supplyAsync(() -> { + Optional location = generateSafeLocation(world).join(); + int attempts = 0; + while (location.isEmpty()) { + location = generateSafeLocation(world).join(); + if (attempts >= maxAttempts) { + return Optional.empty(); + } + attempts++; } - attempts++; - } - return location.map(resolved -> Position.at(resolved, plugin.getServerName())); + return location.map(resolved -> Position.at(resolved, plugin.getServerName())); + }); } } diff --git a/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java index dfd3770c..3bbdeda0 100644 --- a/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java @@ -6,6 +6,7 @@ import net.william278.huskhomes.position.World; import org.jetbrains.annotations.NotNull; import java.util.Optional; +import java.util.concurrent.CompletableFuture; /** * Represents an engine for generating random position targets @@ -54,6 +55,6 @@ public abstract class RandomTeleportEngine { * @return The position, optionally, which will be empty if the random teleport engine timed out after a * {@link #maxAttempts configured number of attempts} */ - public abstract Optional getRandomPosition(@NotNull World world, @NotNull String[] args); + public abstract CompletableFuture> getRandomPosition(@NotNull World world, @NotNull String[] args); }