@@ -23,7 +23,6 @@ import com.lambda.core.Loadable
2323import com.lambda.event.EventFlow.post
2424import com.lambda.event.events.ConnectionEvent
2525import com.lambda.event.events.PacketEvent
26- import com.lambda.event.events.PlayerPacketEvent
2726import com.lambda.event.events.RotationEvent
2827import com.lambda.event.events.TickEvent
2928import com.lambda.event.events.UpdateManagerEvent
@@ -80,69 +79,68 @@ object RotationManager : RequestHandler<RotationRequest>(), Loadable {
8079 }
8180
8281 init {
83- // For some reason we have to update AFTER sending player packets
84- // instead of updating on TickEvent.Pre (am I doing something wrong?)
85- listen<PlayerPacketEvent .Post >(Int .MIN_VALUE ) {
86- preEvent()
87-
88- // Update the request
89- val changed = updateRequest(true ) { entry ->
90- // skip requests that have failed to build the rotation
91- // to free the request place for others
92- entry.value.target.targetRotation.value != null
93- }
94-
95- if (currentRequest != null ) activeThisTick = true
82+ listen<PacketEvent .Send .Post > { event ->
83+ val packet = event.packet
84+ if (packet !is PlayerPositionLookS2CPacket ) return @listen
9685
97- if ( ! changed) { // rebuild the rotation if the same context gets used again
98- currentRequest?.target?.targetRotation?.update( )
86+ runGameScheduled {
87+ reset( Rotation (packet.yaw, packet.pitch) )
9988 }
89+ }
10090
101- // Calculate the target rotation
102- val targetRotation = currentRequest?.let { request ->
103- val rotationTo = if (request.keepTicks >= 0 )
104- request.target.targetRotation.value
105- ? : currentRotation // same context gets used again && the rotation is null this tick
106- else player.rotation
91+ listenUnsafe<ConnectionEvent .Connect .Pre > {
92+ reset(Rotation .ZERO )
93+ }
94+ }
10795
108- val speedMultiplier = if (request.keepTicks < 0 ) 1.0 else request.speedMultiplier
109- val turnSpeed = request.turnSpeed() * speedMultiplier
96+ @JvmStatic
97+ fun processRotations () = runSafe {
98+ preEvent()
99+
100+ // Update the request
101+ val changed = updateRequest(true ) { entry ->
102+ // skip requests that have failed to build the rotation
103+ // to free the request place for others
104+ entry.value.target.targetRotation.value != null
105+ }
110106
111- currentRotation.slerp(rotationTo, turnSpeed)
112- } ? : player.rotation
107+ if (currentRequest != null ) activeThisTick = true
113108
114- // Update the current rotation
115- prevRotation = currentRotation
116- currentRotation = targetRotation /* .fixSensitivity(prevRotation) */
109+ if ( ! changed) { // rebuild the rotation if the same context gets used again
110+ currentRequest?.target?.targetRotation?.update()
111+ }
117112
118- // Handle LOCK mode
119- if (currentRequest?.mode == RotationMode .Lock ) {
120- player.yaw = currentRotation.yawF
121- player.pitch = currentRotation.pitchF
122- }
113+ // Calculate the target rotation
114+ val targetRotation = currentRequest?.let { request ->
115+ val rotationTo = if (request.keepTicks >= 0 )
116+ request.target.targetRotation.value
117+ ? : currentRotation // same context gets used again && the rotation is null this tick
118+ else player.rotation
123119
124- // Tick and reset the context
125- currentRequest?.let {
126- if (-- it.keepTicks > 0 ) return @let
127- if (-- it.decayTicks >= 0 ) return @let
128- currentRequest = null
129- }
120+ val speedMultiplier = if (request.keepTicks < 0 ) 1.0 else request.speedMultiplier
121+ val turnSpeed = request.turnSpeed() * speedMultiplier
130122
131- postEvent( )
132- }
123+ currentRotation.slerp(rotationTo, turnSpeed )
124+ } ? : player.rotation
133125
134- listen< PacketEvent . Send . Post > { event ->
135- val packet = event.packet
136- if (packet !is PlayerPositionLookS2CPacket ) return @listen
126+ // Update the current rotation
127+ prevRotation = currentRotation
128+ currentRotation = targetRotation /* .fixSensitivity(prevRotation) */
137129
138- runGameScheduled {
139- reset(Rotation (packet.yaw, packet.pitch))
140- }
130+ // Handle LOCK mode
131+ if (currentRequest?.mode == RotationMode .Lock ) {
132+ player.yaw = currentRotation.yawF
133+ player.pitch = currentRotation.pitchF
141134 }
142135
143- listenUnsafe<ConnectionEvent .Connect .Pre > {
144- reset(Rotation .ZERO )
136+ // Tick and reset the context
137+ currentRequest?.let {
138+ if (-- it.keepTicks > 0 ) return @let
139+ if (-- it.decayTicks >= 0 ) return @let
140+ currentRequest = null
145141 }
142+
143+ postEvent()
146144 }
147145
148146 private fun reset (rotation : Rotation ) {
0 commit comments