diff options
| author | mat <git@matdoes.dev> | 2025-03-02 05:42:15 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-03-02 05:42:15 +0000 |
| commit | c9022e8f671b5838f4d4ab446013c42191b07f37 (patch) | |
| tree | e07cc01bc86653704e46ff976493a555d5abed8c /azalea-client/src/plugins | |
| parent | 72d87349feaf0240848b735be94448836e0bf4e5 (diff) | |
| download | azalea-drasl-c9022e8f671b5838f4d4ab446013c42191b07f37.tar.xz | |
fix errors when switching to Game state and add fast_login test
Diffstat (limited to 'azalea-client/src/plugins')
| -rw-r--r-- | azalea-client/src/plugins/packet/config/events.rs | 27 | ||||
| -rw-r--r-- | azalea-client/src/plugins/packet/game/events.rs | 27 | ||||
| -rw-r--r-- | azalea-client/src/plugins/packet/mod.rs | 5 |
3 files changed, 52 insertions, 7 deletions
diff --git a/azalea-client/src/plugins/packet/config/events.rs b/azalea-client/src/plugins/packet/config/events.rs index a9b2179a..eddcf72f 100644 --- a/azalea-client/src/plugins/packet/config/events.rs +++ b/azalea-client/src/plugins/packet/config/events.rs @@ -55,7 +55,7 @@ pub fn handle_send_packet_event( } } -pub fn send_packet_events( +pub fn emit_receive_config_packet_events( query: Query<(Entity, &RawConnection), With<InConfigState>>, mut packet_events: ResMut<Events<ReceiveConfigPacketEvent>>, ) { @@ -67,7 +67,9 @@ pub fn send_packet_events( let packets_lock = raw_conn.incoming_packet_queue(); let mut packets = packets_lock.lock(); if !packets.is_empty() { + let mut packets_read = 0; for raw_packet in packets.iter() { + packets_read += 1; let packet = match deserialize_packet::<ClientboundConfigPacket>(&mut Cursor::new( raw_packet, )) { @@ -78,13 +80,32 @@ pub fn send_packet_events( continue; } }; + + let should_interrupt = packet_interrupts(&packet); + packet_events.send(ReceiveConfigPacketEvent { entity: player_entity, packet, }); + + if should_interrupt { + break; + } } - // clear the packets right after we read them - packets.clear(); + packets.drain(0..packets_read); } } } + +/// Whether the given packet should make us stop deserializing the received +/// packets until next update. +/// +/// This is used for packets that can switch the client state. +fn packet_interrupts(packet: &ClientboundConfigPacket) -> bool { + matches!( + packet, + ClientboundConfigPacket::FinishConfiguration(_) + | ClientboundConfigPacket::Disconnect(_) + | ClientboundConfigPacket::Transfer(_) + ) +} diff --git a/azalea-client/src/plugins/packet/game/events.rs b/azalea-client/src/plugins/packet/game/events.rs index 9e7f88fa..e5c87971 100644 --- a/azalea-client/src/plugins/packet/game/events.rs +++ b/azalea-client/src/plugins/packet/game/events.rs @@ -75,7 +75,7 @@ pub fn handle_outgoing_packets( } } -pub fn send_receivepacketevent( +pub fn emit_receive_packet_events( query: Query<(Entity, &RawConnection), With<InGameState>>, mut packet_events: ResMut<Events<ReceivePacketEvent>>, ) { @@ -87,7 +87,9 @@ pub fn send_receivepacketevent( let packets_lock = raw_connection.incoming_packet_queue(); let mut packets = packets_lock.lock(); if !packets.is_empty() { + let mut packets_read = 0; for raw_packet in packets.iter() { + packets_read += 1; let packet = match deserialize_packet::<ClientboundGamePacket>(&mut Cursor::new(raw_packet)) { @@ -98,17 +100,36 @@ pub fn send_receivepacketevent( continue; } }; + + let should_interrupt = packet_interrupts(&packet); + packet_events.send(ReceivePacketEvent { entity: player_entity, packet: Arc::new(packet), }); + + if should_interrupt { + break; + } } - // clear the packets right after we read them - packets.clear(); + packets.drain(0..packets_read); } } } +/// Whether the given packet should make us stop deserializing the received +/// packets until next update. +/// +/// This is used for packets that can switch the client state. +fn packet_interrupts(packet: &ClientboundGamePacket) -> bool { + matches!( + packet, + ClientboundGamePacket::StartConfiguration(_) + | ClientboundGamePacket::Disconnect(_) + | ClientboundGamePacket::Transfer(_) + ) +} + /// A player joined the game (or more specifically, was added to the tab /// list of a local player). #[derive(Event, Debug, Clone)] diff --git a/azalea-client/src/plugins/packet/mod.rs b/azalea-client/src/plugins/packet/mod.rs index cbd8a175..96a396d9 100644 --- a/azalea-client/src/plugins/packet/mod.rs +++ b/azalea-client/src/plugins/packet/mod.rs @@ -38,7 +38,10 @@ impl Plugin for PacketPlugin { fn build(&self, app: &mut App) { app.add_systems( First, - (game::send_receivepacketevent, config::send_packet_events), + ( + game::emit_receive_packet_events, + config::emit_receive_config_packet_events, + ), ) .add_systems( PreUpdate, |
