mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
/rtp API tweaks, add timeout failsafe, add teleporting_random_generation locale
This commit is contained in:
1 parent
a0042ac4c8
commit
6efefb175f
13 files changed
+42
-9
No files matched your search
@@ -70,6 +70,8 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
|
||||
}
|
||||
|
||||
// Get a random position and teleport
|
||||
plugin.getLocales().getLocale("teleporting_random_generation")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
plugin.getRandomTeleportEngine().getRandomPosition(onlineUser.getPosition().world, rtpArguments).thenAccept(position -> {
|
||||
if (position.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
|
||||
|
||||
@@ -85,16 +85,15 @@ public class NormalDistributionEngine extends RandomTeleportEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull World world, @NotNull String[] args) {
|
||||
protected Optional<Position> generatePosition(@NotNull World world, @NotNull String[] args) {
|
||||
Optional<Location> location = generateSafeLocation(world).join();
|
||||
int attempts = 0;
|
||||
while (location.isEmpty()) {
|
||||
location = generateSafeLocation(world).join();
|
||||
if (attempts > randomTimeout) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
return CompletableFuture.completedFuture(location.map(resolved ->
|
||||
new Position(resolved, plugin.getPluginServer())));
|
||||
return location.map(resolved -> new Position(resolved, plugin.getPluginServer()));
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,11 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Represents an engine for generating random position targets
|
||||
*/
|
||||
public abstract class RandomTeleportEngine {
|
||||
|
||||
@NotNull
|
||||
@@ -24,6 +28,12 @@ public abstract class RandomTeleportEngine {
|
||||
*/
|
||||
public long randomTimeout = 8;
|
||||
|
||||
/**
|
||||
* Constructor for a random teleport engine
|
||||
*
|
||||
* @param plugin The HuskHomes plugin instance
|
||||
* @param name The name of the implementing random teleport engine
|
||||
*/
|
||||
protected RandomTeleportEngine(@NotNull HuskHomes plugin, @NotNull String name) {
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
@@ -46,6 +56,18 @@ public abstract class RandomTeleportEngine {
|
||||
world, plugin.getPluginServer()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a random position in the {@link World}, or {@link Optional#empty()} if no position could be found in
|
||||
* the configured number of attempts
|
||||
*
|
||||
* @param world The world to find a random position in
|
||||
* @param args The arguments to pass to the random teleport engine
|
||||
* @return The position, optionally, which will be empty if the random teleport engine timed out after a
|
||||
* {@link #randomTimeout configured number of attempts}
|
||||
* @implNote This is run asynchronously (i.e. not on the main server thread)
|
||||
*/
|
||||
protected abstract Optional<Position> generatePosition(@NotNull World world, @NotNull String[] args);
|
||||
|
||||
/**
|
||||
* Gets a random position in the {@link World}, supplying a future to be completed with the optional position
|
||||
* is found, or empty if the operation times out after a number of attempts.
|
||||
@@ -53,8 +75,12 @@ public abstract class RandomTeleportEngine {
|
||||
* @param world The world to find a random position in
|
||||
* @param args The arguments to pass to the random teleport engine
|
||||
* @return A {@link CompletableFuture} containing the random position, if one is found in
|
||||
* {@link #randomTimeout configured number of attempts}
|
||||
* {@link #randomTimeout configured number of attempts}, or if the operation times out after 10 seconds
|
||||
*/
|
||||
public abstract CompletableFuture<Optional<Position>> getRandomPosition(@NotNull World world, @NotNull String[] args);
|
||||
public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull World world, @NotNull String[] args) {
|
||||
return CompletableFuture.supplyAsync(() -> generatePosition(world, args))
|
||||
.orTimeout(10, TimeUnit.SECONDS)
|
||||
.exceptionally(e -> Optional.empty());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user