1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

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>
This commit is contained in:
Luuk van Oijen 2023-08-22 05:50:21 +02:00 committed by GitHub
parent 1b6e024460
commit a81c4c060b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 6 deletions

View file

@ -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

View file

@ -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(

View file

@ -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

View file

@ -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
}