1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

fix not replying to ping packets in config state

This commit is contained in:
mat 2025-03-27 21:28:08 +00:00
parent 37cde3ad32
commit 02de98240f
5 changed files with 76 additions and 22 deletions

View file

@ -22,7 +22,7 @@ pub struct ReceiveConfigPacketEvent {
/// An event for sending a packet to the server while we're in the /// An event for sending a packet to the server while we're in the
/// `configuration` state. /// `configuration` state.
#[derive(Event)] #[derive(Event, Clone)]
pub struct SendConfigPacketEvent { pub struct SendConfigPacketEvent {
pub sent_by: Entity, pub sent_by: Entity,
pub packet: ServerboundConfigPacket, pub packet: ServerboundConfigPacket,
@ -34,24 +34,33 @@ impl SendConfigPacketEvent {
} }
} }
pub fn handle_outgoing_packets( pub fn handle_outgoing_packets_observer(
mut send_packet_events: EventReader<SendConfigPacketEvent>, trigger: Trigger<SendConfigPacketEvent>,
mut query: Query<(&mut RawConnection, Option<&InConfigState>)>, mut query: Query<(&mut RawConnection, Option<&InConfigState>)>,
) { ) {
for event in send_packet_events.read() { let event = trigger.event();
if let Ok((raw_conn, in_configuration_state)) = query.get_mut(event.sent_by) { if let Ok((raw_conn, in_configuration_state)) = query.get_mut(event.sent_by) {
if in_configuration_state.is_none() { if in_configuration_state.is_none() {
error!( error!(
"Tried to send a configuration packet {:?} while not in configuration state", "Tried to send a configuration packet {:?} while not in configuration state",
event.packet event.packet
); );
continue; return;
}
debug!("Sending packet: {:?}", event.packet);
if let Err(e) = raw_conn.write_packet(event.packet.clone()) {
error!("Failed to send packet: {e}");
}
} }
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());
} }
} }

View file

@ -55,6 +55,7 @@ impl Plugin for PacketPlugin {
), ),
) )
.add_observer(game::handle_outgoing_packets_observer) .add_observer(game::handle_outgoing_packets_observer)
.add_observer(config::handle_outgoing_packets_observer)
.add_systems( .add_systems(
Update, Update,
( (

View file

@ -1,19 +1,63 @@
use std::sync::Arc; 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::{ use azalea_protocol::packets::{
ConnectionProtocol, ConnectionProtocol,
game::{ClientboundPing, ServerboundGamePacket}, config::{
self, ClientboundFinishConfiguration, ClientboundRegistryData, ServerboundConfigPacket,
},
game::{self, ServerboundGamePacket},
}; };
use bevy_ecs::observer::Trigger; use bevy_ecs::observer::Trigger;
use bevy_log::tracing_subscriber; use bevy_log::tracing_subscriber;
use parking_lot::Mutex; use parking_lot::Mutex;
use simdnbt::owned::{NbtCompound, NbtTag};
#[test] #[test]
fn reply_to_ping_with_pong() { fn reply_to_ping_with_pong() {
let _ = tracing_subscriber::fmt::try_init(); 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 = Arc::new(Mutex::new(0));
let reply_count_clone = reply_count.clone(); let reply_count_clone = reply_count.clone();
simulation simulation
@ -28,7 +72,7 @@ fn reply_to_ping_with_pong() {
}); });
simulation.tick(); simulation.tick();
simulation.receive_packet(ClientboundPing { id: 123 }); simulation.receive_packet(game::ClientboundPing { id: 123 });
simulation.tick(); simulation.tick();
assert_eq!(*reply_count.lock(), 1); assert_eq!(*reply_count.lock(), 1);

View file

@ -121,7 +121,7 @@ impl ClientboundPlayerChat {
} }
impl ChatTypeBound { impl ChatTypeBound {
pub fn translation_key<'a>(&'a self) -> &'a str { pub fn translation_key(&self) -> &str {
match &self.chat_type { match &self.chat_type {
Holder::Reference(r) => r.chat_translation_key(), Holder::Reference(r) => r.chat_translation_key(),
Holder::Direct(d) => d.chat.translation_key.as_str(), Holder::Direct(d) => d.chat.translation_key.as_str(),

View file

@ -343,7 +343,7 @@ impl Chunk {
let mut heightmaps = HashMap::new(); let mut heightmaps = HashMap::new();
for (kind, data) in heightmaps_data { 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); let heightmap = Heightmap::new(*kind, dimension_height, min_y, data);
heightmaps.insert(*kind, heightmap); heightmaps.insert(*kind, heightmap);
} }