Paper/patches/server/0253-Mob-Pathfinding-API.patch

205 lines
7.3 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 9 Sep 2018 13:30:00 -0400
Subject: [PATCH] Mob Pathfinding API
Implements Pathfinding API for mobs
== AT ==
public net.minecraft.world.entity.ai.navigation.PathNavigation pathFinder
public net.minecraft.world.level.pathfinder.PathFinder nodeEvaluator
public net.minecraft.world.level.pathfinder.Path nodes
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java b/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java
new file mode 100644
index 0000000000000000000000000000000000000000..064712e7b27a200b29c72076a82f4f5611fa507f
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java
@@ -0,0 +1,143 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.entity;
+
+import org.apache.commons.lang.Validate;
+import org.bukkit.Location;
+import org.bukkit.craftbukkit.entity.CraftLivingEntity;
+import org.bukkit.entity.LivingEntity;
+import org.bukkit.entity.Mob;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import net.minecraft.world.level.pathfinder.Node;
+import net.minecraft.world.level.pathfinder.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+public class PaperPathfinder implements com.destroystokyo.paper.entity.Pathfinder {
+
+ private net.minecraft.world.entity.Mob entity;
2021-06-11 14:02:28 +02:00
+
+ public PaperPathfinder(net.minecraft.world.entity.Mob entity) {
+ this.entity = entity;
+ }
+
+ @Override
+ public Mob getEntity() {
+ return entity.getBukkitMob();
+ }
+
+ public void setHandle(net.minecraft.world.entity.Mob entity) {
+ this.entity = entity;
+ }
+
2021-06-11 14:02:28 +02:00
+ @Override
+ public void stopPathfinding() {
2021-06-17 23:39:36 +02:00
+ entity.getNavigation().stop();
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public boolean hasPath() {
+ return entity.getNavigation().getPath() != null && !entity.getNavigation().getPath().isDone();
2021-06-11 14:02:28 +02:00
+ }
+
+ @Nullable
+ @Override
+ public PathResult getCurrentPath() {
2021-06-17 23:39:36 +02:00
+ Path path = entity.getNavigation().getPath();
+ return path != null && !path.isDone() ? new PaperPathResult(path) : null;
2021-06-11 14:02:28 +02:00
+ }
+
+ @Nullable
+ @Override
+ public PathResult findPath(Location loc) {
+ Validate.notNull(loc, "Location can not be null");
2021-06-17 23:39:36 +02:00
+ Path path = entity.getNavigation().createPath(loc.getX(), loc.getY(), loc.getZ(), 0);
2021-06-11 14:02:28 +02:00
+ return path != null ? new PaperPathResult(path) : null;
+ }
+
+ @Nullable
+ @Override
+ public PathResult findPath(LivingEntity target) {
+ Validate.notNull(target, "Target can not be null");
2021-06-17 23:39:36 +02:00
+ Path path = entity.getNavigation().createPath(((CraftLivingEntity) target).getHandle(), 0);
2021-06-11 14:02:28 +02:00
+ return path != null ? new PaperPathResult(path) : null;
+ }
+
+ @Override
+ public boolean moveTo(@Nonnull PathResult path, double speed) {
+ Validate.notNull(path, "PathResult can not be null");
+ Path pathEntity = ((PaperPathResult) path).path;
2021-06-17 23:39:36 +02:00
+ return entity.getNavigation().moveTo(pathEntity, speed);
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public boolean canOpenDoors() {
2021-06-17 23:39:36 +02:00
+ return entity.getNavigation().pathFinder.nodeEvaluator.canOpenDoors();
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public void setCanOpenDoors(boolean canOpenDoors) {
2021-06-17 23:39:36 +02:00
+ entity.getNavigation().pathFinder.nodeEvaluator.setCanOpenDoors(canOpenDoors);
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public boolean canPassDoors() {
2021-06-17 23:39:36 +02:00
+ return entity.getNavigation().pathFinder.nodeEvaluator.canPassDoors();
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public void setCanPassDoors(boolean canPassDoors) {
2021-06-17 23:39:36 +02:00
+ entity.getNavigation().pathFinder.nodeEvaluator.setCanPassDoors(canPassDoors);
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public boolean canFloat() {
2021-06-17 23:39:36 +02:00
+ return entity.getNavigation().pathFinder.nodeEvaluator.canFloat();
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public void setCanFloat(boolean canFloat) {
2021-06-17 23:39:36 +02:00
+ entity.getNavigation().pathFinder.nodeEvaluator.setCanFloat(canFloat);
2021-06-11 14:02:28 +02:00
+ }
+
+ public class PaperPathResult implements com.destroystokyo.paper.entity.PaperPathfinder.PathResult {
+
+ private final Path path;
+ PaperPathResult(Path path) {
+ this.path = path;
+ }
+
+ @Nullable
+ @Override
+ public Location getFinalPoint() {
2021-06-17 23:39:36 +02:00
+ Node point = path.getEndNode();
2021-06-11 14:02:28 +02:00
+ return point != null ? toLoc(point) : null;
+ }
+
+ @Override
+ public List<Location> getPoints() {
+ List<Location> points = new ArrayList<>();
2021-06-17 23:39:36 +02:00
+ for (Node point : path.nodes) {
2021-06-11 14:02:28 +02:00
+ points.add(toLoc(point));
+ }
+ return points;
+ }
+
+ @Override
+ public int getNextPointIndex() {
2021-06-17 23:39:36 +02:00
+ return path.getNextNodeIndex();
2021-06-11 14:02:28 +02:00
+ }
+
+ @Nullable
+ @Override
+ public Location getNextPoint() {
+ if (!path.hasNext()) {
+ return null;
+ }
2021-06-17 23:39:36 +02:00
+ return toLoc(path.nodes.get(path.getNextNodeIndex()));
2021-06-11 14:02:28 +02:00
+ }
+ }
+
+ private Location toLoc(Node point) {
2023-06-07 23:35:19 +02:00
+ return new Location(entity.level().getWorld(), point.x, point.y, point.z);
2021-06-11 14:02:28 +02:00
+ }
+}
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/Path.java b/src/main/java/net/minecraft/world/level/pathfinder/Path.java
2023-09-22 19:59:56 +02:00
index 539dc45c3c7ffc60cf3fb47ae4df65e604e8627b..eea4c932d909145e7af848cf76e3f49dbb2deff2 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/pathfinder/Path.java
+++ b/src/main/java/net/minecraft/world/level/pathfinder/Path.java
2023-09-22 00:26:51 +02:00
@@ -18,6 +18,7 @@ public class Path {
2021-06-11 14:02:28 +02:00
private final BlockPos target;
private final float distToTarget;
private final boolean reached;
2021-06-17 23:39:36 +02:00
+ public boolean hasNext() { return getNextNodeIndex() < this.nodes.size(); } // Paper
2021-06-11 14:02:28 +02:00
public Path(List<Node> nodes, BlockPos target, boolean reachesTarget) {
this.nodes = nodes;
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java
2023-10-27 01:34:58 +02:00
index a75623fd1b2d36530c55c7a380e68b8dc7e58021..c2acbf533b5f3aebe4837512f694fe25abee65d6 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java
@@ -15,8 +15,11 @@ import org.bukkit.loot.LootTable;
2021-06-11 14:02:28 +02:00
public abstract class CraftMob extends CraftLivingEntity implements Mob {
public CraftMob(CraftServer server, net.minecraft.world.entity.Mob entity) {
super(server, entity);
+ paperPathfinder = new com.destroystokyo.paper.entity.PaperPathfinder(entity); // Paper
}
+ private final com.destroystokyo.paper.entity.PaperPathfinder paperPathfinder; // Paper
+ @Override public com.destroystokyo.paper.entity.Pathfinder getPathfinder() { return paperPathfinder; } // Paper
@Override
public void setTarget(LivingEntity target) {
Preconditions.checkState(!this.getHandle().generation, "Cannot set target during world generation");
@@ -57,6 +60,14 @@ public abstract class CraftMob extends CraftLivingEntity implements Mob {
2023-10-27 01:34:58 +02:00
return (net.minecraft.world.entity.Mob) this.entity;
}
+ // Paper start
+ @Override
+ public void setHandle(net.minecraft.world.entity.Entity entity) {
+ super.setHandle(entity);
+ paperPathfinder.setHandle(getHandle());
+ }
+ // Paper end
+
@Override
public String toString() {
return "CraftMob";