1368 lines
67 KiB
Diff
1368 lines
67 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: IPECTER <ipectert@gmail.com>
|
|
Date: Thu, 7 Sep 2023 13:54:43 +0900
|
|
Subject: [PATCH] Async-PathProcessing
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java b/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
|
|
index fe502445a77afe7e3807afae48d7bf03f370e290..3473629e55427ecae262a0a8357439332410d13e 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
|
|
@@ -68,28 +68,62 @@ public class AcquirePoi {
|
|
io.papermc.paper.util.PoiAccess.findNearestPoiPositions(poiManager, poiPredicate, predicate2, entity.blockPosition(), 48, 48*48, PoiManager.Occupancy.HAS_SPACE, false, 5, poiposes);
|
|
Set<Pair<Holder<PoiType>, BlockPos>> set = new java.util.HashSet<>(poiposes);
|
|
// Paper end - optimise POI access
|
|
- Path path = findPathToPois(entity, set);
|
|
- if (path != null && path.canReach()) {
|
|
- BlockPos blockPos = path.getTarget();
|
|
- poiManager.getType(blockPos).ifPresent((poiType) -> {
|
|
- poiManager.take(poiPredicate, (holder, blockPos2) -> {
|
|
- return blockPos2.equals(blockPos);
|
|
- }, blockPos, 1);
|
|
- queryResult.set(GlobalPos.of(world.dimension(), blockPos));
|
|
- entityStatus.ifPresent((status) -> {
|
|
- world.broadcastEntityEvent(entity, status);
|
|
+ // Plazma start - Async Path Processing
|
|
+ if (entity.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) {
|
|
+ // await on patoh async
|
|
+ Path possiblePath = findPathToPois(entity, set);
|
|
+
|
|
+ // wait on the path to be processed
|
|
+ org.plazmamc.plazma.entity.path.AsyncPathProcessor.awaitProcessing(possiblePath, path -> {
|
|
+ // read canReach check
|
|
+ if (path == null || !path.canReach()) {
|
|
+ for(Pair<Holder<PoiType>, BlockPos> pair : set) {
|
|
+ long2ObjectMap.computeIfAbsent(
|
|
+ pair.getSecond().asLong(),
|
|
+ (m) -> new JitteredLinearRetry(entity.level.random, time)
|
|
+ );
|
|
+ }
|
|
+ return;
|
|
+ }
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ poiManager.getType(blockPos).ifPresent((poiType) -> {
|
|
+ poiManager.take(poiPredicate,
|
|
+ (holder, blockPos2) -> blockPos2.equals(blockPos),
|
|
+ blockPos,
|
|
+ 1
|
|
+ );
|
|
+ queryResult.set(GlobalPos.of(world.dimension(), blockPos));
|
|
+ entityStatus.ifPresent((status) -> {
|
|
+ world.broadcastEntityEvent(entity, status);
|
|
+ });
|
|
+ long2ObjectMap.clear();
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
});
|
|
- long2ObjectMap.clear();
|
|
- DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
});
|
|
} else {
|
|
- for(Pair<Holder<PoiType>, BlockPos> pair : set) {
|
|
- long2ObjectMap.computeIfAbsent(pair.getSecond().asLong(), (m) -> {
|
|
- return new AcquirePoi.JitteredLinearRetry(world.random, time);
|
|
+ Path path = findPathToPois(entity, set);
|
|
+ if (path != null && path.canReach()) {
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ poiManager.getType(blockPos).ifPresent((poiType) -> {
|
|
+ poiManager.take(poiPredicate, (holder, blockPos2) -> {
|
|
+ return blockPos2.equals(blockPos);
|
|
+ }, blockPos, 1);
|
|
+ queryResult.set(GlobalPos.of(world.dimension(), blockPos));
|
|
+ entityStatus.ifPresent((status) -> {
|
|
+ world.broadcastEntityEvent(entity, status);
|
|
+ });
|
|
+ long2ObjectMap.clear();
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
});
|
|
+ } else {
|
|
+ for (Pair<Holder<PoiType>, BlockPos> pair : set) {
|
|
+ long2ObjectMap.computeIfAbsent(pair.getSecond().asLong(), (m) -> {
|
|
+ return new AcquirePoi.JitteredLinearRetry(world.random, time);
|
|
+ });
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
+ // Plazma end
|
|
return true;
|
|
}
|
|
};
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java b/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
|
|
index a25d86941d7ed63ac9bcd6f4be71232b0ef6a7a9..dbc0c121be78e8e878d33a43c642fe00db8dc90b 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
|
|
@@ -21,6 +21,7 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
private int remainingCooldown;
|
|
@Nullable
|
|
private Path path;
|
|
+ private boolean finishedProcessing; // Plazma - track when path is processed
|
|
@Nullable
|
|
private BlockPos lastTargetPos;
|
|
private float speedModifier;
|
|
@@ -42,9 +43,11 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
Brain<?> brain = entity.getBrain();
|
|
WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
|
boolean bl = this.reachedTarget(entity, walkTarget);
|
|
- if (!bl && this.tryComputePath(entity, walkTarget, world.getGameTime())) {
|
|
+ if (!world.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled && !bl && this.tryComputePath(entity, walkTarget, world.getGameTime())) { // Plazma - async path processing means we can't know if the path is reachable here
|
|
this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
|
return true;
|
|
+ } else if (world.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled && !bl) {
|
|
+ return true; // Plazma - async pathfinding
|
|
} else {
|
|
brain.eraseMemory(MemoryModuleType.WALK_TARGET);
|
|
if (bl) {
|
|
@@ -58,6 +61,7 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
|
|
@Override
|
|
protected boolean canStillUse(ServerLevel world, Mob entity, long time) {
|
|
+ if (world.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled && !this.finishedProcessing) return true; // Plazma - wait for processing
|
|
if (this.path != null && this.lastTargetPos != null) {
|
|
Optional<WalkTarget> optional = entity.getBrain().getMemory(MemoryModuleType.WALK_TARGET);
|
|
PathNavigation pathNavigation = entity.getNavigation();
|
|
@@ -81,28 +85,99 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
|
|
@Override
|
|
protected void start(ServerLevel serverLevel, Mob mob, long l) {
|
|
+ // Plazma start - petal - start processing
|
|
+ if (serverLevel.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) {
|
|
+ Brain<?> brain = mob.getBrain();
|
|
+ WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
|
+
|
|
+ this.finishedProcessing = false;
|
|
+ this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
|
+ this.path = this.computePath(mob, walkTarget);
|
|
+ return;
|
|
+ }
|
|
+ // Plazma end
|
|
mob.getBrain().setMemory(MemoryModuleType.PATH, this.path);
|
|
mob.getNavigation().moveTo(this.path, (double)this.speedModifier);
|
|
}
|
|
|
|
@Override
|
|
protected void tick(ServerLevel serverLevel, Mob mob, long l) {
|
|
- Path path = mob.getNavigation().getPath();
|
|
- Brain<?> brain = mob.getBrain();
|
|
- if (this.path != path) {
|
|
- this.path = path;
|
|
- brain.setMemory(MemoryModuleType.PATH, path);
|
|
- }
|
|
+ // Plazma start - Async path processing
|
|
+ if (serverLevel.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) {
|
|
+ if (this.path != null && !this.path.isProcessed()) return; // wait for processing
|
|
+
|
|
+ if (!this.finishedProcessing) {
|
|
+ this.finishedProcessing = true;
|
|
+
|
|
+ Brain<?> brain = mob.getBrain();
|
|
+ boolean canReach = this.path != null && this.path.canReach();
|
|
+ if (canReach) {
|
|
+ brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
|
+ } else if (!brain.hasMemoryValue(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE)) {
|
|
+ brain.setMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE, l);
|
|
+ }
|
|
+
|
|
+ if (!canReach) {
|
|
+ Optional<WalkTarget> walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET);
|
|
+
|
|
+ if (!walkTarget.isPresent()) return;
|
|
+
|
|
+ BlockPos blockPos = walkTarget.get().getTarget().currentBlockPosition();
|
|
+ Vec3 vec3 = DefaultRandomPos.getPosTowards((PathfinderMob) mob, 10, 7, Vec3.atBottomCenterOf(blockPos), (float) Math.PI / 2F);
|
|
+ if (vec3 != null) {
|
|
+ // try recalculating the path using a random position
|
|
+ this.path = mob.getNavigation().createPath(vec3.x, vec3.y, vec3.z, 0);
|
|
+ this.finishedProcessing = false;
|
|
+ return;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ mob.getBrain().setMemory(MemoryModuleType.PATH, this.path);
|
|
+ mob.getNavigation().moveTo(this.path, this.speedModifier);
|
|
+ }
|
|
+
|
|
+ Path path = mob.getNavigation().getPath();
|
|
+ Brain<?> brain = mob.getBrain();
|
|
+
|
|
+ if (path != null && this.lastTargetPos != null && brain.hasMemoryValue(MemoryModuleType.WALK_TARGET)) {
|
|
+ WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get(); // we know isPresent = true
|
|
+ if (walkTarget.getTarget().currentBlockPosition().distSqr(this.lastTargetPos) > 4.0D) {
|
|
+ this.start(serverLevel, mob, l);
|
|
+ }
|
|
+ }
|
|
+ } else {
|
|
+ Path path = mob.getNavigation().getPath();
|
|
+ Brain<?> brain = mob.getBrain();
|
|
+ if (this.path != path) {
|
|
+ this.path = path;
|
|
+ brain.setMemory(MemoryModuleType.PATH, path);
|
|
+ }
|
|
+
|
|
+ if (path != null && this.lastTargetPos != null) {
|
|
+ WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
|
+ if (walkTarget.getTarget().currentBlockPosition().distSqr(this.lastTargetPos) > 4.0D && this.tryComputePath(mob, walkTarget, serverLevel.getGameTime())) {
|
|
+ this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
|
+ this.start(serverLevel, mob, l);
|
|
+ }
|
|
|
|
- if (path != null && this.lastTargetPos != null) {
|
|
- WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
|
- if (walkTarget.getTarget().currentBlockPosition().distSqr(this.lastTargetPos) > 4.0D && this.tryComputePath(mob, walkTarget, serverLevel.getGameTime())) {
|
|
- this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
|
- this.start(serverLevel, mob, l);
|
|
}
|
|
+ }
|
|
+ // Plazma end
|
|
+ }
|
|
|
|
+ // Plazma start - Async path processing
|
|
+ @Nullable
|
|
+ private Path computePath(Mob entity, WalkTarget walkTarget) {
|
|
+ BlockPos blockPos = walkTarget.getTarget().currentBlockPosition();
|
|
+ // don't pathfind outside region
|
|
+ this.speedModifier = walkTarget.getSpeedModifier();
|
|
+ Brain<?> brain = entity.getBrain();
|
|
+ if (this.reachedTarget(entity, walkTarget)) {
|
|
+ brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
|
}
|
|
+ return entity.getNavigation().createPath(blockPos, 0);
|
|
}
|
|
+ // Plazma end
|
|
|
|
private boolean tryComputePath(Mob entity, WalkTarget walkTarget, long time) {
|
|
BlockPos blockPos = walkTarget.getTarget().currentBlockPosition();
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java b/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java
|
|
index 271efbb027f6f5d69ac5bc5dc51102a1eb00ab31..f93a2aa2eedbd5786a3287d901132b89c30e53a7 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java
|
|
@@ -57,20 +57,40 @@ public class SetClosestHomeAsWalkTarget {
|
|
Set<Pair<Holder<PoiType>, BlockPos>> set = poiManager.findAllWithType((poiType) -> {
|
|
return poiType.is(PoiTypes.HOME);
|
|
}, predicate, entity.blockPosition(), 48, PoiManager.Occupancy.ANY).collect(Collectors.toSet());
|
|
- Path path = AcquirePoi.findPathToPois(entity, set);
|
|
- if (path != null && path.canReach()) {
|
|
- BlockPos blockPos = path.getTarget();
|
|
- Optional<Holder<PoiType>> optional2 = poiManager.getType(blockPos);
|
|
- if (optional2.isPresent()) {
|
|
- walkTarget.set(new WalkTarget(blockPos, speed, 1));
|
|
- DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
- }
|
|
- } else if (mutableInt.getValue() < 5) {
|
|
- long2LongMap.long2LongEntrySet().removeIf((entry) -> {
|
|
- return entry.getLongValue() < mutableLong.getValue();
|
|
+ // Plazma start - Async path processing
|
|
+ if (entity.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) {
|
|
+ // await on path async
|
|
+ Path possiblePath = AcquirePoi.findPathToPois(entity, set);
|
|
+
|
|
+ // wait on the path to be processed
|
|
+ org.plazmamc.plazma.entity.path.AsyncPathProcessor.awaitProcessing(possiblePath, path -> {
|
|
+ if (path == null || !path.canReach() || mutableInt.getValue() < 5) { // read canReach check
|
|
+ long2LongMap.long2LongEntrySet().removeIf((entry) -> entry.getLongValue() < mutableLong.getValue());
|
|
+ return;
|
|
+ }
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional2 = poiManager.getType(blockPos);
|
|
+ if (optional2.isPresent()) {
|
|
+ walkTarget.set(new WalkTarget(blockPos, speed, 1));
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
+ }
|
|
});
|
|
+ } else {
|
|
+ Path path = AcquirePoi.findPathToPois(entity, set);
|
|
+ if (path != null && path.canReach()) {
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional2 = poiManager.getType(blockPos);
|
|
+ if (optional2.isPresent()) {
|
|
+ walkTarget.set(new WalkTarget(blockPos, speed, 1));
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
+ }
|
|
+ } else if (mutableInt.getValue() < 5) {
|
|
+ long2LongMap.long2LongEntrySet().removeIf((entry) -> {
|
|
+ return entry.getLongValue() < mutableLong.getValue();
|
|
+ });
|
|
+ }
|
|
}
|
|
-
|
|
+ // Plazma end
|
|
return true;
|
|
} else {
|
|
return false;
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/DoorInteractGoal.java b/src/main/java/net/minecraft/world/entity/ai/goal/DoorInteractGoal.java
|
|
index 2fa3e3b3e9888010c7a6976351d889057f5fbe54..7fe7275800d2da99d779cda281cfb14fbca493fc 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/goal/DoorInteractGoal.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/DoorInteractGoal.java
|
|
@@ -57,7 +57,7 @@ public abstract class DoorInteractGoal extends Goal {
|
|
} else {
|
|
GroundPathNavigation groundPathNavigation = (GroundPathNavigation)this.mob.getNavigation();
|
|
Path path = groundPathNavigation.getPath();
|
|
- if (path != null && !path.isDone() && groundPathNavigation.canOpenDoors()) {
|
|
+ if (path != null && path.isProcessed() && !path.isDone() && groundPathNavigation.canOpenDoors()) { // Plazma - async pathfinding - ensure path is processed
|
|
for(int i = 0; i < Math.min(path.getNextNodeIndex() + 2, path.getNodeCount()); ++i) {
|
|
Node node = path.getNode(i);
|
|
this.doorPos = new BlockPos(node.x, node.y + 1, node.z);
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java
|
|
index 97bd4c9f83257c8c54694110d369d0487e4df9f9..30ce068d985d418f4c97e2e72fceafa0e1e945fe 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java
|
|
@@ -12,10 +12,22 @@ public class AmphibiousPathNavigation extends PathNavigation {
|
|
super(mob, world);
|
|
}
|
|
|
|
+ // Plamza start - async path processing
|
|
+ private static final org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = (org.plazmamc.plazma.entity.path.NodeEvaluatorFeatures nodeEvaluatorFeatures) -> {
|
|
+ AmphibiousNodeEvaluator nodeEvaluator = new AmphibiousNodeEvaluator(false);
|
|
+ nodeEvaluator.setCanPassDoors(nodeEvaluatorFeatures.canPassDoors());
|
|
+ nodeEvaluator.setCanFloat(nodeEvaluatorFeatures.canFloat());
|
|
+ nodeEvaluator.setCanWalkOverFences(nodeEvaluatorFeatures.canWalkOverFences());
|
|
+ nodeEvaluator.setCanOpenDoors(nodeEvaluatorFeatures.canOpenDoors());
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Plazma end
|
|
+
|
|
@Override
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new AmphibiousNodeEvaluator(false);
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator); // Plazma - async path processing
|
|
return new PathFinder(this.nodeEvaluator, range);
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
|
|
index acd0b946cab86eb173e713535194d3a9347c7d48..4aabbd23c45a8b0f366e4234340bc1e6929093e2 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
|
|
@@ -16,10 +16,22 @@ public class FlyingPathNavigation extends PathNavigation {
|
|
super(entity, world);
|
|
}
|
|
|
|
+ // Plamza start - async path processing
|
|
+ private static final org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = (org.plazmamc.plazma.entity.path.NodeEvaluatorFeatures nodeEvaluatorFeatures) -> {
|
|
+ FlyNodeEvaluator nodeEvaluator = new FlyNodeEvaluator();
|
|
+ nodeEvaluator.setCanPassDoors(nodeEvaluatorFeatures.canPassDoors());
|
|
+ nodeEvaluator.setCanFloat(nodeEvaluatorFeatures.canFloat());
|
|
+ nodeEvaluator.setCanWalkOverFences(nodeEvaluatorFeatures.canWalkOverFences());
|
|
+ nodeEvaluator.setCanOpenDoors(nodeEvaluatorFeatures.canOpenDoors());
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Plazma end
|
|
+
|
|
@Override
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new FlyNodeEvaluator();
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator); // Plazma - async path processing
|
|
return new PathFinder(this.nodeEvaluator, range);
|
|
}
|
|
|
|
@@ -49,7 +61,7 @@ public class FlyingPathNavigation extends PathNavigation {
|
|
if (this.hasDelayedRecomputation) {
|
|
this.recomputePath();
|
|
}
|
|
-
|
|
+ if (this.path != null && !this.path.isProcessed()) return; // Plamza - async path processing
|
|
if (!this.isDone()) {
|
|
if (this.canUpdatePath()) {
|
|
this.followThePath();
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java
|
|
index 4f07e406288cc005e632d60c4586eb378891b5c0..831114c39bf06a5b12951d50941e87813b7795ff 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java
|
|
@@ -21,10 +21,22 @@ public class GroundPathNavigation extends PathNavigation {
|
|
super(entity, world);
|
|
}
|
|
|
|
+ // Plamza start - async path processing
|
|
+ protected static final org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = (org.plazmamc.plazma.entity.path.NodeEvaluatorFeatures nodeEvaluatorFeatures) -> {
|
|
+ WalkNodeEvaluator nodeEvaluator = new WalkNodeEvaluator();
|
|
+ nodeEvaluator.setCanPassDoors(nodeEvaluatorFeatures.canPassDoors());
|
|
+ nodeEvaluator.setCanFloat(nodeEvaluatorFeatures.canFloat());
|
|
+ nodeEvaluator.setCanWalkOverFences(nodeEvaluatorFeatures.canWalkOverFences());
|
|
+ nodeEvaluator.setCanOpenDoors(nodeEvaluatorFeatures.canOpenDoors());
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Plazma end
|
|
+
|
|
@Override
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new WalkNodeEvaluator();
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator); // Plazma - async path processing
|
|
return new PathFinder(this.nodeEvaluator, range);
|
|
}
|
|
|
|
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
|
|
index 6d127ed3da899851ca95b2be6792e2abca1aca12..1618ff66dea38e104598ef535511897ecb8a3479 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
|
|
@@ -152,6 +152,8 @@ public abstract class PathNavigation {
|
|
return null;
|
|
} else if (!this.canUpdatePath()) {
|
|
return null;
|
|
+ } else if (this.path instanceof org.plazmamc.plazma.entity.path.AsyncPath asyncPath && !asyncPath.isProcessed() && asyncPath.hasSameProcessingPositions(positions)) { // Plazma start - catch early if it's still processing these positions let it keep processing
|
|
+ return this.path; // plazma end
|
|
} else if (this.path != null && !this.path.isDone() && positions.contains(this.targetPos)) {
|
|
return this.path;
|
|
} else {
|
|
@@ -177,13 +179,30 @@ public abstract class PathNavigation {
|
|
int i = (int)(followRange + (float)range);
|
|
PathNavigationRegion pathNavigationRegion = new PathNavigationRegion(this.level, blockPos.offset(-i, -i, -i), blockPos.offset(i, i, i));
|
|
Path path = this.pathFinder.findPath(pathNavigationRegion, this.mob, positions, followRange, distance, this.maxVisitedNodesMultiplier);
|
|
- //this.level.getProfiler().pop(); // Purpur
|
|
- if (path != null && path.getTarget() != null) {
|
|
- this.targetPos = path.getTarget();
|
|
- this.reachRange = distance;
|
|
- this.resetStuckTimeout();
|
|
+ // Plazma start - async path processing
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) {
|
|
+ // assign early a target position. most calls will only have 1 position
|
|
+ if (!positions.isEmpty()) this.targetPos = positions.iterator().next();
|
|
+
|
|
+ org.plazmamc.plazma.entity.path.AsyncPathProcessor.awaitProcessing(path, processedPath -> {
|
|
+ // check that processing didn't take so long that we calculated a new path
|
|
+ if (processedPath != this.path) return;
|
|
+
|
|
+ if (processedPath != null && processedPath.getTarget() != null) {
|
|
+ this.targetPos = processedPath.getTarget();
|
|
+ this.reachRange = distance;
|
|
+ this.resetStuckTimeout();
|
|
+ }
|
|
+ });
|
|
+ } else {
|
|
+ //this.level.getProfiler().pop(); // Purpur
|
|
+ if (path != null && path.getTarget() != null) {
|
|
+ this.targetPos = path.getTarget();
|
|
+ this.reachRange = distance;
|
|
+ this.resetStuckTimeout();
|
|
+ }
|
|
}
|
|
-
|
|
+ // Plazma end
|
|
return path;
|
|
}
|
|
}
|
|
@@ -229,8 +248,8 @@ public abstract class PathNavigation {
|
|
if (this.isDone()) {
|
|
return false;
|
|
} else {
|
|
- this.trimPath();
|
|
- if (this.path.getNodeCount() <= 0) {
|
|
+ if (path.isProcessed()) this.trimPath(); // Plazma start - only trim if processed
|
|
+ if (path.isProcessed() && this.path.getNodeCount() <= 0) { // Plazma end
|
|
return false;
|
|
} else {
|
|
this.speedModifier = speed;
|
|
@@ -253,7 +272,7 @@ public abstract class PathNavigation {
|
|
if (this.hasDelayedRecomputation) {
|
|
this.recomputePath();
|
|
}
|
|
-
|
|
+ if (this.path != null && !this.path.isProcessed()) return; // Plazma - skip pathfinding if we're still processing
|
|
if (!this.isDone()) {
|
|
if (this.canUpdatePath()) {
|
|
this.followThePath();
|
|
@@ -279,6 +298,7 @@ public abstract class PathNavigation {
|
|
}
|
|
|
|
protected void followThePath() {
|
|
+ if (!this.path.isProcessed()) return; // Plazma - skip if not processed
|
|
Vec3 vec3 = this.getTempMobPos();
|
|
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
|
Vec3i vec3i = this.path.getNextNodePos();
|
|
@@ -438,7 +458,7 @@ public abstract class PathNavigation {
|
|
public boolean shouldRecomputePath(BlockPos pos) {
|
|
if (this.hasDelayedRecomputation) {
|
|
return false;
|
|
- } else if (this.path != null && !this.path.isDone() && this.path.getNodeCount() != 0) {
|
|
+ } else if (this.path != null && this.path.isProcessed() && !this.path.isDone() && this.path.getNodeCount() != 0) { // Plazma - Skip if not processed
|
|
Node node = this.path.getEndNode();
|
|
Vec3 vec3 = new Vec3(((double)node.x + this.mob.getX()) / 2.0D, ((double)node.y + this.mob.getY()) / 2.0D, ((double)node.z + this.mob.getZ()) / 2.0D);
|
|
return pos.closerToCenterThan(vec3, (double)(this.path.getNodeCount() - this.path.getNextNodeIndex()));
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/WaterBoundPathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/WaterBoundPathNavigation.java
|
|
index 87887916ff2f685346699989ba30c35ef7e5715f..f33e96de71a2e2815e859a8d8ad7136c50959b69 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/WaterBoundPathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/WaterBoundPathNavigation.java
|
|
@@ -15,10 +15,22 @@ public class WaterBoundPathNavigation extends PathNavigation {
|
|
super(entity, world);
|
|
}
|
|
|
|
+ // Plazma start - async path processing
|
|
+ private static final org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = (org.plazmamc.plazma.entity.path.NodeEvaluatorFeatures nodeEvaluatorFeatures) -> {
|
|
+ SwimNodeEvaluator nodeEvaluator = new SwimNodeEvaluator(nodeEvaluatorFeatures.allowBreaching());
|
|
+ nodeEvaluator.setCanPassDoors(nodeEvaluatorFeatures.canPassDoors());
|
|
+ nodeEvaluator.setCanFloat(nodeEvaluatorFeatures.canFloat());
|
|
+ nodeEvaluator.setCanWalkOverFences(nodeEvaluatorFeatures.canWalkOverFences());
|
|
+ nodeEvaluator.setCanOpenDoors(nodeEvaluatorFeatures.canOpenDoors());
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Plazma end
|
|
+
|
|
@Override
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.allowBreaching = this.mob.getType() == EntityType.DOLPHIN;
|
|
this.nodeEvaluator = new SwimNodeEvaluator(this.allowBreaching);
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator); // Plazma - async path processing
|
|
return new PathFinder(this.nodeEvaluator, range);
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
|
|
index 8db20db72cd51046213625fac46c35854c59ec5d..3dace9e190a65dc5a6cca484ab900c696e7d6034 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
|
|
@@ -57,20 +57,39 @@ public class NearestBedSensor extends Sensor<Mob> {
|
|
java.util.List<Pair<Holder<PoiType>, BlockPos>> poiposes = new java.util.ArrayList<>();
|
|
// don't ask me why it's unbounded. ask mojang.
|
|
io.papermc.paper.util.PoiAccess.findAnyPoiPositions(poiManager, type -> type.is(PoiTypes.HOME), predicate, entity.blockPosition(), 48, PoiManager.Occupancy.ANY, false, Integer.MAX_VALUE, poiposes);
|
|
- Path path = AcquirePoi.findPathToPois(entity, new java.util.HashSet<>(poiposes));
|
|
- // Paper end - optimise POI access
|
|
- if (path != null && path.canReach()) {
|
|
- BlockPos blockPos = path.getTarget();
|
|
- Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
- if (optional.isPresent()) {
|
|
- entity.getBrain().setMemory(MemoryModuleType.NEAREST_BED, blockPos);
|
|
- }
|
|
- } else if (this.triedCount < 5) {
|
|
- this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
- return entry.getLongValue() < this.lastUpdate;
|
|
+ // Plazma start - await on async path processing
|
|
+ if (world.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) {
|
|
+ Path possiblePath = AcquirePoi.findPathToPois(entity, new java.util.HashSet<>(poiposes));
|
|
+ org.plazmamc.plazma.entity.path.AsyncPathProcessor.awaitProcessing(possiblePath, path -> {
|
|
+ // read canReach check
|
|
+ if ((path == null || !path.canReach()) && this.triedCount < 5) {
|
|
+ this.batchCache.long2LongEntrySet().removeIf((entry) -> entry.getLongValue() < this.lastUpdate);
|
|
+ return;
|
|
+ }
|
|
+ if (path == null) return;
|
|
+
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
+ if (optional.isPresent()) {
|
|
+ entity.getBrain().setMemory(MemoryModuleType.NEAREST_BED, blockPos);
|
|
+ }
|
|
});
|
|
+ } else {
|
|
+ Path path = AcquirePoi.findPathToPois(entity, new java.util.HashSet<>(poiposes));
|
|
+ // Paper end - optimise POI access
|
|
+ if (path != null && path.canReach()) {
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
+ if (optional.isPresent()) {
|
|
+ entity.getBrain().setMemory(MemoryModuleType.NEAREST_BED, blockPos);
|
|
+ }
|
|
+ } else if (this.triedCount < 5) {
|
|
+ this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
+ return entry.getLongValue() < this.lastUpdate;
|
|
+ });
|
|
+ }
|
|
}
|
|
-
|
|
+ // Plazma end
|
|
}
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/animal/Bee.java b/src/main/java/net/minecraft/world/entity/animal/Bee.java
|
|
index 71d2649e4046323250a85f3b071e98f58a161302..ff1bff87ffd9d7132fb367b95df43fc51b15b519 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/animal/Bee.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/animal/Bee.java
|
|
@@ -1144,7 +1144,7 @@ public class Bee extends Animal implements NeutralMob, FlyingAnimal {
|
|
} else {
|
|
Bee.this.pathfindRandomlyTowards(Bee.this.hivePos);
|
|
}
|
|
- } else {
|
|
+ } else if (navigation.getPath() != null && navigation.getPath().isProcessed()) { // Plazma - check processing
|
|
boolean flag = this.pathfindDirectlyTowards(Bee.this.hivePos);
|
|
|
|
if (!flag) {
|
|
@@ -1206,7 +1206,7 @@ public class Bee extends Animal implements NeutralMob, FlyingAnimal {
|
|
} else {
|
|
Path pathentity = Bee.this.navigation.getPath();
|
|
|
|
- return pathentity != null && pathentity.getTarget().equals(pos) && pathentity.canReach() && pathentity.isDone();
|
|
+ return pathentity != null && pathentity.isProcessed() && pathentity.getTarget().equals(pos) && pathentity.canReach() && pathentity.isDone(); // Plazma - ensure path is processed
|
|
}
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java b/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
|
|
index dcc1b75361fe9eb250e3946e54454253a8f0e788..c4933b3c56e7b0a6a04acaae8b2aaa54f8515dab 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
|
|
@@ -482,6 +482,17 @@ public class Frog extends Animal implements VariantHolder<FrogVariant> {
|
|
super(frog, world);
|
|
}
|
|
|
|
+ // Plazma start - async path processing
|
|
+ private static final org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = (org.plazmamc.plazma.entity.path.NodeEvaluatorFeatures nodeEvaluatorFeatures) -> {
|
|
+ Frog.FrogNodeEvaluator nodeEvaluator = new Frog.FrogNodeEvaluator(true);
|
|
+ nodeEvaluator.setCanPassDoors(nodeEvaluatorFeatures.canPassDoors());
|
|
+ nodeEvaluator.setCanFloat(nodeEvaluatorFeatures.canFloat());
|
|
+ nodeEvaluator.setCanWalkOverFences(nodeEvaluatorFeatures.canWalkOverFences());
|
|
+ nodeEvaluator.setCanOpenDoors(nodeEvaluatorFeatures.canOpenDoors());
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Plazma end
|
|
+
|
|
@Override
|
|
public boolean canCutCorner(BlockPathTypes nodeType) {
|
|
return nodeType != BlockPathTypes.WATER_BORDER && super.canCutCorner(nodeType);
|
|
@@ -491,6 +502,7 @@ public class Frog extends Animal implements VariantHolder<FrogVariant> {
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new Frog.FrogNodeEvaluator(true);
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator); // Plazma - async path processing
|
|
return new PathFinder(this.nodeEvaluator, range);
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/monster/Drowned.java b/src/main/java/net/minecraft/world/entity/monster/Drowned.java
|
|
index a6980d85455234d4f89ff423e013f3c479bd3fe8..33783ad91e8cc352a5c6c5cc8b7fd7f6d3812735 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/monster/Drowned.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/monster/Drowned.java
|
|
@@ -293,7 +293,7 @@ public class Drowned extends Zombie implements RangedAttackMob {
|
|
protected boolean closeToNextPos() {
|
|
Path pathentity = this.getNavigation().getPath();
|
|
|
|
- if (pathentity != null) {
|
|
+ if (pathentity != null && pathentity.isProcessed()) { // Plazma - ensure path is processed
|
|
BlockPos blockposition = pathentity.getTarget();
|
|
|
|
if (blockposition != null) {
|
|
diff --git a/src/main/java/net/minecraft/world/entity/monster/Strider.java b/src/main/java/net/minecraft/world/entity/monster/Strider.java
|
|
index 0b5d3837536d526c25ba1e12be142bb476d03519..604bdb11adb900ee5db944fac17e8deae3631cd0 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/monster/Strider.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/monster/Strider.java
|
|
@@ -100,6 +100,17 @@ public class Strider extends Animal implements ItemSteerable, Saddleable {
|
|
this.setPathfindingMalus(BlockPathTypes.DAMAGE_FIRE, 0.0F);
|
|
}
|
|
|
|
+ // Plazma start - async path processing
|
|
+ private static final org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = (org.plazmamc.plazma.entity.path.NodeEvaluatorFeatures nodeEvaluatorFeatures) -> {
|
|
+ WalkNodeEvaluator nodeEvaluator = new WalkNodeEvaluator();
|
|
+ nodeEvaluator.setCanPassDoors(nodeEvaluatorFeatures.canPassDoors());
|
|
+ nodeEvaluator.setCanFloat(nodeEvaluatorFeatures.canFloat());
|
|
+ nodeEvaluator.setCanWalkOverFences(nodeEvaluatorFeatures.canWalkOverFences());
|
|
+ nodeEvaluator.setCanOpenDoors(nodeEvaluatorFeatures.canOpenDoors());
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Plazma end
|
|
+
|
|
// Purpur start
|
|
@Override
|
|
public boolean isRidable() {
|
|
@@ -615,6 +626,7 @@ public class Strider extends Animal implements ItemSteerable, Saddleable {
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new WalkNodeEvaluator();
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator); // Plazma - async path processing
|
|
return new PathFinder(this.nodeEvaluator, range);
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/monster/warden/Warden.java b/src/main/java/net/minecraft/world/entity/monster/warden/Warden.java
|
|
index 41301ad56fbcbe0f13447bd3b515d15bf58554c4..1e27b0b0f704e0e42ad0897e1cb1111f30693b01 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/monster/warden/Warden.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/monster/warden/Warden.java
|
|
@@ -708,12 +708,23 @@ public class Warden extends Monster implements VibrationListener.VibrationListen
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new WalkNodeEvaluator();
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
- return new PathFinder(this.nodeEvaluator, range) {
|
|
- @Override
|
|
- protected float distance(Node a, Node b) {
|
|
- return a.distanceToXZ(b);
|
|
- }
|
|
- };
|
|
+ // Plazma start - async path processing
|
|
+ if (this.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) {
|
|
+ return new PathFinder(this.nodeEvaluator, range, GroundPathNavigation.nodeEvaluatorGenerator) {
|
|
+ @Override
|
|
+ protected float distance(Node a, Node b) {
|
|
+ return a.distanceToXZ(b);
|
|
+ }
|
|
+ };
|
|
+ } else {
|
|
+ return new PathFinder(this.nodeEvaluator, range) {
|
|
+ @Override
|
|
+ protected float distance(Node a, Node b) {
|
|
+ return a.distanceToXZ(b);
|
|
+ }
|
|
+ };
|
|
+ }
|
|
+ // Plazma end
|
|
}
|
|
};
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/Path.java b/src/main/java/net/minecraft/world/level/pathfinder/Path.java
|
|
index c5597c9b902857ed83b54fd74fcd9463749e3dbc..ad6fd5983a3d7abc271f18b9fde38583c584d0c3 100644
|
|
--- a/src/main/java/net/minecraft/world/level/pathfinder/Path.java
|
|
+++ b/src/main/java/net/minecraft/world/level/pathfinder/Path.java
|
|
@@ -30,6 +30,17 @@ public class Path {
|
|
this.reached = reachesTarget;
|
|
}
|
|
|
|
+ // Plazma start - async path processing
|
|
+ /**
|
|
+ * checks if the path is completely processed in the case of it being computed async
|
|
+ *
|
|
+ * @return true if the path is processed
|
|
+ */
|
|
+ public boolean isProcessed() {
|
|
+ return true;
|
|
+ }
|
|
+ // Plazma end
|
|
+
|
|
public void advance() {
|
|
++this.nextNodeIndex;
|
|
}
|
|
@@ -104,6 +115,7 @@ public class Path {
|
|
}
|
|
|
|
public boolean sameAs(@Nullable Path o) {
|
|
+ if (o == this) return true; // Plazma - short circuit
|
|
if (o == null) {
|
|
return false;
|
|
} else if (o.nodes.size() != this.nodes.size()) {
|
|
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java b/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java
|
|
index a8af51a25b0f99c3a64d9150fdfcd6b818aa7581..6d9b052a860b6c99bcc25372758d4603231d7768 100644
|
|
--- a/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java
|
|
+++ b/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java
|
|
@@ -24,35 +24,76 @@ public class PathFinder {
|
|
public final NodeEvaluator nodeEvaluator;
|
|
private static final boolean DEBUG = false;
|
|
private final BinaryHeap openSet = new BinaryHeap();
|
|
+ private final @Nullable org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator; // Plazma - we use this later to generate an evaluator
|
|
|
|
+ // Plazma start - support nodeEvaluatorgenerators
|
|
public PathFinder(NodeEvaluator pathNodeMaker, int range) {
|
|
+ this(pathNodeMaker, range, null);
|
|
+ }
|
|
+
|
|
+ public PathFinder(NodeEvaluator pathNodeMaker, int range, @Nullable org.plazmamc.plazma.entity.path.NodeEvaluatorGenerator nodeEvaluatorGenerator) {
|
|
this.nodeEvaluator = pathNodeMaker;
|
|
this.maxVisitedNodes = range;
|
|
+ this.nodeEvaluatorGenerator = nodeEvaluatorGenerator;
|
|
}
|
|
+ // Plazma end
|
|
|
|
@Nullable
|
|
public Path findPath(PathNavigationRegion world, Mob mob, Set<BlockPos> positions, float followRange, int distance, float rangeMultiplier) {
|
|
- this.openSet.clear();
|
|
- this.nodeEvaluator.prepare(world, mob);
|
|
- Node node = this.nodeEvaluator.getStart();
|
|
+ if (!mob.level.plazmaLevelConfiguration().entity.asyncPathProcessing.enabled) this.openSet.clear();// Plazma - it's always cleared in processPath
|
|
+ // Plazma start - use a generated evaluator if we have one otherwise run sync
|
|
+ NodeEvaluator nodeEvaluator = this.nodeEvaluatorGenerator == null ? this.nodeEvaluator : org.plazmamc.plazma.entity.path.NodeEvaluatorCache.takeNodeEvaluator(this.nodeEvaluatorGenerator, this.nodeEvaluator);
|
|
+ nodeEvaluator.prepare(world, mob);
|
|
+ Node node = nodeEvaluator.getStart();
|
|
+ // Plazma end
|
|
if (node == null) {
|
|
+ org.plazmamc.plazma.entity.path.NodeEvaluatorCache.removeNodeEvaluator(nodeEvaluator); // Plazma - handle nodeEvaluatorGenerator
|
|
return null;
|
|
} else {
|
|
// Paper start - remove streams - and optimize collection
|
|
List<Map.Entry<Target, BlockPos>> map = Lists.newArrayList();
|
|
for (BlockPos pos : positions) {
|
|
- map.add(new java.util.AbstractMap.SimpleEntry<>(this.nodeEvaluator.getGoal(pos.getX(), pos.getY(), pos.getZ()), pos));
|
|
+ map.add(new java.util.AbstractMap.SimpleEntry<>(nodeEvaluator.getGoal(pos.getX(), pos.getY(), pos.getZ()), pos)); // Plazma - handle nodeEvaluatorGenerator
|
|
}
|
|
// Paper end
|
|
- Path path = this.findPath(world.getProfiler(), node, map, followRange, distance, rangeMultiplier);
|
|
- this.nodeEvaluator.done();
|
|
- return path;
|
|
+ // Plazma start - async path processing
|
|
+ if (this.nodeEvaluatorGenerator == null) {
|
|
+ // run sync :(
|
|
+ org.plazmamc.plazma.entity.path.NodeEvaluatorCache.removeNodeEvaluator(nodeEvaluator);
|
|
+ return this.findPath(world.getProfiler(), node, map, followRange, distance, rangeMultiplier);
|
|
+ }
|
|
+
|
|
+ return new org.plazmamc.plazma.entity.path.AsyncPath(Lists.newArrayList(), positions, () -> {
|
|
+ try {
|
|
+ return this.processPath(nodeEvaluator, node, map, followRange, distance, rangeMultiplier);
|
|
+ } catch (Exception e) {
|
|
+ e.printStackTrace();
|
|
+ return null;
|
|
+ } finally {
|
|
+ nodeEvaluator.done();
|
|
+ org.plazmamc.plazma.entity.path.NodeEvaluatorCache.returnNodeEvaluator(nodeEvaluator);
|
|
+ }
|
|
+ });
|
|
+ // Plazma end
|
|
}
|
|
}
|
|
|
|
- @Nullable
|
|
+ // Plazma start - split pathfinding into the original sync method for compat and processing for delaying
|
|
+ //@Nullable // Plazma - Always not null
|
|
// Paper start - optimize collection
|
|
private Path findPath(ProfilerFiller profiler, Node startNode, List<Map.Entry<Target, BlockPos>> positions, float followRange, int distance, float rangeMultiplier) {
|
|
+ try {
|
|
+ return this.processPath(this.nodeEvaluator, startNode, positions, followRange, distance, rangeMultiplier);
|
|
+ } catch (Exception e) {
|
|
+ e.printStackTrace();
|
|
+ return null;
|
|
+ } finally {
|
|
+ this.nodeEvaluator.done();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private synchronized @org.jetbrains.annotations.NotNull Path processPath(NodeEvaluator nodeEvaluator, Node startNode, List<Map.Entry<Target, BlockPos>> positions, float followRange, int distance, float rangeMultiplier) { // sync to only use the caching functions in this class on a single thread
|
|
+ org.apache.commons.lang3.Validate.isTrue(!positions.isEmpty()); // ensure that we have at least one position, which means we'll always return a path
|
|
//profiler.push("find_path"); // Purpur
|
|
//profiler.markForCharting(MetricCategory.PATH_FINDING); // Purpur
|
|
// Set<Target> set = positions.keySet();
|
|
@@ -91,7 +132,7 @@ public class PathFinder {
|
|
}
|
|
|
|
if (!(node.distanceTo(startNode) >= followRange)) {
|
|
- int k = this.nodeEvaluator.getNeighbors(this.neighbors, node);
|
|
+ int k = nodeEvaluator.getNeighbors(this.neighbors, node); // Plazma - use provided nodeEvaluator
|
|
|
|
for(int l = 0; l < k; ++l) {
|
|
Node node2 = this.neighbors[l];
|
|
@@ -126,6 +167,7 @@ public class PathFinder {
|
|
return best;
|
|
// Paper end
|
|
}
|
|
+ // Plazma end
|
|
|
|
protected float distance(Node a, Node b) {
|
|
return a.distanceTo(b);
|
|
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/SwimNodeEvaluator.java b/src/main/java/net/minecraft/world/level/pathfinder/SwimNodeEvaluator.java
|
|
index 0e2b14e7dfedf209d63279c81723fd7955122d78..6e2c0790c3b932d3aad1b1eaf11d279840f19914 100644
|
|
--- a/src/main/java/net/minecraft/world/level/pathfinder/SwimNodeEvaluator.java
|
|
+++ b/src/main/java/net/minecraft/world/level/pathfinder/SwimNodeEvaluator.java
|
|
@@ -16,7 +16,7 @@ import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.material.FluidState;
|
|
|
|
public class SwimNodeEvaluator extends NodeEvaluator {
|
|
- private final boolean allowBreaching;
|
|
+ public final boolean allowBreaching; // Plazma - make this public
|
|
private final Long2ObjectMap<BlockPathTypes> pathTypesByPosCache = new Long2ObjectOpenHashMap<>();
|
|
|
|
public SwimNodeEvaluator(boolean canJumpOutOfWater) {
|
|
diff --git a/src/main/java/org/plazmamc/plazma/configurations/GlobalConfiguration.java b/src/main/java/org/plazmamc/plazma/configurations/GlobalConfiguration.java
|
|
index e68ec6e28b0b3a1e3ced2bbcad029d6e1fac17b6..614b1147e963397a64e064cf1e06a212b56f9021 100644
|
|
--- a/src/main/java/org/plazmamc/plazma/configurations/GlobalConfiguration.java
|
|
+++ b/src/main/java/org/plazmamc/plazma/configurations/GlobalConfiguration.java
|
|
@@ -93,4 +93,33 @@ public class GlobalConfiguration extends ConfigurationPart {
|
|
}
|
|
|
|
}
|
|
+
|
|
+ public Entity entity;
|
|
+
|
|
+ public class Entity extends ConfigurationPart {
|
|
+
|
|
+ public AsyncPathProcessing asyncPathProcessing;
|
|
+
|
|
+ public class AsyncPathProcessing extends ConfigurationPart.Post {
|
|
+
|
|
+ public boolean enabled = false;
|
|
+ public int maxThreads = 0;
|
|
+ public int keepAlive = 60;
|
|
+
|
|
+ @Override
|
|
+ public void postProcess() {
|
|
+ if (maxThreads < 0) {
|
|
+ maxThreads = Math.max(Runtime.getRuntime().availableProcessors() + maxThreads, 1);
|
|
+ } else if (maxThreads == 0) {
|
|
+ maxThreads = Math.max(Runtime.getRuntime().availableProcessors() / 4, 1);
|
|
+ }
|
|
+ if (!enabled) {
|
|
+ maxThreads = 0;
|
|
+ } else {
|
|
+ org.bukkit.Bukkit.getLogger().log(java.util.logging.Level.INFO, "Using " + maxThreads + " threads for Async Pathfinding");
|
|
+ }
|
|
+ }
|
|
+
|
|
+ }
|
|
+ }
|
|
}
|
|
diff --git a/src/main/java/org/plazmamc/plazma/configurations/LevelConfigurations.java b/src/main/java/org/plazmamc/plazma/configurations/LevelConfigurations.java
|
|
index 29d451f55dabc15478e9c32d2c37bd0ea2d9bebc..ed1963723db04aa027e987f1aa421b2a3e914cc4 100644
|
|
--- a/src/main/java/org/plazmamc/plazma/configurations/LevelConfigurations.java
|
|
+++ b/src/main/java/org/plazmamc/plazma/configurations/LevelConfigurations.java
|
|
@@ -130,6 +130,19 @@ public class LevelConfigurations extends ConfigurationPart {
|
|
|
|
}
|
|
|
|
+ public AsyncPathProcessing asyncPathProcessing;
|
|
+
|
|
+ public class AsyncPathProcessing extends ConfigurationPart.Post {
|
|
+
|
|
+ public boolean enabled = DO_OPTIMIZE;
|
|
+
|
|
+ @Override
|
|
+ public void postProcess() {
|
|
+ enabled = enabled && org.plazmamc.plazma.configurations.GlobalConfiguration.get().entity.asyncPathProcessing.enabled;
|
|
+ }
|
|
+
|
|
+ }
|
|
+
|
|
}
|
|
|
|
public CarpetFixes carpetFixes;
|
|
diff --git a/src/main/java/org/plazmamc/plazma/entity/path/AsyncPath.java b/src/main/java/org/plazmamc/plazma/entity/path/AsyncPath.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..dace533ffe536a56a38b5db8084fd8e84d91f759
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/plazmamc/plazma/entity/path/AsyncPath.java
|
|
@@ -0,0 +1,283 @@
|
|
+package org.plazmamc.plazma.entity.path;
|
|
+
|
|
+import net.minecraft.core.BlockPos;
|
|
+import net.minecraft.world.entity.Entity;
|
|
+import net.minecraft.world.level.pathfinder.Node;
|
|
+import net.minecraft.world.level.pathfinder.Path;
|
|
+import net.minecraft.world.phys.Vec3;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+
|
|
+import java.util.ArrayList;
|
|
+import java.util.List;
|
|
+import java.util.Set;
|
|
+import java.util.function.Supplier;
|
|
+
|
|
+/**
|
|
+ * i'll be using this to represent a path that not be processed yet!
|
|
+ */
|
|
+public class AsyncPath extends Path {
|
|
+
|
|
+ /**
|
|
+ * runnables waiting for this to be processed
|
|
+ */
|
|
+ private final List<Runnable> postProcessing = new ArrayList<>(0);
|
|
+ /**
|
|
+ * a list of positions that this path could path towards
|
|
+ */
|
|
+ private final Set<BlockPos> positions;
|
|
+ /**
|
|
+ * the supplier of the real processed path
|
|
+ */
|
|
+ private final Supplier<Path> pathSupplier;
|
|
+ /**
|
|
+ * this is a reference to the nodes list in the parent `Path` object
|
|
+ */
|
|
+ private final List<Node> nodes;
|
|
+
|
|
+ /*
|
|
+ * Processed values
|
|
+ */
|
|
+ /**
|
|
+ * marks whether this async path has been processed
|
|
+ */
|
|
+ private volatile boolean processed = false;
|
|
+ /**
|
|
+ * the block we're trying to path to
|
|
+ * <p>
|
|
+ * while processing, we have no idea where this is so consumers of `Path` should check that the path is processed before checking the target block
|
|
+ */
|
|
+ private @Nullable BlockPos target;
|
|
+ /**
|
|
+ * how far we are to the target
|
|
+ * <p>
|
|
+ * while processing, the target could be anywhere but theoretically we're always "close" to a theoretical target so default is 0
|
|
+ */
|
|
+ private float distToTarget = 0;
|
|
+ /**
|
|
+ * whether we can reach the target
|
|
+ * <p>
|
|
+ * while processing, we can always theoretically reach the target so default is true
|
|
+ */
|
|
+ private boolean canReach = true;
|
|
+
|
|
+ public AsyncPath(@NotNull List<Node> emptyNodeList, @NotNull Set<BlockPos> positions, @NotNull Supplier<Path> pathSupplier) {
|
|
+ //noinspection ConstantConditions
|
|
+ super(emptyNodeList, null, false);
|
|
+
|
|
+ this.nodes = emptyNodeList;
|
|
+ this.positions = positions;
|
|
+ this.pathSupplier = pathSupplier;
|
|
+
|
|
+ AsyncPathProcessor.queue(this);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean isProcessed() {
|
|
+ return this.processed;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * returns the future representing the processing state of this path
|
|
+ */
|
|
+ public synchronized void postProcessing(@NotNull Runnable runnable) {
|
|
+ if (this.processed) {
|
|
+ runnable.run();
|
|
+ } else {
|
|
+ this.postProcessing.add(runnable);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * an easy way to check if this processing path is the same as an attempted new path
|
|
+ *
|
|
+ * @param positions - the positions to compare against
|
|
+ * @return true if we are processing the same positions
|
|
+ */
|
|
+ public boolean hasSameProcessingPositions(final Set<BlockPos> positions) {
|
|
+ if (this.positions.size() != positions.size()) {
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ return this.positions.containsAll(positions);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * starts processing this path
|
|
+ */
|
|
+ public synchronized void process() {
|
|
+ if (this.processed) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ final Path bestPath = this.pathSupplier.get();
|
|
+
|
|
+ this.nodes.addAll(bestPath.nodes); // we mutate this list to reuse the logic in Path
|
|
+ this.target = bestPath.getTarget();
|
|
+ this.distToTarget = bestPath.getDistToTarget();
|
|
+ this.canReach = bestPath.canReach();
|
|
+
|
|
+ this.processed = true;
|
|
+
|
|
+ for (Runnable runnable : this.postProcessing) {
|
|
+ runnable.run();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * if this path is accessed while it hasn't processed, just process it in-place
|
|
+ */
|
|
+ private void checkProcessed() {
|
|
+ if (!this.processed) {
|
|
+ this.process();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * overrides we need for final fields that we cannot modify after processing
|
|
+ */
|
|
+
|
|
+ @Override
|
|
+ public @NotNull BlockPos getTarget() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return this.target;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public float getDistToTarget() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return this.distToTarget;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean canReach() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return this.canReach;
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * overrides to ensure we're processed first
|
|
+ */
|
|
+
|
|
+ @Override
|
|
+ public boolean isDone() {
|
|
+ return this.isProcessed() && super.isDone();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void advance() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.advance();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean notStarted() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.notStarted();
|
|
+ }
|
|
+
|
|
+ @Nullable
|
|
+ @Override
|
|
+ public Node getEndNode() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getEndNode();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Node getNode(int index) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNode(index);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void truncateNodes(int length) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.truncateNodes(length);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void replaceNode(int index, Node node) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.replaceNode(index, node);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public int getNodeCount() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNodeCount();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public int getNextNodeIndex() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextNodeIndex();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void setNextNodeIndex(int nodeIndex) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.setNextNodeIndex(nodeIndex);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Vec3 getEntityPosAtNode(Entity entity, int index) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getEntityPosAtNode(entity, index);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public BlockPos getNodePos(int index) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNodePos(index);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Vec3 getNextEntityPos(Entity entity) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextEntityPos(entity);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public BlockPos getNextNodePos() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextNodePos();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Node getNextNode() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextNode();
|
|
+ }
|
|
+
|
|
+ @Nullable
|
|
+ @Override
|
|
+ public Node getPreviousNode() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getPreviousNode();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean hasNext() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.hasNext();
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/plazmamc/plazma/entity/path/AsyncPathProcessor.java b/src/main/java/org/plazmamc/plazma/entity/path/AsyncPathProcessor.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..8e19489f53c4386963ef03c4b5ac716aa1733be6
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/plazmamc/plazma/entity/path/AsyncPathProcessor.java
|
|
@@ -0,0 +1,49 @@
|
|
+package org.plazmamc.plazma.entity.path;
|
|
+
|
|
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
+import net.minecraft.server.MinecraftServer;
|
|
+import net.minecraft.world.level.pathfinder.Path;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+import org.plazmamc.plazma.configurations.GlobalConfiguration;
|
|
+
|
|
+import java.util.concurrent.*;
|
|
+import java.util.function.Consumer;
|
|
+
|
|
+/**
|
|
+ * used to handle the scheduling of async path processing
|
|
+ */
|
|
+public class AsyncPathProcessor {
|
|
+
|
|
+ private static final Executor mainThreadExecutor = MinecraftServer.getServer();
|
|
+ private static final Executor pathProcessingExecutor = new ThreadPoolExecutor(
|
|
+ 1,
|
|
+ GlobalConfiguration.get().entity.asyncPathProcessing.maxThreads,
|
|
+ GlobalConfiguration.get().entity.asyncPathProcessing.keepAlive, TimeUnit.SECONDS,
|
|
+ new LinkedBlockingQueue<>(),
|
|
+ new ThreadFactoryBuilder()
|
|
+ .setNameFormat("plazma-path-processor-%d")
|
|
+ .setPriority(Thread.NORM_PRIORITY - 2)
|
|
+ .build()
|
|
+ );
|
|
+
|
|
+ protected static CompletableFuture<Void> queue(@NotNull AsyncPath path) {
|
|
+ return CompletableFuture.runAsync(path::process, pathProcessingExecutor);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * takes a possibly unprocessed path, and waits until it is completed
|
|
+ * the consumer will be immediately invoked if the path is already processed
|
|
+ * the consumer will always be called on the main thread
|
|
+ *
|
|
+ * @param path a path to wait on
|
|
+ * @param afterProcessing a consumer to be called
|
|
+ */
|
|
+ public static void awaitProcessing(@Nullable Path path, Consumer<@Nullable Path> afterProcessing) {
|
|
+ if (path != null && !path.isProcessed() && path instanceof AsyncPath asyncPath) {
|
|
+ asyncPath.postProcessing(() -> mainThreadExecutor.execute(() -> afterProcessing.accept(path)));
|
|
+ } else {
|
|
+ afterProcessing.accept(path);
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorCache.java b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorCache.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..9bdc1b8db55bc29012755edcee742c1daddc2077
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorCache.java
|
|
@@ -0,0 +1,44 @@
|
|
+package org.plazmamc.plazma.entity.path;
|
|
+
|
|
+import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
|
+import org.apache.commons.lang.Validate;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.util.Map;
|
|
+import java.util.Queue;
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
+import java.util.concurrent.ConcurrentLinkedQueue;
|
|
+
|
|
+public class NodeEvaluatorCache {
|
|
+ private static final Map<NodeEvaluatorFeatures, ConcurrentLinkedQueue<NodeEvaluator>> threadLocalNodeEvaluators = new ConcurrentHashMap<>();
|
|
+ private static final Map<NodeEvaluator, NodeEvaluatorGenerator> nodeEvaluatorToGenerator = new ConcurrentHashMap<>();
|
|
+
|
|
+ private static @NotNull Queue<NodeEvaluator> getQueueForFeatures(@NotNull NodeEvaluatorFeatures nodeEvaluatorFeatures) {
|
|
+ return threadLocalNodeEvaluators.computeIfAbsent(nodeEvaluatorFeatures, (key) -> new ConcurrentLinkedQueue<>());
|
|
+ }
|
|
+
|
|
+ public static @NotNull NodeEvaluator takeNodeEvaluator(@NotNull NodeEvaluatorGenerator generator, @NotNull NodeEvaluator localNodeEvaluator) {
|
|
+ final NodeEvaluatorFeatures nodeEvaluatorFeatures = NodeEvaluatorFeatures.fromNodeEvaluator(localNodeEvaluator);
|
|
+ NodeEvaluator nodeEvaluator = getQueueForFeatures(nodeEvaluatorFeatures).poll();
|
|
+
|
|
+ if (nodeEvaluator == null) {
|
|
+ nodeEvaluator = generator.generate(nodeEvaluatorFeatures);
|
|
+ }
|
|
+
|
|
+ nodeEvaluatorToGenerator.put(nodeEvaluator, generator);
|
|
+
|
|
+ return nodeEvaluator;
|
|
+ }
|
|
+
|
|
+ public static void returnNodeEvaluator(@NotNull NodeEvaluator nodeEvaluator) {
|
|
+ final NodeEvaluatorGenerator generator = nodeEvaluatorToGenerator.remove(nodeEvaluator);
|
|
+ Validate.notNull(generator, "NodeEvaluator already returned");
|
|
+
|
|
+ final NodeEvaluatorFeatures nodeEvaluatorFeatures = NodeEvaluatorFeatures.fromNodeEvaluator(nodeEvaluator);
|
|
+ getQueueForFeatures(nodeEvaluatorFeatures).offer(nodeEvaluator);
|
|
+ }
|
|
+
|
|
+ public static void removeNodeEvaluator(@NotNull NodeEvaluator nodeEvaluator) {
|
|
+ nodeEvaluatorToGenerator.remove(nodeEvaluator);
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorFeatures.java b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorFeatures.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..967683e8d3cd8e946c7e8478bf70e68ec62baead
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorFeatures.java
|
|
@@ -0,0 +1,21 @@
|
|
+package org.plazmamc.plazma.entity.path;
|
|
+
|
|
+import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
|
+import net.minecraft.world.level.pathfinder.SwimNodeEvaluator;
|
|
+
|
|
+public record NodeEvaluatorFeatures(NodeEvaluatorType type,
|
|
+ boolean canPassDoors,
|
|
+ boolean canFloat,
|
|
+ boolean canWalkOverFences,
|
|
+ boolean canOpenDoors,
|
|
+ boolean allowBreaching) {
|
|
+ public static NodeEvaluatorFeatures fromNodeEvaluator(NodeEvaluator nodeEvaluator) {
|
|
+ NodeEvaluatorType type = NodeEvaluatorType.fromNodeEvaluator(nodeEvaluator);
|
|
+ boolean canPassDoors = nodeEvaluator.canPassDoors();
|
|
+ boolean canFloat = nodeEvaluator.canFloat();
|
|
+ boolean canWalkOverFences = nodeEvaluator.canWalkOverFences();
|
|
+ boolean canOpenDoors = nodeEvaluator.canOpenDoors();
|
|
+ boolean allowBreaching = nodeEvaluator instanceof SwimNodeEvaluator swimNodeEvaluator && swimNodeEvaluator.allowBreaching;
|
|
+ return new NodeEvaluatorFeatures(type, canPassDoors, canFloat, canWalkOverFences, canOpenDoors, allowBreaching);
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorGenerator.java b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorGenerator.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..ac46dc9f8184a2429471b3dd8755a6b64f750539
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorGenerator.java
|
|
@@ -0,0 +1,10 @@
|
|
+package org.plazmamc.plazma.entity.path;
|
|
+
|
|
+import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public interface NodeEvaluatorGenerator {
|
|
+
|
|
+ @NotNull NodeEvaluator generate(NodeEvaluatorFeatures nodeEvaluatorFeatures);
|
|
+
|
|
+}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorType.java b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorType.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..300abc5963754002956efbc3b9c3d9921129038e
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/plazmamc/plazma/entity/path/NodeEvaluatorType.java
|
|
@@ -0,0 +1,20 @@
|
|
+package org.plazmamc.plazma.entity.path;
|
|
+
|
|
+import net.minecraft.world.level.pathfinder.AmphibiousNodeEvaluator;
|
|
+import net.minecraft.world.level.pathfinder.FlyNodeEvaluator;
|
|
+import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
|
+import net.minecraft.world.level.pathfinder.SwimNodeEvaluator;
|
|
+
|
|
+public enum NodeEvaluatorType {
|
|
+ WALK,
|
|
+ SWIM,
|
|
+ AMPHIBIOUS,
|
|
+ FLY;
|
|
+
|
|
+ public static NodeEvaluatorType fromNodeEvaluator(NodeEvaluator nodeEvaluator) {
|
|
+ if (nodeEvaluator instanceof SwimNodeEvaluator) return SWIM;
|
|
+ if (nodeEvaluator instanceof FlyNodeEvaluator) return FLY;
|
|
+ if (nodeEvaluator instanceof AmphibiousNodeEvaluator) return AMPHIBIOUS;
|
|
+ return WALK;
|
|
+ }
|
|
+}
|