Skip to content

Commit 82deac4

Browse files
committed
Rotation system fixes
1 parent b322d38 commit 82deac4

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.lambda.event.events
33
import com.lambda.event.Event
44
import com.lambda.event.callback.Cancellable
55
import com.lambda.event.callback.ICancellable
6-
import com.lambda.interaction.RotationManager
76
import com.lambda.interaction.rotation.RotationContext
7+
import net.minecraft.client.input.Input
88

99
abstract class RotationEvent : Event {
1010
/**
@@ -16,18 +16,15 @@ abstract class RotationEvent : Event {
1616
*
1717
* @property context The rotation context that listeners can set. Only one rotation can "win" each tick.
1818
*/
19-
class Update : RotationEvent(), ICancellable by Cancellable() {
20-
// Always check if baritone wants to rotate as well
21-
var context = RotationManager.BaritoneProcessor.baritoneContext
22-
}
19+
class Update(var context: RotationContext?) : RotationEvent(), ICancellable by Cancellable()
2320

2421
/**
2522
* This event allows listeners to modify the yaw relative to which the movement input is going to be constructed
2623
*
2724
* @property strafeYaw The angle at which the player will move when pressing W
2825
* Changing this value will never force the anti cheat to flag you because RotationManager is designed to modify the key input instead
2926
*/
30-
class Strafe(var strafeYaw: Double) : RotationEvent()
27+
class Strafe(var strafeYaw: Double, val input: Input) : RotationEvent()
3128

3229
class Post(val context: RotationContext) : RotationEvent()
3330
}

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lambda.interaction
22

3-
import baritone.utils.PlayerMovementInput
43
import com.lambda.Lambda.mc
4+
import com.lambda.config.groups.IRotationConfig
55
import com.lambda.config.groups.RotationSettings
66
import com.lambda.core.Loadable
77
import com.lambda.context.SafeContext
@@ -20,6 +20,7 @@ import com.lambda.threading.runSafe
2020
import com.lambda.util.math.MathUtils.lerp
2121
import com.lambda.util.math.MathUtils.toRadian
2222
import com.lambda.util.math.Vec2d
23+
import com.lambda.util.player.MovementUtils.handledByBaritone
2324
import com.lambda.util.primitives.extension.partialTicks
2425
import com.lambda.util.primitives.extension.rotation
2526
import 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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.lambda.event.events.ClientEvent
66
import com.lambda.event.events.MovementEvent
77
import com.lambda.event.events.RotationEvent
88
import com.lambda.event.listener.SafeListener.Companion.listener
9-
import com.lambda.interaction.RotationManager
109
import com.lambda.interaction.rotation.Rotation
1110
import com.lambda.interaction.rotation.RotationContext
1211
import com.lambda.interaction.rotation.RotationMode
@@ -15,6 +14,7 @@ import com.lambda.module.tag.ModuleTag
1514
import com.lambda.util.Nameable
1615
import com.lambda.util.player.MovementUtils.addSpeed
1716
import com.lambda.util.player.MovementUtils.calcMoveYaw
17+
import com.lambda.util.player.MovementUtils.handledByBaritone
1818
import com.lambda.util.player.MovementUtils.isInputting
1919
import com.lambda.util.player.MovementUtils.motionY
2020
import com.lambda.util.player.MovementUtils.moveDelta
@@ -110,10 +110,10 @@ object Speed : Module(
110110
listener<RotationEvent.Update> { event ->
111111
if (mode != Mode.GRIM_STRAFE) return@listener
112112
if (!shouldWork() || !isInputting) return@listener
113+
if (player.input.handledByBaritone) return@listener
113114

114115
val input = newMovementInput()
115-
val moveYaw = RotationManager.BaritoneProcessor.baritoneContext?.rotation?.yawF ?: player.yaw
116-
val yaw = calcMoveYaw(moveYaw, input.roundedForward, input.roundedStrafing)
116+
val yaw = calcMoveYaw(player.yaw, input.roundedForward, input.roundedStrafing)
117117
val rotation = Rotation(yaw, event.context?.rotation?.pitch ?: player.pitch.toDouble())
118118

119119
event.context = RotationContext(rotation, rotationConfig)

common/src/main/kotlin/com/lambda/util/player/MovementUtils.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.lambda.util.player
22

3-
import baritone.utils.PlayerMovementInput
43
import com.lambda.context.SafeContext
54
import com.lambda.interaction.RotationManager
65
import com.lambda.util.math.MathUtils.random

0 commit comments

Comments
 (0)