From 1c2f1134d21cd42b5d5e5813ca1f21007fc76ffb Mon Sep 17 00:00:00 2001 From: William278 Date: Tue, 6 Sep 2022 01:00:48 +0300 Subject: [PATCH] API: Change `#getRandomPosition` to accept an `OnlineUser` as arg instead of their position --- .../william278/huskhomes/api/BaseHuskHomesAPI.java | 2 +- .../net/william278/huskhomes/command/RtpCommand.java | 10 +++++----- .../huskhomes/random/RandomTeleportEngine.java | 12 +++++++----- 3 files changed, 13 insertions(+), 11 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 9f5d53a9..98a7790f 100644 --- a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java +++ b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java @@ -357,7 +357,7 @@ public abstract class BaseHuskHomesAPI { final boolean timedTeleport, @NotNull String... rtpArgs) { return CompletableFuture.supplyAsync(() -> plugin.getRandomTeleportEngine() - .getRandomPosition(user.getPosition(), rtpArgs) + .getRandomPosition(user, rtpArgs) .thenApply(position -> { if (position.isPresent()) { return teleportPlayer(user, position.get(), timedTeleport).join(); 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 6f831261..356e6fb0 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java @@ -61,8 +61,8 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable { } final Instant currentTime = Instant.now(); if (!isExecutorTeleporting - && !currentTime.isAfter(userData.get().rtpCooldown()) - && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) { + && !currentTime.isAfter(userData.get().rtpCooldown()) + && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) { plugin.getLocales().getLocale("error_rtp_cooldown", Long.toString(currentTime.until(userData.get().rtpCooldown(), ChronoUnit.MINUTES))) .ifPresent(onlineUser::sendMessage); @@ -70,7 +70,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable { } // Get a random position and teleport - plugin.getRandomTeleportEngine().getRandomPosition(userPosition, rtpArguments) + plugin.getRandomTeleportEngine().getRandomPosition(onlineUser, rtpArguments) .thenAccept(position -> { if (position.isEmpty()) { plugin.getLocales().getLocale("error_rtp_randomization_timeout") @@ -80,7 +80,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable { (isExecutorTeleporting ? plugin.getTeleportManager().timedTeleport(userToTeleport, position.get(), Settings.EconomyAction.RANDOM_TELEPORT) : plugin.getTeleportManager().teleport(userToTeleport, position.get())).thenAccept(result -> { if (isExecutorTeleporting && - result.successful && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) { + result.successful && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) { plugin.getDatabase().updateUserData(new UserData(onlineUser, userData.get().homeSlots(), userData.get().ignoringTeleports(), Instant.now().plus(plugin.getSettings().rtpCooldownLength, ChronoUnit.MINUTES))); @@ -105,7 +105,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable { } plugin.getLoggingAdapter().log(Level.INFO, "Finding a random position for " + foundUser.get().username + "..."); - plugin.getRandomTeleportEngine().getRandomPosition(foundUser.get().getPosition(), ArrayUtils.subarray(args, 1, args.length)).thenAccept(position -> { + plugin.getRandomTeleportEngine().getRandomPosition(foundUser.get(), ArrayUtils.subarray(args, 1, args.length)).thenAccept(position -> { if (position.isEmpty()) { plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport " + foundUser.get().username + " to a random position; randomization timed out!"); return; 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 bfd01328..daab79c0 100644 --- a/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.random; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.World; @@ -119,7 +120,7 @@ public abstract class RandomTeleportEngine { * Implementations of HuskHomes validate location safety using {@link HuskHomes#getSafeGroundLocation(Location)}. *

* RandomTeleportEngines that wish to return cross-server random {@link Position}s can use this method to cache - * locations on this server, and override {@link #getRandomPosition(Position, String...)} to return a random position + * locations on this server, and override {@link #getRandomPosition(OnlineUser, String...)} to return a random position * from the caches of the engine on other servers. *

* If you'd prefer not to use caching for whatever reason, you can simply override {@link #getRandomLocation(World, String...)} @@ -139,14 +140,15 @@ public abstract class RandomTeleportEngine { /** * Returns a random {@link Position} from the cache, generating if the cache is empty * - * @param userPosition The {@link Position} of the requesting user - * @param args Arguments specified by the random location request. These may be used to alter which locations are - * generated by the randomizer. + * @param onlineUser The {@link OnlineUser} requesting the random position + * @param args Arguments specified by the random location request. These may be used to alter which locations are + * generated by the randomizer. * @return a generated {@link Location} if one was found; an {@link Optional#empty()} otherwise * @since 3.0 */ - public CompletableFuture> getRandomPosition(@NotNull Position userPosition, + public CompletableFuture> getRandomPosition(@NotNull OnlineUser onlineUser, @NotNull String... args) { + final Position userPosition = onlineUser.getPosition(); return getRandomLocation(userPosition.world, args).thenApply(location -> { if (location.isEmpty()) { return Optional.empty();