Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvsch committed Dec 1, 2024
1 parent f29cfbc commit 51de1cf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
3 changes: 2 additions & 1 deletion pumpkin-protocol/src/client/play/c_entity_animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ impl CEntityAnimation {
}
}

#[derive(Debug)]
#[repr(u8)]
pub enum Animation {
SwingMainArm,
LeaveBed,
LeaveBed = 2,
SwingOffhand,
CriticalEffect,
MagicCriticaleffect,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Default for PlayerConfig {
chat_mode: ChatMode::Enabled,
chat_colors: true,
skin_parts: 0,
main_hand: Hand::Main,
main_hand: Hand::Right,
text_filtering: false,
server_listing: false,
}
Expand Down
40 changes: 24 additions & 16 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,25 +359,32 @@ impl Player {
}

pub async fn handle_swing_arm(&self, swing_arm: SSwingArm) {
match Hand::from_i32(swing_arm.hand.0) {
Some(hand) => {
let animation = match hand {
Hand::Main => Animation::SwingMainArm,
Hand::Off => Animation::SwingOffhand,
};
let id = self.entity_id();
let world = self.world();
world
.broadcast_packet_except(
&[self.gameprofile.id],
&CEntityAnimation::new(id.into(), animation as u8),
)
.await;
}
None => {
let animation = match swing_arm.hand.0 {
0 => Animation::SwingMainArm,
1 => Animation::SwingOffhand,
_ => {
self.kick(TextComponent::text("Invalid hand")).await;
return;
}
};
// Invert hand if player is left handed
let animation = match self.config.lock().await.main_hand {
Hand::Left => match animation {
Animation::SwingMainArm => Animation::SwingOffhand,
Animation::SwingOffhand => Animation::SwingMainArm,
_ => unreachable!(),
},
Hand::Right => animation,
};

let id = self.entity_id();
let world = self.world();
world
.broadcast_packet_except(
&[self.gameprofile.id],
&CEntityAnimation::new(id.into(), animation as u8),
)
.await;
}

pub async fn handle_chat_message(&self, chat_message: SChatMessage) {
Expand Down Expand Up @@ -442,6 +449,7 @@ impl Player {
text_filtering: client_information.text_filtering,
server_listing: client_information.server_listing,
};
self.update_client_information().await;
} else {
self.kick(TextComponent::text("Invalid hand or chat type"))
.await;
Expand Down
38 changes: 30 additions & 8 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use pumpkin_core::{
use pumpkin_entity::{entity_type::EntityType, EntityId};
use pumpkin_inventory::player::PlayerInventory;
use pumpkin_macros::sound;
use pumpkin_protocol::server::play::{SClickContainer, SKeepAlive};
use pumpkin_protocol::server::play::{SCookieResponse as SPCookieResponse, SPlayPingRequest};
use pumpkin_protocol::{
bytebuf::packet_id::Packet,
Expand All @@ -41,6 +40,10 @@ use pumpkin_protocol::{
},
RawPacket, ServerPacket, SoundCategory, VarInt,
};
use pumpkin_protocol::{
client::play::{CSetEntityMetadata, Metadata},
server::play::{SClickContainer, SKeepAlive},
};
use pumpkin_world::{cylindrical_chunk_iterator::Cylindrical, item::ItemStack};
use tokio::sync::{Mutex, Notify};
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -656,6 +659,24 @@ impl Player {
.await;
}

/// Send skin layers and used hand to all players
pub async fn update_client_information(&self) {
let config = self.config.lock().await;
let world = self.world();
world
.broadcast_packet_all(&CSetEntityMetadata::new(
self.entity_id().into(),
Metadata::new(17, 0.into(), config.skin_parts),
))
.await;
world
.broadcast_packet_all(&CSetEntityMetadata::new(
self.entity_id().into(),
Metadata::new(18, 0.into(), config.main_hand as u8),
))
.await;
}

pub async fn send_system_message<'a>(&self, text: &TextComponent<'a>) {
self.client
.send_packet(&CSystemChatMessage::new(text, false))
Expand Down Expand Up @@ -845,16 +866,17 @@ impl Default for Abilities {
}

/// Represents the player's dominant hand.
#[derive(FromPrimitive, Clone)]
#[derive(Debug, FromPrimitive, Clone, Copy)]
#[repr(u8)]
pub enum Hand {
/// The player's primary hand (usually the right hand).
Main,
/// The player's off-hand (usually the left hand).
Off,
/// Usually the player's off-hand.
Left,
/// Usually the player's primary hand.
Right,
}

/// Represents the player's chat mode settings.
#[derive(FromPrimitive, Clone)]
#[derive(Debug, FromPrimitive, Clone)]
pub enum ChatMode {
/// Chat is enabled for the player.
Enabled,
Expand All @@ -865,7 +887,7 @@ pub enum ChatMode {
}

/// the player's permission level
#[derive(FromPrimitive, ToPrimitive, Clone, Copy)]
#[derive(Debug, FromPrimitive, ToPrimitive, Clone, Copy)]
#[repr(i8)]
pub enum PermissionLvl {
Zero = 0,
Expand Down
1 change: 1 addition & 0 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ impl World {
}],
))
.await;
player.update_client_information().await;

// here we send all the infos of already joined players
let mut entries = Vec::new();
Expand Down

0 comments on commit 51de1cf

Please sign in to comment.