-
Notifications
You must be signed in to change notification settings - Fork 21
PathfinderResult
So you fired off a request. Now the PathfindingSearch has finished. This object tells you if you reached the destination or if you just hit a wall.
Don't just blindly assume success. Always check the PathState.
pathfindingResult.ifPresent(result -> {
// 1. The Happy Path
if (result.successful()) {
System.out.println("Success. Length: " + path.length());
}
// 2. The "Participation Trophy" (Fallback)
if (result.hasFallenBack()) {
System.out.println("Couldn't reach target, but here is the closest point.");
}
Path path = result.getPath();
moveEntity(path);
}).orElse(result -> {
// 3. Total Failure
System.err.println("Gave up. Reason: " + result.state());
});The PathState enum tells you why something happened.
-
FOUND: Success. We made it.
-
FAILED: Impossible. There is no valid path between A and B.
-
MAX_ITERATIONS_REACHED: Timeout. The search took too long and was killed to save your CPU.
-
LENGTH_LIMITED: Too far. The path exists, but it's longer than your maxLength config allowed.
-
FALLBACK: "At least I tried." We couldn't reach the target, but found the closest reachable node.
-
ABORTED: Rage quit. You called .abort() externally.
The Path is just an ordered list of PathPosition (x, y, z). It implements Iterable, so just loop over it.
Path path = result.getPath();
for (PathPosition pos : path) {
// Move your entity here
entity.teleport(pos.getX(), pos.getY(), pos.getZ());
}