From 37e02efd956478fc93a9516ee232c8d379dc30cb Mon Sep 17 00:00:00 2001 From: "tofik.07" Date: Thu, 26 Feb 2026 13:41:00 +0100 Subject: [PATCH] feat: Add new overloads for Pathfinder (#13192) --- .../paper/entity/Pathfinder.java | 60 +++++++++++++++++-- .../paper/entity/PaperPathfinder.java | 8 +-- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/paper-api/src/main/java/com/destroystokyo/paper/entity/Pathfinder.java b/paper-api/src/main/java/com/destroystokyo/paper/entity/Pathfinder.java index dd4c4d06ff..b952097d9e 100644 --- a/paper-api/src/main/java/com/destroystokyo/paper/entity/Pathfinder.java +++ b/paper-api/src/main/java/com/destroystokyo/paper/entity/Pathfinder.java @@ -3,6 +3,7 @@ package com.destroystokyo.paper.entity; import java.util.List; import org.bukkit.Location; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Entity; import org.bukkit.entity.Mob; import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; @@ -45,7 +46,7 @@ public interface Pathfinder { @Nullable PathResult findPath(Location loc); /** - * Calculates a destination for the Entity to navigate to to reach the target entity, + * Calculates a destination for the Entity to navigate to reach the target entity, * but does not set it as the current target. * Useful for calculating what would happen before setting it. *

@@ -57,7 +58,25 @@ public interface Pathfinder { * @param target the Entity to navigate to * @return The closest Location the Entity can get to for this navigation, or null if no path could be calculated */ - @Nullable PathResult findPath(LivingEntity target); + @Nullable + default PathResult findPath(LivingEntity target) { + return this.findPath((Entity) target); + } + + /** + * Calculates a destination for the Entity to navigate to reach the target entity, + * but does not set it as the current target. + * Useful for calculating what would happen before setting it. + *

+ * The behavior of this PathResult is subject to the games pathfinding rules, and may + * result in the pathfinding automatically updating to follow the target Entity. + *

+ * However, this behavior is not guaranteed, and is subject to the game's behavior. + * + * @param target the Entity to navigate to + * @return The closest Location the Entity can get to for this navigation, or null if no path could be calculated + */ + @Nullable PathResult findPath(Entity target); /** * Calculates a destination for the Entity to navigate to, and sets it with default speed @@ -84,7 +103,7 @@ public interface Pathfinder { } /** - * Calculates a destination for the Entity to navigate to to reach the target entity, + * Calculates a destination for the Entity to navigate to reach the target entity, * and sets it with default speed. *

* The behavior of this PathResult is subject to the games pathfinding rules, and may @@ -100,7 +119,7 @@ public interface Pathfinder { } /** - * Calculates a destination for the Entity to navigate to to reach the target entity, + * Calculates a destination for the Entity to navigate to reach the target entity, * and sets it with specified speed. *

* The behavior of this PathResult is subject to the games pathfinding rules, and may @@ -113,6 +132,39 @@ public interface Pathfinder { * @return If the pathfinding was successfully started */ default boolean moveTo(LivingEntity target, double speed) { + return this.moveTo((Entity) target, speed); + } + + /** + * Calculates a destination for the Entity to navigate to reach the target entity, + * and sets it with default speed. + *

+ * The behavior of this PathResult is subject to the games pathfinding rules, and may + * result in the pathfinding automatically updating to follow the target Entity. + *

+ * However, this behavior is not guaranteed, and is subject to the game's behavior. + * + * @param target the Entity to navigate to + * @return If the pathfinding was successfully started + */ + default boolean moveTo(Entity target) { + return this.moveTo(target, 1); + } + + /** + * Calculates a destination for the Entity to navigate to reach the target entity, + * and sets it with specified speed. + *

+ * The behavior of this PathResult is subject to the games pathfinding rules, and may + * result in the pathfinding automatically updating to follow the target Entity. + *

+ * However, this behavior is not guaranteed, and is subject to the game's behavior. + * + * @param target the Entity to navigate to + * @param speed Speed multiplier to navigate at, where 1 is 'normal' + * @return If the pathfinding was successfully started + */ + default boolean moveTo(Entity target, double speed) { PathResult path = this.findPath(target); return path != null && this.moveTo(path, speed); } diff --git a/paper-server/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java b/paper-server/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java index 25779c8710..0a9da3d25d 100644 --- a/paper-server/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java +++ b/paper-server/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java @@ -8,9 +8,9 @@ import javax.annotation.Nullable; import net.minecraft.world.level.pathfinder.Node; import net.minecraft.world.level.pathfinder.Path; import org.bukkit.Location; -import org.bukkit.craftbukkit.entity.CraftLivingEntity; +import org.bukkit.craftbukkit.entity.CraftEntity; import org.bukkit.craftbukkit.util.CraftLocation; -import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Entity; import org.bukkit.entity.Mob; public class PaperPathfinder implements com.destroystokyo.paper.entity.Pathfinder { @@ -57,9 +57,9 @@ public class PaperPathfinder implements com.destroystokyo.paper.entity.Pathfinde @Nullable @Override - public PathResult findPath(LivingEntity target) { + public PathResult findPath(Entity target) { Preconditions.checkArgument(target != null, "Target can not be null"); - Path path = this.entity.getNavigation().createPath(((CraftLivingEntity) target).getHandle(), 0); + Path path = this.entity.getNavigation().createPath(((CraftEntity) target).getHandle(), 0); return path != null ? new PaperPathResult(path) : null; }