Skip to content

Commit

Permalink
Add Play Ping/Pong
Browse files Browse the repository at this point in the history
Used for Debug, F3+3
  • Loading branch information
Snowiiii committed Aug 20, 2024
1 parent 5122097 commit 938a415
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 12 deletions.
14 changes: 14 additions & 0 deletions pumpkin-protocol/src/client/play/c_ping_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use pumpkin_macros::packet;
use serde::Serialize;

#[derive(Serialize)]
#[packet(0x36)]
pub struct CPingResponse {
payload: i64,
}

impl CPingResponse {
pub fn new(payload: i64) -> Self {
Self { payload }
}
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod c_hurt_animation;
mod c_login;
mod c_open_screen;
mod c_particle;
mod c_ping_response;
mod c_play_disconnect;
mod c_player_abilities;
mod c_player_chat_message;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub use c_hurt_animation::*;
pub use c_login::*;
pub use c_open_screen::*;
pub use c_particle::*;
pub use c_ping_response::*;
pub use c_play_disconnect::*;
pub use c_player_abilities::*;
pub use c_player_chat_message::*;
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 @@ -3,6 +3,7 @@ mod s_chat_message;
mod s_client_information;
mod s_confirm_teleport;
mod s_interact;
mod s_ping_request;
mod s_player_action;
mod s_player_command;
mod s_player_position;
Expand All @@ -18,6 +19,7 @@ pub use s_chat_message::*;
pub use s_client_information::*;
pub use s_confirm_teleport::*;
pub use s_interact::*;
pub use s_ping_request::*;
pub use s_player_action::*;
pub use s_player_command::*;
pub use s_player_position::*;
Expand Down
8 changes: 8 additions & 0 deletions pumpkin-protocol/src/server/play/s_ping_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use pumpkin_macros::packet;
use serde::Deserialize;

#[derive(Deserialize)]
#[packet(0x21)]
pub struct SPlayPingRequest {
pub payload: i64,
}
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/server/status/s_ping_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use pumpkin_macros::packet;

#[derive(serde::Deserialize)]
#[packet(0x01)]
pub struct SPingRequest {
pub struct SStatusPingRequest {
pub payload: i64,
}
4 changes: 2 additions & 2 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pumpkin_protocol::{
config::{SAcknowledgeFinishConfig, SClientInformationConfig, SKnownPacks, SPluginMessage},
handshake::SHandShake,
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
status::{SPingRequest, SStatusRequest},
status::{SStatusPingRequest, SStatusRequest},
},
ConnectionState, KnownPack, CURRENT_MC_PROTOCOL,
};
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Client {
self.send_packet(&CStatusResponse::new(&server.status_response_json));
}

pub fn handle_ping_request(&mut self, _server: &mut Server, ping_request: SPingRequest) {
pub fn handle_ping_request(&mut self, _server: &mut Server, ping_request: SStatusPingRequest) {
dbg!("ping");
self.send_packet(&CPingResponse::new(ping_request.payload));
self.close();
Expand Down
14 changes: 9 additions & 5 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ use pumpkin_protocol::{
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
play::{
SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SInteract,
SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn,
SPlayPingRequest, SPlayerAction, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm,
SUseItemOn,
},
status::{SPingRequest, SStatusRequest},
status::{SStatusPingRequest, SStatusRequest},
},
ClientPacket, ConnectionState, PacketError, RawPacket, ServerPacket,
};
Expand Down Expand Up @@ -201,8 +202,8 @@ impl Client {
SStatusRequest::PACKET_ID => {
self.handle_status_request(server, SStatusRequest::read(bytebuf).unwrap())
}
SPingRequest::PACKET_ID => {
self.handle_ping_request(server, SPingRequest::read(bytebuf).unwrap())
SStatusPingRequest::PACKET_ID => {
self.handle_ping_request(server, SStatusPingRequest::read(bytebuf).unwrap())
}
_ => log::error!(
"Failed to handle packet id {} while in Status state",
Expand Down Expand Up @@ -307,6 +308,9 @@ impl Client {
SSetCreativeSlot::PACKET_ID => {
self.handle_set_creative_slot(server, SSetCreativeSlot::read(bytebuf).unwrap())
}
SPlayPingRequest::PACKET_ID => {
self.handle_play_ping_request(server, SPlayPingRequest::read(bytebuf).unwrap())
}
_ => log::error!("Failed to handle player packet id {}", packet.id.0),
}
}
Expand Down
13 changes: 9 additions & 4 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use pumpkin_entity::EntityId;
use pumpkin_protocol::{
client::play::{
Animation, CAcknowledgeBlockChange, CBlockUpdate, CEntityAnimation, CEntityVelocity,
CHeadRot, CHurtAnimation, CPlayerChatMessage, CUpdateEntityPos, CUpdateEntityPosRot,
CUpdateEntityRot, CWorldEvent, FilterType,
CHeadRot, CHurtAnimation, CPingResponse, CPlayerChatMessage, CUpdateEntityPos,
CUpdateEntityPosRot, CUpdateEntityRot, CWorldEvent, FilterType,
},
position::WorldPosition,
server::play::{
Action, ActionType, SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport,
SInteract, SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn, Status,
SInteract, SPlayPingRequest, SPlayerAction, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm,
SUseItemOn, Status,
},
};
use pumpkin_text::TextComponent;
Expand Down Expand Up @@ -361,6 +362,10 @@ impl Client {
}
}

pub fn handle_play_ping_request(&mut self, _server: &mut Server, request: SPlayPingRequest) {
self.send_packet(&CPingResponse::new(request.payload));
}

pub fn handle_use_item_on(&mut self, server: &mut Server, use_item_on: SUseItemOn) {
self.send_packet(&CAcknowledgeBlockChange::new(use_item_on.sequence));

Expand Down

0 comments on commit 938a415

Please sign in to comment.