Prevent getSafeGroundLocation returning spots outside of the world border

This commit is contained in:
William committed 2022-08-29 11:55:43 +01:00
1 parent 4768793229
commit 70ed11fc65
2 files changed
+16 -7

No files matched your search

@@ -407,12 +407,21 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
public CompletableFuture<Optional<Location>> getSafeGroundLocation(@NotNull Location location) { public CompletableFuture<Optional<Location>> getSafeGroundLocation(@NotNull Location location) {
final CompletableFuture<Optional<Location>> future = new CompletableFuture<>(); final CompletableFuture<Optional<Location>> future = new CompletableFuture<>();
Bukkit.getScheduler().runTask(getInstance(), () -> BukkitAdapter.adaptLocation(location).ifPresentOrElse( Bukkit.getScheduler().runTask(getInstance(), () -> BukkitAdapter.adaptLocation(location).ifPresentOrElse(
bukkitLocation -> PaperLib.getChunkAtAsync(bukkitLocation).thenAccept(chunk -> bukkitLocation -> {
future.complete(BukkitSafetyUtil.findSafeLocation(location.world, bukkitLocation, // Validate the location is within the world's border
chunk.getChunkSnapshot()))).exceptionally(throwable -> { assert bukkitLocation.getWorld() != null;
throwable.printStackTrace(); if (!bukkitLocation.getWorld().getWorldBorder().isInside(bukkitLocation)) {
return null; 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()))); () -> future.complete(Optional.empty())));
return future; return future;
} }
@@ -19,7 +19,7 @@ public class BukkitSafetyUtil {
* @return The safe location, if found * @return The safe location, if found
*/ */
public static Optional<Location> findSafeLocation(@NotNull World world, @NotNull org.bukkit.Location bukkitLocation, public static Optional<Location> findSafeLocation(@NotNull World world, @NotNull org.bukkit.Location bukkitLocation,
@NotNull ChunkSnapshot chunkSnapshot) { @NotNull ChunkSnapshot chunkSnapshot) {
for (int i = -1; i <= 1; i++) { for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) { for (int j = -1; j <= 1; j++) {
int chunkX = Math.abs(bukkitLocation.getBlockX() % 16) + i; int chunkX = Math.abs(bukkitLocation.getBlockX() % 16) + i;