-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_profile.rs
88 lines (75 loc) · 2.94 KB
/
player_profile.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
79
80
81
82
83
84
85
86
87
88
use std::collections::HashMap;
use azalea::{
app::{App, Plugin, PostUpdate, Update},
auth::game_profile::GameProfile,
ecs::prelude::*,
packet_handling::game::PacketEvent,
prelude::*,
protocol::packets::game::ClientboundGamePacket,
registry::EntityKind,
TabList,
};
/// Tracks player profiles.
pub struct PlayerProfilePlugin;
impl Plugin for PlayerProfilePlugin {
fn build(&self, app: &mut App) {
app.insert_resource(PlayerProfiles::default())
.add_systems(Update, Self::handle_add_entity_packets)
.add_systems(PostUpdate, Self::handle_remove_entities_packets);
}
}
#[derive(Clone, Component, Default, Resource)]
pub struct PlayerProfiles(pub HashMap<u32, GameProfile>);
impl PlayerProfilePlugin {
pub fn handle_add_entity_packets(
mut packet_events: EventReader<PacketEvent>,
mut player_profiles: ResMut<PlayerProfiles>,
mut query_profiles: Query<&mut PlayerProfiles>,
mut commands: Commands,
query: Query<&TabList>,
) {
for event in packet_events.read() {
let ClientboundGamePacket::AddEntity(packet) = event.packet.as_ref() else {
continue;
};
if packet.entity_type != EntityKind::Player {
continue;
}
let Ok(tab_list) = query.get(event.entity) else {
continue;
};
let Some((_, info)) = tab_list.iter().find(|(uuid, _)| uuid == &&packet.uuid) else {
continue;
};
/* Insert to the global player profiles resource */
player_profiles.0.insert(packet.id, info.profile.clone());
/* Insert to or insert the local player profiles component */
if let Ok(mut player_profiles) = query_profiles.get_mut(event.entity) {
player_profiles.0.insert(packet.id, info.profile.clone());
} else {
let mut player_profiles = PlayerProfiles::default();
player_profiles.0.insert(packet.id, info.profile.clone());
commands.entity(event.entity).insert(player_profiles);
}
}
}
pub fn handle_remove_entities_packets(
mut packet_events: EventReader<PacketEvent>,
mut player_profiles: ResMut<PlayerProfiles>,
mut query: Query<&mut PlayerProfiles>,
) {
for event in packet_events.read() {
let ClientboundGamePacket::RemoveEntities(packet) = event.packet.as_ref() else {
continue;
};
for entity_id in &packet.entity_ids {
/* Remove from the global player profiles resource */
player_profiles.0.remove(entity_id);
/* Remove from the local player profiles component */
if let Ok(mut player_profiles) = query.get_mut(event.entity) {
player_profiles.0.remove(entity_id);
}
}
}
}
}