Skip to content

Commit d64849d

Browse files
committed
Added closest entity function
1 parent 5a3aaa0 commit d64849d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ import kotlin.math.ceil
99

1010

1111
object EntityUtils {
12+
/**
13+
* Gets the closest entity of type [T] within a specified range.
14+
*
15+
* @param pos The position to search from.
16+
* @param range The maximum distance to search for entities.
17+
* @param predicate Optional predicate to filter entities.
18+
* @return The first entity of type [T] that is closest to the position within the specified range.
19+
*/
20+
inline fun <reified T : Entity> SafeContext.getClosestEntity(
21+
pos: Vec3d = player.pos,
22+
range: Double = 6.0,
23+
noinline predicate: (T) -> Boolean = { true },
24+
): T? {
25+
26+
// Speculative execution trolling
27+
val entities =
28+
if (range > 64) getEntities(predicate)
29+
else getFastEntities(pos, range, predicate)
30+
31+
return entities.minByOrNull { it.squaredDistanceTo(pos) } // There is probably a faster way to do this
32+
}
33+
1234
/**
1335
* Gets all entities of type [T] within a specified distance from a position.
1436
*

0 commit comments

Comments
 (0)