@@ -21,31 +21,20 @@ import com.lambda.context.SafeContext
2121import com.lambda.event.EventFlow.post
2222import com.lambda.event.EventFlow.postChecked
2323import com.lambda.event.events.PlayerPacketEvent
24- import com.lambda.interaction.managers.rotating.Rotation
2524import com.lambda.interaction.managers.rotating.RotationManager
2625import com.lambda.threading.runSafe
2726import com.lambda.util.collections.LimitedOrderedSet
28- import com.lambda.util.math.approximate
29- import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket
27+ import net.minecraft.network.packet.Packet
3028import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.Full
3129import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.LookAndOnGround
3230import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.OnGroundOnly
3331import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.PositionAndOnGround
34- import net.minecraft.util.math.Vec3d
3532
3633object PlayerPacketHandler {
3734 val configurations = LimitedOrderedSet <PlayerPacketEvent .Pre >(100 )
3835
39- var lastPosition: Vec3d = Vec3d .ZERO
40- var lastRotation = Rotation .ZERO
41- var lastSprint = false
42- var lastOnGround = false
43- var lastHorizontalCollision = false
44-
45- private var sendTicks = 0
46-
4736 @JvmStatic
48- fun sendPlayerPackets () {
37+ fun sendPlayerPackets (packet : Packet < * > ) {
4938 runSafe {
5039 PlayerPacketEvent .Pre (
5140 player.pos,
@@ -54,68 +43,30 @@ object PlayerPacketHandler {
5443 player.isSprinting,
5544 player.horizontalCollision,
5645 ).post {
57- updatePlayerPackets(this )
46+ updatePlayerPackets(this , packet )
5847 }
5948 }
6049 }
6150
62- private fun SafeContext.updatePlayerPackets (new : PlayerPacketEvent .Pre ) {
51+ private fun SafeContext.updatePlayerPackets (new : PlayerPacketEvent .Pre , packet : Packet < * > ) {
6352 configurations.add(new)
6453
65- reportSprint(lastSprint, new.isSprinting)
66-
67- if (mc.cameraEntity != player) return
68-
69- val position = new.position
7054 val (yaw, pitch) = new.rotation
71- val onGround = new.onGround
72- val isCollidingHorizontally = new.isCollidingHorizontally
7355
74- val updatePosition = position.approximate(lastPosition) || ++ sendTicks >= 20
75- val updateRotation = lastRotation.yaw != yaw || lastRotation.pitch != pitch
76-
77- when {
78- updatePosition && updateRotation -> Full (position, yaw.toFloat(), pitch.toFloat(), onGround, isCollidingHorizontally)
79- updatePosition -> PositionAndOnGround (position, onGround, isCollidingHorizontally)
80- updateRotation -> LookAndOnGround (yaw.toFloat(), pitch.toFloat(), onGround, isCollidingHorizontally)
81- lastOnGround != onGround || lastHorizontalCollision != isCollidingHorizontally -> OnGroundOnly (onGround, isCollidingHorizontally)
56+ when (packet) {
57+ is Full -> Full (new.position, yaw.toFloat(), pitch.toFloat(), new.onGround, new.isCollidingHorizontally)
58+ is PositionAndOnGround -> PositionAndOnGround (new.position, new.onGround, new.isCollidingHorizontally)
59+ is LookAndOnGround -> LookAndOnGround (yaw.toFloat(), pitch.toFloat(), new.onGround, new.isCollidingHorizontally)
60+ is OnGroundOnly -> OnGroundOnly (new.onGround, new.isCollidingHorizontally)
8261 else -> null
8362 }?.let {
8463 PlayerPacketEvent .Send (it).postChecked {
8564 connection.sendPacket(this .packet)
86-
87- if (updatePosition) {
88- lastPosition = position
89- sendTicks = 0
90- }
91-
92- if (updateRotation) {
93- lastRotation = new.rotation
94- }
95-
96- lastOnGround = onGround
97- lastHorizontalCollision = isCollidingHorizontally
9865 }
9966 }
10067
101- // Update the server rotation in RotationManager
102- RotationManager .onRotationSend()
103-
10468 PlayerPacketEvent .Post ().post()
10569 }
106-
107- fun SafeContext.reportSprint (previous : Boolean , new : Boolean ) {
108- if (previous == new) return
109-
110- val state = if (new) {
111- ClientCommandC2SPacket .Mode .START_SPRINTING
112- } else {
113- ClientCommandC2SPacket .Mode .STOP_SPRINTING
114- }
115-
116- connection.sendPacket(ClientCommandC2SPacket (player, state))
117- lastSprint = new
118- }
11970}
12071
12172
0 commit comments