azalea_client/local_player.rs
1use std::{collections::HashMap, sync::Arc};
2
3use azalea_core::game_type::GameMode;
4use azalea_world::{PartialWorld, World};
5use bevy_ecs::{component::Component, prelude::*};
6use derive_more::{Deref, DerefMut};
7use parking_lot::RwLock;
8use uuid::Uuid;
9
10use crate::{ClientInformation, player::PlayerInfo};
11
12/// A component that keeps strong references to our [`PartialWorld`] and
13/// [`World`] for local players.
14///
15/// This can also act as a convenient way to access the player's `World`, since
16/// the alternative is to look up the player's [`WorldName`] in the [`Worlds`]
17/// resource.
18///
19/// [`Worlds`]: azalea_world::Worlds
20/// [`WorldName`]: azalea_world::WorldName
21#[derive(Clone, Component)]
22pub struct WorldHolder {
23 /// The slice of the world that this client actually has loaded, based on
24 /// its render distance.
25 pub partial: Arc<RwLock<PartialWorld>>,
26 /// The combined [`PartialWorld`]s of all clients in the same world.
27 ///
28 /// The distinction between this and `partial` is mostly only relevant if
29 /// you're using a shared world (i.e. a swarm). If in doubt, prefer to use
30 /// the shared world.
31 pub shared: Arc<RwLock<World>>,
32}
33#[deprecated = "renamed to `WorldHolder`."]
34pub type InstanceHolder = WorldHolder;
35
36/// A local player's previous game mode.
37///
38/// This component is not present on non-local players. This is `None` if the
39/// server specifically told us that the player has no previous gamemode.
40///
41/// Also see [`GameMode`].
42#[derive(Clone, Component, Copy, Debug)]
43pub struct PreviousGameMode(pub Option<GameMode>);
44
45/// Level must be 0..=4
46#[derive(Clone, Component, Default, Deref, DerefMut)]
47pub struct PermissionLevel(pub u8);
48
49/// A component that contains a map of player UUIDs to their information in the
50/// tab list.
51///
52/// ```
53/// # use azalea_client::local_player::TabList;
54/// fn example(tab_list: &TabList) {
55/// println!("Online players:");
56/// for (uuid, player_info) in tab_list.iter() {
57/// println!("- {} ({}ms)", player_info.profile.name, player_info.latency);
58/// }
59/// }
60/// ```
61///
62/// For convenience, `TabList` is also a resource in the ECS.
63/// It's set to be the same as the tab list for the last client whose tab list
64/// was updated.
65/// This means you should avoid using `TabList` as a resource unless you know
66/// all of your clients will have the same tab list.
67#[derive(Clone, Component, Debug, Default, Deref, DerefMut, Resource)]
68pub struct TabList(HashMap<Uuid, PlayerInfo>);
69
70#[derive(Clone, Component, Debug)]
71pub struct Hunger {
72 /// The main hunger bar. This is typically in the range `0..=20`.
73 pub food: u32,
74 /// The amount of saturation the player has.
75 ///
76 /// This isn't displayed in the vanilla Minecraft GUI, but it's used
77 /// internally by the game. It's a decrementing counter, and the player's
78 /// [`Hunger::food`] only starts decreasing when their saturation reaches 0.
79 pub saturation: f32,
80}
81
82impl Default for Hunger {
83 fn default() -> Self {
84 Hunger {
85 food: 20,
86 saturation: 5.,
87 }
88 }
89}
90impl Hunger {
91 /// Returns true if we have enough food level to sprint.
92 ///
93 /// Note that this doesn't consider our gamemode or passenger status.
94 pub fn is_enough_to_sprint(&self) -> bool {
95 // hasEnoughFoodToSprint
96 self.food >= 6
97 }
98}
99
100/// The player's experience state.
101#[derive(Clone, Component, Debug)]
102pub struct Experience {
103 /// Progress towards the next level, in the range 0.0..1.0.
104 pub progress: f32,
105 /// The current experience level. You'll mostly be using this.
106 pub level: u32,
107 /// Total experience points accumulated.
108 pub total: u32,
109}
110
111impl Default for Experience {
112 fn default() -> Self {
113 Experience {
114 progress: 0.0,
115 level: 0,
116 total: 0,
117 }
118 }
119}
120
121impl WorldHolder {
122 /// Create a new `WorldHolder` for the given entity.
123 ///
124 /// The partial world will be created for you. The render distance will
125 /// be set to a default value, which you can change by creating a new
126 /// partial world.
127 pub fn new(entity: Entity, shared: Arc<RwLock<World>>) -> Self {
128 let client_information = ClientInformation::default();
129
130 WorldHolder {
131 shared,
132 partial: Arc::new(RwLock::new(PartialWorld::new(
133 azalea_world::chunk::calculate_chunk_storage_range(
134 client_information.view_distance.into(),
135 ),
136 Some(entity),
137 ))),
138 }
139 }
140
141 /// Reset the [`World`] to be a reference to an empty world, but with
142 /// the same registries as the current one.
143 ///
144 /// This is used by Azalea when entering the config state.
145 pub fn reset(&mut self) {
146 let registries = self.shared.read().registries.clone();
147
148 let new_world = World {
149 registries,
150 ..Default::default()
151 };
152 self.shared = Arc::new(RwLock::new(new_world));
153
154 self.partial.write().reset();
155 }
156}