azalea_client/plugins/
pong.rs

1use azalea_protocol::packets::{config, game};
2use bevy_app::{App, Plugin};
3use bevy_ecs::prelude::*;
4
5use super::packet::{
6    config::{ConfigPingEvent, SendConfigPacketEvent},
7    game::GamePingEvent,
8};
9use crate::packet::game::SendGamePacketEvent;
10
11/// A plugin that replies to [`ClientboundPing`] packets with
12/// [`ServerboundPong`].
13///
14/// This works in both the `game` and `config` states.
15///
16/// [`ClientboundPing`]: azalea_protocol::packets::game::ClientboundPing
17/// [`ServerboundPong`]: azalea_protocol::packets::game::ServerboundPong
18pub struct PongPlugin;
19impl Plugin for PongPlugin {
20    fn build(&self, app: &mut App) {
21        app.add_observer(reply_to_game_ping)
22            .add_observer(reply_to_config_ping);
23    }
24}
25
26pub fn reply_to_game_ping(ping: On<GamePingEvent>, mut commands: Commands) {
27    commands.trigger(SendGamePacketEvent::new(
28        ping.entity,
29        game::ServerboundPong { id: ping.packet.id },
30    ));
31}
32
33pub fn reply_to_config_ping(ping: On<ConfigPingEvent>, mut commands: Commands) {
34    commands.trigger(SendConfigPacketEvent::new(
35        ping.entity,
36        config::ServerboundPong { id: ping.packet.id },
37    ));
38}