Skip to content

Commit

Permalink
feat: SnapshotManager now has a SnapshotManager#getHighestBlockAt method
Browse files Browse the repository at this point in the history
  • Loading branch information
Metaphoriker committed Oct 7, 2024
1 parent f2a2207 commit b4c5788
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ public interface SnapshotManager {
* @return {@link PathBlock} the block.
*/
PathBlock getBlock(PathPosition position);

/**
* Gets the highest block at the given position
*
* @api.Note If the pathfinder is not permitted to load chunks, this method will return null if
* the chunk is not loaded.
* @param position the position to get as block-form
* @return {@link PathBlock} the block.
*/
PathBlock getHighestBlockAt(PathPosition position);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.patheloper.model.snapshot;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
Expand All @@ -20,8 +21,6 @@
import org.patheloper.util.ChunkUtils;
import org.patheloper.util.ErrorLogger;

import java.util.concurrent.ConcurrentHashMap;

/**
* The FailingSnapshotManager class implements the SnapshotManager interface and provides a default
* implementation for retrieving block data snapshots from a Minecraft world. It utilizes chunk
Expand Down Expand Up @@ -123,6 +122,36 @@ public PathBlock getBlock(@NonNull PathPosition position) {
return block.orElse(null);
}

@Override
public PathBlock getHighestBlockAt(PathPosition position) {
Optional<ChunkSnapshot> chunkSnapshotOptional = getChunkSnapshot(position);

if (chunkSnapshotOptional.isPresent()) {
ChunkSnapshot chunkSnapshot = chunkSnapshotOptional.get();

int chunkX = position.getBlockX() & 0xF;
int chunkZ = position.getBlockZ() & 0xF;

for (int y = chunkSnapshot.getHighestBlockYAt(chunkX, chunkZ); y >= 0; y--) {
Material material = chunkSnapshot.getBlockType(chunkX, y, chunkZ);

if (!material.isAir() && material.isSolid()) {
PathPosition highestBlockPosition =
new PathPosition(
position.getPathEnvironment(), position.getBlockX(), y, position.getBlockZ());
BlockState blockState =
CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
.getBlockState(chunkSnapshot, chunkX, y, chunkZ);
return new PathBlock(highestBlockPosition, new BlockInformation(material, blockState));
}
}
}

// If no solid block was found or the chunk snapshot wasn't present
return null;
}

/**
* The RequestingSnapshotManager is an inner class of FailingSnapshotManager, extending it. This
* class provides additional functionality for ensuring that block data snapshots are available,
Expand Down Expand Up @@ -178,5 +207,41 @@ public PathBlock getBlock(@NonNull PathPosition position) {
PathBlock block = super.getBlock(position);
return block == null ? ensureBlock(position) : block;
}

@Override
public PathBlock getHighestBlockAt(@NonNull PathPosition position) {
PathBlock block = super.getHighestBlockAt(position);
return block == null ? ensureHighestBlock(position) : block;
}

private PathBlock ensureHighestBlock(PathPosition pathPosition) {
ChunkSnapshot chunkSnapshot = retrieveSnapshot(pathPosition);

int chunkX = pathPosition.getBlockX() & 0xF;
int chunkZ = pathPosition.getBlockZ() & 0xF;

for (int y = chunkSnapshot.getHighestBlockYAt(chunkX, chunkZ); y >= 0; y--) {
Material material = ChunkUtils.getMaterial(chunkSnapshot, chunkX, y, chunkZ);

if (!material.isAir() && material.isSolid()) {
BlockState blockState =
CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
.getBlockState(chunkSnapshot, chunkX, y, chunkZ);

PathPosition highestBlockPosition =
new PathPosition(
pathPosition.getPathEnvironment(),
pathPosition.getBlockX(),
y,
pathPosition.getBlockZ());

return new PathBlock(highestBlockPosition, new BlockInformation(material, blockState));
}
}

// If no solid block is found
return null;
}
}
}

0 comments on commit b4c5788

Please sign in to comment.