Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ index 5959e1b1772ffbdfb108365171fe37cbf56ef825..0a32caf62273b5ab7d88ab7f29a8507e

@Override
diff --git a/net/minecraft/world/level/pathfinder/PathFinder.java b/net/minecraft/world/level/pathfinder/PathFinder.java
index 168b475b38b2872b27c1ab15f6846323ac16dd2c..ce46232f844d8318ab5067f91c8d90352e2c42cb 100644
index 168b475b38b2872b27c1ab15f6846323ac16dd2c..201e0f1f41e1707f21d4d0df580c7a0d3287ae91 100644
--- a/net/minecraft/world/level/pathfinder/PathFinder.java
+++ b/net/minecraft/world/level/pathfinder/PathFinder.java
@@ -26,10 +26,17 @@ public class PathFinder {
Expand Down Expand Up @@ -834,9 +834,9 @@ index 168b475b38b2872b27c1ab15f6846323ac16dd2c..ce46232f844d8318ab5067f91c8d9035
+ return this.findPath(start, map, maxRange, reachRange, maxVisitedNodesMultiplier);
+ }
+
+ return new org.dreeam.leaf.async.path.AsyncPath(Lists.newArrayList(), targets, () -> {
+ return new org.dreeam.leaf.async.path.AsyncPath(this, Lists.newArrayList(), targets, finder -> {
+ try {
+ return this.processPath(nodeEvaluator, start, map, maxRange, reachRange, maxVisitedNodesMultiplier);
+ return finder.processPath(nodeEvaluator, start, map, maxRange, reachRange, maxVisitedNodesMultiplier);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
Expand Down
43 changes: 25 additions & 18 deletions leaf-server/src/main/java/org/dreeam/leaf/async/path/AsyncPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.pathfinder.Node;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.level.pathfinder.PathFinder;
import net.minecraft.world.phys.Vec3;
import org.jspecify.annotations.Nullable;

Expand All @@ -12,7 +13,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.Function;

/**
* I'll be using this to represent a path that not be processed yet!
Expand All @@ -28,7 +29,7 @@ public final class AsyncPath extends Path {
*/
private final Set<BlockPos> positions;

private @Nullable Supplier<Path> pathSupplier;
private @Nullable Function<PathFinder, Path> pathFn;

/// Represents an asynchronous task. `null` indicates that is not ready.
private volatile @Nullable Path ret;
Expand All @@ -45,17 +46,20 @@ public final class AsyncPath extends Path {
* While processing, we can always theoretically reach the target so default is true
*/
private boolean canReach = true;
private final PathFinder finder;

@SuppressWarnings("ConstantConditions")
public AsyncPath(List<Node> emptyNodeList, Set<BlockPos> positions, Supplier<Path> pathSupplier) {
super(emptyNodeList, null, false);
public AsyncPath(PathFinder finder, List<Node> emptyNodeList, Set<BlockPos> positions, Function<PathFinder, Path> pathFn) {
super(emptyNodeList, BlockPos.ZERO, false);

this.finder = finder;
this.positions = positions;
this.pathSupplier = pathSupplier;
this.pathFn = pathFn;

AsyncPathProcessor.queue(() -> {
if (this.ret == null) {
this.ret = pathSupplier.get();
synchronized (finder) {
if (this.ret == null) {
this.ret = pathFn.apply(finder);
}
}
});
}
Expand Down Expand Up @@ -94,29 +98,32 @@ public boolean hasSameProcessingPositions(final Set<BlockPos> positions) {
return this.positions.equals(positions);
}

/**
* Starts processing this path
* Since this is no longer a synchronized function, checkProcessed is no longer required
*/
private void process() {
if (this.ready) {
return;
}
final Path ret = this.ret;
final Path bestPath = ret != null ? ret : (this.ret = Objects.requireNonNull(pathSupplier).get());
complete(bestPath);
Path ret = this.ret;
if (ret == null) {
synchronized (finder) {
if ((ret = this.ret) == null) {
ret = (this.ret = Objects.requireNonNull(pathFn).apply(finder));
}
}
}
complete(ret);
}

/// not [#ready]
///
/// @see #isDone
/// @see #process
/// @see #isDone()
/// @see #process()
/// @see #isProcessed()
private void complete(Path bestPath) {
this.nodes = bestPath.nodes;
this.target = bestPath.getTarget();
this.distToTarget = bestPath.getDistToTarget();
this.canReach = bestPath.canReach();
this.pathSupplier = null;
this.pathFn = null;
this.ready = true;
for (Consumer<Path> consumer : this.postProcessing) {
consumer.accept(this);
Expand Down