Random teleport: return a future to support x-server pos retrieval

This commit is contained in:
William committed 2023-03-24 01:24:49 +00:00
1 parent 88f34ac133
commit f98510b184
4 files changed
+14 -8

No files matched your search

@@ -551,9 +551,9 @@ public abstract class BaseHuskHomesAPI {
* @since 3.0
*/
public final void randomlyTeleportPlayer(@NotNull OnlineUser user, boolean timedTeleport, @NotNull String... rtpArgs) {
plugin.runAsync(() -> {
final Optional<Position> position = plugin.getRandomTeleportEngine()
.getRandomPosition(user.getPosition().getWorld(), rtpArgs);
plugin.getRandomTeleportEngine()
.getRandomPosition(user.getPosition().getWorld(), rtpArgs)
.thenAccept(position -> {
if (position.isEmpty()) {
throw new IllegalStateException("Random teleport engine returned an empty position");
}
@@ -570,6 +570,8 @@ public abstract class BaseHuskHomesAPI {
} catch (TeleportationException e) {
e.displayMessage(user, plugin, rtpArgs);
}
}).exceptionally(e -> {
throw new IllegalStateException("Random teleport engine threw an exception", e);
});
}
@@ -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,8 +78,9 @@ public class RtpCommand extends Command {
// Generate a random position
plugin.getLocales().getLocale("teleporting_random_generation")
.ifPresent(teleporter::sendMessage);
final Optional<Position> position = plugin.getRandomTeleportEngine()
.getRandomPosition(teleporter.getPosition().getWorld(), args.length > 1 ? removeFirstArg(args) : args);
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);
@@ -103,6 +103,7 @@ public class RtpCommand extends Command {
user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES));
plugin.getDatabase().updateUserData(user);
});
}
}
@@ -90,7 +90,8 @@ public final class NormalDistributionEngine extends RandomTeleportEngine {
}
@Override
public Optional<Position> getRandomPosition(@NotNull World world, @NotNull String[] args) {
public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull World world, @NotNull String[] args) {
return plugin.supplyAsync(() -> {
Optional<Location> location = generateSafeLocation(world).join();
int attempts = 0;
while (location.isEmpty()) {
@@ -101,5 +102,6 @@ public final class NormalDistributionEngine extends RandomTeleportEngine {
attempts++;
}
return location.map(resolved -> Position.at(resolved, plugin.getServerName()));
});
}
}
@@ -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<Position> getRandomPosition(@NotNull World world, @NotNull String[] args);
public abstract CompletableFuture<Optional<Position>> getRandomPosition(@NotNull World world, @NotNull String[] args);
}