aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/respawn.rs
blob: 244798055a42322a058c330538556cc7eba254c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use azalea_protocol::packets::game::s_client_command::{self, ServerboundClientCommand};
use bevy_app::{App, Plugin, Update};
use bevy_ecs::prelude::*;

use super::packet::game::handle_outgoing_packets;
use crate::packet::game::SendPacketEvent;

/// Tell the server that we're respawning.
#[derive(Event, Debug, Clone)]
pub struct PerformRespawnEvent {
    pub entity: Entity,
}

/// A plugin that makes [`PerformRespawnEvent`] send the packet to respawn.
pub struct RespawnPlugin;
impl Plugin for RespawnPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<PerformRespawnEvent>()
            .add_systems(Update, perform_respawn.before(handle_outgoing_packets));
    }
}

pub fn perform_respawn(mut events: EventReader<PerformRespawnEvent>, mut commands: Commands) {
    for event in events.read() {
        commands.trigger(SendPacketEvent::new(
            event.entity,
            ServerboundClientCommand {
                action: s_client_command::Action::PerformRespawn,
            },
        ));
    }
}