diff options
Diffstat (limited to 'azalea-client/src')
| -rw-r--r-- | azalea-client/src/client.rs | 10 | ||||
| -rw-r--r-- | azalea-client/src/local_player.rs | 10 | ||||
| -rw-r--r-- | azalea-client/src/packet_handling.rs | 13 |
3 files changed, 29 insertions, 4 deletions
diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index a7cceaed..e2ca6c4e 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -7,7 +7,7 @@ use crate::{ inventory::{InventoryComponent, InventoryPlugin}, local_player::{ death_event, handle_send_packet_event, update_in_loaded_chunk, GameProfileComponent, - LocalPlayer, PhysicsState, SendPacketEvent, + Hunger, LocalPlayer, PhysicsState, SendPacketEvent, }, mining::{self, MinePlugin}, movement::{LastSentLookDirection, PlayerMovePlugin}, @@ -565,6 +565,14 @@ impl Client { pub fn health(&self) -> f32 { *self.component::<Health>() } + + /// Get the hunger level of this client, which includes both food and + /// saturation. + /// + /// This is a shortcut for `self.component::<Hunger>().to_owned()`. + pub fn hunger(&self) -> Hunger { + self.component::<Hunger>().to_owned() + } } /// A bundle for the components that are present on a local player that received diff --git a/azalea-client/src/local_player.rs b/azalea-client/src/local_player.rs index 8317a72d..27ac28dc 100644 --- a/azalea-client/src/local_player.rs +++ b/azalea-client/src/local_player.rs @@ -89,6 +89,16 @@ pub struct LocalGameMode { pub previous: Option<GameMode>, } +#[derive(Component, Clone)] +pub struct Hunger { + /// The main hunger bar. Goes from 0 to 20. + pub food: u32, + /// The amount of saturation the player has. This isn't shown in normal + /// vanilla clients but it's a separate counter that makes it so your hunger + /// only starts decreasing when this is 0. + pub saturation: f32, +} + impl LocalPlayer { /// Create a new `LocalPlayer`. pub fn new( diff --git a/azalea-client/src/packet_handling.rs b/azalea-client/src/packet_handling.rs index 158fbb83..1805002f 100644 --- a/azalea-client/src/packet_handling.rs +++ b/azalea-client/src/packet_handling.rs @@ -46,7 +46,7 @@ use crate::{ ClientSideCloseContainerEvent, InventoryComponent, MenuOpenedEvent, SetContainerContentEvent, }, - local_player::{GameProfileComponent, LocalGameMode, LocalPlayer}, + local_player::{GameProfileComponent, Hunger, LocalGameMode, LocalPlayer}, ClientInformation, PlayerInfo, }; @@ -280,6 +280,11 @@ pub fn process_packet_events(ecs: &mut World) { current: p.game_type, previous: p.previous_game_type.into(), }, + // this gets overwritten later by the SetHealth packet + Hunger { + food: 20, + saturation: 5., + }, player_bundle, )); } @@ -699,11 +704,13 @@ pub fn process_packet_events(ecs: &mut World) { ClientboundGamePacket::SetHealth(p) => { debug!("Got set health packet {:?}", p); - let mut system_state: SystemState<Query<&mut Health>> = SystemState::new(ecs); + let mut system_state: SystemState<Query<(&mut Health, &mut Hunger)>> = + SystemState::new(ecs); let mut query = system_state.get_mut(ecs); - let mut health = query.get_mut(player_entity).unwrap(); + let (mut health, mut hunger) = query.get_mut(player_entity).unwrap(); **health = p.health; + (hunger.food, hunger.saturation) = (p.food, p.saturation); // the `Dead` component is added by the `update_dead` system // in azalea-world and then the `dead_event` system fires |
