11package com.lambda.util.world
22
33import com.lambda.context.SafeContext
4- import com.lambda.util.collections.filterIsInstanceTo
4+ import com.lambda.util.collections.filterPointer
55import net.minecraft.entity.Entity
66import net.minecraft.util.math.ChunkSectionPos
77import net.minecraft.util.math.Vec3d
88import kotlin.math.ceil
99
10- val nullptr = null
11-
1210object 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