API: Change #getRandomPosition to accept an OnlineUser as arg instead of their position

This commit is contained in:
William278 committed 2022-09-06 01:00:48 +03:00
1 parent 3f8bbf96d3
commit 1c2f1134d2
3 files changed
+8 -6

No files matched your search

@@ -357,7 +357,7 @@ public abstract class BaseHuskHomesAPI {
final boolean timedTeleport, final boolean timedTeleport,
@NotNull String... rtpArgs) { @NotNull String... rtpArgs) {
return CompletableFuture.supplyAsync(() -> plugin.getRandomTeleportEngine() return CompletableFuture.supplyAsync(() -> plugin.getRandomTeleportEngine()
.getRandomPosition(user.getPosition(), rtpArgs) .getRandomPosition(user, rtpArgs)
.thenApply(position -> { .thenApply(position -> {
if (position.isPresent()) { if (position.isPresent()) {
return teleportPlayer(user, position.get(), timedTeleport).join(); return teleportPlayer(user, position.get(), timedTeleport).join();
@@ -70,7 +70,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
} }
// Get a random position and teleport // Get a random position and teleport
plugin.getRandomTeleportEngine().getRandomPosition(userPosition, rtpArguments) plugin.getRandomTeleportEngine().getRandomPosition(onlineUser, rtpArguments)
.thenAccept(position -> { .thenAccept(position -> {
if (position.isEmpty()) { if (position.isEmpty()) {
plugin.getLocales().getLocale("error_rtp_randomization_timeout") plugin.getLocales().getLocale("error_rtp_randomization_timeout")
@@ -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.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()) { if (position.isEmpty()) {
plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport " + foundUser.get().username + " to a random position; randomization timed out!"); plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport " + foundUser.get().username + " to a random position; randomization timed out!");
return; return;
@@ -1,6 +1,7 @@
package net.william278.huskhomes.random; package net.william278.huskhomes.random;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World; 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)}. * Implementations of HuskHomes validate location safety using {@link HuskHomes#getSafeGroundLocation(Location)}.
* <p> * <p>
* RandomTeleportEngines that wish to return cross-server random {@link Position}s can use this method to cache * 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. * from the caches of the engine on other servers.
* <p> * <p>
* If you'd prefer not to use caching for whatever reason, you can simply override {@link #getRandomLocation(World, String...)} * 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 * Returns a random {@link Position} from the cache, generating if the cache is empty
* *
* @param userPosition The {@link Position} of the requesting user * @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 * @param args Arguments specified by the random location request. These may be used to alter which locations are
* generated by the randomizer. * generated by the randomizer.
* @return a generated {@link Location} if one was found; an {@link Optional#empty()} otherwise * @return a generated {@link Location} if one was found; an {@link Optional#empty()} otherwise
* @since 3.0 * @since 3.0
*/ */
public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull Position userPosition, public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull OnlineUser onlineUser,
@NotNull String... args) { @NotNull String... args) {
final Position userPosition = onlineUser.getPosition();
return getRandomLocation(userPosition.world, args).thenApply(location -> { return getRandomLocation(userPosition.world, args).thenApply(location -> {
if (location.isEmpty()) { if (location.isEmpty()) {
return Optional.empty(); return Optional.empty();