azalea_client/player.rs
1use azalea_auth::game_profile::GameProfile;
2use azalea_chat::FormattedText;
3use azalea_core::game_type::GameMode;
4use azalea_entity::indexing::EntityUuidIndex;
5use bevy_ecs::{
6 component::Component,
7 event::EventReader,
8 system::{Commands, Res},
9};
10use derive_more::{Deref, DerefMut};
11use uuid::Uuid;
12
13use crate::packet::game::AddPlayerEvent;
14
15/// A player in the tab list.
16#[derive(Debug, Clone)]
17pub struct PlayerInfo {
18 /// Information about the player's Minecraft account, including their
19 /// username.
20 pub profile: GameProfile,
21 /// The player's UUID.
22 pub uuid: Uuid,
23 /// The current gamemode of the player, like survival or creative.
24 pub gamemode: GameMode,
25 /// The player's latency in milliseconds. The bars in the tab screen depend
26 /// on this.
27 pub latency: i32,
28 /// The player's display name in the tab list, but only if it's different
29 /// from the player's normal username. Use `player_info.profile.name` to get
30 /// the player's actual username.
31 pub display_name: Option<Box<FormattedText>>,
32}
33
34/// A component only present in players that contains the [`GameProfile`] (which
35/// you can use to get a player's name).
36///
37/// Note that it's possible for this to be missing in a player if the server
38/// never sent the player info for them (though this is uncommon).
39#[derive(Component, Clone, Debug, Deref, DerefMut)]
40pub struct GameProfileComponent(pub GameProfile);
41
42/// Add a [`GameProfileComponent`] when an [`AddPlayerEvent`] is received.
43/// Usually the `GameProfileComponent` will be added from the
44/// `ClientboundGamePacket::AddPlayer` handler though.
45pub fn retroactively_add_game_profile_component(
46 mut commands: Commands,
47 mut events: EventReader<AddPlayerEvent>,
48 entity_uuid_index: Res<EntityUuidIndex>,
49) {
50 for event in events.read() {
51 if let Some(entity) = entity_uuid_index.get(&event.info.uuid) {
52 commands
53 .entity(entity)
54 .insert(GameProfileComponent(event.info.profile.clone()));
55 }
56 }
57}