Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import com.lambda.util.math.minus
import com.lambda.util.math.plus
import com.lambda.util.player.SlotUtils.hotbarStacks
import com.lambda.util.world.fastEntitySearch
import com.lambda.util.world.toFastVec
import net.minecraft.block.Blocks
import net.minecraft.entity.Entity
import net.minecraft.entity.LivingEntity
Expand Down Expand Up @@ -356,7 +357,7 @@ object CrystalAura : Module(
// Exclude blocks blocked by entities
val crystalBox = pos.crystalBox

val entitiesNearby = fastEntitySearch<Entity>(3.5, pos)
val entitiesNearby = fastEntitySearch<Entity>(3.5, pos.toFastVec())
val crystals = entitiesNearby.filterIsInstance<EndCrystalEntity>()
val otherEntities = entitiesNearby - crystals + player

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/lambda/util/world/WorldDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ inline fun <reified T : Entity> SafeContext.entitySearch(
@EntityMarker
inline fun <reified T : Entity> SafeContext.fastEntitySearch(
range: Double,
pos: BlockPos = player.blockPos,
pos: FastVector = player.pos.toFastVec(),
noinline filter: (T) -> Boolean = { true },
) = internalGetFastEntities<T>(pos.toFastVec(), range, filter = filter)
) = internalGetFastEntities<T>(pos, range, filter = filter)

@DslMarker
annotation class FluidMarker
Expand Down
17 changes: 16 additions & 1 deletion src/main/kotlin/com/lambda/util/world/WorldUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import net.minecraft.entity.Entity
import net.minecraft.fluid.Fluid
import net.minecraft.fluid.FluidState
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Box
import net.minecraft.util.math.ChunkSectionPos
import net.minecraft.util.math.Vec3d
import kotlin.collections.asSequence
import kotlin.math.ceil
import kotlin.sequences.filter
Expand Down Expand Up @@ -69,7 +71,7 @@ object WorldUtils {
?.filterIsInstance<T>()
?.filter {
it != player &&
pos distSq it.pos <= distance * distance &&
getDistSqToClosestSideOfBoundBox(pos.toVec3d(), it.boundingBox) <= distance * distance &&
filter(it)
} ?: emptySequence()
)
Expand All @@ -79,6 +81,19 @@ object WorldUtils {
}
}

/**
* Returns the distance squared to the closest side of a [Box] to the specified [point]
*/
fun getDistSqToClosestSideOfBoundBox(point: Vec3d, box: Box): Double {
Comment thread
TwinkNet marked this conversation as resolved.
Outdated
val closestX = point.x.coerceIn(box.minX, box.maxX)
val closestY = point.y.coerceIn(box.minY, box.maxY)
val closestZ = point.z.coerceIn(box.minZ, box.maxZ)
val dx = point.x - closestX
val dy = point.y - closestY
val dz = point.z - closestZ
return dx * dx + dy * dy + dz * dz
}

/**
* Returns a sequence of entities.
* Unlike [internalGetFastEntities], it traverses all entities in the world to find matches.
Expand Down