Archiviert
13
0
Dieses Repository wurde am 2024-12-25 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
Paper-Old/patches/server/0265-Mob-Pathfinding-API.patch

290 Zeilen
12 KiB
Diff

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
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
2021-06-13 06:03:02 +02:00
index 0000000000000000000000000000000000000000..92d1bb8b9cdb9eb0c04574c0b6ba5acdca9fb377
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/entity/PaperPathfinder.java
2021-06-13 06:03:02 +02:00
@@ -0,0 +1,139 @@
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 final net.minecraft.world.entity.Mob entity;
+
+ public PaperPathfinder(net.minecraft.world.entity.Mob entity) {
+ this.entity = entity;
+ }
+
+ @Override
+ public Mob getEntity() {
+ return entity.getBukkitMob();
+ }
+
+ @Override
+ public void stopPathfinding() {
+ entity.getNavigation().stopPathfinding();
+ }
+
+ @Override
+ public boolean hasPath() {
+ return entity.getNavigation().getPathEntity() != null;
+ }
+
+ @Nullable
+ @Override
+ public PathResult getCurrentPath() {
+ Path path = entity.getNavigation().getPathEntity();
+ return path != null ? new PaperPathResult(path) : null;
+ }
+
+ @Nullable
+ @Override
+ public PathResult findPath(Location loc) {
+ Validate.notNull(loc, "Location can not be null");
+ Path path = entity.getNavigation().calculateDestination(loc.getX(), loc.getY(), loc.getZ());
+ return path != null ? new PaperPathResult(path) : null;
+ }
+
+ @Nullable
+ @Override
+ public PathResult findPath(LivingEntity target) {
+ Validate.notNull(target, "Target can not be null");
+ Path path = entity.getNavigation().calculateDestination(((CraftLivingEntity) target).getHandle());
+ 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;
+ return entity.getNavigation().setDestination(pathEntity, speed);
+ }
+
+ @Override
+ public boolean canOpenDoors() {
+ return entity.getNavigation().getPathfinder().getPathfinder().shouldOpenDoors();
+ }
+
+ @Override
+ public void setCanOpenDoors(boolean canOpenDoors) {
+ entity.getNavigation().getPathfinder().getPathfinder().setShouldOpenDoors(canOpenDoors);
+ }
+
+ @Override
+ public boolean canPassDoors() {
+ return entity.getNavigation().getPathfinder().getPathfinder().shouldPassDoors();
+ }
+
+ @Override
+ public void setCanPassDoors(boolean canPassDoors) {
+ entity.getNavigation().getPathfinder().getPathfinder().setShouldPassDoors(canPassDoors);
+ }
+
+ @Override
+ public boolean canFloat() {
+ return entity.getNavigation().getPathfinder().getPathfinder().shouldFloat();
+ }
+
+ @Override
+ public void setCanFloat(boolean canFloat) {
+ entity.getNavigation().getPathfinder().getPathfinder().setShouldFloat(canFloat);
+ }
+
+ 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() {
+ Node point = path.getFinalPoint();
+ return point != null ? toLoc(point) : null;
+ }
+
+ @Override
+ public List<Location> getPoints() {
+ List<Location> points = new ArrayList<>();
+ for (Node point : path.getPoints()) {
+ points.add(toLoc(point));
+ }
+ return points;
+ }
+
+ @Override
+ public int getNextPointIndex() {
+ return path.getNextIndex();
+ }
+
+ @Nullable
+ @Override
+ public Location getNextPoint() {
+ if (!path.hasNext()) {
+ return null;
+ }
+ return toLoc(path.getPoints().get(path.getNextIndex()));
+ }
+ }
+
+ private Location toLoc(Node point) {
+ return new Location(entity.level.getWorld(), point.getX(), point.getY(), point.getZ());
+ }
+}
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
2021-06-17 21:52:26 +02:00
index 83243e1d35829a384f8680b18e96d82e630310ed..289494db53d368d56f1abd34abad8ce57e4694b1 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
2021-06-13 06:03:02 +02:00
@@ -93,7 +93,7 @@ public abstract class PathNavigation {
2021-06-11 14:02:28 +02:00
}
@Nullable
- public final Path createPath(double x, double y, double z, int distance) {
2021-06-17 21:52:26 +02:00
+ @Deprecated public final Path calculateDestination(double d0, double d1, double d2) { return createPath(d0, d1, d2, 0); } public final Path createPath(double x, double y, double z, int distance) { // Paper - OBFHELPER
2021-06-11 14:02:28 +02:00
return this.createPath(new BlockPos(x, y, z), distance);
}
2021-06-13 06:03:02 +02:00
@@ -123,7 +123,7 @@ public abstract class PathNavigation {
2021-06-11 14:02:28 +02:00
}
@Nullable
- public Path createPath(Entity entity, int distance) {
+ public final Path calculateDestination(Entity entity) { return createPath(entity, 0); } public Path createPath(Entity entity, int distance) {
2021-06-13 06:03:02 +02:00
return this.createPath(ImmutableSet.of(entity.blockPosition()), entity, 16, true, distance); // Paper
2021-06-11 14:02:28 +02:00
}
2021-06-13 06:03:02 +02:00
@@ -195,6 +195,7 @@ public abstract class PathNavigation {
return path != null && this.moveTo(path, speed);
2021-06-11 14:02:28 +02:00
}
2021-06-17 21:52:26 +02:00
+ @Deprecated public boolean setDestination(@Nullable Path pathentity, double speed) { return moveTo(pathentity, speed); } // Paper - OBFHELPER
2021-06-11 14:02:28 +02:00
public boolean moveTo(@Nullable Path path, double speed) {
if (path == null) {
this.path = null;
2021-06-13 06:03:02 +02:00
@@ -221,7 +222,7 @@ public abstract class PathNavigation {
2021-06-11 14:02:28 +02:00
}
}
- @Nullable
2021-06-17 21:52:26 +02:00
+ @Deprecated @Nullable public Path getPathEntity() { return getPath(); } @Nullable // Paper - OBFHELPER
2021-06-11 14:02:28 +02:00
public Path getPath() {
return this.path;
}
2021-06-13 06:03:02 +02:00
@@ -335,6 +336,7 @@ public abstract class PathNavigation {
2021-06-11 14:02:28 +02:00
return !this.isDone();
}
2021-06-17 21:52:26 +02:00
+ @Deprecated public void stopPathfinding() { stop(); } // Paper - OBFHELPER
2021-06-11 14:02:28 +02:00
public void stop() {
this.path = null;
}
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/Node.java b/src/main/java/net/minecraft/world/level/pathfinder/Node.java
2021-06-17 21:52:26 +02:00
index d7a86444d0e76154319c409317fc5ac9c54403a8..9f8ecc9eb5d5f8b45d03f333ca43295f3d3ce3d3 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/pathfinder/Node.java
+++ b/src/main/java/net/minecraft/world/level/pathfinder/Node.java
2021-06-13 06:03:02 +02:00
@@ -6,9 +6,9 @@ import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
2021-06-11 14:02:28 +02:00
public class Node {
- public final int x;
- public final int y;
- public final int z;
2021-06-17 21:52:26 +02:00
+ public final int x; @Deprecated public final int getX() { return x; } // Paper - OBFHELPER
+ public final int y; @Deprecated public final int getY() { return y; } // Paper - OBFHELPER
+ public final int z; @Deprecated public final int getZ() { return z; } // Paper - OBFHELPER
2021-06-11 14:02:28 +02:00
private final int hash;
public int heapIdx = -1;
public float g;
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/NodeEvaluator.java b/src/main/java/net/minecraft/world/level/pathfinder/NodeEvaluator.java
2021-06-17 21:52:26 +02:00
index 72ca8adb9fa65588c6b1e19be2dc27a36c0146a6..a2126606fa727e24a524f74289d498ef2cb881c7 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/pathfinder/NodeEvaluator.java
+++ b/src/main/java/net/minecraft/world/level/pathfinder/NodeEvaluator.java
2021-06-13 06:03:02 +02:00
@@ -15,9 +15,9 @@ public abstract class NodeEvaluator {
2021-06-11 14:02:28 +02:00
protected int entityWidth;
protected int entityHeight;
protected int entityDepth;
- protected boolean canPassDoors;
- protected boolean canOpenDoors;
- protected boolean canFloat;
2021-06-17 21:52:26 +02:00
+ protected boolean canPassDoors; @Deprecated public boolean shouldPassDoors() { return canPassDoors; } @Deprecated public void setShouldPassDoors(boolean b) { canPassDoors = b; } // Paper - obfhelper
+ protected boolean canOpenDoors; @Deprecated public boolean shouldOpenDoors() { return canOpenDoors; } @Deprecated public void setShouldOpenDoors(boolean b) { canOpenDoors = b; } // Paper - obfhelper
+ protected boolean canFloat; @Deprecated public boolean shouldFloat() { return canFloat; } @Deprecated public void setShouldFloat(boolean b) { canFloat = b; } // Paper - obfhelper
2021-06-11 14:02:28 +02:00
2021-06-13 06:03:02 +02:00
public void prepare(PathNavigationRegion cachedWorld, Mob entity) {
this.level = cachedWorld;
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
2021-06-17 21:52:26 +02:00
index 6928c415e328dd7cff2e9ec553bc4faa1ff8facf..c704c126ad1844110dcd8013b1cf0916857dd7ed 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
2021-06-13 06:03:02 +02:00
@@ -12,14 +12,15 @@ import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
2021-06-11 14:02:28 +02:00
public class Path {
- private final List<Node> nodes;
2021-06-17 21:52:26 +02:00
+ private final List<Node> nodes; @Deprecated public List<Node> getPoints() { return nodes; } // Paper - OBFHELPER
2021-06-11 14:02:28 +02:00
private Node[] openSet = new Node[0];
private Node[] closedSet = new Node[0];
2021-06-13 06:03:02 +02:00
private Set<Target> targetNodes;
2021-06-11 14:02:28 +02:00
- private int nextNodeIndex;
2021-06-17 21:52:26 +02:00
+ private int nextNodeIndex; @Deprecated public int getNextIndex() { return this.nextNodeIndex; } // Paper - OBFHELPER
2021-06-11 14:02:28 +02:00
private final BlockPos target;
private final float distToTarget;
private final boolean reached;
+ public boolean hasNext() { return getNextIndex() < getPoints().size(); } // Paper
public Path(List<Node> nodes, BlockPos target, boolean reachesTarget) {
this.nodes = nodes;
2021-06-13 06:03:02 +02:00
@@ -41,7 +42,7 @@ public class Path {
2021-06-11 14:02:28 +02:00
}
@Nullable
- public Node getEndNode() {
2021-06-17 21:52:26 +02:00
+ @Deprecated public Node getFinalPoint() { return getEndNode(); } @Nullable public Node getEndNode() { // Paper - OBFHELPER
2021-06-13 06:03:02 +02:00
return !this.nodes.isEmpty() ? this.nodes.get(this.nodes.size() - 1) : null;
2021-06-11 14:02:28 +02:00
}
2021-06-13 06:03:02 +02:00
@@ -88,7 +89,7 @@ public class Path {
2021-06-11 14:02:28 +02:00
return this.getEntityPosAtNode(entity, this.nextNodeIndex);
}
- public BlockPos getNextNodePos() {
2021-06-17 21:52:26 +02:00
+ @Deprecated public BlockPos getNext() { return getNextNodePos(); } public BlockPos getNextNodePos() { // Paper - OBFHELPER
2021-06-13 06:03:02 +02:00
return this.nodes.get(this.nextNodeIndex).asBlockPos();
2021-06-11 14:02:28 +02:00
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java
2021-06-13 06:03:02 +02:00
index 71872fdfafca82cf745eecee4bf984726d49f5a4..9c9fa83615cd06539ce5e4e3d4feaa69f65b7931 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
@@ -11,8 +11,11 @@ import org.bukkit.loot.LootTable;
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) {
2021-06-13 06:03:02 +02:00
net.minecraft.world.entity.Mob entity = this.getHandle();