diff options
| author | mat <git@matdoes.dev> | 2025-03-27 21:28:08 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-03-27 21:28:13 +0000 |
| commit | 02de98240f3642019b1c8104b00bc4a9d6d17e71 (patch) | |
| tree | 1fd7074164a9cb1eb6d1012e4662cef61f10891d /azalea-client | |
| parent | 37cde3ad3245675c54e70a4ded362f2a60f0d8a4 (diff) | |
| download | azalea-drasl-02de98240f3642019b1c8104b00bc4a9d6d17e71.tar.xz | |
fix not replying to ping packets in config state
Diffstat (limited to 'azalea-client')
| -rw-r--r-- | azalea-client/src/plugins/packet/config/events.rs | 41 | ||||
| -rw-r--r-- | azalea-client/src/plugins/packet/mod.rs | 1 | ||||
| -rw-r--r-- | azalea-client/tests/reply_to_ping_with_pong.rs | 52 |
3 files changed, 74 insertions, 20 deletions
diff --git a/azalea-client/src/plugins/packet/config/events.rs b/azalea-client/src/plugins/packet/config/events.rs index d0a7f3be..24a1157b 100644 --- a/azalea-client/src/plugins/packet/config/events.rs +++ b/azalea-client/src/plugins/packet/config/events.rs @@ -22,7 +22,7 @@ pub struct ReceiveConfigPacketEvent { /// An event for sending a packet to the server while we're in the /// `configuration` state. -#[derive(Event)] +#[derive(Event, Clone)] pub struct SendConfigPacketEvent { pub sent_by: Entity, pub packet: ServerboundConfigPacket, @@ -34,26 +34,35 @@ impl SendConfigPacketEvent { } } -pub fn handle_outgoing_packets( - mut send_packet_events: EventReader<SendConfigPacketEvent>, +pub fn handle_outgoing_packets_observer( + trigger: Trigger<SendConfigPacketEvent>, mut query: Query<(&mut RawConnection, Option<&InConfigState>)>, ) { - for event in send_packet_events.read() { - if let Ok((raw_conn, in_configuration_state)) = query.get_mut(event.sent_by) { - if in_configuration_state.is_none() { - error!( - "Tried to send a configuration packet {:?} while not in configuration state", - event.packet - ); - continue; - } - debug!("Sending packet: {:?}", event.packet); - if let Err(e) = raw_conn.write_packet(event.packet.clone()) { - error!("Failed to send packet: {e}"); - } + let event = trigger.event(); + if let Ok((raw_conn, in_configuration_state)) = query.get_mut(event.sent_by) { + if in_configuration_state.is_none() { + error!( + "Tried to send a configuration packet {:?} while not in configuration state", + event.packet + ); + return; + } + debug!("Sending packet: {:?}", event.packet); + if let Err(e) = raw_conn.write_packet(event.packet.clone()) { + error!("Failed to send packet: {e}"); } } } +/// A system that converts [`SendConfigPacketEvent`] events into triggers so +/// they get received by [`handle_outgoing_packets_observer`]. +pub fn handle_outgoing_packets( + mut commands: Commands, + mut events: EventReader<SendConfigPacketEvent>, +) { + for event in events.read() { + commands.trigger(event.clone()); + } +} pub fn emit_receive_config_packet_events( query: Query<(Entity, &RawConnection), With<InConfigState>>, diff --git a/azalea-client/src/plugins/packet/mod.rs b/azalea-client/src/plugins/packet/mod.rs index 826cd5cf..05f64e19 100644 --- a/azalea-client/src/plugins/packet/mod.rs +++ b/azalea-client/src/plugins/packet/mod.rs @@ -55,6 +55,7 @@ impl Plugin for PacketPlugin { ), ) .add_observer(game::handle_outgoing_packets_observer) + .add_observer(config::handle_outgoing_packets_observer) .add_systems( Update, ( diff --git a/azalea-client/tests/reply_to_ping_with_pong.rs b/azalea-client/tests/reply_to_ping_with_pong.rs index 4ef5b2cc..d921c905 100644 --- a/azalea-client/tests/reply_to_ping_with_pong.rs +++ b/azalea-client/tests/reply_to_ping_with_pong.rs @@ -1,19 +1,63 @@ use std::sync::Arc; -use azalea_client::{packet::game::SendPacketEvent, test_simulation::*}; +use azalea_client::{ + packet::{config::SendConfigPacketEvent, game::SendPacketEvent}, + test_simulation::*, +}; +use azalea_core::resource_location::ResourceLocation; use azalea_protocol::packets::{ ConnectionProtocol, - game::{ClientboundPing, ServerboundGamePacket}, + config::{ + self, ClientboundFinishConfiguration, ClientboundRegistryData, ServerboundConfigPacket, + }, + game::{self, ServerboundGamePacket}, }; use bevy_ecs::observer::Trigger; use bevy_log::tracing_subscriber; use parking_lot::Mutex; +use simdnbt::owned::{NbtCompound, NbtTag}; #[test] fn reply_to_ping_with_pong() { let _ = tracing_subscriber::fmt::try_init(); - let mut simulation = Simulation::new(ConnectionProtocol::Game); + let mut simulation = Simulation::new(ConnectionProtocol::Configuration); + + let reply_count = Arc::new(Mutex::new(0)); + let reply_count_clone = reply_count.clone(); + simulation + .app + .add_observer(move |trigger: Trigger<SendConfigPacketEvent>| { + if trigger.sent_by == simulation.entity { + if let ServerboundConfigPacket::Pong(packet) = &trigger.packet { + assert_eq!(packet.id, 321); + *reply_count_clone.lock() += 1; + } + } + }); + + simulation.receive_packet(config::ClientboundPing { id: 321 }); + simulation.tick(); + assert_eq!(*reply_count.lock(), 1); + + // move into game state and test ClientboundPing there + + simulation.receive_packet(ClientboundRegistryData { + registry_id: ResourceLocation::new("minecraft:dimension_type"), + entries: vec![( + ResourceLocation::new("minecraft:overworld"), + Some(NbtCompound::from_values(vec![ + ("height".into(), NbtTag::Int(384)), + ("min_y".into(), NbtTag::Int(-64)), + ])), + )] + .into_iter() + .collect(), + }); + + simulation.receive_packet(ClientboundFinishConfiguration); + simulation.tick(); + let reply_count = Arc::new(Mutex::new(0)); let reply_count_clone = reply_count.clone(); simulation @@ -28,7 +72,7 @@ fn reply_to_ping_with_pong() { }); simulation.tick(); - simulation.receive_packet(ClientboundPing { id: 123 }); + simulation.receive_packet(game::ClientboundPing { id: 123 }); simulation.tick(); assert_eq!(*reply_count.lock(), 1); |
