From b63b085dc929651928d6920526b1b43edf250d7f Mon Sep 17 00:00:00 2001 From: William278 Date: Tue, 6 Sep 2022 14:59:51 +0300 Subject: [PATCH] Check `y` value on `#isSafePosition`; prevent IllegalArgumentException (Close #202) --- .../huskhomes/util/BukkitSafetyUtil.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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 203d3d44..5c19a28c 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyUtil.java +++ b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyUtil.java @@ -20,6 +20,13 @@ public class BukkitSafetyUtil { */ public static Optional findSafeLocation(@NotNull World world, @NotNull org.bukkit.Location bukkitLocation, @NotNull ChunkSnapshot chunkSnapshot) { + // Validate world + final org.bukkit.World bukkitWorld = bukkitLocation.getWorld(); + if (bukkitWorld == null) { + return Optional.empty(); + } + + // Search nearby blocks for a safe, valid position for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int chunkX = Math.abs(bukkitLocation.getBlockX() % 16) + i; @@ -28,7 +35,7 @@ public class BukkitSafetyUtil { continue; } int chunkY = chunkSnapshot.getHighestBlockYAt(chunkX, chunkZ); - if (isSafePosition(chunkSnapshot, chunkX, chunkY, chunkZ)) { + if (isSafePosition(chunkSnapshot, chunkX, chunkY, chunkZ, bukkitWorld)) { return Optional.of(new Location(bukkitLocation.getBlockX() + i, chunkY + 1.5d, bukkitLocation.getBlockZ() + j, 90, 0, world)); @@ -45,9 +52,18 @@ public class BukkitSafetyUtil { * @param chunkX The chunk x coordinate to search at * @param chunkY The chunk y coordinate to search at * @param chunkZ The chunk z coordinate to search at + * @param world The world to search in * @return True if the block is safe, false otherwise */ - public static boolean isSafePosition(@NotNull ChunkSnapshot chunkSnapshot, final int chunkX, final int chunkY, final int chunkZ) { + public static boolean isSafePosition(@NotNull ChunkSnapshot chunkSnapshot, + final int chunkX, final int chunkY, final int chunkZ, + @NotNull org.bukkit.World world) { + // Check validity of the y-coordinate + if (chunkY < world.getMinHeight() || chunkY > world.getMaxHeight()) { + return false; + } + + // Check block safety final Material blockType = chunkSnapshot.getBlockType(chunkX, chunkY, chunkZ); return switch (blockType) { // Special case handling for safe, un-solid blocks