Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Use Item (packet id 0x39) packet #61

Merged
merged 10 commits into from
Aug 25, 2024
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod s_player_rotation;
mod s_set_creative_slot;
mod s_set_held_item;
mod s_swing_arm;
mod s_use_item;
mod s_use_item_on;

pub use s_chat_command::*;
Expand All @@ -32,4 +33,5 @@ pub use s_player_rotation::*;
pub use s_set_creative_slot::*;
pub use s_set_held_item::*;
pub use s_swing_arm::*;
pub use s_use_item::*;
pub use s_use_item_on::*;
14 changes: 14 additions & 0 deletions pumpkin-protocol/src/server/play/s_use_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use pumpkin_macros::packet;
use serde::Deserialize;

use crate::VarInt;

#[derive(Deserialize)]
#[packet(0x39)]
pub struct SUseItem {
// 0 for main hand, 1 for off hand
pub hand: VarInt,
pub sequence: VarInt,
pub yaw: f32,
pub pitch: f32,
}
7 changes: 6 additions & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use num_traits::FromPrimitive;
use pumpkin_core::text::TextComponent;
use pumpkin_entity::EntityId;
use pumpkin_inventory::WindowType;
use pumpkin_protocol::server::play::{SCloseContainer, SSetPlayerGround};
use pumpkin_protocol::server::play::{SCloseContainer, SSetPlayerGround, SUseItem};
use pumpkin_protocol::{
client::play::{
Animation, CAcknowledgeBlockChange, CBlockUpdate, CEntityAnimation, CEntityVelocity,
Expand Down Expand Up @@ -400,6 +400,11 @@ impl Player {
}
}

pub fn handle_use_item(&mut self, _server: &mut Server, _use_item: SUseItem) {
// TODO: handle packet correctly
log::error!("An item was used(SUseItem), but the packet is not implemented yet");
}

pub fn handle_set_held_item(&mut self, _server: &mut Server, held: SSetHeldItem) {
let slot = held.slot;
if !(0..=8).contains(&slot) {
Expand Down
7 changes: 6 additions & 1 deletion pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use pumpkin_protocol::{
server::play::{
SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SInteract,
SPlayPingRequest, SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSetPlayerGround, SSwingArm, SUseItemOn,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSetPlayerGround, SSwingArm, SUseItem,
SUseItemOn,
},
ConnectionState, RawPacket, ServerPacket, VarInt,
};
Expand Down Expand Up @@ -237,6 +238,10 @@ impl Player {
self.handle_use_item_on(server, SUseItemOn::read(bytebuf)?);
Ok(())
}
SUseItem::PACKET_ID => {
self.handle_use_item(server, SUseItem::read(bytebuf)?);
Ok(())
}
SSetHeldItem::PACKET_ID => {
self.handle_set_held_item(server, SSetHeldItem::read(bytebuf)?);
Ok(())
Expand Down