@@ -22,15 +22,11 @@ import com.lambda.event.EventFlow.post
2222import com.lambda.event.EventFlow.postChecked
2323import com.lambda.event.events.PlayerPacketEvent
2424import com.lambda.interaction.request.rotating.Rotation
25+ import com.lambda.interaction.request.rotating.Rotation.Companion.rotation
2526import com.lambda.interaction.request.rotating.RotationManager
2627import com.lambda.threading.runSafe
2728import com.lambda.util.collections.LimitedOrderedSet
2829import com.lambda.util.math.approximate
29- import com.lambda.util.math.component1
30- import com.lambda.util.math.component2
31- import com.lambda.util.math.component3
32- import com.lambda.util.player.MovementUtils.motionX
33- import com.lambda.util.player.MovementUtils.motionZ
3430import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket
3531import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.Full
3632import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.LookAndOnGround
@@ -46,6 +42,7 @@ object PlayerPacketManager {
4642 var lastSprint = false
4743 var lastSneak = false
4844 var lastOnGround = false
45+ var lastHorizontalCollision = false
4946
5047 private var sendTicks = 0
5148
@@ -57,80 +54,66 @@ object PlayerPacketManager {
5754 RotationManager .activeRotation,
5855 player.isOnGround,
5956 player.isSprinting,
60- player.isSneaking,
6157 player.horizontalCollision,
6258 ).post {
6359 updatePlayerPackets(this )
6460 }
6561 }
6662 }
6763
64+ @JvmStatic
65+ fun sendSneakPackets () {
66+ runSafe {
67+ val sneaking = player.isSneaking
68+ if (sneaking == lastSneak) return @runSafe
69+ val mode = if (sneaking) {
70+ ClientCommandC2SPacket .Mode .PRESS_SHIFT_KEY
71+ } else {
72+ ClientCommandC2SPacket .Mode .RELEASE_SHIFT_KEY
73+ }
74+ connection.sendPacket(ClientCommandC2SPacket (player, mode))
75+ lastSneak = sneaking
76+ }
77+ }
78+
6879 private fun SafeContext.updatePlayerPackets (new : PlayerPacketEvent .Pre ) {
6980 configurations.add(new)
7081
7182 reportSprint(lastSprint, new.isSprinting)
72- reportSneak(lastSneak, new.isSneaking)
7383
7484 if (mc.cameraEntity != player) return
7585
76- val rotation = new.rotation
7786 val position = new.position
78- val (yaw, pitch) = rotation.float
87+ val (yaw, pitch) = new.rotation
7988 val onGround = new.onGround
8089 val isCollidingHorizontally = new.isCollidingHorizontally
8190
82- if (player.hasVehicle()) {
83- connection.sendPacket(
84- Full (
85- player.motionX,
86- - 999.0 ,
87- player.motionZ,
88- yaw,
89- pitch,
90- onGround,
91- isCollidingHorizontally,
92- )
93- )
94-
95- return
96- }
97-
98- val updatePosition = position.approximate(lastPosition, 2 .0E- 4 ) || ++ sendTicks >= 20
99- // has to be different in float precision
100- val updateRotation = ! rotation.equalFloat(lastRotation)
101-
102- val (x, y, z) = position
103-
104- val packet = when {
105- updatePosition && updateRotation ->
106- Full (x, y, z, yaw, pitch, onGround, isCollidingHorizontally)
107-
108- updatePosition ->
109- PositionAndOnGround (x, y, z, onGround, isCollidingHorizontally)
91+ val updatePosition = position.approximate(lastPosition) || ++ sendTicks >= 20
92+ val updateRotation = lastRotation.yaw != yaw || lastRotation.pitch != pitch
11093
111- updateRotation ->
112- LookAndOnGround (yaw, pitch, onGround, isCollidingHorizontally)
113-
114- lastOnGround != onGround ->
94+ when {
95+ updatePosition && updateRotation -> Full (position, yaw.toFloat(), pitch.toFloat(), onGround, isCollidingHorizontally)
96+ updatePosition -> PositionAndOnGround (position, onGround, isCollidingHorizontally)
97+ updateRotation -> LookAndOnGround (yaw.toFloat(), pitch.toFloat(), onGround, isCollidingHorizontally)
98+ lastOnGround != onGround || lastHorizontalCollision != isCollidingHorizontally -> {
11599 OnGroundOnly (onGround, isCollidingHorizontally)
116-
100+ }
117101 else -> null
118- }
119-
120- packet?.let {
102+ }?.let {
121103 PlayerPacketEvent .Send (it).postChecked {
122104 connection.sendPacket(this .packet)
123105
124106 if (updatePosition) {
125- sendTicks = 0
126107 lastPosition = position
108+ sendTicks = 0
127109 }
128110
129111 if (updateRotation) {
130- lastRotation = rotation
112+ lastRotation = new. rotation
131113 }
132114
133115 lastOnGround = onGround
116+ lastHorizontalCollision = isCollidingHorizontally
134117 }
135118 }
136119
@@ -152,19 +135,6 @@ object PlayerPacketManager {
152135 connection.sendPacket(ClientCommandC2SPacket (player, state))
153136 lastSprint = new
154137 }
155-
156- fun SafeContext.reportSneak (previous : Boolean , new : Boolean ) {
157- if (previous == new) return
158-
159- val state = if (new) {
160- ClientCommandC2SPacket .Mode .PRESS_SHIFT_KEY
161- } else {
162- ClientCommandC2SPacket .Mode .RELEASE_SHIFT_KEY
163- }
164-
165- connection.sendPacket(ClientCommandC2SPacket (player, state))
166- lastSneak = new
167- }
168138}
169139
170140
0 commit comments