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
This commit is contained in:
Sekky61 2022-09-10 02:54:09 +02:00 committed by GitHub
parent 7b3e317c06
commit a80052097a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 5 deletions

View file

@ -32,11 +32,11 @@ use crate::protocol::packets::s2c::play::{
BiomeRegistry, ChatTypeRegistry, ChunkLoadDistance, ChunkRenderDistanceCenter, ClearTitles, BiomeRegistry, ChatTypeRegistry, ChunkLoadDistance, ChunkRenderDistanceCenter, ClearTitles,
DimensionTypeRegistry, DimensionTypeRegistryEntry, Disconnect, EntitiesDestroy, DimensionTypeRegistry, DimensionTypeRegistryEntry, Disconnect, EntitiesDestroy,
EntityAnimation, EntityAttributes, EntityAttributesProperty, EntityPosition, EntitySetHeadYaw, EntityAnimation, EntityAttributes, EntityAttributesProperty, EntityPosition, EntitySetHeadYaw,
EntityStatus, EntityTrackerUpdate, EntityVelocityUpdate, GameJoin, GameMessage, EntityStatus, EntityTrackerUpdate, EntityVelocityUpdate, ExperienceBarUpdate, GameJoin,
GameStateChange, GameStateChangeReason, KeepAlive, MoveRelative, OverlayMessage, PlaySoundId, GameMessage, GameStateChange, GameStateChangeReason, HealthUpdate, KeepAlive, MoveRelative,
PlayerActionResponse, PlayerPositionLook, PlayerPositionLookFlags, PlayerRespawn, OverlayMessage, PlaySoundId, PlayerActionResponse, PlayerPositionLook, PlayerPositionLookFlags,
PlayerSpawnPosition, RegistryCodec, Rotate, RotateAndMoveRelative, S2cPlayPacket, PlayerRespawn, PlayerSpawnPosition, RegistryCodec, Rotate, RotateAndMoveRelative,
SoundCategory, UnloadChunk, UpdateSubtitle, UpdateTitle, S2cPlayPacket, SoundCategory, UnloadChunk, UpdateSubtitle, UpdateTitle,
}; };
use crate::protocol::{BoundedInt, ByteAngle, NbtBridge, RawBytes, VarInt}; use crate::protocol::{BoundedInt, ByteAngle, NbtBridge, RawBytes, VarInt};
use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer}; use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer};
@ -544,6 +544,35 @@ impl<C: Config> Client<C> {
self.send_packet(ClearTitles { reset: true }); 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. /// Gets whether or not the client is connected to the server.
/// ///
/// A disconnected client object will never become reconnected. It is your /// A disconnected client object will never become reconnected. It is your

View file

@ -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! { def_struct! {
UpdateSubtitle { UpdateSubtitle {
subtitle_text: Text, subtitle_text: Text,
@ -843,6 +859,8 @@ pub mod play {
PlayerSpawnPosition = 77, PlayerSpawnPosition = 77,
EntityTrackerUpdate = 80, EntityTrackerUpdate = 80,
EntityVelocityUpdate = 82, EntityVelocityUpdate = 82,
ExperienceBarUpdate = 84,
HealthUpdate = 85,
UpdateSubtitle = 91, UpdateSubtitle = 91,
WorldTimeUpdate = 92, WorldTimeUpdate = 92,
UpdateTitle = 93, UpdateTitle = 93,