mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Random teleport: return a future to support x-server pos retrieval
This commit is contained in:
4 files changed
+60
-54
No files matched your search
@@ -551,26 +551,28 @@ 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);
|
||||
if (position.isEmpty()) {
|
||||
throw new IllegalStateException("Random teleport engine returned an empty position");
|
||||
}
|
||||
plugin.getRandomTeleportEngine()
|
||||
.getRandomPosition(user.getPosition().getWorld(), rtpArgs)
|
||||
.thenAccept(position -> {
|
||||
if (position.isEmpty()) {
|
||||
throw new IllegalStateException("Random teleport engine returned an empty position");
|
||||
}
|
||||
|
||||
final TeleportBuilder builder = Teleport.builder(plugin)
|
||||
.teleporter(user)
|
||||
.target(position.get());
|
||||
try {
|
||||
if (timedTeleport) {
|
||||
builder.toTimedTeleport().execute();
|
||||
} else {
|
||||
builder.toTeleport().execute();
|
||||
}
|
||||
} catch (TeleportationException e) {
|
||||
e.displayMessage(user, plugin, rtpArgs);
|
||||
}
|
||||
});
|
||||
final TeleportBuilder builder = Teleport.builder(plugin)
|
||||
.teleporter(user)
|
||||
.target(position.get());
|
||||
try {
|
||||
if (timedTeleport) {
|
||||
builder.toTimedTeleport().execute();
|
||||
} else {
|
||||
builder.toTeleport().execute();
|
||||
}
|
||||
} 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,30 +78,32 @@ 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);
|
||||
if (position.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
final TeleportBuilder builder = Teleport.builder(plugin)
|
||||
.teleporter(teleporter)
|
||||
.target(position.get());
|
||||
try {
|
||||
if (executor.equals(teleporter)) {
|
||||
builder.toTimedTeleport().execute();
|
||||
} else {
|
||||
builder.toTeleport().execute();
|
||||
}
|
||||
} catch (TeleportationException e) {
|
||||
e.displayMessage(executor, plugin, args);
|
||||
return;
|
||||
}
|
||||
final TeleportBuilder builder = Teleport.builder(plugin)
|
||||
.teleporter(teleporter)
|
||||
.target(position.get());
|
||||
try {
|
||||
if (executor.equals(teleporter)) {
|
||||
builder.toTimedTeleport().execute();
|
||||
} else {
|
||||
builder.toTeleport().execute();
|
||||
}
|
||||
} catch (TeleportationException e) {
|
||||
e.displayMessage(executor, plugin, args);
|
||||
return;
|
||||
}
|
||||
|
||||
user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES));
|
||||
plugin.getDatabase().updateUserData(user);
|
||||
user.setRtpCooldown(Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES));
|
||||
plugin.getDatabase().updateUserData(user);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+13
-11
@@ -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);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user