Skip to content

Commit 2363cba

Browse files
committed
Implement SetExperience
1 parent b7ad0e6 commit 2363cba

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

azalea-client/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ use crate::{
2929
connection::RawConnection,
3030
cookies::ServerCookies,
3131
interact::BlockStatePredictionHandler,
32-
local_player::{Hunger, PermissionLevel, TabList, WorldHolder},
32+
local_player::{Experience, Hunger, PermissionLevel, TabList, WorldHolder},
3333
mining,
3434
movement::LastSentLookDirection,
3535
player::retroactively_add_game_profile_component,
3636
};
37-
3837
/// A bundle of components that's inserted right when we switch to the `login`
3938
/// state and stay present on our clients until we disconnect.
4039
///
@@ -67,6 +66,7 @@ pub struct JoinedClientBundle {
6766
pub permission_level: PermissionLevel,
6867
pub chunk_batch_info: ChunkBatchInfo,
6968
pub hunger: Hunger,
69+
pub experience: Experience,
7070
pub cookies: ServerCookies,
7171

7272
pub entity_id_index: EntityIdIndex,

azalea-client/src/local_player.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use derive_more::{Deref, DerefMut};
77
use parking_lot::RwLock;
88
use uuid::Uuid;
99

10-
use crate::{ClientInformation, player::PlayerInfo};
10+
use crate::{player::PlayerInfo, ClientInformation};
1111

1212
/// A component that keeps strong references to our [`PartialWorld`] and
1313
/// [`World`] for local players.
@@ -104,6 +104,27 @@ impl Hunger {
104104
}
105105
}
106106

107+
/// The player's experience state.
108+
#[derive(Clone, Component, Debug)]
109+
pub struct Experience {
110+
/// Progress towards the next level, in the range 0.0..1.0.
111+
pub progress: f32,
112+
/// The current experience level. You'll mostly be using this.
113+
pub level: u32,
114+
/// Total experience points accumulated.
115+
pub total: u32,
116+
}
117+
118+
impl Default for Experience {
119+
fn default() -> Self {
120+
Experience {
121+
progress: 0.0,
122+
level: 0,
123+
total: 0,
124+
}
125+
}
126+
}
127+
107128
impl WorldHolder {
108129
/// Create a new `WorldHolder` for the given entity.
109130
///

azalea-client/src/plugins/packet/game/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::sync::{Arc, Weak};
22

33
use azalea_chat::FormattedText;
44
use azalea_protocol::packets::{
5-
Packet,
65
game::{ClientboundGamePacket, ClientboundPlayerCombatKill, ServerboundGamePacket},
6+
Packet,
77
};
88
use azalea_world::{World, WorldName};
99
use bevy_ecs::prelude::*;

azalea-client/src/plugins/packet/game/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use azalea_core::{
88
position::{ChunkPos, Vec3},
99
};
1010
use azalea_entity::{
11-
ActiveEffects, Dead, EntityBundle, EntityKindComponent, HasClientLoaded, LoadedBy, LocalEntity,
12-
LookDirection, Physics, PlayerAbilities, Position, RelativeEntityUpdate,
1311
indexing::{EntityIdIndex, EntityUuidIndex},
1412
inventory::Inventory,
15-
metadata::{Health, apply_metadata},
13+
metadata::{apply_metadata, Health},
14+
ActiveEffects, Dead, EntityBundle, EntityKindComponent, HasClientLoaded, LoadedBy, LocalEntity,
15+
LookDirection, Physics, PlayerAbilities, Position, RelativeEntityUpdate,
1616
};
1717
use azalea_protocol::{
1818
common::movements::MoveFlags,
19-
packets::{ConnectionProtocol, game::*},
19+
packets::{game::*, ConnectionProtocol},
2020
};
2121
use azalea_registry::builtin::EntityKind;
2222
use azalea_world::{PartialWorld, WorldName, Worlds};
@@ -25,7 +25,6 @@ pub use events::*;
2525
use tracing::{debug, error, trace, warn};
2626

2727
use crate::{
28-
ClientInformation,
2928
block_update::QueuedServerBlockUpdates,
3029
chat::{ChatPacket, ChatReceivedEvent},
3130
chunks,
@@ -34,11 +33,12 @@ use crate::{
3433
disconnect::DisconnectEvent,
3534
interact::BlockStatePredictionHandler,
3635
inventory::{ClientsideCloseContainerEvent, MenuOpenedEvent, SetContainerContentEvent},
37-
local_player::{Hunger, LocalGameMode, TabList, WorldHolder},
36+
local_player::{Experience, Hunger, LocalGameMode, TabList, WorldHolder},
3837
movement::{KnockbackData, KnockbackEvent},
3938
packet::{as_system, declare_packet_handlers},
4039
player::{GameProfileComponent, PlayerInfo},
4140
tick_counter::TicksConnected,
41+
ClientInformation,
4242
};
4343

4444
pub fn process_packet(ecs: &mut World, player: Entity, packet: &ClientboundGamePacket) {
@@ -781,6 +781,13 @@ impl GamePacketHandler<'_> {
781781

782782
pub fn set_experience(&mut self, p: &ClientboundSetExperience) {
783783
debug!("Got set experience packet {p:?}");
784+
785+
as_system::<Query<&mut Experience>>(self.ecs, |mut query| {
786+
let mut experience = query.get_mut(self.player).unwrap();
787+
experience.progress = p.experience_progress;
788+
experience.level = p.experience_level;
789+
experience.total = p.total_experience;
790+
});
784791
}
785792

786793
pub fn teleport_entity(&mut self, p: &ClientboundTeleportEntity) {

azalea/src/client_impl/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use azalea_client::{
77
connection::RawConnection,
88
disconnect::DisconnectEvent,
99
join::{ConnectOpts, StartJoinServerEvent},
10-
local_player::{Hunger, TabList, WorldHolder},
10+
local_player::{Experience, Hunger, TabList, WorldHolder},
1111
packet::game::SendGamePacketEvent,
1212
player::{GameProfileComponent, PlayerInfo},
1313
start_ecs_runner,
@@ -327,6 +327,13 @@ impl Client {
327327
self.component::<Hunger>().to_owned()
328328
}
329329

330+
/// Get the experience of this client.
331+
///
332+
/// This is a shortcut for `self.component::<Experience>().to_owned()`.
333+
pub fn experience(&self) -> Experience {
334+
self.component::<Experience>().to_owned()
335+
}
336+
330337
/// Get the username of this client.
331338
///
332339
/// This is a shortcut for

0 commit comments

Comments
 (0)