@@ -8,32 +8,34 @@ import net.minecraft.util.math.Vec3d
88import kotlin.math.ceil
99
1010
11- /* *
12- * Utility class for working with entities in a Minecraft environment.
13- */
1411object EntityUtils {
15-
16- /* *
17- * Gets the closest entity of type [T] within a specified range.
18- *
19- * @param pos The position to search from.
20- * @param range The maximum distance to search for entities.
21- * @param predicate Optional predicate to filter entities.
22- * @return The closest entity of type [T] within the specified range, or null if none is found.
23- */
24- inline fun <reified T : Entity > SafeContext.getClosestEntity (
25- pos : Vec3d ,
26- range : Double ,
27- noinline predicate : (T ) -> Boolean = { true },
28- ): T ? = getFastEntities(pos, range, predicate).firstOrNull { it.pos.squaredDistanceTo(pos) <= range * range }
29-
3012 /* *
3113 * Gets all entities of type [T] within a specified distance from a position.
3214 *
15+ * This function retrieves entities of type [T] within a specified distance from a given position. It efficiently
16+ * queries nearby chunks based on the distance and returns a list of matching entities, excluding the player entity.
17+ *
18+ *
19+ * Getting all Zombie entities within a certain distance:
20+ * ```
21+ * val nearbyZombies = getFastEntities<ZombieEntity>(playerPos, 20.0)
22+ * ```
23+ *
24+ * Getting all hostile entities within a certain distance:
25+ * ```
26+ * val hostileEntities = getFastEntities<HostileEntity>(playerPos, 30.0)
27+ * ```
28+ * This fetches all hostile entities (e.g., Monsters) within a 30-block radius from the player's position.
29+ *
30+ * Please note that this implementation is optimized for performance at small distances. For larger distances, it is
31+ * recommended to use the [getEntities] function instead.
32+ * With the time complexity, we can determine that after 64 blocks, the performance of this function will degrade.
33+ *
3334 * @param pos The position to search from.
3435 * @param distance The maximum distance to search for entities.
35- * @param predicate Optional predicate to filter entities.
36- * @return A list of entities of type [T] within the specified distance from the position.
36+ * @param predicate Optional predicate to filter entities. It allows custom filtering based on entity properties.
37+ * @return A list of entities of type [T] within the specified distance from the position, excluding the player.
38+ *
3739 */
3840 inline fun <reified T : Entity > SafeContext.getFastEntities (
3941 pos : Vec3d ,
@@ -61,4 +63,23 @@ object EntityUtils {
6163
6264 return entities
6365 }
66+
67+ /* *
68+ * Gets all entities of type [T] within a specified distance from a position.
69+ *
70+ * This function retrieves entities of type [T] within a specified distance from a given position. Unlike
71+ * [getFastEntities], it traverses all entities in the world to find matches, while also excluding the player entity.
72+ *
73+ * @param predicate Optional predicate to filter entities.
74+ * @return A list of entities of type [T] within the specified distance from the position without the player.
75+ */
76+ inline fun <reified T : Entity > SafeContext.getEntities (noinline predicate : (T ) -> Boolean = { true }): List <T > {
77+ val entities = ArrayList <T >()
78+
79+ world.entities.filterIsInstanceTo(entities) { entity ->
80+ entity != player && predicate(entity)
81+ }
82+
83+ return entities
84+ }
6485}
0 commit comments