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    message::MessageReader,
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.
26    ///
27    /// The bars in the tab screen depend on this.
28    pub latency: i32,
29    /// The player's display name in the tab list, but only if it's different
30    /// from the player's normal username.
31    ///
32    /// Use [`GameProfile::name`] to get the player's actual username.
33    pub display_name: Option<Box<FormattedText>>,
34}
35
36/// A component only present in players that contains the [`GameProfile`] (which
37/// you can use to get a player's name).
38///
39/// Note that it's possible for this to be missing in a player if the server
40/// never sent the player info for them (though this is uncommon).
41#[derive(Component, Clone, Debug, Deref, DerefMut)]
42pub struct GameProfileComponent(pub GameProfile);
43
44/// Add a [`GameProfileComponent`] when an [`AddPlayerEvent`] is received.
45/// Usually the `GameProfileComponent` will be added from the
46/// `ClientboundGamePacket::AddPlayer` handler though.
47pub fn retroactively_add_game_profile_component(
48    mut commands: Commands,
49    mut events: MessageReader<AddPlayerEvent>,
50    entity_uuid_index: Res<EntityUuidIndex>,
51) {
52    for event in events.read() {
53        if let Some(entity) = entity_uuid_index.get(&event.info.uuid) {
54            commands
55                .entity(entity)
56                .insert(GameProfileComponent(event.info.profile.clone()));
57        }
58    }
59}