@@ -210,21 +210,27 @@ pub(super) async fn login(
210210
211211 // =============================================================================================
212212 // 13 Await client's teleport acceptance
213- let mut skel = PacketSkeleton :: new ( conn_read, compressed, Play ) . await ?;
213+ // The client may send other packets (like client_tick_end) before accepting the teleport,
214+ // so we loop until we get the accept_teleportation packet
214215 let expected_id = lookup_packet ! ( "play" , "serverbound" , "accept_teleportation" ) ;
215- if skel. id != expected_id {
216- return Err ( NetError :: Packet ( PacketError :: UnexpectedPacket {
217- expected : expected_id,
218- received : skel. id ,
219- state : Play ,
220- } ) ) ;
221- }
222-
223- let confirm_player_teleport =
224- crate :: packets:: incoming:: confirm_player_teleport:: ConfirmPlayerTeleport :: decode (
225- & mut skel. data ,
226- & NetDecodeOpts :: None ,
227- ) ?;
216+ let confirm_player_teleport = loop {
217+ let mut skel = PacketSkeleton :: new ( conn_read, compressed, Play ) . await ?;
218+ if skel. id == expected_id {
219+ // Got the teleport confirmation
220+ let confirm =
221+ crate :: packets:: incoming:: confirm_player_teleport:: ConfirmPlayerTeleport :: decode (
222+ & mut skel. data ,
223+ & NetDecodeOpts :: None ,
224+ ) ?;
225+ break confirm;
226+ } else {
227+ // Client sent another packet before confirming teleport - just ignore it
228+ trace ! (
229+ "Ignoring packet 0x{:02X} while waiting for teleport confirmation" ,
230+ skel. id
231+ ) ;
232+ }
233+ } ;
228234
229235 if confirm_player_teleport. teleport_id . 0 != teleport_id_i32 {
230236 error ! (
@@ -235,21 +241,25 @@ pub(super) async fn login(
235241
236242 // =============================================================================================
237243 // 14 Receive first movement packet from player
238- let mut skel = PacketSkeleton :: new ( conn_read , compressed , Play ) . await ? ;
244+ // Similarly, the client may send other packets before the movement packet
239245 let expected_id = lookup_packet ! ( "play" , "serverbound" , "move_player_pos_rot" ) ;
240- if skel. id != expected_id {
241- return Err ( NetError :: Packet ( PacketError :: UnexpectedPacket {
242- expected : expected_id,
243- received : skel. id ,
244- state : Play ,
245- } ) ) ;
246- }
247-
248- let _player_pos_and_rot =
249- crate :: packets:: incoming:: set_player_position_and_rotation:: SetPlayerPositionAndRotationPacket :: decode (
250- & mut skel. data ,
251- & NetDecodeOpts :: None ,
252- ) ?;
246+ let _player_pos_and_rot = loop {
247+ let mut skel = PacketSkeleton :: new ( conn_read, compressed, Play ) . await ?;
248+
249+ if skel. id == expected_id {
250+ let pos_rot = crate :: packets:: incoming:: set_player_position_and_rotation:: SetPlayerPositionAndRotationPacket :: decode (
251+ & mut skel. data ,
252+ & NetDecodeOpts :: None ,
253+ ) ?;
254+ break pos_rot;
255+ } else {
256+ // Client sent another packet before movement - ignore it
257+ trace ! (
258+ "Ignoring packet 0x{:02X} while waiting for initial movement packet" ,
259+ skel. id
260+ ) ;
261+ }
262+ } ;
253263
254264 // =============================================================================================
255265 // 15 Send initial game event (e.g., "change game mode")
0 commit comments