diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 2bf2d44c..9ea934af 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -407,12 +407,21 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { public CompletableFuture> getSafeGroundLocation(@NotNull Location location) { final CompletableFuture> future = new CompletableFuture<>(); Bukkit.getScheduler().runTask(getInstance(), () -> BukkitAdapter.adaptLocation(location).ifPresentOrElse( - bukkitLocation -> PaperLib.getChunkAtAsync(bukkitLocation).thenAccept(chunk -> - future.complete(BukkitSafetyUtil.findSafeLocation(location.world, bukkitLocation, - chunk.getChunkSnapshot()))).exceptionally(throwable -> { - throwable.printStackTrace(); - return null; - }), + bukkitLocation -> { + // Validate the location is within the world's border + assert bukkitLocation.getWorld() != null; + if (!bukkitLocation.getWorld().getWorldBorder().isInside(bukkitLocation)) { + future.complete(Optional.empty()); + } + + // Find a safe position and return + PaperLib.getChunkAtAsync(bukkitLocation).thenAccept(chunk -> + future.complete(BukkitSafetyUtil.findSafeLocation(location.world, bukkitLocation, + chunk.getChunkSnapshot()))).exceptionally(throwable -> { + throwable.printStackTrace(); + return null; + }); + }, () -> future.complete(Optional.empty()))); return future; } diff --git a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyUtil.java b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyUtil.java index cc0f791e..203d3d44 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyUtil.java +++ b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyUtil.java @@ -19,7 +19,7 @@ public class BukkitSafetyUtil { * @return The safe location, if found */ public static Optional findSafeLocation(@NotNull World world, @NotNull org.bukkit.Location bukkitLocation, - @NotNull ChunkSnapshot chunkSnapshot) { + @NotNull ChunkSnapshot chunkSnapshot) { for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int chunkX = Math.abs(bukkitLocation.getBlockX() % 16) + i;