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

feat: PlayerCombatKill client event (#44)

* feat: PlayerCombatKill client event

* Event name changed to Death

* dead client state and respawn packet

* fix doc comment
This commit is contained in:
Ryan 2022-11-21 21:20:09 -08:00 committed by GitHub
parent 6dfce515bd
commit 087e056bbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ use azalea_protocol::{
connect::{Connection, ConnectionError, ReadConnection, WriteConnection}, connect::{Connection, ConnectionError, ReadConnection, WriteConnection},
packets::{ packets::{
game::{ game::{
clientbound_player_combat_kill_packet::ClientboundPlayerCombatKillPacket,
serverbound_accept_teleportation_packet::ServerboundAcceptTeleportationPacket, serverbound_accept_teleportation_packet::ServerboundAcceptTeleportationPacket,
serverbound_client_information_packet::ServerboundClientInformationPacket, serverbound_client_information_packet::ServerboundClientInformationPacket,
serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, serverbound_custom_payload_packet::ServerboundCustomPayloadPacket,
@ -64,6 +65,8 @@ pub enum Event {
Packet(Box<ClientboundGamePacket>), Packet(Box<ClientboundGamePacket>),
/// Happens when a player is added, removed, or updated in the tab list. /// Happens when a player is added, removed, or updated in the tab list.
UpdatePlayers(UpdatePlayersEvent), UpdatePlayers(UpdatePlayersEvent),
/// Emits when the player dies.
Death(Option<Box<ClientboundPlayerCombatKillPacket>>),
} }
/// Happens when a player is added, removed, or updated in the tab list. /// Happens when a player is added, removed, or updated in the tab list.
@ -102,6 +105,7 @@ pub struct Client {
pub world: Arc<RwLock<World>>, pub world: Arc<RwLock<World>>,
pub physics_state: Arc<Mutex<PhysicsState>>, pub physics_state: Arc<Mutex<PhysicsState>>,
pub client_information: Arc<RwLock<ClientInformation>>, pub client_information: Arc<RwLock<ClientInformation>>,
pub dead: Arc<Mutex<bool>>,
/// Plugins are a way for other crates to add custom functionality to the /// Plugins are a way for other crates to add custom functionality to the
/// client and keep state. If you're not making a plugin and you're using /// client and keep state. If you're not making a plugin and you're using
/// the `azalea` crate. you can ignore this field. /// the `azalea` crate. you can ignore this field.
@ -280,6 +284,7 @@ impl Client {
world: Arc::new(RwLock::new(World::default())), world: Arc::new(RwLock::new(World::default())),
physics_state: Arc::new(Mutex::new(PhysicsState::default())), physics_state: Arc::new(Mutex::new(PhysicsState::default())),
client_information: Arc::new(RwLock::new(ClientInformation::default())), client_information: Arc::new(RwLock::new(ClientInformation::default())),
dead: Arc::new(Mutex::new(false)),
// The plugins can be modified by the user by replacing the plugins // The plugins can be modified by the user by replacing the plugins
// field right after this. No Mutex so the user doesn't need to .lock(). // field right after this. No Mutex so the user doesn't need to .lock().
plugins: Arc::new(Plugins::new()), plugins: Arc::new(Plugins::new()),
@ -753,6 +758,13 @@ impl Client {
} }
ClientboundGamePacket::SetHealth(p) => { ClientboundGamePacket::SetHealth(p) => {
debug!("Got set health packet {:?}", p); debug!("Got set health packet {:?}", p);
if p.health == 0.0 {
let mut dead_lock = client.dead.lock();
if !*dead_lock {
*dead_lock = true;
tx.send(Event::Death(None)).unwrap();
}
}
} }
ClientboundGamePacket::SetExperience(p) => { ClientboundGamePacket::SetExperience(p) => {
debug!("Got set experience packet {:?}", p); debug!("Got set experience packet {:?}", p);
@ -877,11 +889,25 @@ impl Client {
ClientboundGamePacket::PlayerChatHeader(_) => {} ClientboundGamePacket::PlayerChatHeader(_) => {}
ClientboundGamePacket::PlayerCombatEnd(_) => {} ClientboundGamePacket::PlayerCombatEnd(_) => {}
ClientboundGamePacket::PlayerCombatEnter(_) => {} ClientboundGamePacket::PlayerCombatEnter(_) => {}
ClientboundGamePacket::PlayerCombatKill(_) => {} ClientboundGamePacket::PlayerCombatKill(p) => {
debug!("Got player kill packet {:?}", p);
if *client.entity_id.read() == p.player_id {
let mut dead_lock = client.dead.lock();
if !*dead_lock {
*dead_lock = true;
tx.send(Event::Death(Some(Box::new(p.clone())))).unwrap();
}
}
}
ClientboundGamePacket::PlayerLookAt(_) => {} ClientboundGamePacket::PlayerLookAt(_) => {}
ClientboundGamePacket::RemoveMobEffect(_) => {} ClientboundGamePacket::RemoveMobEffect(_) => {}
ClientboundGamePacket::ResourcePack(_) => {} ClientboundGamePacket::ResourcePack(_) => {}
ClientboundGamePacket::Respawn(_) => {} ClientboundGamePacket::Respawn(p) => {
debug!("Got respawn packet {:?}", p);
// Sets clients dead state to false.
let mut dead_lock = client.dead.lock();
*dead_lock = false;
}
ClientboundGamePacket::SelectAdvancementsTab(_) => {} ClientboundGamePacket::SelectAdvancementsTab(_) => {}
ClientboundGamePacket::SetActionBarText(_) => {} ClientboundGamePacket::SetActionBarText(_) => {}
ClientboundGamePacket::SetBorderCenter(_) => {} ClientboundGamePacket::SetBorderCenter(_) => {}