Skip to content

Commit

Permalink
Changed Toggleable to Killable for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
freedownloadhere committed Sep 7, 2024
1 parent 75b646a commit fb81d0a
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent
import java.time.Instant

class EventManager {
private var lastTime = Instant.now().toEpochMilli()

@SubscribeEvent
fun onTick(e : ClientTickEvent) {
fun fixedUpdate(e : ClientTickEvent) {
if(!BotState.ingame) return
if(e.phase != TickEvent.Phase.END) return

TerrainTraversal.updateRouteTraversal()
AutoFighter.update()
}

private var lastTime = Instant.now().toEpochMilli()

@SubscribeEvent
fun onRenderTick(e : TickEvent.RenderTickEvent) {
fun nonFixedUpdate(e : TickEvent.RenderTickEvent) {
if(!BotState.ingame) return

val newTime = Instant.now().toEpochMilli()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
package com.github.freedownloadhere.pitplayer.combat

import com.github.freedownloadhere.pitplayer.extensions.*
import com.github.freedownloadhere.pitplayer.interfaces.Toggleable
import com.github.freedownloadhere.pitplayer.interfaces.Killable
import com.github.freedownloadhere.pitplayer.utils.RandomHelper
import net.minecraft.util.Vec3
import kotlin.math.atan2
import kotlin.math.hypot
import kotlin.math.min

object AimHelper : Toggleable("Aim Helper", true) {
object AimHelper : Killable("Aim Helper") {
private var aimTime = 100L
private var currTime = 0L
private var shouldUpdate = false

private var maxError = 0.2f
private var delta = ViewAngles()

fun lookAt(pos : Vec3) : AimHelper {
if(!toggled) return this

val error = RandomHelper.fromRange(1.0f - maxError .. 1.0f + maxError)
delta = calcDelta(pos) * error

currTime = 0L
shouldUpdate = true

return this
}

fun update(deltaTime : Long) {
if(!toggled) return
if(killed) return
if(!shouldUpdate) return

currTime = min(aimTime, currTime + deltaTime)
Expand All @@ -43,8 +31,20 @@ object AimHelper : Toggleable("Aim Helper", true) {
gradualAim(deltaTime)
}

fun lookAt(pos : Vec3) : AimHelper {
if(killed) return this

val error = RandomHelper.fromRange(1.0f - maxError .. 1.0f + maxError)
delta = calcDelta(pos) * error

currTime = 0L
shouldUpdate = true

return this
}

fun setVA(yaw : Float?, pitch : Float?) : AimHelper {
if(!toggled) return this
if(killed) return this

if(yaw != null) player.rotationYaw = yaw
if(pitch != null) player.rotationPitch = pitch
Expand All @@ -53,7 +53,7 @@ object AimHelper : Toggleable("Aim Helper", true) {
}

fun pitch(pitch : Float?) : AimHelper {
if(!toggled) return this
if(killed) return this

val deltaPitch = if(pitch == null) 0.0f else pitch - player.rotationPitch
delta = ViewAngles(delta.yaw, deltaPitch)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
package com.github.freedownloadhere.pitplayer.combat

import com.github.freedownloadhere.pitplayer.extensions.settings
import com.github.freedownloadhere.pitplayer.interfaces.Toggleable
import com.github.freedownloadhere.pitplayer.interfaces.Killable
import com.github.freedownloadhere.pitplayer.utils.KeyBindHelper
import kotlinx.coroutines.delay

object AutoClicker : Toggleable("AutoClicker") {
private var cps = 10L
set(v) {
field = v
timeBeforeNextClick = 1000L / v
}
import com.github.freedownloadhere.pitplayer.utils.RandomHelper

object AutoClicker : Killable("Auto Clicker") {
// 10 cps default
private var timeBeforeNextClick = 100L
private var lastClickTime = 0L
private var maxErrorTime = 10L
private var shouldUpdate = false

fun update(deltaTime : Long) {
if(cps < 1) disable()
if(!toggled) return
if(killed) return
if(!shouldUpdate) return

lastClickTime += deltaTime
val errorTime = RandomHelper.fromRange(-maxErrorTime .. maxErrorTime)
lastClickTime += deltaTime + errorTime

if(lastClickTime >= timeBeforeNextClick) {
KeyBindHelper.press(settings.keyBindAttack)
lastClickTime = 0L
}
}

fun cps(cps : Long) {
timeBeforeNextClick = 1000L / cps
}

fun start() {
shouldUpdate = true
}

fun stop() {
shouldUpdate = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.freedownloadhere.pitplayer.extensions.eyePosition
import com.github.freedownloadhere.pitplayer.extensions.player
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.interfaces.Killable
import com.github.freedownloadhere.pitplayer.utils.KeyBindHelper
import net.minecraft.client.settings.KeyBinding
import net.minecraft.entity.Entity
Expand All @@ -14,13 +14,14 @@ import net.minecraft.entity.player.EntityPlayer
import kotlin.math.max
import kotlin.random.Random

object AutoFighter : Toggleable("AutoFighter", true) {
object AutoFighter : Killable("AutoFighter") {
enum class SprintResetMethod(val key : KeyBinding) {
WTap(settings.keyBindForward),
STap(settings.keyBindBack),
Blockhit(settings.keyBindUseItem),
SneakTap(settings.keyBindSneak)
}

enum class StrafeDirection(val key : KeyBinding) {
Left(settings.keyBindLeft),
Right(settings.keyBindRight)
Expand All @@ -31,11 +32,36 @@ object AutoFighter : Toggleable("AutoFighter", true) {
var sprintResetTicks : Int = 0
private set
var justAttacked : Boolean = false
var sprintResetMethod : SprintResetMethod = SprintResetMethod.SneakTap
var strafeDirection : StrafeDirection = StrafeDirection.Left
var strafeSwapCooldown : Int = 10

private var sprintResetMethod : SprintResetMethod = SprintResetMethod.SneakTap
private var strafeDirection : StrafeDirection = StrafeDirection.Left
private var strafeSwapCooldown : Int = 10

fun update() {
if(killed) return
if(target?.isDead == true) onTargetLost()
if(target == null) return

AimHelper.lookAt(target!!.eyePosition)

doStrafe()

if(justAttacked) {
sprintResetTicks = 5
justAttacked = false
}

if(sprintResetTicks > 0) {
sprintReset()
return
}

KeyBindHelper.press(settings.keyBindForward)
}

fun findTarget() {
if(killed) return

val entityList = world.loadedEntityList
val playerPos = player.positionVector
var bestDist = Double.MAX_VALUE
Expand All @@ -61,28 +87,7 @@ object AutoFighter : Toggleable("AutoFighter", true) {

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

fun update() {
if(target?.isDead == true) onTargetLost()
if(target == null) return

AimHelper.lookAt(target!!.eyePosition)

doStrafe()

if(justAttacked) {
sprintResetTicks = 5
justAttacked = false
}

if(sprintResetTicks > 0) {
sprintReset()
return
}

KeyBindHelper.press(settings.keyBindForward)
AutoClicker.start()
}

private fun sprintReset() {
Expand All @@ -100,9 +105,9 @@ object AutoFighter : Toggleable("AutoFighter", true) {
}

private fun onTargetLost() {
target = null
AutoClicker.disable()
Debug.Logger.regular("Target was lost")
target = null
AutoClicker.stop()
}

private fun pickStrafeDirection() : StrafeDirection {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.freedownloadhere.pitplayer.interfaces

import com.github.freedownloadhere.pitplayer.debug.Debug

/*
This should only be used for debugging purposes.
It's meant to entirely halt any class that misbehaves during testing.
*/
open class Killable(private val moduleName : String, kill : Boolean = false) {
var killed : Boolean = kill
private set
fun killToggle() {
if(killed) unkill()
else kill()
}
fun kill() {
killed = true
Debug.Logger.regular("Killed \u00A7a$moduleName")
}
fun unkill() {
killed = false
Debug.Logger.regular("Un-killed \u00A7c$moduleName")
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.github.freedownloadhere.pitplayer.utils

import com.github.freedownloadhere.pitplayer.interfaces.Toggleable
import com.github.freedownloadhere.pitplayer.interfaces.Killable
import com.github.freedownloadhere.pitplayer.mixin.AccessorKeyBinding
import net.minecraft.client.settings.KeyBinding

object KeyBindHelper : Toggleable("Keybind Helper", true) {
object KeyBindHelper : Killable("Keybind Helper") {
private val affectedKeys = mutableSetOf<AccessorKeyBinding>()

fun reset() {
if(killed) return

for(key in affectedKeys) {
key.pressed_pitplayer = false
key.pressTime_pitplayer = 0
Expand All @@ -16,15 +18,17 @@ object KeyBindHelper : Toggleable("Keybind Helper", true) {
}

fun press(key : KeyBinding) {
if(!toggled) return
if(killed) return

val accessor = key as AccessorKeyBinding
affectedKeys.add(accessor)
accessor.pressed_pitplayer = true
accessor.pressTime_pitplayer = 1
}

fun release(key : KeyBinding) {
if(!toggled) return
if(killed) return

val accessor = key as AccessorKeyBinding
affectedKeys.add(accessor)
accessor.pressed_pitplayer = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.freedownloadhere.pitplayer.utils

import com.github.freedownloadhere.pitplayer.utils.KeyBindHelper
import net.minecraft.client.settings.KeyBinding
import org.lwjgl.input.Keyboard

enum class Keybinds(val key: KeyBinding, val action: () -> Unit) {
ToggleRemote(KeyBinding("key.pitplayer.toggleremote", Keyboard.KEY_J, "key.category.pitplayer"), { KeyBindHelper.toggle() });
ToggleRemote(KeyBinding("key.pitplayer.toggleremote", Keyboard.KEY_J, "key.category.pitplayer"), { KeyBindHelper.killToggle() });
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ object RandomHelper {
fun fromRange(range : ClosedFloatingPointRange<Float>) : Float {
return range.start + (range.endInclusive - range.start) * Random.nextFloat()
}

fun fromRange(range : LongRange) : Long {
return range.first + (range.last - range.first) * Random.nextLong()
}
}

0 comments on commit fb81d0a

Please sign in to comment.