aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/player.rs
blob: b1978e7c73fdcbb9276cbd810267fc1b20e8f0e8 (plain)
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
use azalea_auth::game_profile::GameProfile;
use azalea_chat::FormattedText;
use azalea_core::game_type::GameMode;
use azalea_entity::indexing::EntityUuidIndex;
use bevy_ecs::{
    component::Component,
    message::MessageReader,
    system::{Commands, Res},
};
use derive_more::{Deref, DerefMut};
use uuid::Uuid;

use crate::packet::game::AddPlayerEvent;

/// A player in the tab list.
#[derive(Clone, Debug)]
pub struct PlayerInfo {
    /// Information about the player's Minecraft account, including their
    /// username.
    pub profile: GameProfile,
    /// The player's UUID.
    pub uuid: Uuid,
    /// The current gamemode of the player, like survival or creative.
    pub gamemode: GameMode,
    /// The player's latency in milliseconds.
    ///
    /// The bars in the tab screen depend on this.
    pub latency: i32,
    /// The player's display name in the tab list, but only if it's different
    /// from the player's normal username.
    ///
    /// Use [`GameProfile::name`] to get the player's actual username.
    pub display_name: Option<Box<FormattedText>>,
}

/// A component only present in players that contains the [`GameProfile`] (which
/// you can use to get a player's name).
///
/// Note that it's possible for this to be missing in a player if the server
/// never sent the player info for them (though this is uncommon).
#[derive(Clone, Component, Debug, Deref, DerefMut)]
pub struct GameProfileComponent(pub GameProfile);

/// Add a [`GameProfileComponent`] when an [`AddPlayerEvent`] is received.
/// Usually the `GameProfileComponent` will be added from the
/// `ClientboundGamePacket::AddPlayer` handler though.
pub fn retroactively_add_game_profile_component(
    mut commands: Commands,
    mut events: MessageReader<AddPlayerEvent>,
    entity_uuid_index: Res<EntityUuidIndex>,
) {
    for event in events.read() {
        if let Some(entity) = entity_uuid_index.get(&event.info.uuid) {
            commands
                .entity(entity)
                .insert(GameProfileComponent(event.info.profile.clone()));
        }
    }
}