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,24 +34,33 @@ impl SendConfigPacketEvent { } } -pub fn handle_outgoing_packets( - mut send_packet_events: EventReader, +pub fn handle_outgoing_packets_observer( + trigger: Trigger, 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, +) { + for event in events.read() { + commands.trigger(event.clone()); } } 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| { + 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); diff --git a/azalea-protocol/src/packets/game/c_player_chat.rs b/azalea-protocol/src/packets/game/c_player_chat.rs index a9a57301..77dce487 100644 --- a/azalea-protocol/src/packets/game/c_player_chat.rs +++ b/azalea-protocol/src/packets/game/c_player_chat.rs @@ -121,7 +121,7 @@ impl ClientboundPlayerChat { } impl ChatTypeBound { - pub fn translation_key<'a>(&'a self) -> &'a str { + pub fn translation_key(&self) -> &str { match &self.chat_type { Holder::Reference(r) => r.chat_translation_key(), Holder::Direct(d) => d.chat.translation_key.as_str(), diff --git a/azalea-world/src/chunk_storage.rs b/azalea-world/src/chunk_storage.rs index d6243b87..39cbc84f 100755 --- a/azalea-world/src/chunk_storage.rs +++ b/azalea-world/src/chunk_storage.rs @@ -343,7 +343,7 @@ impl Chunk { let mut heightmaps = HashMap::new(); for (kind, data) in heightmaps_data { - let data: Box<[u64]> = data.iter().map(|x| *x as u64).collect(); + let data: Box<[u64]> = data.iter().copied().collect(); let heightmap = Heightmap::new(*kind, dimension_height, min_y, data); heightmaps.insert(*kind, heightmap); }