Skip to content

Commit d66910d

Browse files
authored
account for non-living entities in Targeting (#266)
1 parent 6f0375f commit d66910d

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

src/main/kotlin/com/lambda/config/groups/Targeting.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ import com.lambda.interaction.managers.rotating.Rotation.Companion.dist
2626
import com.lambda.interaction.managers.rotating.Rotation.Companion.rotation
2727
import com.lambda.interaction.managers.rotating.Rotation.Companion.rotationTo
2828
import com.lambda.threading.runSafe
29-
import com.lambda.util.EntityUtils.EntityGroup
30-
import com.lambda.util.EntityUtils.entityGroup
3129
import com.lambda.util.NamedEnum
3230
import com.lambda.util.extension.fullHealth
3331
import com.lambda.util.math.distSq
3432
import com.lambda.util.world.fastEntitySearch
3533
import net.minecraft.client.network.ClientPlayerEntity
3634
import net.minecraft.client.network.OtherClientPlayerEntity
37-
import net.minecraft.client.toast.SystemToast.hide
35+
import net.minecraft.entity.Entity
3836
import net.minecraft.entity.LivingEntity
3937
import java.util.*
4038

@@ -76,7 +74,7 @@ abstract class Targeting(
7674
* @param entity The [LivingEntity] being evaluated.
7775
* @return `true` if the entity is valid for targeting, `false` otherwise.
7876
*/
79-
open fun validate(player: ClientPlayerEntity, entity: LivingEntity) =
77+
open fun validate(player: ClientPlayerEntity, entity: Entity) =
8078
targets.isSelected(entity) && (entity !is OtherClientPlayerEntity || !entity.isFriend)
8179

8280
/**
@@ -107,23 +105,23 @@ abstract class Targeting(
107105
* Validates whether a given entity is targetable for combat based on the field of view limit and other settings.
108106
*
109107
* @param player The [ClientPlayerEntity] performing the targeting.
110-
* @param entity The [LivingEntity] being evaluated.
108+
* @param entity The [Entity] being evaluated.
111109
* @return `true` if the entity is valid for targeting, `false` otherwise.
112110
*/
113-
override fun validate(player: ClientPlayerEntity, entity: LivingEntity): Boolean {
111+
override fun validate(player: ClientPlayerEntity, entity: Entity): Boolean {
114112
if (fov < 180 && player.rotation dist player.eyePos.rotationTo(entity.pos) > fov) return false
115113
if (entity.uuid in illegalTargets) return false
116-
if (entity.isDead) return false
114+
if ((entity as? LivingEntity)?.isDead == true) return false
117115
return super.validate(player, entity)
118116
}
119117

120118
/**
121119
* Gets the best target for combat based on the current settings and priority.
122120
*
123-
* @return The best [LivingEntity] target, or `null` if no valid target is found.
121+
* @return The best [Entity] target, or `null` if no valid target is found.
124122
*/
125-
fun target(): LivingEntity? = runSafe {
126-
return@runSafe fastEntitySearch<LivingEntity>(targetingRange) {
123+
inline fun <reified T : Entity> target(): T? = runSafe {
124+
return@runSafe fastEntitySearch<T>(targetingRange) {
127125
validate(player, it)
128126
}.minByOrNull {
129127
priority.factor(this, it)
@@ -140,19 +138,21 @@ abstract class Targeting(
140138
/**
141139
* Enum representing the different priority factors used for determining the best target.
142140
*
143-
* @property factor A lambda function that calculates the priority factor for a given [LivingEntity].
141+
* @property factor A lambda function that calculates the priority factor for a given [Entity].
144142
*/
145143
@Suppress("Unused")
146-
enum class Priority(val factor: SafeContext.(LivingEntity) -> Double) {
144+
enum class Priority(val factor: SafeContext.(Entity) -> Double) {
147145
/**
148146
* Prioritizes entities based on their distance from the player.
149147
*/
150148
Distance({ player.pos distSq it.pos }),
151149

152150
/**
153151
* Prioritizes entities based on their health.
152+
* Entities that aren't an instanceof LivingEntity will be treated as if they have Double.MAX_VALUE health,
153+
* therefore having least priority
154154
*/
155-
Health({ it.fullHealth }),
155+
Health({ (it as? LivingEntity)?.fullHealth ?: Double.MAX_VALUE }),
156156

157157
/**
158158
* Prioritizes entities based on their angle relative to the player's field of view.

src/main/kotlin/com/lambda/module/modules/combat/CrystalAura.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ object CrystalAura : Module(
245245

246246
private fun SafeContext.tick() {
247247
// Update the target
248-
currentTarget = targeting.target()
248+
currentTarget = targeting.target<LivingEntity>()
249249

250250
// Update the blueprint
251251
currentTarget?.let {

src/main/kotlin/com/lambda/module/modules/combat/KillAura.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import com.lambda.util.item.ItemStackUtils.attackDamage
3636
import com.lambda.util.item.ItemStackUtils.attackSpeed
3737
import com.lambda.util.math.random
3838
import com.lambda.util.player.SlotUtils.hotbarStacks
39-
import net.minecraft.entity.LivingEntity
39+
import net.minecraft.entity.Entity
4040
import net.minecraft.item.ItemStack
4141
import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket
4242
import net.minecraft.util.Hand
@@ -59,8 +59,8 @@ object KillAura : Module(
5959
// Targeting
6060
private val targeting = Targeting.Combat(c = this, baseGroup = arrayOf(Group.Targeting))
6161

62-
val target: LivingEntity?
63-
get() = targeting.target()
62+
val target: Entity?
63+
get() = targeting.target<Entity>()
6464

6565
private var prevEntity = target
6666
private var validServerRot = false

src/main/kotlin/com/lambda/module/modules/movement/BackTrack.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ object BackTrack : Module(
9595
init {
9696
listen<TickEvent.Pre> {
9797
val prevTarget = target
98-
target = if (KillAura.isDisabled) null else KillAura.target
98+
target = KillAura.target.takeIf { KillAura.isEnabled } as? LivingEntity
9999
val currentTarget = target
100100

101101
if (prevTarget != currentTarget || currentTarget == null) {

0 commit comments

Comments
 (0)