File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
common/src/main/kotlin/com/lambda/util/world Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,28 @@ import kotlin.math.ceil
99
1010
1111object 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 *
You can’t perform that action at this time.
0 commit comments