diff options
| author | Luuk van Oijen <lazyluuk.channel@gmail.com> | 2023-08-22 05:50:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-21 22:50:21 -0500 |
| commit | a81c4c060b9a32d1dccf451158750fac52349acc (patch) | |
| tree | fc0760495c1197b0e6ef1c743ebcc0f2edb766ae | |
| parent | 1b6e0244606cf7917e68918d32928550aabf85b0 (diff) | |
| download | azalea-drasl-a81c4c060b9a32d1dccf451158750fac52349acc.tar.xz | |
Food/saturation component support (#97)
* modified for food stuff
* moved food/saturation to a separate file
* hunger component
* simplify some logic
---------
Co-authored-by: mat <git@matdoes.dev>
| -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 | ||||
| -rw-r--r-- | azalea/src/swarm/mod.rs | 4 |
4 files changed, 31 insertions, 6 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 diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index f84905ec..05f39388 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -159,13 +159,13 @@ where pub fn set_handler<S, Fut>(self, handler: HandleFn<S, Fut>) -> SwarmBuilder<S, SS> where Fut: Future<Output = Result<(), anyhow::Error>> + Send + 'static, - S: Send + Sync + Clone + Component + 'static, + S: Send + Sync + Clone + Component + Default + 'static, { SwarmBuilder { handler: Some(Box::new(move |bot, event, state: S| { Box::pin(handler(bot, event, state)) })), - states: Vec::new(), + states: vec![S::default(); self.accounts.len()], app: self.app, ..self } |
