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

@@ -551,26 +551,28 @@ public abstract class BaseHuskHomesAPI {
* @since 3.0 * @since 3.0
*/ */
public final void randomlyTeleportPlayer(@NotNull OnlineUser user, boolean timedTeleport, @NotNull String... rtpArgs) { public final void randomlyTeleportPlayer(@NotNull OnlineUser user, boolean timedTeleport, @NotNull String... rtpArgs) {
plugin.runAsync(() -> { plugin.getRandomTeleportEngine()
final Optional<Position> position = plugin.getRandomTeleportEngine() .getRandomPosition(user.getPosition().getWorld(), rtpArgs)
.getRandomPosition(user.getPosition().getWorld(), rtpArgs); .thenAccept(position -> {
if (position.isEmpty()) { if (position.isEmpty()) {
throw new IllegalStateException("Random teleport engine returned an empty position"); throw new IllegalStateException("Random teleport engine returned an empty position");
} }
final TeleportBuilder builder = Teleport.builder(plugin) final TeleportBuilder builder = Teleport.builder(plugin)
.teleporter(user) .teleporter(user)
.target(position.get()); .target(position.get());
try { try {
if (timedTeleport) { if (timedTeleport) {
builder.toTimedTeleport().execute(); builder.toTimedTeleport().execute();
} else { } else {
builder.toTeleport().execute(); builder.toTeleport().execute();
} }
} catch (TeleportationException e) { } catch (TeleportationException e) {
e.displayMessage(user, plugin, rtpArgs); 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.HuskHomes;
import net.william278.huskhomes.hook.EconomyHook; import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TeleportBuilder; import net.william278.huskhomes.teleport.TeleportBuilder;
import net.william278.huskhomes.teleport.TeleportationException; import net.william278.huskhomes.teleport.TeleportationException;
@@ -79,30 +78,32 @@ public class RtpCommand extends Command {
// Generate a random position // Generate a random position
plugin.getLocales().getLocale("teleporting_random_generation") plugin.getLocales().getLocale("teleporting_random_generation")
.ifPresent(teleporter::sendMessage); .ifPresent(teleporter::sendMessage);
final Optional<Position> position = plugin.getRandomTeleportEngine() plugin.getRandomTeleportEngine()
.getRandomPosition(teleporter.getPosition().getWorld(), args.length > 1 ? removeFirstArg(args) : args); .getRandomPosition(teleporter.getPosition().getWorld(), args.length > 1 ? removeFirstArg(args) : args)
if (position.isEmpty()) { .thenAccept(position -> {
plugin.getLocales().getLocale("error_rtp_randomization_timeout") if (position.isEmpty()) {
.ifPresent(executor::sendMessage); plugin.getLocales().getLocale("error_rtp_randomization_timeout")
return; .ifPresent(executor::sendMessage);
} return;
}
final TeleportBuilder builder = Teleport.builder(plugin) final TeleportBuilder builder = Teleport.builder(plugin)
.teleporter(teleporter) .teleporter(teleporter)
.target(position.get()); .target(position.get());
try { try {
if (executor.equals(teleporter)) { if (executor.equals(teleporter)) {
builder.toTimedTeleport().execute(); builder.toTimedTeleport().execute();
} else { } else {
builder.toTeleport().execute(); builder.toTeleport().execute();
} }
} catch (TeleportationException e) { } catch (TeleportationException e) {
e.displayMessage(executor, plugin, args); e.displayMessage(executor, plugin, args);
return; return;
} }
user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES)); user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES));
plugin.getDatabase().updateUserData(user); plugin.getDatabase().updateUserData(user);
});
} }
} }
@@ -37,7 +37,7 @@ public final class NormalDistributionEngine extends RandomTeleportEngine {
*/ */
@NotNull @NotNull
public static Location generateLocation(@NotNull Location origin, float mean, float standardDeviation, public static Location generateLocation(@NotNull Location origin, float mean, float standardDeviation,
float spawnRadius, float maxRadius) { float spawnRadius, float maxRadius) {
// Generate random values // Generate random values
final float radius = getDistributedRadius(mean, standardDeviation, spawnRadius, maxRadius); final float radius = getDistributedRadius(mean, standardDeviation, spawnRadius, maxRadius);
final float angle = getRandomAngle(); final float angle = getRandomAngle();
@@ -90,16 +90,18 @@ public final class NormalDistributionEngine extends RandomTeleportEngine {
} }
@Override @Override
public Optional<Position> getRandomPosition(@NotNull World world, @NotNull String[] args) { public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull World world, @NotNull String[] args) {
Optional<Location> location = generateSafeLocation(world).join(); return plugin.supplyAsync(() -> {
int attempts = 0; Optional<Location> location = generateSafeLocation(world).join();
while (location.isEmpty()) { int attempts = 0;
location = generateSafeLocation(world).join(); while (location.isEmpty()) {
if (attempts >= maxAttempts) { location = generateSafeLocation(world).join();
return Optional.empty(); 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 org.jetbrains.annotations.NotNull;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CompletableFuture;
/** /**
* Represents an engine for generating random position targets * 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 * @return The position, optionally, which will be empty if the random teleport engine timed out after a
* {@link #maxAttempts configured number of attempts} * {@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);
} }