11package com.lambda.interaction
22
3+ import baritone.utils.PlayerMovementInput
34import com.lambda.Lambda.mc
45import com.lambda.core.Loadable
56import com.lambda.config.RotationSettings
7+ import com.lambda.context.SafeContext
68import com.lambda.event.EventFlow.post
79import com.lambda.event.events.*
810import com.lambda.event.listener.SafeListener.Companion.listener
911import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
1012import com.lambda.interaction.rotation.Rotation
1113import com.lambda.interaction.rotation.Rotation.Companion.angleDifference
12- import com.lambda.interaction.rotation.Rotation.Companion.fixSensitivity
1314import com.lambda.interaction.rotation.Rotation.Companion.slerp
1415import com.lambda.interaction.rotation.Rotation.Companion.lerp
1516import com.lambda.interaction.rotation.RotationContext
@@ -22,8 +23,6 @@ import com.lambda.util.math.MathUtils.toRadian
2223import com.lambda.util.math.Vec2d
2324import com.lambda.util.primitives.extension.partialTicks
2425import com.lambda.util.primitives.extension.rotation
25- import net.minecraft.client.input.KeyboardInput
26- import net.minecraft.enchantment.EnchantmentHelper
2726import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket
2827import net.minecraft.util.math.MathHelper
2928import kotlin.math.roundToInt
@@ -88,25 +87,22 @@ object RotationManager : Loadable {
8887 keepTicks = request.config.keepTicks
8988 }
9089
91- currentContext?.let { current ->
92- val rotationTo = if (keepTicks >= 0 ) current .rotation else currentRotation
90+ currentRotation = currentContext?.let { context ->
91+ val rotationTo = if (keepTicks >= 0 ) context .rotation else player.rotation
9392
94- var speedMultiplier = (current .config as ? RotationSettings )?.speedMultiplier ? : 1.0
93+ var speedMultiplier = (context .config as ? RotationSettings )?.speedMultiplier ? : 1.0
9594 if (keepTicks < 0 ) speedMultiplier = 1.0
9695
97- val turnSpeed = current.config.turnSpeed * speedMultiplier
98-
99- val interpolation = prevRotation.slerp(rotationTo, turnSpeed)
100-
101- currentRotation = interpolation.fixSensitivity(prevRotation)
96+ val turnSpeed = context.config.turnSpeed * speedMultiplier
10297
103- if (current.config.rotationMode == RotationMode .LOCK ) {
104- player.yaw = currentRotation.yaw.toFloat()
105- player.pitch = currentRotation.pitch.toFloat()
106- }
107- } ? : run {
108- currentRotation = player.rotation
109- }
98+ prevRotation
99+ .slerp(rotationTo, turnSpeed)
100+ .apply {
101+ if (context.config.rotationMode != RotationMode .LOCK ) return @apply
102+ player.yaw = this .yawF
103+ player.pitch = this .pitchF
104+ }
105+ } ? : player.rotation
110106 }
111107
112108 private fun reset (rotation : Rotation ) {
@@ -171,7 +167,7 @@ object RotationManager : Loadable {
171167 }
172168
173169 listener<MovementEvent .InputUpdate > {
174- processPlayerMovement()
170+ processPlayerMovement(it )
175171 }
176172 }
177173
@@ -180,29 +176,27 @@ object RotationManager : Loadable {
180176 baritoneContext = RotationContext (Rotation (yaw, pitch), Baritone .rotation)
181177 }
182178
183- @JvmStatic
184- fun processPlayerMovement () = runSafe {
185- val config = currentContext?.config ? : return @runSafe
179+ private fun SafeContext.processPlayerMovement (event : MovementEvent .InputUpdate ) {
180+ val config = currentContext?.config ? : return
186181
187- val input = player.input
188- val handledByBaritone = input !is KeyboardInput
182+ // No changes are needed, when we don't modify the yaw used to move the player
183+ if (config.rotationMode == RotationMode .SILENT ) return
184+
185+ val input = event.input
186+ val handledByBaritone = input is PlayerMovementInput
189187
190188 // Sign it to remove previous speed modifier
191189 val signForward = sign(input.movementForward)
192190 val signStrafe = sign(input.movementSideways)
193191
194192 // No changes are needed when no inputs are pressed
195- if (signForward == 0f && signStrafe == 0f ) return @runSafe
193+ if (signForward == 0f && signStrafe == 0f ) return
196194
197195 // Movement speed modifier
198- val multiplier = if (! player.shouldSlowDown()) 1f else
199- (0.3f + EnchantmentHelper .getSwiftSneakSpeedBoost(player)).coerceIn(0f , 1f )
200-
201- // No changes are needed, when we don't modify the yaw used to move the player
202- if (config.rotationMode == RotationMode .SILENT ) return @runSafe
196+ val multiplier = if (event.slowDown) event.slowDownFactor else 1f
203197
204198 val modifyMovement = config.rotationMode == RotationMode .SYNC || handledByBaritone
205- if (! modifyMovement) return @runSafe
199+ if (! modifyMovement) return
206200
207201 val playerYaw = player.yaw.toDouble()
208202 val baritoneYaw = if (handledByBaritone) baritoneContext?.rotation?.yaw else null
@@ -222,10 +216,12 @@ object RotationManager : Loadable {
222216 val newZ = signForward * cosDelta + signStrafe * sinDelta
223217
224218 // Apply new movement
225- input.movementSideways = newX.roundToInt().toFloat() * multiplier
226- input.movementForward = newZ.roundToInt().toFloat() * multiplier
219+ input.apply {
220+ movementSideways = newX.roundToInt().toFloat() * multiplier
221+ movementForward = newZ.roundToInt().toFloat() * multiplier
222+ }
227223
228- baritoneYaw ? : return @runSafe
224+ baritoneYaw ? : return
229225
230226 // Makes baritone movement safe
231227 // when yaw difference is too big to compensate it by modifying keyboard input
0 commit comments