Skip to content

Commit cf7691b

Browse files
committed
Fix: Entity Utils
1 parent 07204bf commit cf7691b

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

common/src/main/kotlin/com/lambda/util/collections/Extensions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ package com.lambda.util.collections
1313
* @param destination The mutable collection to which the filtered elements will be added.
1414
* @param predicate The predicate function that determines whether an element should be included based on its type and other criteria.
1515
*/
16-
inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsInstanceTo(
16+
inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterPointer(
1717
destination: C? = null,
18-
predicate: (R) -> Boolean
18+
iterator: (R) -> Unit,
19+
predicate: (R) -> Boolean,
1920
) {
20-
if (destination == null) return
21-
for (element in this) if (element is R && predicate(element)) destination.add(element)
21+
for (element in this) if (element is R && predicate(element)) { iterator(element); destination?.add(element) }
2222
}

common/src/main/kotlin/com/lambda/util/world/EntityUtils.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.lambda.util.world
22

33
import com.lambda.context.SafeContext
4-
import com.lambda.util.collections.filterIsInstanceTo
4+
import com.lambda.util.collections.filterPointer
55
import net.minecraft.entity.Entity
66
import net.minecraft.util.math.ChunkSectionPos
77
import net.minecraft.util.math.Vec3d
88
import kotlin.math.ceil
99

10-
val nullptr = null
11-
1210
object EntityUtils {
1311
/**
1412
* Gets the closest entity of type [T] within a specified range.
@@ -26,6 +24,7 @@ object EntityUtils {
2624
var closest: T? = null
2725
var closestDistance = Double.MAX_VALUE
2826

27+
// change this to not create a new lambda every time
2928
val iterator: (T) -> Unit = {
3029
val distance = it.squaredDistanceTo(pos)
3130
if (distance < closestDistance) {
@@ -35,8 +34,8 @@ object EntityUtils {
3534
}
3635

3736
// Speculative execution trolling
38-
if (range > 64) getEntities(nullptr, predicate, iterator)
39-
else getFastEntities(pos, range, nullptr, predicate, iterator)
37+
if (range > 64) getEntities(null, predicate, iterator)
38+
else getFastEntities(pos, range, null, predicate, iterator)
4039

4140
return closest
4241
}
@@ -74,7 +73,7 @@ object EntityUtils {
7473
inline fun <reified T : Entity> SafeContext.getFastEntities(
7574
pos: Vec3d,
7675
distance: Double,
77-
pointer: MutableList<T>? = nullptr,
76+
pointer: MutableList<T>? = null,
7877
noinline predicate: (T) -> Boolean = { true },
7978
noinline iterator: (T) -> Unit = { },
8079
) {
@@ -90,8 +89,7 @@ object EntityUtils {
9089
for (y in sectionY - chunks..sectionY + chunks) {
9190
for (z in sectionZ - chunks..sectionZ + chunks) {
9291
val section = world.entityManager.cache.findTrackingSection(ChunkSectionPos.asLong(x, y, z)) ?: continue
93-
section.collection.filterIsInstanceTo(pointer) { entity ->
94-
iterator(entity)
92+
section.collection.filterPointer(pointer, iterator) { entity ->
9593
entity != player && entity.squaredDistanceTo(pos) <= distance * distance && predicate(entity)
9694
}
9795
}
@@ -110,12 +108,11 @@ object EntityUtils {
110108
* @param iterator Optional iterator to perform operations on each entity.
111109
*/
112110
inline fun <reified T : Entity> SafeContext.getEntities(
113-
pointer: MutableList<T>? = nullptr,
111+
pointer: MutableList<T>? = null,
114112
noinline predicate: (T) -> Boolean = { true },
115113
noinline iterator: (T) -> Unit = { },
116114
) {
117-
world.entities.filterIsInstanceTo(pointer) { entity ->
118-
iterator(entity)
115+
world.entities.filterPointer(pointer, iterator) { entity ->
119116
entity != player && predicate(entity)
120117
}
121118
}

0 commit comments

Comments
 (0)