From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Sotr Date: Wed, 15 Apr 2020 17:49:07 +0700 Subject: [PATCH] Remove stream and simplify operation in pathfinder diff --git a/src/main/java/net/minecraft/server/PathDestination.java b/src/main/java/net/minecraft/server/PathDestination.java index 9071d43d8b85a64a11566b2b535c3f6db65028f8..2f33aafe34d9cfd664abf006f8dbdf5cb981b692 100644 --- a/src/main/java/net/minecraft/server/PathDestination.java +++ b/src/main/java/net/minecraft/server/PathDestination.java @@ -5,6 +5,7 @@ public class PathDestination extends PathPoint { private float m = Float.MAX_VALUE; private PathPoint n; private boolean o; + protected BlockPosition position; // Akarin - add BlockPosition public PathDestination(PathPoint pathpoint) { super(pathpoint.a, pathpoint.b, pathpoint.c); diff --git a/src/main/java/net/minecraft/server/Pathfinder.java b/src/main/java/net/minecraft/server/Pathfinder.java index 848e2fe8b84bfec5be332fd051512eda3838c108..379ee96b0801faaffc83c9f1b7473bc4b493bb6f 100644 --- a/src/main/java/net/minecraft/server/Pathfinder.java +++ b/src/main/java/net/minecraft/server/Pathfinder.java @@ -2,7 +2,9 @@ package net.minecraft.server; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -31,9 +33,20 @@ public class Pathfinder { this.a.a(); this.e.a(chunkcache, entityinsentient); PathPoint pathpoint = this.e.b(); + // Akarin start - remove stream, add BlockPosition + /* Map map = (Map) set.stream().collect(Collectors.toMap((blockposition) -> { - return this.e.a((double) blockposition.getX(), (double) blockposition.getY(), (double) blockposition.getZ()); + return this.e.a((double) blockposition.getX(), (double) blockposition.getY(), (double) blockposition.getZ()); }, Function.identity())); + */ + List map = new java.util.ArrayList(set.size()); + for (BlockPosition position : set) { + PathDestination dest = this.e.a((double) position.getX(), position.getY(), position.getZ()); + dest.position = position; + + map.add(dest); + } + // Akarin end PathEntity pathentity = this.a(pathpoint, map, f, i, f1); this.e.a(); @@ -41,8 +54,8 @@ public class Pathfinder { } @Nullable - private PathEntity a(PathPoint pathpoint, Map map, float f, int i, float f1) { - Set set = map.keySet(); + private PathEntity a(PathPoint pathpoint, List map, float f, int i, float f1) { // Akarin - Map -> List + List set = map; // Akarin - Map -> List pathpoint.e = 0.0F; pathpoint.f = this.a(pathpoint, set); @@ -53,6 +66,7 @@ public class Pathfinder { int j = 0; int k = (int) ((float) this.d * f1); + List destinations = new java.util.ArrayList(); // Akarin - remove stream while (!this.a.e()) { ++j; if (j >= k) { @@ -62,12 +76,26 @@ public class Pathfinder { PathPoint pathpoint1 = this.a.c(); pathpoint1.i = true; + // Akarin start - remove stream + /* set.stream().filter((pathdestination) -> { return pathpoint1.c((PathPoint) pathdestination) <= (float) i; }).forEach(PathDestination::e); if (set.stream().anyMatch(PathDestination::f)) { break; } + */ + for (PathDestination dest : set) { + if (pathpoint1.c((PathPoint) dest) <= (float) i) + dest.e(); + + if (dest.f()) + destinations.add(createPathEntityBy(dest.d(), dest.position, true)); // copied from below + } + + if (!destinations.isEmpty()) + break; + // Akarin end if (pathpoint1.a(pathpoint) < f) { int l = this.e.a(this.c, pathpoint1); @@ -94,15 +122,17 @@ public class Pathfinder { } } + // Akarin start - remove stream + /* Stream stream; if (set.stream().anyMatch(PathDestination::f)) { stream = set.stream().filter(PathDestination::f).map((pathdestination) -> { - return this.a(pathdestination.d(), (BlockPosition) map.get(pathdestination), true); + return this.a(pathdestination.d(), (BlockPosition) pathdestination.position, true); }).sorted(Comparator.comparingInt(PathEntity::e)); } else { stream = set.stream().map((pathdestination) -> { - return this.a(pathdestination.d(), (BlockPosition) map.get(pathdestination), false); + return this.a(pathdestination.d(), (BlockPosition) pathdestination.position, false); }).sorted(Comparator.comparingDouble(PathEntity::l).thenComparingInt(PathEntity::e)); } @@ -115,9 +145,21 @@ public class Pathfinder { return pathentity; } + */ + if (!destinations.isEmpty()) { + Collections.sort(destinations, Comparator.comparingInt(PathEntity::e)); + } else { + for (PathDestination dest : set) + destinations.add(createPathEntityBy(dest.d(), dest.position, false)); + + Collections.sort(destinations, Comparator.comparingDouble(PathEntity::l).thenComparingInt(PathEntity::e)); + } + + return destinations.get(0); + // Akarin end } - private float a(PathPoint pathpoint, Set set) { + private float a(PathPoint pathpoint, List set) { // Akarin - Set -> List float f = Float.MAX_VALUE; float f1; @@ -132,6 +174,7 @@ public class Pathfinder { return f; } + private PathEntity createPathEntityBy(PathPoint pathpoint, BlockPosition blockposition, boolean flag) { return this.a(pathpoint, blockposition, flag); } // Akarin - OBFHELPER private PathEntity a(PathPoint pathpoint, BlockPosition blockposition, boolean flag) { List list = Lists.newArrayList(); PathPoint pathpoint1 = pathpoint;