Skip to content

Commit 99d4b8f

Browse files
committed
Refactored movement handlers
1 parent e57a611 commit 99d4b8f

File tree

4 files changed

+36
-52
lines changed

4 files changed

+36
-52
lines changed

common/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
5555
@Redirect(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/input/Input;tick(ZF)V"))
5656
void processMovement(Input input, boolean slowDown, float slowDownFactor) {
5757
input.tick(slowDown, slowDownFactor);
58+
RotationManager.BaritoneProcessor.processPlayerMovement(input, slowDown, slowDownFactor);
5859
EventFlow.post(new MovementEvent.InputUpdate(input, slowDown, slowDownFactor));
5960
}
6061

common/src/main/kotlin/com/lambda/event/events/RotationEvent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class RotationEvent : Event {
2424
* @property strafeYaw The angle at which the player will move when pressing W
2525
* Changing this value will never force the anti cheat to flag you because RotationManager is designed to modify the key input instead
2626
*/
27-
class Strafe(var strafeYaw: Double, val input: Input) : RotationEvent()
27+
class StrafeInput(var strafeYaw: Double, val input: Input) : RotationEvent()
2828

2929
class Post(val context: RotationContext) : RotationEvent()
3030
}

common/src/main/kotlin/com/lambda/interaction/RotationManager.kt

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.lambda.interaction
22

33
import com.lambda.Lambda.mc
4-
import com.lambda.config.groups.IRotationConfig
54
import com.lambda.config.groups.RotationSettings
65
import com.lambda.core.Loadable
7-
import com.lambda.context.SafeContext
86
import com.lambda.event.EventFlow.post
97
import com.lambda.event.events.*
108
import com.lambda.event.listener.SafeListener.Companion.listener
@@ -20,19 +18,17 @@ import com.lambda.threading.runSafe
2018
import com.lambda.util.math.MathUtils.lerp
2119
import com.lambda.util.math.MathUtils.toRadian
2220
import com.lambda.util.math.Vec2d
23-
import com.lambda.util.player.MovementUtils.handledByBaritone
2421
import com.lambda.util.primitives.extension.partialTicks
2522
import com.lambda.util.primitives.extension.rotation
23+
import net.minecraft.client.input.Input
2624
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket
27-
import net.minecraft.util.math.MathHelper
28-
import kotlin.math.roundToInt
29-
import kotlin.math.sign
25+
import kotlin.math.*
3026

3127
object RotationManager : Loadable {
3228
var currentRotation = Rotation.ZERO
3329
var prevRotation = Rotation.ZERO
3430

35-
var currentContext: RotationContext? = null
31+
private var currentContext: RotationContext? = null
3632

3733
private var keepTicks = 0
3834
private var pauseTicks = 0
@@ -175,12 +171,6 @@ object RotationManager : Loadable {
175171
270.0, 315.0,
176172
)
177173

178-
init {
179-
listener<MovementEvent.InputUpdate>(Int.MAX_VALUE) {
180-
processPlayerMovement(it)
181-
}
182-
}
183-
184174
@JvmStatic
185175
fun handleBaritoneRotation(yaw: Float, pitch: Float) {
186176
baritoneContext = RotationContext(Rotation(yaw, pitch), Baritone.rotation.apply {
@@ -189,47 +179,45 @@ object RotationManager : Loadable {
189179
})
190180
}
191181

192-
private fun SafeContext.processPlayerMovement(event: MovementEvent.InputUpdate) {
193-
val config = currentContext?.config ?: return
182+
@JvmStatic
183+
fun processPlayerMovement(input: Input, slowDown: Boolean, slowDownFactor: Float) = runSafe {
184+
// The yaw relative to which the movement was constructed
185+
val baritoneYaw = baritoneContext?.rotation?.yaw
186+
val strafeEvent = RotationEvent.StrafeInput(baritoneYaw ?: player.yaw.toDouble(), input)
187+
val movementYaw = strafeEvent.post().strafeYaw
194188

195-
val input = event.input
189+
// No changes are needed, when we don't modify the yaw used to move the player
190+
// val config = currentContext?.config ?: return@runSafe
191+
// if (config.rotationMode == RotationMode.SILENT && !input.handledByBaritone && baritoneContext == null) return@runSafe
196192

197193
// Sign it to remove previous speed modifier
198194
val signForward = sign(input.movementForward)
199195
val signStrafe = sign(input.movementSideways)
200196

201197
// No changes are needed when no inputs are pressed
202-
if (signForward == 0f && signStrafe == 0f) return
203-
204-
// Movement speed modifier
205-
val multiplier = if (event.slowDown) event.slowDownFactor else 1f
206-
207-
// No changes are needed, when we don't modify the yaw used to move the player
208-
if (config.rotationMode == RotationMode.SILENT && !input.handledByBaritone && baritoneContext == null) return
209-
210-
// The yaw relative to which the movement was constructed
211-
val baritoneYaw = baritoneContext?.rotation?.yaw
212-
val strafeEvent = RotationEvent.Strafe(baritoneYaw ?: player.yaw.toDouble(), input)
213-
val movementYaw = strafeEvent.post().strafeYaw
198+
if (signForward == 0f && signStrafe == 0f) return@runSafe
214199

215200
// Actual yaw used to move the player
216201
val actualYaw = currentRotation.yaw
217202

218-
val yawRad = (movementYaw - actualYaw).toRadian().toFloat()
203+
val yawRad = (movementYaw - actualYaw).toRadian()
219204

220-
val cosDelta = MathHelper.cos(yawRad)
221-
val sinDelta = MathHelper.sin(yawRad)
205+
val cosDelta = cos(yawRad)
206+
val sinDelta = sin(yawRad)
222207

223208
val newX = signStrafe * cosDelta - signForward * sinDelta
224209
val newZ = signForward * cosDelta + signStrafe * sinDelta
225210

226211
// Apply new movement
227212
input.apply {
228-
movementSideways = newX.roundToInt().toFloat() * multiplier
229-
movementForward = newZ.roundToInt().toFloat() * multiplier
213+
// Movement speed modifier
214+
val multiplier = if (slowDown) slowDownFactor else 1f
215+
216+
movementSideways = round(newX).toFloat() * multiplier
217+
movementForward = round(newZ).toFloat() * multiplier
230218
}
231219

232-
baritoneYaw ?: return
220+
baritoneYaw ?: return@runSafe
233221

234222
// Makes baritone movement safe
235223
// when yaw difference is too big to compensate it by modifying keyboard input

common/src/main/kotlin/com/lambda/module/modules/movement/TargetStrafe.kt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.lambda.module.modules.movement
22

33
import com.lambda.config.groups.IRotationConfig
4-
import com.lambda.event.events.MovementEvent
54
import com.lambda.event.events.RotationEvent
65
import com.lambda.event.events.TickEvent
76
import com.lambda.event.listener.SafeListener.Companion.listener
@@ -59,23 +58,10 @@ object TargetStrafe : Module(
5958
}
6059
}
6160

62-
listener<RotationEvent.Strafe> { event ->
63-
targetEntity?.let {
64-
event.strafeYaw = player.eyePos.rotationTo(it.boundingBox.center).yaw
65-
}
66-
}
67-
68-
listener<RotationEvent.Update> { event ->
69-
targetEntity?.let {
70-
event.context = RotationContext(
71-
player.eyePos.rotationTo(it.boundingBox.center),
72-
rotationConfig
73-
)
74-
}
75-
}
76-
77-
listener<MovementEvent.InputUpdate> { event ->
61+
listener<RotationEvent.StrafeInput> { event ->
7862
targetEntity?.let { target ->
63+
event.strafeYaw = player.eyePos.rotationTo(target.boundingBox.center).yaw
64+
7965
val distSq = player.pos distSq target.pos
8066
val keepRange = 0.5 * jitterCompensation
8167

@@ -108,6 +94,15 @@ object TargetStrafe : Module(
10894
}
10995
}
11096

97+
listener<RotationEvent.Update> { event ->
98+
targetEntity?.let {
99+
event.context = RotationContext(
100+
player.eyePos.rotationTo(it.boundingBox.center),
101+
rotationConfig
102+
)
103+
}
104+
}
105+
111106
onEnable {
112107
targetEntity = null
113108
forwardDirection = 1

0 commit comments

Comments
 (0)