From a80052097a2cf74dfbab91daf6b8eccfff9fe85f Mon Sep 17 00:00:00 2001 From: Sekky61 Date: Sat, 10 Sep 2022 02:54:09 +0200 Subject: [PATCH] Added SetExperience and SetHealth packets (#34) * Added `SetExperience` and `SetHealth` packets Packets were tested * Rename packets in accordance with `packets.json` Renamed `SetExperience` to `ExperienceBarUpdate` and `SetHealth` to `HealthUpdate` * `set_level` and `set_health_and_food` functions --- src/client.rs | 39 ++++++++++++++++++++++++++++++++----- src/protocol/packets/s2c.rs | 18 +++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/client.rs b/src/client.rs index 3e29c58..ab5f21e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -32,11 +32,11 @@ use crate::protocol::packets::s2c::play::{ BiomeRegistry, ChatTypeRegistry, ChunkLoadDistance, ChunkRenderDistanceCenter, ClearTitles, DimensionTypeRegistry, DimensionTypeRegistryEntry, Disconnect, EntitiesDestroy, EntityAnimation, EntityAttributes, EntityAttributesProperty, EntityPosition, EntitySetHeadYaw, - EntityStatus, EntityTrackerUpdate, EntityVelocityUpdate, GameJoin, GameMessage, - GameStateChange, GameStateChangeReason, KeepAlive, MoveRelative, OverlayMessage, PlaySoundId, - PlayerActionResponse, PlayerPositionLook, PlayerPositionLookFlags, PlayerRespawn, - PlayerSpawnPosition, RegistryCodec, Rotate, RotateAndMoveRelative, S2cPlayPacket, - SoundCategory, UnloadChunk, UpdateSubtitle, UpdateTitle, + EntityStatus, EntityTrackerUpdate, EntityVelocityUpdate, ExperienceBarUpdate, GameJoin, + GameMessage, GameStateChange, GameStateChangeReason, HealthUpdate, KeepAlive, MoveRelative, + OverlayMessage, PlaySoundId, PlayerActionResponse, PlayerPositionLook, PlayerPositionLookFlags, + PlayerRespawn, PlayerSpawnPosition, RegistryCodec, Rotate, RotateAndMoveRelative, + S2cPlayPacket, SoundCategory, UnloadChunk, UpdateSubtitle, UpdateTitle, }; use crate::protocol::{BoundedInt, ByteAngle, NbtBridge, RawBytes, VarInt}; use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer}; @@ -544,6 +544,35 @@ impl Client { self.send_packet(ClearTitles { reset: true }); } + /// Sets the XP bar visible above hotbar and total experience. + /// + /// # Arguments + /// * `bar` - Floating value in the range `0.0..=1.0` indicating progress on the XP bar. + /// * `level` - Number above the XP bar. + /// * `total_xp` - TODO. + pub fn set_level(&mut self, bar: f32, level: i32, total_xp: i32) { + self.send_packet(ExperienceBarUpdate { + bar, + level: level.into(), + total_xp: total_xp.into(), + }) + } + + /// Sets the health and food of the player. + /// You can read more about hunger and saturation [here](https://minecraft.fandom.com/wiki/Food#Hunger_vs._Saturation). + /// + /// # Arguments + /// * `health` - Float in range `0.0..=20.0`. Value `<=0` is legal and will kill the player. + /// * `food` - Integer in range `0..=20`. + /// * `food_saturation` - Float in range `0.0..=5.0`. + pub fn set_health_and_food(&mut self, health: f32, food: i32, food_saturation: f32) { + self.send_packet(HealthUpdate { + health, + food: food.into(), + food_saturation, + }) + } + /// Gets whether or not the client is connected to the server. /// /// A disconnected client object will never become reconnected. It is your diff --git a/src/protocol/packets/s2c.rs b/src/protocol/packets/s2c.rs index 925a44b..409e88a 100644 --- a/src/protocol/packets/s2c.rs +++ b/src/protocol/packets/s2c.rs @@ -701,6 +701,22 @@ pub mod play { } } + def_struct! { + ExperienceBarUpdate { + bar: f32, + level: VarInt, + total_xp: VarInt, + } + } + + def_struct! { + HealthUpdate { + health: f32, + food: VarInt, + food_saturation: f32, + } + } + def_struct! { UpdateSubtitle { subtitle_text: Text, @@ -843,6 +859,8 @@ pub mod play { PlayerSpawnPosition = 77, EntityTrackerUpdate = 80, EntityVelocityUpdate = 82, + ExperienceBarUpdate = 84, + HealthUpdate = 85, UpdateSubtitle = 91, WorldTimeUpdate = 92, UpdateTitle = 93,