Skip to content

Commit

Permalink
refact: SnapshotManager is now BlockProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Metaphoriker committed Dec 15, 2024
1 parent 33c3066 commit 5398136
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.metaphoriker.pathetic.api.pathing.filter;

import lombok.Value;
import de.metaphoriker.pathetic.api.snapshot.SnapshotManager;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.PathPosition;

/**
Expand Down Expand Up @@ -40,9 +40,9 @@ public class PathValidationContext {
PathPosition absoluteTarget;

/**
* The snapshot manager provides access to world data, such as block information, in the context
* The BlockProvider provides access to world data, such as block information, in the context
* of the pathfinding process. It is used to retrieve block data from the world at different
* positions during path validation.
*/
SnapshotManager snapshotManager;
BlockProvider blockProvider;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PassablePathFilter implements PathFilter {
@Override
public boolean filter(@NonNull PathValidationContext pathValidationContext) {
return pathValidationContext
.getSnapshotManager()
.getBlockProvider()
.getBlock(pathValidationContext.getPosition())
.isPassable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.NonNull;
import de.metaphoriker.pathetic.api.pathing.filter.PathValidationContext;
import de.metaphoriker.pathetic.api.pathing.filter.PathFilter;
import de.metaphoriker.pathetic.api.snapshot.SnapshotManager;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.PathBlock;

/**
Expand All @@ -13,13 +13,13 @@ public class SolidGroundPathFilter implements PathFilter {

@Override
public boolean filter(@NonNull PathValidationContext pathValidationContext) {
SnapshotManager snapshotManager = pathValidationContext.getSnapshotManager();
PathBlock block = snapshotManager.getBlock(pathValidationContext.getPosition());
return hasGround(block, snapshotManager);
BlockProvider blockProvider = pathValidationContext.getBlockProvider();
PathBlock block = blockProvider.getBlock(pathValidationContext.getPosition());
return hasGround(block, blockProvider);
}

protected boolean hasGround(PathBlock block, SnapshotManager snapshotManager) {
PathBlock below = snapshotManager.getBlock(block.getPathPosition().add(0, -1, 0));
protected boolean hasGround(PathBlock block, BlockProvider blockProvider) {
PathBlock below = blockProvider.getBlock(block.getPathPosition().add(0, -1, 0));
return below.isSolid();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import org.bukkit.Material;
import de.metaphoriker.pathetic.api.pathing.filter.PathValidationContext;
import de.metaphoriker.pathetic.api.pathing.filter.PathFilter;
import de.metaphoriker.pathetic.api.snapshot.SnapshotManager;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.PathPosition;

/** A PathFilter implementation that determines if a path is through water. */
public class WaterPathFilter implements PathFilter {

@Override
public boolean filter(@NonNull PathValidationContext pathValidationContext) {
SnapshotManager snapshotManager = pathValidationContext.getSnapshotManager();
BlockProvider blockProvider = pathValidationContext.getBlockProvider();
PathPosition pathPosition = pathValidationContext.getPosition();

return snapshotManager.getBlock(pathPosition).getBlockInformation().getMaterial()
return blockProvider.getBlock(pathPosition).getBlockInformation().getMaterial()
== Material.WATER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import de.metaphoriker.pathetic.api.wrapper.PathPosition;

/**
* The SnapshotManager interface defines methods for retrieving block data snapshots at specific
* The BlockProvider interface defines methods for retrieving block data snapshots at specific
* positions within a Minecraft world.
*/
public interface SnapshotManager {
public interface BlockProvider {

/**
* Gets the block at the given position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.LeavesDecayEvent;
import de.metaphoriker.pathetic.model.snapshot.FailingSnapshotManager;
import de.metaphoriker.pathetic.model.snapshot.FailingBlockProvider;

public class ChunkInvalidateListener implements Listener {

Expand Down Expand Up @@ -69,7 +69,7 @@ public void onDecay(LeavesDecayEvent event) {

private void handleEvent(Block... blocks) {
for (Block block : blocks)
FailingSnapshotManager.invalidateChunk(
FailingBlockProvider.invalidateChunk(
block.getWorld().getUID(), block.getChunk().getX(), block.getChunk().getZ());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private boolean isHeightDifferencePassable(
int yDifference = from.getPosition().getBlockY() - to.getPosition().getBlockY();
Node neighbour3 = createNeighbourNode(from, vector1.add(new PathVector(0, yDifference, 0)));

return snapshotManager.getBlock(neighbour3.getPosition()).isPassable();
return blockProvider.getBlock(neighbour3.getPosition()).isPassable();
}

private Collection<Node> fetchValidNeighbours(
Expand Down Expand Up @@ -207,7 +207,7 @@ private boolean doAllFiltersPass(List<PathFilter> filters, Node node) {
node.getParent() != null ? node.getParent().getPosition() : null,
node.getStart(),
node.getTarget(),
snapshotManager);
blockProvider);

if (!filter.filter(context)) {
return false;
Expand All @@ -226,7 +226,7 @@ private boolean doAnyFilterStagePass(List<PathFilterStage> filterStages, Node no
node.getParent() != null ? node.getParent().getPosition() : null,
node.getStart(),
node.getTarget(),
snapshotManager))) {
blockProvider))) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import de.metaphoriker.pathetic.api.pathing.result.Path;
import de.metaphoriker.pathetic.api.pathing.result.PathState;
import de.metaphoriker.pathetic.api.pathing.result.PathfinderResult;
import de.metaphoriker.pathetic.api.snapshot.SnapshotManager;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.Depth;
import de.metaphoriker.pathetic.api.wrapper.PathPosition;
import de.metaphoriker.pathetic.model.pathing.Node;
import de.metaphoriker.pathetic.model.pathing.result.PathImpl;
import de.metaphoriker.pathetic.model.pathing.result.PathfinderResultImpl;
import de.metaphoriker.pathetic.model.snapshot.FailingSnapshotManager;
import de.metaphoriker.pathetic.model.snapshot.FailingBlockProvider;
import de.metaphoriker.pathetic.util.ErrorLogger;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -51,9 +51,9 @@ abstract class AbstractPathfinder implements Pathfinder {
protected static final Set<PathPosition> EMPTY_LINKED_HASHSET =
Collections.unmodifiableSet(new LinkedHashSet<>(0));

private static final SnapshotManager SIMPLE_SNAPSHOT_MANAGER = new FailingSnapshotManager();
private static final SnapshotManager LOADING_SNAPSHOT_MANAGER =
new FailingSnapshotManager.RequestingSnapshotManager();
private static final BlockProvider SIMPLE_SNAPSHOT_MANAGER = new FailingBlockProvider();
private static final BlockProvider LOADING_SNAPSHOT_MANAGER =
new FailingBlockProvider.RequestingBlockProvider();

private static final ExecutorService PATHING_EXECUTOR = Executors.newWorkStealingPool();

Expand All @@ -64,16 +64,16 @@ abstract class AbstractPathfinder implements Pathfinder {
private final Set<PathfinderHook> pathfinderHooks = new HashSet<>();

protected final PathfinderConfiguration pathfinderConfiguration;
protected final SnapshotManager snapshotManager;
protected final BlockProvider blockProvider;

private volatile boolean aborted;

protected AbstractPathfinder(PathfinderConfiguration pathfinderConfiguration) {
this.pathfinderConfiguration = pathfinderConfiguration;
this.snapshotManager = determineSnapshotManager(pathfinderConfiguration);
this.blockProvider = determineSnapshotManager(pathfinderConfiguration);
}

private SnapshotManager determineSnapshotManager(
private BlockProvider determineSnapshotManager(
PathfinderConfiguration pathfinderConfiguration) {
return pathfinderConfiguration.isLoadingChunks()
? LOADING_SNAPSHOT_MANAGER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import de.metaphoriker.pathetic.api.snapshot.SnapshotManager;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.BlockInformation;
import de.metaphoriker.pathetic.api.wrapper.PathBlock;
import de.metaphoriker.pathetic.api.wrapper.PathEnvironment;
Expand All @@ -22,11 +22,11 @@
import de.metaphoriker.pathetic.util.ErrorLogger;

/**
* The FailingSnapshotManager class implements the SnapshotManager interface and provides a default
* The FailingBlockProvider class implements the BlockProvider interface and provides a default
* implementation for retrieving block data snapshots from a Minecraft world. It utilizes chunk
* snapshots to efficiently access block information, even in asynchronous contexts.
*
* <p>FailingSnapshotManager also uses NMS (net.minecraft.server) utilities to bypass the Spigot
* <p>FailingBlockProvider also uses NMS (net.minecraft.server) utilities to bypass the Spigot
* AsyncCatcher and fetch snapshots natively from an asynchronous context. This allows for more
* flexible and efficient access to world data.
*
Expand All @@ -35,7 +35,7 @@
* are not loaded in the world. Developers using this manager should handle potential failures
* gracefully.
*/
public class FailingSnapshotManager implements SnapshotManager {
public class FailingBlockProvider implements BlockProvider {

private static final Map<UUID, WorldDomain> SNAPSHOTS_MAP = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -150,11 +150,11 @@ public PathBlock getHighestBlockAt(PathPosition position) {
}

/**
* The RequestingSnapshotManager is an inner class of FailingSnapshotManager, extending it. This
* The RequestingBlockProvider is an inner class of FailingBlockProvider, extending it. This
* class provides additional functionality for ensuring that block data snapshots are available,
* even if not initially loaded.
*/
public static class RequestingSnapshotManager extends FailingSnapshotManager {
public static class RequestingBlockProvider extends FailingBlockProvider {

private static ChunkSnapshot retrieveChunkSnapshot(
PathEnvironment world, int chunkX, int chunkZ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.bukkit.Material;
import de.metaphoriker.pathetic.api.pathing.filter.PathFilter;
import de.metaphoriker.pathetic.api.pathing.filter.PathValidationContext;
import de.metaphoriker.pathetic.api.snapshot.SnapshotManager;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.PathBlock;
import de.metaphoriker.pathetic.api.wrapper.PathPosition;

Expand Down Expand Up @@ -40,12 +40,12 @@ public DangerousMaterialsFilter(EnumSet<Material> dangerousMaterials, int radius
@Override
public boolean filter(PathValidationContext context) {
PathPosition position = context.getPosition();
SnapshotManager snapshotManager = context.getSnapshotManager();
BlockProvider blockProvider = context.getBlockProvider();

for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
for (int z = -radius; z <= radius; z++) {
PathBlock block = snapshotManager.getBlock(position.add(x, y, z));
PathBlock block = blockProvider.getBlock(position.add(x, y, z));
if (block != null
&& dangerousMaterials.contains(block.getBlockInformation().getMaterial())) {
return false; // The node is near a dangerous material, so it's excluded.
Expand Down

0 comments on commit 5398136

Please sign in to comment.