|
20 | 20 | import org.jetbrains.annotations.Nullable;
|
21 | 21 | import org.joml.Vector3d;
|
22 | 22 |
|
23 |
| -import java.lang.ref.Reference; |
24 |
| -import java.util.ArrayList; |
25 |
| -import java.util.List; |
26 |
| -import java.util.stream.Stream; |
| 23 | +import java.lang.ref.WeakReference; |
| 24 | +import java.util.*; |
27 | 25 |
|
28 | 26 | /**
|
29 | 27 | * See {@link ContraptionCollider}
|
30 | 28 | */
|
31 | 29 | public class CreateUtils {
|
32 | 30 | public static final boolean[] trueAndFalse = {true, false};
|
33 | 31 |
|
34 |
| - public static Stream<AbstractContraptionEntity> contraptions(ClientLevel level) { |
35 |
| - return ContraptionHandler.loadedContraptions.get(level) |
36 |
| - .values() |
37 |
| - .stream() |
38 |
| - .map(Reference::get); |
| 32 | + public static Collection<WeakReference<AbstractContraptionEntity>> contraptions(ClientLevel level) { |
| 33 | + return loadedContraptions(level).values(); |
39 | 34 | }
|
40 | 35 |
|
41 | 36 | /**
|
@@ -114,18 +109,23 @@ public static boolean collideWithContraption(ClientLevel level, Vec3 originalPos
|
114 | 109 | @Nullable
|
115 | 110 | public static Vec3 collideMotionWithContraptions(ClientLevel level, Vec3 position, Vec3 motion, AABB bounds) {
|
116 | 111 | AABB bounds1 = bounds.inflate(0.1).move(motion);
|
117 |
| - Vector3d collect = contraptions(level) |
118 |
| - .filter(c -> c.getBoundingBox().intersects(bounds1)) |
119 |
| - .map(c -> { |
120 |
| - Vec3 vec3 = collideMotionWithContraption(level, position, motion, bounds1, c); |
121 |
| - return new Vector3d(vec3.x, vec3.y, vec3.z); |
122 |
| - }) |
123 |
| - .reduce(new Vector3d(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE), Vector3d::min); |
124 |
| - if (collect.x == Double.MAX_VALUE |
125 |
| - || (motion.x == collect.x && motion.y == collect.y && motion.z == collect.z)) { |
| 112 | + Vector3d result = new Vector3d(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE); |
| 113 | + for (WeakReference<AbstractContraptionEntity> r : contraptions(level)) { |
| 114 | + AbstractContraptionEntity contraptionEntity = r.get(); |
| 115 | + if (contraptionEntity == null || !contraptionEntity.isAliveOrStale()) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + if (!contraptionEntity.getBoundingBox().intersects(bounds1)) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + Vec3 vec3 = collideMotionWithContraption(level, position, motion, bounds1, contraptionEntity); |
| 122 | + result.set(Math.min(result.x, vec3.x), Math.min(result.y, vec3.y), Math.min(result.z, vec3.z)); |
| 123 | + } |
| 124 | + if (result.x == Double.MAX_VALUE |
| 125 | + || (motion.x == result.x && motion.y == result.y && motion.z == result.z)) { |
126 | 126 | return null;
|
127 | 127 | }
|
128 |
| - return new Vec3(collect.x, collect.y, collect.z); |
| 128 | + return new Vec3(result.x, result.y, result.z); |
129 | 129 | }
|
130 | 130 |
|
131 | 131 | private static Vec3 getWorldToLocalTranslation(Vec3 entityPosition,
|
@@ -358,4 +358,10 @@ public static Vec3 getCenterOf(BlockPos blockPos) {
|
358 | 358 | ? Create5Utils.getCenterOf(blockPos)
|
359 | 359 | : Create6Utils.getCenterOf(blockPos);
|
360 | 360 | }
|
| 361 | + |
| 362 | + public static Map<Integer, WeakReference<AbstractContraptionEntity>> loadedContraptions(ClientLevel level) { |
| 363 | + return ModListHelper.CREATE_MAJOR_VERSION < 6 |
| 364 | + ? Create5Utils.loadedContraptions(level) |
| 365 | + : Create6Utils.loadedContraptions(level); |
| 366 | + } |
361 | 367 | }
|
0 commit comments