Skip to content

Commit

Permalink
Add Player swing animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 7, 2024
1 parent bb6e3d9 commit 0cec9bf
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 159 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Pumpkin is currently under heavy development.
- [x] Player Client brand
- [x] Player Teleport
- [x] Player Movement
- [ ] Player Animation
- [x] Player Animation
- [ ] Player Inventory
- [ ] Player Attack
- Server
Expand Down
31 changes: 31 additions & 0 deletions pumpkin-protocol/src/client/play/c_entity_animation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use num_derive::ToPrimitive;
use pumpkin_macros::packet;
use serde::Serialize;

use crate::VarInt;

#[derive(Serialize, Clone)]
#[packet(0x03)]
pub struct CEntityAnimation {
entity_id: VarInt,
/// See `Animation`
animation: u8,
}

impl CEntityAnimation {
pub fn new(entity_id: VarInt, animation: u8) -> Self {
Self {
entity_id,
animation,
}
}
}

#[derive(ToPrimitive)]
pub enum Animation {
SwingMainArm,
LeaveBed,
SwingOffhand,
CriticalEffect,
MagicCriticaleffect,
}
1 change: 0 additions & 1 deletion pumpkin-protocol/src/client/play/c_player_abilities.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use pumpkin_macros::packet;
use serde::Serialize;


#[derive(Serialize)]
#[packet(0x38)]
pub struct CPlayerAbilities {
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod c_change_difficulty;
mod c_chunk_data_update_light;
mod c_entity_animation;
mod c_entity_metadata;
mod c_game_event;
mod c_head_rot;
Expand All @@ -19,6 +20,7 @@ mod player_action;

pub use c_change_difficulty::*;
pub use c_chunk_data_update_light::*;
pub use c_entity_animation::*;
pub use c_entity_metadata::*;
pub use c_game_event::*;
pub use c_head_rot::*;
Expand Down
14 changes: 14 additions & 0 deletions pumpkin-protocol/src/server/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ impl ServerPacket for SPlayerPosition {
}
}

pub struct SSwingArm {
pub hand: VarInt,
}

impl ServerPacket for SSwingArm {
const PACKET_ID: VarInt = VarInt(0x36);

fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
hand: bytebuf.get_var_int(),
}
}
}

pub struct SPlayerCommand {
pub entitiy_id: VarInt,
pub action: Action,
Expand Down
3 changes: 2 additions & 1 deletion pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use pumpkin_protocol::{
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
play::{
SChatCommand, SConfirmTeleport, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation,
SPlayerPositionRotation, SPlayerRotation, SSwingArm,
},
status::{SPingRequest, SStatusRequest},
},
Expand Down Expand Up @@ -256,6 +256,7 @@ impl Client {
SPlayerCommand::PACKET_ID => {
self.handle_player_command(server, SPlayerCommand::read(bytebuf))
}
SSwingArm::PACKET_ID => self.handle_swing_arm(server, SSwingArm::read(bytebuf)),
_ => log::error!("Failed to handle player packet id {}", packet.id.0),
}
}
Expand Down
19 changes: 17 additions & 2 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use num_traits::FromPrimitive;
use pumpkin_protocol::{
client::play::{CHeadRot, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot},
client::play::{
Animation, CEntityAnimation, CHeadRot, CUpdateEntityPos, CUpdateEntityPosRot,
CUpdateEntityRot,
},
server::play::{
SChatCommand, SConfirmTeleport, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation,
SPlayerRotation, SSwingArm,
},
};

use crate::{
commands::{handle_command, CommandSender},
entity::player::Hand,
server::Server,
};

Expand Down Expand Up @@ -156,4 +161,14 @@ impl Client {
pumpkin_protocol::server::play::Action::StartFlyingElytra => todo!(),
}
}

pub fn handle_swing_arm(&mut self, server: &mut Server, swing_arm: SSwingArm) {
let animation = match Hand::from_i32(swing_arm.hand.0).unwrap() {
Hand::Main => Animation::SwingMainArm,
Hand::Off => Animation::SwingOffhand,
};
let player = self.player.as_mut().unwrap();
let id = player.entity_id();
server.broadcast_packet_expect(self, CEntityAnimation::new(id.into(), animation as u8))
}
}
153 changes: 0 additions & 153 deletions pumpkin/src/config/configuration.rs

This file was deleted.

3 changes: 2 additions & 1 deletion pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use pumpkin_protocol::{
client::{
config::CPluginMessage,
play::{
CChunkDataUpdateLight, CGameEvent, CLogin, CPlayerAbilities, CPlayerInfoUpdate, CSpawnEntity, PlayerAction,
CChunkDataUpdateLight, CGameEvent, CLogin, CPlayerAbilities, CPlayerInfoUpdate,
CSpawnEntity, PlayerAction,
},
},
BitSet, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL,
Expand Down

0 comments on commit 0cec9bf

Please sign in to comment.