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

Handle player packet id 0x23 #163

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ Cargo.lock

# === PROJECT SPECIFIC ===
# mc decompiled source
mc-source-code/DecompilerMC/
mc-source-code/src/
mc-source-code/target/
mc-source-code/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you modified the .gitignore ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I've used the "decompile.sh" file and that folder for save researches, to prevent any accidental upload I've changed to include any file and subfolder


plugins/*
world/*
Expand Down
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 @@ -7,6 +7,7 @@ mod s_confirm_teleport;
mod s_interact;
mod s_keep_alive;
mod s_ping_request;
mod s_player_abilities;
mod s_player_action;
mod s_player_command;
mod s_player_ground;
Expand All @@ -28,6 +29,7 @@ pub use s_confirm_teleport::*;
pub use s_interact::*;
pub use s_keep_alive::*;
pub use s_ping_request::*;
pub use s_player_abilities::*;
pub use s_player_action::*;
pub use s_player_command::*;
pub use s_player_ground::*;
Expand Down
10 changes: 10 additions & 0 deletions pumpkin-protocol/src/server/play/s_player_abilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use pumpkin_macros::packet;
use serde::Deserialize;

//The vanilla client sends this packet when the player starts/stops flying. Bitmask 0x02 is set when the player is flying.

#[derive(Deserialize)]
#[packet(0x23)]
pub struct SPlayerAbilities {
pub flags: i8,
}
17 changes: 12 additions & 5 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ use pumpkin_core::{
};
use pumpkin_entity::EntityId;
use pumpkin_inventory::{InventoryError, WindowType};
use pumpkin_protocol::server::play::{SCloseContainer, SSetPlayerGround, SUseItem};
use pumpkin_protocol::{
client::play::{
Animation, CAcknowledgeBlockChange, CBlockUpdate, CEntityAnimation, CEntityVelocity,
CHeadRot, CHurtAnimation, CPingResponse, CPlayerChatMessage, CUpdateEntityPos,
CUpdateEntityPosRot, CUpdateEntityRot, CWorldEvent, FilterType,
},
server::play::{
Action, ActionType, SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport,
SInteract, SPlayPingRequest, SPlayerAction, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm,
SUseItemOn, Status,
Action, ActionType, SChatCommand, SChatMessage, SClientInformationPlay, SCloseContainer,
SConfirmTeleport, SInteract, SPlayPingRequest, SPlayerAbilities, SPlayerAction,
SPlayerCommand, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation,
SSetCreativeSlot, SSetHeldItem, SSetPlayerGround, SSwingArm, SUseItem, SUseItemOn, Status,
},
};
use pumpkin_world::block::{BlockFace, BlockState};
Expand Down Expand Up @@ -466,6 +465,7 @@ impl Player {
None => self.kick(TextComponent::text("Invalid action type")).await,
}
}

pub async fn handle_player_action(&self, player_action: SPlayerAction) {
match Status::from_i32(player_action.status.0) {
Some(status) => match status {
Expand Down Expand Up @@ -539,6 +539,13 @@ impl Player {
}
}

pub async fn handle_player_abilities(&self, player_abilities: SPlayerAbilities) {
let mut abilities = self.abilities.lock().await;

// Set the flying ability
abilities.flying = player_abilities.flags & 0x02 != 0 && abilities.allow_flying;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The abilities have allow_flying as well, Should should set them and not check if they true in the packet

Copy link
Contributor Author

@tuttarealstep tuttarealstep Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen that this packet is sent by the client only when the player starts/stops flying and is not sending the allow_flying property. I've implemented the same check that vanilla does (bit mask 0x02 on the Flags field of the packed and the additional check on the player allow_flying flag) to set the flying property

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, Just looked at vanilla code and your right!, nvm then

}

pub async fn handle_play_ping_request(&self, request: SPlayPingRequest) {
self.client
.send_packet(&CPingResponse::new(request.payload))
Expand Down
17 changes: 11 additions & 6 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use pumpkin_protocol::{
},
server::play::{
SChatCommand, SChatMessage, SClickContainer, SClientInformationPlay, SConfirmTeleport,
SInteract, SPlayPingRequest, SPlayerAction, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSetPlayerGround,
SSwingArm, SUseItem, SUseItemOn,
SInteract, SPlayPingRequest, SPlayerAbilities, SPlayerAction, SPlayerCommand,
SPlayerPosition, SPlayerPositionRotation, SPlayerRotation, SSetCreativeSlot, SSetHeldItem,
SSetPlayerGround, SSwingArm, SUseItem, SUseItemOn,
},
RawPacket, ServerPacket, VarInt,
};
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct Player {
/// This field represents the various abilities that the player possesses, such as flight, invulnerability, and other special effects.
///
/// **Note:** When the `abilities` field is updated, the server should send a `send_abilities_update` packet to the client to notify them of the changes.
pub abilities: PlayerAbilities,
pub abilities: Mutex<PlayerAbilities>,
/// The player's last known position.
///
/// This field is used to calculate the player's movement delta for network synchronization and other purposes.
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Player {
open_container: AtomicCell::new(None),
carried_item: AtomicCell::new(None),
teleport_id_count: AtomicI32::new(0),
abilities: PlayerAbilities::default(),
abilities: Mutex::new(PlayerAbilities::default()),
gamemode: AtomicCell::new(gamemode),
watched_section: AtomicCell::new(Vector3::new(0, 0, 0)),
last_position: AtomicCell::new(Vector3::new(0.0, 0.0, 0.0)),
Expand Down Expand Up @@ -157,7 +157,7 @@ impl Player {
/// Updates the current abilities the Player has
pub async fn send_abilties_update(&mut self) {
let mut b = 0i8;
let abilities = &self.abilities;
let abilities = &self.abilities.lock().await;

if abilities.invulnerable {
b |= 1;
Expand Down Expand Up @@ -379,6 +379,11 @@ impl Player {
.await;
Ok(())
}
SPlayerAbilities::PACKET_ID => {
self.handle_player_abilities(SPlayerAbilities::read(bytebuf)?)
.await;
Ok(())
}
SUseItemOn::PACKET_ID => {
self.handle_use_item_on(SUseItemOn::read(bytebuf)?).await;
Ok(())
Expand Down