azalea_client/packet_handling/
mod.rs

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use azalea_entity::{metadata::Health, EntityUpdateSet};
use bevy_app::{App, First, Plugin, PreUpdate, Update};
use bevy_ecs::prelude::*;

use self::{
    game::{
        AddPlayerEvent, DeathEvent, InstanceLoadedEvent, KeepAliveEvent, RemovePlayerEvent,
        ResourcePackEvent, UpdatePlayerEvent,
    },
    login::{LoginPacketEvent, SendLoginPacketEvent},
};
use crate::{chat::ChatReceivedEvent, events::death_listener};

pub mod configuration;
pub mod game;
pub mod login;

pub struct PacketHandlerPlugin;

pub fn death_event_on_0_health(
    query: Query<(Entity, &Health), Changed<Health>>,
    mut death_events: EventWriter<DeathEvent>,
) {
    for (entity, health) in query.iter() {
        if **health == 0. {
            death_events.send(DeathEvent {
                entity,
                packet: None,
            });
        }
    }
}

impl Plugin for PacketHandlerPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(
            First,
            (game::send_packet_events, configuration::send_packet_events),
        )
        .add_systems(
            PreUpdate,
            (
                game::process_packet_events
                    // we want to index and deindex right after
                    .before(EntityUpdateSet::Deindex),
                configuration::process_packet_events,
                login::handle_send_packet_event,
                login::process_packet_events,
            ),
        )
        .add_systems(
            Update,
            (
                (
                    configuration::handle_send_packet_event,
                    game::handle_send_packet_event,
                )
                    .chain(),
                death_event_on_0_health.before(death_listener),
            ),
        )
        // we do this instead of add_event so we can handle the events ourselves
        .init_resource::<Events<game::PacketEvent>>()
        .init_resource::<Events<configuration::ConfigurationPacketEvent>>()
        .add_event::<game::SendPacketEvent>()
        .add_event::<configuration::SendConfigurationPacketEvent>()
        .add_event::<AddPlayerEvent>()
        .add_event::<RemovePlayerEvent>()
        .add_event::<UpdatePlayerEvent>()
        .add_event::<ChatReceivedEvent>()
        .add_event::<DeathEvent>()
        .add_event::<KeepAliveEvent>()
        .add_event::<ResourcePackEvent>()
        .add_event::<InstanceLoadedEvent>()
        .add_event::<LoginPacketEvent>()
        .add_event::<SendLoginPacketEvent>();
    }
}