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    event::EventReader,
7    system::{Commands, Res},
8};
9use uuid::Uuid;
10
11use crate::{packet_handling::game::AddPlayerEvent, GameProfileComponent};
12
13/// A player in the tab list.
14#[derive(Debug, Clone)]
15pub struct PlayerInfo {
16    /// Information about the player's Minecraft account, including their
17    /// username.
18    pub profile: GameProfile,
19    /// The player's UUID.
20    pub uuid: Uuid,
21    /// The current gamemode of the player, like survival or creative.
22    pub gamemode: GameMode,
23    /// The player's latency in milliseconds. The bars in the tab screen depend
24    /// on this.
25    pub latency: i32,
26    /// The player's display name in the tab list, but only if it's different
27    /// from the player's normal username. Use `player_info.profile.name` to get
28    /// the player's actual username.
29    pub display_name: Option<FormattedText>,
30}
31
32/// Add a [`GameProfileComponent`] when an [`AddPlayerEvent`] is received.
33/// Usually the `GameProfileComponent` will be added from the
34/// `ClientboundGamePacket::AddPlayer` handler though.
35pub fn retroactively_add_game_profile_component(
36    mut commands: Commands,
37    mut events: EventReader<AddPlayerEvent>,
38    entity_uuid_index: Res<EntityUuidIndex>,
39) {
40    for event in events.read() {
41        if let Some(entity) = entity_uuid_index.get(&event.info.uuid) {
42            commands
43                .entity(entity)
44                .insert(GameProfileComponent(event.info.profile.clone()));
45        }
46    }
47}