azalea_client/plugins/
respawn.rs

1use azalea_protocol::packets::game::s_client_command::{self, ServerboundClientCommand};
2use bevy_app::{App, Plugin, Update};
3use bevy_ecs::prelude::*;
4
5use super::packet::game::handle_outgoing_packets;
6use crate::packet::game::SendPacketEvent;
7
8/// Tell the server that we're respawning.
9#[derive(Event, Debug, Clone)]
10pub struct PerformRespawnEvent {
11    pub entity: Entity,
12}
13
14/// A plugin that makes [`PerformRespawnEvent`] send the packet to respawn.
15pub struct RespawnPlugin;
16impl Plugin for RespawnPlugin {
17    fn build(&self, app: &mut App) {
18        app.add_event::<PerformRespawnEvent>()
19            .add_systems(Update, perform_respawn.before(handle_outgoing_packets));
20    }
21}
22
23pub fn perform_respawn(mut events: EventReader<PerformRespawnEvent>, mut commands: Commands) {
24    for event in events.read() {
25        commands.trigger(SendPacketEvent::new(
26            event.entity,
27            ServerboundClientCommand {
28                action: s_client_command::Action::PerformRespawn,
29            },
30        ));
31    }
32}