diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 7ef1af41..cc222c27 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -441,10 +441,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { final int y = snapshot.getHighestBlockYAt(x, z); final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ); if (!isBlockUnsafe(blockType.getKey().toString())) { - Bukkit.broadcastMessage("✔ Safe block: " + blockType.getKey()); return new Location((location.x + dX) + 0.5d, y + 1.25d, (location.z + dZ) + 0.5d, location.world); - } else { - Bukkit.broadcastMessage("❌ Unsafe block: " + blockType.getKey()); } } } diff --git a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java index f61f1edf..f6076ce5 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java @@ -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") diff --git a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java index c85cfe7d..400d714f 100644 --- a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java @@ -85,16 +85,15 @@ public class NormalDistributionEngine extends RandomTeleportEngine { } @Override - public CompletableFuture> getRandomPosition(@NotNull World world, @NotNull String[] args) { + protected Optional generatePosition(@NotNull World world, @NotNull String[] args) { Optional 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())); } } diff --git a/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java index cf2165a8..7ddf67f3 100644 --- a/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java @@ -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 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> getRandomPosition(@NotNull World world, @NotNull String[] args); + public CompletableFuture> getRandomPosition(@NotNull World world, @NotNull String[] args) { + return CompletableFuture.supplyAsync(() -> generatePosition(world, args)) + .orTimeout(10, TimeUnit.SECONDS) + .exceptionally(e -> Optional.empty()); + } } diff --git a/common/src/main/resources/locales/bg-bg.yml b/common/src/main/resources/locales/bg-bg.yml index 3d4532e4..b51f771c 100644 --- a/common/src/main/resources/locales/bg-bg.yml +++ b/common/src/main/resources/locales/bg-bg.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[Телепортацията отказан teleporting_other_complete: '[Teleported %1% to %2%.](#00fb9a)' teleporting_other_complete_position: '[Teleported %1% to ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[Teleporting](#00fb9a) [%1%](#00fb9a bold) [player(s) to your position…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[You sent a teleport request asking to teleport to](#00fb9a) [%1%](#00fb9a bold)[.](#00fb9a)' tpahere_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [to teleport to you.](#00fb9a)' tpaall_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [player(s) to teleport to you.](#00fb9a)' diff --git a/common/src/main/resources/locales/de-de.yml b/common/src/main/resources/locales/de-de.yml index 768babd4..98b1fae9 100644 --- a/common/src/main/resources/locales/de-de.yml +++ b/common/src/main/resources/locales/de-de.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[Teleportation abgebrochen!](#ff7e5e)' teleporting_other_complete: '[%1% wurde zu %2% teleportiert.](#00fb9a)' teleporting_other_complete_position: '[%1% wurde zu ⚐ x: %2%, y: %3%, z: %4% teleportiert.](#00fb9a)' teleporting_all_players: '[Teleportiere](#00fb9a) [%1%](#00fb9a bold) [Spieler zu deiner Position…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[Du hast](#00fb9a) [%1%](#00fb9a bold) [eine Teleportanfrage gesendet.](#00fb9a)' tpahere_request_sent: '[Du hast eine Teleportanfrage gestellt und](#00fb9a) [%1%](#00fb9a bold) [gebeten sich zu dir zu teleportieren.](#00fb9a)' tpaall_request_sent: '[Du hast eine Teleportanfrage gestellt und](#00fb9a) [%1%](#00fb9a bold) [Spieler gebeten sich zu dir zu teleportieren.](#00fb9a)' diff --git a/common/src/main/resources/locales/en-gb.yml b/common/src/main/resources/locales/en-gb.yml index 38ff371e..6dafea43 100644 --- a/common/src/main/resources/locales/en-gb.yml +++ b/common/src/main/resources/locales/en-gb.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[Teleportation cancelled!](#ff7e5e)' teleporting_other_complete: '[Teleported %1% to %2%.](#00fb9a)' teleporting_other_complete_position: '[Teleported %1% to ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[Teleporting](#00fb9a) [%1%](#00fb9a bold) [player(s) to your position…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[You sent a teleport request asking to teleport to](#00fb9a) [%1%](#00fb9a bold)[.](#00fb9a)' tpahere_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [to teleport to you.](#00fb9a)' tpaall_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [player(s) to teleport to you.](#00fb9a)' diff --git a/common/src/main/resources/locales/es-es.yml b/common/src/main/resources/locales/es-es.yml index 091db469..4dee5246 100644 --- a/common/src/main/resources/locales/es-es.yml +++ b/common/src/main/resources/locales/es-es.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[¡Teleportación cancelada!](#ff7e5e)' teleporting_other_complete: '[Teletransportado %1% a %2%.](#00fb9a)' teleporting_other_complete_position: '[Teletransportado %1% a ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[Teletransportando a ](#00fb9a) [%1%](#00fb9a bold) [jugador(es) a su posición...](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[Has enviado una solicitud de teletransporte a](#00fb9a) [%1%](#00fb9a bold)[.](#00fb9a)' tpahere_request_sent: '[Has enviado una solicitud de teletransporte pidiendo a](#00fb9a) [%1%](#00fb9a bold) [que se teletransporte hacia ti.](#00fb9a)' tpaall_request_sent: '[Has enviado una solicitud de teletransporte a](#00fb9a) [%1%](#00fb9a bold) [jugador(es) para teletransportarse hacia ti.](#00fb9a)' diff --git a/common/src/main/resources/locales/it-it.yml b/common/src/main/resources/locales/it-it.yml index 5e48dac3..8a90b81a 100644 --- a/common/src/main/resources/locales/it-it.yml +++ b/common/src/main/resources/locales/it-it.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[Teletrasporto cancellato!](#ff7e5e)' teleporting_other_complete: '[Hai teletrasportato %1% da %2%.](#00fb9a)' teleporting_other_complete_position: '[Hai telerasportato %1% a ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[Stai teletrasportando](#00fb9a) [%1%](#00fb9a bold) [giocatore(i) nella tua posizione…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[Hai richiesto di teletrasportarti da](#00fb9a) [%1%](#00fb9a bold)[.](#00fb9a)' tpahere_request_sent: '[Hai richiesto il teletrasporto a](#00fb9a) [%1%](#00fb9a bold) [ verso la tua posizione.](#00fb9a)' tpaall_request_sent: '[Hai chiesto a](#00fb9a) [%1%](#00fb9a bold) [ giocatore(i) di teletrasportarsi da te.](#00fb9a)' diff --git a/common/src/main/resources/locales/pl.yml b/common/src/main/resources/locales/pl.yml index a2e31f72..486ab122 100644 --- a/common/src/main/resources/locales/pl.yml +++ b/common/src/main/resources/locales/pl.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[Teleportacja anulowana!](#ff7e5e)' teleporting_other_complete: '[Teleported %1% to %2%.](#00fb9a)' teleporting_other_complete_position: '[Teleported %1% to ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[Teleporting](#00fb9a) [%1%](#00fb9a bold) [player(s) to your position…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[You sent a teleport request asking to teleport to](#00fb9a) [%1%](#00fb9a bold)[.](#00fb9a)' tpahere_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [to teleport to you.](#00fb9a)' tpaall_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [player(s) to teleport to you.](#00fb9a)' diff --git a/common/src/main/resources/locales/uk-ua.yml b/common/src/main/resources/locales/uk-ua.yml index 15f1fe62..27d19548 100644 --- a/common/src/main/resources/locales/uk-ua.yml +++ b/common/src/main/resources/locales/uk-ua.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[Телепортацію скасовано! teleporting_other_complete: '[Teleported %1% to %2%.](#00fb9a)' teleporting_other_complete_position: '[Teleported %1% to ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[Teleporting](#00fb9a) [%1%](#00fb9a bold) [player(s) to your position…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[You sent a teleport request asking to teleport to](#00fb9a) [%1%](#00fb9a bold)[.](#00fb9a)' tpahere_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [to teleport to you.](#00fb9a)' tpaall_request_sent: '[You sent a teleport request asking](#00fb9a) [%1%](#00fb9a bold) [player(s) to teleport to you.](#00fb9a)' diff --git a/common/src/main/resources/locales/zh-cn.yml b/common/src/main/resources/locales/zh-cn.yml index 545bc593..e8b7a789 100644 --- a/common/src/main/resources/locales/zh-cn.yml +++ b/common/src/main/resources/locales/zh-cn.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[传送取消!](#ff7e5e)' teleporting_other_complete: '[将 %1% 传送到 %2%.](#00fb9a)' teleporting_other_complete_position: '[将 %1% 传送到 ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[将](#00fb9a) [%1%](#00fb9a bold) [名玩家传送到您的位置…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[已发送传送请求, 传送到](#00fb9a) [%1%](#00fb9a bold)[.](#00fb9a)' tpahere_request_sent: '[已发送传送请求, 请求](#00fb9a) [%1%](#00fb9a bold) [传送过来.](#00fb9a)' tpaall_request_sent: '[已发送传送请求, 请求](#00fb9a) [%1%](#00fb9a bold) [传送过来.](#00fb9a)' diff --git a/common/src/main/resources/locales/zh-tw.yml b/common/src/main/resources/locales/zh-tw.yml index cb824e8f..8776da8e 100644 --- a/common/src/main/resources/locales/zh-tw.yml +++ b/common/src/main/resources/locales/zh-tw.yml @@ -9,6 +9,7 @@ teleporting_action_bar_cancelled: '[傳送取消!](#ff7e5e)' teleporting_other_complete: '[將 %1% 傳送到 %2%.](#00fb9a)' teleporting_other_complete_position: '[將 %1% 傳送到 ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' teleporting_all_players: '[將](#00fb9a) [%1%](#00fb9a bold) [名玩家傳送到您的位置…](#00fb9a)' +teleporting_random_generation: '[Finding a safe random position to teleport you to…](#00fb9a)' tpa_request_sent: '[您發送了一個傳送請求給](#00fb9a) [%1%](#00fb9a bold)' tpahere_request_sent: '[您發送了一個傳送請求, 請求](#00fb9a) [%1%](#00fb9a bold) [傳送過來.](#00fb9a)' tpaall_request_sent: '[你向所有玩家發送了一個傳送請求,要求](#00fb9a) [%1%](#00fb9a bold) [個玩家傳送到你身邊!.](#00fb9a)'