Skip to content

Commit a95ae8d

Browse files
authored
Expose reachRange argument in findPath for PathFinder (#13026)
1 parent 4ba9bed commit a95ae8d

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

paper-api/src/main/java/com/destroystokyo/paper/entity/Pathfinder.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.bukkit.entity.LivingEntity;
66
import org.bukkit.entity.Entity;
77
import org.bukkit.entity.Mob;
8+
import org.checkerframework.checker.index.qual.NonNegative;
89
import org.jspecify.annotations.NullMarked;
910
import org.jspecify.annotations.Nullable;
1011

@@ -43,7 +44,20 @@ public interface Pathfinder {
4344
* @param loc Location to navigate to
4445
* @return The closest Location the Entity can get to for this navigation, or null if no path could be calculated
4546
*/
46-
@Nullable PathResult findPath(Location loc);
47+
default @Nullable PathResult findPath(Location loc) {
48+
return this.findPath(loc, 0);
49+
}
50+
51+
/**
52+
* Calculates a destination for the Entity to navigate to, but does not set it
53+
* as the current target. Useful for calculating what would happen before setting it.
54+
*
55+
* @param loc Location to navigate to
56+
* @param reachRange The <a href="https://cp-algorithms.com/geometry/manhattan-distance.html">Manhattan-distance</a> threshold
57+
* from the target position at which the path is considered reached, where {@code 0} requires the exact target position
58+
* @return The closest Location the Entity can get to for this navigation, or null if no path could be calculated
59+
*/
60+
@Nullable PathResult findPath(final Location loc, final @NonNegative int reachRange);
4761

4862
/**
4963
* Calculates a destination for the Entity to navigate to reach the target entity,
@@ -76,7 +90,26 @@ default PathResult findPath(LivingEntity target) {
7690
* @param target the Entity to navigate to
7791
* @return The closest Location the Entity can get to for this navigation, or null if no path could be calculated
7892
*/
79-
@Nullable PathResult findPath(Entity target);
93+
default @Nullable PathResult findPath(Entity target) {
94+
return this.findPath(target, 0);
95+
}
96+
97+
/**
98+
* Calculates a destination for the Entity to navigate to to reach the target entity,
99+
* but does not set it as the current target.
100+
* Useful for calculating what would happen before setting it.
101+
* <p>
102+
* The behavior of this PathResult is subject to the games pathfinding rules, and may
103+
* result in the pathfinding automatically updating to follow the target Entity.
104+
* <p>
105+
* However, this behavior is not guaranteed, and is subject to the games behavior.
106+
*
107+
* @param target the Entity to navigate to
108+
* @param reachRange The <a href="https://cp-algorithms.com/geometry/manhattan-distance.html">Manhattan-distance</a> threshold
109+
* from the target position at which the path is considered reached, where {@code 0} requires the exact target position
110+
* @return The closest Location the Entity can get to for this navigation, or null if no path could be calculated
111+
*/
112+
@Nullable PathResult findPath(final Entity target, final @NonNegative int reachRange);
80113

81114
/**
82115
* Calculates a destination for the Entity to navigate to, and sets it with default speed

paper-server/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@ public PathResult getCurrentPath() {
4949

5050
@Nullable
5151
@Override
52-
public PathResult findPath(Location loc) {
52+
public PathResult findPath(final Location loc, final int reachRange) {
5353
Preconditions.checkArgument(loc != null, "Location can not be null");
54-
Path path = this.entity.getNavigation().createPath(loc.getX(), loc.getY(), loc.getZ(), 0);
54+
Preconditions.checkArgument(reachRange >= 0, "Reach range can not be negative");
55+
Path path = this.entity.getNavigation().createPath(loc.getX(), loc.getY(), loc.getZ(), reachRange);
5556
return path != null ? new PaperPathResult(path) : null;
5657
}
5758

5859
@Nullable
5960
@Override
60-
public PathResult findPath(Entity target) {
61+
public PathResult findPath(final Entity target, final int reachRange) {
6162
Preconditions.checkArgument(target != null, "Target can not be null");
62-
Path path = this.entity.getNavigation().createPath(((CraftEntity) target).getHandle(), 0);
63+
Preconditions.checkArgument(reachRange >= 0, "Reach range can not be negative");
64+
Path path = this.entity.getNavigation().createPath(((CraftEntity) target).getHandle(), reachRange);
6365
return path != null ? new PaperPathResult(path) : null;
6466
}
6567

0 commit comments

Comments
 (0)