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
+60 -54

No files matched your search

@@ -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<Position> getRandomPosition(@NotNull World world, @NotNull String[] args) {
Optional<Location> location = generateSafeLocation(world).join();
int attempts = 0;
while (location.isEmpty()) {
location = generateSafeLocation(world).join();
if (attempts >= maxAttempts) {
return Optional.empty();
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()) {
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()));
});
}
}
@@ -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);
}