11package com.lambda.interaction
22
3- import baritone.utils.PlayerMovementInput
43import com.lambda.Lambda.mc
4+ import com.lambda.config.groups.IRotationConfig
55import com.lambda.config.groups.RotationSettings
66import com.lambda.core.Loadable
77import com.lambda.context.SafeContext
@@ -20,6 +20,7 @@ import com.lambda.threading.runSafe
2020import com.lambda.util.math.MathUtils.lerp
2121import com.lambda.util.math.MathUtils.toRadian
2222import com.lambda.util.math.Vec2d
23+ import com.lambda.util.player.MovementUtils.handledByBaritone
2324import com.lambda.util.primitives.extension.partialTicks
2425import com.lambda.util.primitives.extension.rotation
2526import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket
@@ -38,9 +39,12 @@ object RotationManager : Loadable {
3839
3940 init {
4041 listener<TickEvent .Pre > {
41- RotationEvent .Update ().post {
42+ RotationEvent .Update (BaritoneProcessor .poolContext() ).post {
4243 rotate(context)
43- currentContext?.let { RotationEvent .Post (it).post() }
44+
45+ currentContext?.let {
46+ RotationEvent .Post (it).post()
47+ }
4448 }
4549 }
4650
@@ -83,7 +87,7 @@ object RotationManager : Loadable {
8387
8488 val turnSpeed = context.config.turnSpeed * speedMultiplier
8589
86- prevRotation
90+ currentRotation
8791 .slerp(rotationTo, turnSpeed)
8892 .apply {
8993 if (context.config.rotationMode != RotationMode .LOCK ) return @apply
@@ -156,7 +160,13 @@ object RotationManager : Loadable {
156160 }
157161
158162 object BaritoneProcessor {
159- var baritoneContext: RotationContext ? = null ; private set
163+ private var baritoneContext: RotationContext ? = null
164+
165+ fun poolContext (): RotationContext ? {
166+ val ctx = baritoneContext
167+ baritoneContext = null
168+ return ctx
169+ }
160170
161171 private val movementYawList = arrayOf(
162172 0.0 , 45.0 ,
@@ -166,28 +176,23 @@ object RotationManager : Loadable {
166176 )
167177
168178 init {
169- listener<TickEvent .Post > {
170- baritoneContext = null
171- }
172-
173179 listener<MovementEvent .InputUpdate >(Int .MAX_VALUE ) {
174180 processPlayerMovement(it)
175181 }
176182 }
177183
178184 @JvmStatic
179- fun handleBaritoneRotation (yaw : Float , pitch : Float ) = runSafe {
180- baritoneContext = RotationContext (Rotation (yaw, pitch), Baritone .rotation)
185+ fun handleBaritoneRotation (yaw : Float , pitch : Float ) {
186+ baritoneContext = RotationContext (Rotation (yaw, pitch), Baritone .rotation.apply {
187+ if (rotationMode != RotationMode .SILENT ) return @apply
188+ rotationMode = RotationMode .SYNC
189+ })
181190 }
182191
183192 private fun SafeContext.processPlayerMovement (event : MovementEvent .InputUpdate ) {
184193 val config = currentContext?.config ? : return
185194
186- // No changes are needed, when we don't modify the yaw used to move the player
187- if (config.rotationMode == RotationMode .SILENT ) return
188-
189195 val input = event.input
190- val handledByBaritone = input is PlayerMovementInput
191196
192197 // Sign it to remove previous speed modifier
193198 val signForward = sign(input.movementForward)
@@ -199,14 +204,12 @@ object RotationManager : Loadable {
199204 // Movement speed modifier
200205 val multiplier = if (event.slowDown) event.slowDownFactor else 1f
201206
202- val modifyMovement = config.rotationMode == RotationMode .SYNC || handledByBaritone
203- if (! modifyMovement) return
204-
205- val playerYaw = player.yaw.toDouble()
206- val baritoneYaw = if (handledByBaritone) baritoneContext?.rotation?.yaw else null
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
207209
208210 // The yaw relative to which the movement was constructed
209- val strafeEvent = RotationEvent .Strafe (baritoneYaw ? : playerYaw)
211+ val baritoneYaw = baritoneContext?.rotation?.yaw
212+ val strafeEvent = RotationEvent .Strafe (baritoneYaw ? : player.yaw.toDouble(), input)
210213 val movementYaw = strafeEvent.post().strafeYaw
211214
212215 // Actual yaw used to move the player
0 commit comments