Skip to content

Commit

Permalink
auto attacker iteration 2
Browse files Browse the repository at this point in the history
  • Loading branch information
freedownloadhere committed Sep 4, 2024
1 parent 5985d1a commit fae7a20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import com.github.freedownloadhere.pitplayer.extensions.settings
import com.github.freedownloadhere.pitplayer.extensions.world
import com.github.freedownloadhere.pitplayer.interfaces.Toggleable
import com.github.freedownloadhere.pitplayer.pathing.movement.PlayerControlHelper
import kotlinx.coroutines.delay
import net.minecraft.entity.EntityLiving
import kotlin.random.Random

object AutoFighter : Toggleable(true) {
var target : EntityLiving? = null
private set
var attackTicks : Int = 0
private set
var justAttacked : Boolean = false

fun findTarget() {
val entityList = world.loadedEntityList
val playerPos = player.positionVector
var bestDist = Double.MAX_VALUE

target = null
for(entity in entityList) {
if(entity == null) continue
if(entity == player) continue
Expand All @@ -33,26 +34,39 @@ object AutoFighter : Toggleable(true) {
bestDist = dist;
target = entity
}

if(target != null)
onFoundTarget()
}

private fun onFoundTarget() {
Debug.Logger.regular("Found target \u00A73${target!!.name}")
AutoClicker.enable()
}

fun update() {
if(target?.isDead == true)
onTargetLost()
if(target == null)
return
AutoClicker.enable()
PlayerControlHelper.lookAt(target!!)
if(justAttacked) {
attackTicks = 4
justAttacked = false
}
if(attackTicks > 0) {
onAttack()
return
}
PlayerControlHelper.press(settings.keyBindForward)
}

suspend fun onAttack() {
if(justAttacked) return
fun onAttack() {
Debug.Logger.regular("Attacked target..")
justAttacked = true
PlayerControlHelper.press(settings.keyBindUseItem)
delay(50L)
attackTicks--
if(attackTicks > 0) return
PlayerControlHelper.release(settings.keyBindUseItem)
justAttacked = false
}

private fun onTargetLost() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ class EventManager {
AutoFighter.update()
}

@OptIn(DelicateCoroutinesApi::class)
@SubscribeEvent
fun onAttackEntity(e : AttackEntityEvent) {
if(e.target == null) return
if(e.target == AutoFighter.target)
GlobalScope.launch { AutoFighter.onAttack() }
if(e.target == AutoFighter.target && AutoFighter.attackTicks == 0)
AutoFighter.justAttacked = true
}

@SubscribeEvent
Expand Down

0 comments on commit fae7a20

Please sign in to comment.