Ensure safety resolver returns y within height limits, close #401

This commit is contained in:
William278 committed 2023-05-22 12:37:07 +01:00
1 parent cb9d4808f2
commit fa40494221
2 files changed
+10 -5

No files matched your search

@@ -46,7 +46,11 @@ public interface BukkitSafetyResolver extends SafetyResolver {
// Search nearby blocks for a safe location // Search nearby blocks for a safe location
return PaperLib.getChunkAtAsync(bukkitLocation) return PaperLib.getChunkAtAsync(bukkitLocation)
.thenApply(Chunk::getChunkSnapshot) .thenApply(Chunk::getChunkSnapshot)
.thenApply(snapshot -> findSafeLocationNear(location, snapshot)); .thenApply(snapshot -> findSafeLocationNear(
location,
snapshot,
bukkitLocation.getWorld().getMinHeight()
));
} }
/** /**
@@ -54,9 +58,10 @@ public interface BukkitSafetyResolver extends SafetyResolver {
* *
* @param location The location to search around * @param location The location to search around
* @param snapshot The chunk snapshot to search * @param snapshot The chunk snapshot to search
* @param minY The minimum Y value of the world
* @return An optional safe location, within 4 blocks of the given location * @return An optional safe location, within 4 blocks of the given location
*/ */
private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ChunkSnapshot snapshot) { private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ChunkSnapshot snapshot, int minY) {
final int chunkX = ((int) location.getX()) & 0xF; final int chunkX = ((int) location.getX()) & 0xF;
final int chunkZ = ((int) location.getZ()) & 0xF; final int chunkZ = ((int) location.getZ()) & 0xF;
@@ -67,12 +72,12 @@ public interface BukkitSafetyResolver extends SafetyResolver {
if (x < 0 || x >= 16 || z < 0 || z >= 16) { if (x < 0 || x >= 16 || z < 0 || z >= 16) {
continue; continue;
} }
final int y = snapshot.getHighestBlockYAt(x, z); final int y = Math.max((minY + 1), snapshot.getHighestBlockYAt(x, z));
final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ); final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ);
if (isBlockSafe(blockType.getKey().toString())) { if (isBlockSafe(blockType.getKey().toString())) {
return Optional.of(Location.at( return Optional.of(Location.at(
(location.getX() + dX) + 0.5d, (location.getX() + dX) + 0.5d,
y + 1.25d, y + 1,
(location.getZ() + dZ) + 0.5d, (location.getZ() + dZ) + 0.5d,
location.getWorld() location.getWorld()
)); ));
@@ -65,7 +65,7 @@ public interface FabricSafetyResolver extends SafetyResolver {
* @param location The location to search around * @param location The location to search around
* @return An optional safe location, within 4 blocks of the given location * @return An optional safe location, within 4 blocks of the given location
*/ */
private Optional<Location> findSafeLocationNear(Location location, ServerWorld world) { private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ServerWorld world) {
final BlockPos.Mutable blockPos = new BlockPos.Mutable(location.getX(), location.getY(), location.getZ()); final BlockPos.Mutable blockPos = new BlockPos.Mutable(location.getX(), location.getY(), location.getZ());
for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) { for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) { for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) {