11package com.lambda.interaction
22
33import com.lambda.Lambda.mc
4- import com.lambda.config.groups.IRotationConfig
54import com.lambda.config.groups.RotationSettings
65import com.lambda.core.Loadable
7- import com.lambda.context.SafeContext
86import com.lambda.event.EventFlow.post
97import com.lambda.event.events.*
108import com.lambda.event.listener.SafeListener.Companion.listener
@@ -20,19 +18,17 @@ import com.lambda.threading.runSafe
2018import com.lambda.util.math.MathUtils.lerp
2119import com.lambda.util.math.MathUtils.toRadian
2220import com.lambda.util.math.Vec2d
23- import com.lambda.util.player.MovementUtils.handledByBaritone
2421import com.lambda.util.primitives.extension.partialTicks
2522import com.lambda.util.primitives.extension.rotation
23+ import net.minecraft.client.input.Input
2624import 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
3127object 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
0 commit comments