mirror of
https://github.com/PaperMC/Paper
synced 2026-08-20 18:26:12 -04:00
feat: Add new overloads for Pathfinder (#13192)
This commit is contained in:
parent
ad767b0c5c
commit
37e02efd95
2 changed files with 60 additions and 8 deletions
|
|
@ -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.
|
||||
* <p>
|
||||
|
|
@ -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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue