Skip to content

Commit

Permalink
Begin Refactoring Clientbound Packets to new structure
Browse files Browse the repository at this point in the history
See #19 for details
  • Loading branch information
Snowiiii committed Aug 5, 2024
1 parent 1e114c0 commit 9e63429
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 151 deletions.
17 changes: 6 additions & 11 deletions pumpkin-protocol/src/client/login/c_login_disconnect.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket};

#[derive(Serialize)]
#[packet(0x00)]
pub struct CLoginDisconnect<'a> {
reason: &'a str,
json_reason: &'a str,
}

impl<'a> CLoginDisconnect<'a> {
pub fn new(reason: &'a str) -> Self {
Self { reason }
}
}

impl<'a> ClientPacket for CLoginDisconnect<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_string(&serde_json::to_string_pretty(&self.reason).unwrap());
// input json!
pub fn new(json_reason: &'a str) -> Self {
Self { json_reason }
}
}
14 changes: 5 additions & 9 deletions pumpkin-protocol/src/client/login/c_set_compression.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket, VarInt};
use crate::VarInt32;

#[derive(Serialize)]
#[packet(0x03)]
pub struct CSetCompression {
threshold: VarInt,
threshold: VarInt32,
}

impl CSetCompression {
pub fn new(threshold: VarInt) -> Self {
pub fn new(threshold: VarInt32) -> Self {
Self { threshold }
}
}

impl ClientPacket for CSetCompression {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_var_int(self.threshold);
}
}
11 changes: 2 additions & 9 deletions pumpkin-protocol/src/client/play/c_change_difficulty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket};

#[derive(Serialize)]
#[packet(0x0B)]
pub struct CChangeDifficulty {
difficulty: u8,
Expand All @@ -13,10 +13,3 @@ impl CChangeDifficulty {
Self { difficulty, locked }
}
}

impl ClientPacket for CChangeDifficulty {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_u8(self.difficulty);
bytebuf.put_bool(self.locked);
}
}
11 changes: 2 additions & 9 deletions pumpkin-protocol/src/client/play/c_game_event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket};

#[derive(Serialize)]
#[packet(0x22)]
pub struct CGameEvent {
event: u8,
Expand All @@ -13,10 +13,3 @@ impl CGameEvent {
Self { event, value }
}
}

impl ClientPacket for CGameEvent {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_u8(self.event);
bytebuf.put_f32(self.value);
}
}
10 changes: 3 additions & 7 deletions pumpkin-protocol/src/client/play/c_play_disconnect.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, text::TextComponent, ClientPacket};
use crate::text::TextComponent;

#[derive(Serialize)]
#[packet(0x1D)]
pub struct CPlayDisconnect {
reason: TextComponent,
Expand All @@ -12,9 +14,3 @@ impl CPlayDisconnect {
Self { reason }
}
}

impl ClientPacket for CPlayDisconnect {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_slice(&self.reason.encode());
}
}
10 changes: 2 additions & 8 deletions pumpkin-protocol/src/client/play/c_set_held_item.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket};

#[derive(Serialize)]
#[packet(0x53)]
pub struct CSetHeldItem {
slot: i8,
Expand All @@ -12,9 +12,3 @@ impl CSetHeldItem {
Self { slot }
}
}

impl ClientPacket for CSetHeldItem {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_i8(self.slot);
}
}
20 changes: 5 additions & 15 deletions pumpkin-protocol/src/client/play/c_sync_player_position.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket, VarInt};
use crate::VarInt32;

#[derive(Serialize)]
#[packet(0x40)]
pub struct CSyncPlayerPostion {
x: f64,
Expand All @@ -10,7 +12,7 @@ pub struct CSyncPlayerPostion {
yaw: f32,
pitch: f32,
flags: i8,
teleport_id: VarInt,
teleport_id: VarInt32,
}

impl CSyncPlayerPostion {
Expand All @@ -21,7 +23,7 @@ impl CSyncPlayerPostion {
yaw: f32,
pitch: f32,
flags: i8,
teleport_id: VarInt,
teleport_id: VarInt32,
) -> Self {
Self {
x,
Expand All @@ -34,15 +36,3 @@ impl CSyncPlayerPostion {
}
}
}

impl ClientPacket for CSyncPlayerPostion {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_f64(self.x);
bytebuf.put_f64(self.y);
bytebuf.put_f64(self.z);
bytebuf.put_f32(self.yaw.to_degrees());
bytebuf.put_f32(self.pitch.to_degrees());
bytebuf.put_i8(self.flags);
bytebuf.put_var_int(self.teleport_id);
}
}
11 changes: 3 additions & 8 deletions pumpkin-protocol/src/client/play/c_system_chat_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, text::TextComponent, ClientPacket};
use crate::text::TextComponent;

#[derive(Serialize)]
#[packet(0x6C)]
pub struct CSystemChatMessge {
content: TextComponent,
Expand All @@ -13,10 +15,3 @@ impl CSystemChatMessge {
Self { content, overlay }
}
}

impl ClientPacket for CSystemChatMessge {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_slice(&self.content.encode());
bytebuf.put_bool(self.overlay);
}
}
6 changes: 6 additions & 0 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ impl VarInt32 {
}
}

impl From<i32> for VarInt32 {
fn from(value: i32) -> Self {
VarInt32(value)
}
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Error)]
pub enum VarIntDecodeError {
#[error("incomplete VarInt decode")]
Expand Down
68 changes: 15 additions & 53 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,27 @@ use crate::{
use super::{authentication::auth_digest, Client, EncryptionError, PlayerConfig};

/// Processes incoming Packets from the Client to the Server
/// Implements the `Client` Packets, So everything before the Play state, then will use the `PlayerPacketProcessor`
pub trait ClientPacketProcessor {
// Handshake
fn handle_handshake(&mut self, server: &mut Server, handshake: SHandShake);
// Status
fn handle_status_request(&mut self, server: &mut Server, status_request: SStatusRequest);
fn handle_ping_request(&mut self, server: &mut Server, ping_request: SPingRequest);
// Login
fn handle_login_start(&mut self, server: &mut Server, login_start: SLoginStart);
fn handle_encryption_response(
&mut self,
server: &mut Server,
encryption_response: SEncryptionResponse,
);
fn handle_plugin_response(
&mut self,
server: &mut Server,
plugin_response: SLoginPluginResponse,
);
fn handle_login_acknowledged(
&mut self,
server: &mut Server,
login_acknowledged: SLoginAcknowledged,
);
// Config
fn handle_client_information(
&mut self,
server: &mut Server,
client_information: SClientInformation,
);
fn handle_plugin_message(&mut self, server: &mut Server, plugin_message: SPluginMessage);
fn handle_known_packs(&mut self, server: &mut Server, config_acknowledged: SKnownPacks);
fn handle_config_acknowledged(
&mut self,
server: &mut Server,
config_acknowledged: SAcknowledgeFinishConfig,
);
}

impl ClientPacketProcessor for Client {
fn handle_handshake(&mut self, _server: &mut Server, handshake: SHandShake) {
/// Implements the `Client` Packets
impl Client {
pub fn handle_handshake(&mut self, _server: &mut Server, handshake: SHandShake) {
// TODO set protocol version and check protocol version
self.connection_state = handshake.next_state;
dbg!("handshake");
}

fn handle_status_request(&mut self, server: &mut Server, _status_request: SStatusRequest) {
pub fn handle_status_request(&mut self, server: &mut Server, _status_request: SStatusRequest) {
self.send_packet(CStatusResponse::new(&server.status_response_json))
.unwrap_or_else(|e| self.kick(&e.to_string()));
}

fn handle_ping_request(&mut self, _server: &mut Server, ping_request: SPingRequest) {
pub fn handle_ping_request(&mut self, _server: &mut Server, ping_request: SPingRequest) {
dbg!("ping");
self.send_packet(CPingResponse::new(ping_request.payload))
.unwrap_or_else(|e| self.kick(&e.to_string()));
self.close();
}

fn handle_login_start(&mut self, server: &mut Server, login_start: SLoginStart) {
pub fn handle_login_start(&mut self, server: &mut Server, login_start: SLoginStart) {
dbg!("login start");
// default game profile, when no online mode
self.gameprofile = Some(GameProfile {
Expand All @@ -105,7 +67,7 @@ impl ClientPacketProcessor for Client {
.unwrap_or_else(|e| self.kick(&e.to_string()));
}

fn handle_encryption_response(
pub fn handle_encryption_response(
&mut self,
server: &mut Server,
encryption_response: SEncryptionResponse,
Expand Down Expand Up @@ -145,14 +107,14 @@ impl ClientPacketProcessor for Client {
}
}

fn handle_plugin_response(
pub fn handle_plugin_response(
&mut self,
_server: &mut Server,
_plugin_response: SLoginPluginResponse,
) {
}

fn handle_login_acknowledged(
pub fn handle_login_acknowledged(
&mut self,
server: &mut Server,
_login_acknowledged: SLoginAcknowledged,
Expand All @@ -170,24 +132,24 @@ impl ClientPacketProcessor for Client {
.unwrap_or_else(|e| self.kick(&e.to_string()));
dbg!("login achnowlaged");
}
fn handle_client_information(
pub fn handle_client_information(
&mut self,
_server: &mut Server,
client_information: SClientInformation,
) {
self.config = Some(PlayerConfig {
locale: client_information.locale,
view_distance: client_information.view_distance,
chat_mode: ChatMode::from_varint(client_information.chat_mode),
chat_mode: ChatMode::from(client_information.chat_mode),
chat_colors: client_information.chat_colors,
skin_parts: client_information.skin_parts,
main_hand: Hand::from_varint(client_information.main_hand),
main_hand: Hand::from(client_information.main_hand),
text_filtering: client_information.text_filtering,
server_listing: client_information.server_listing,
});
}

fn handle_plugin_message(&mut self, _server: &mut Server, plugin_message: SPluginMessage) {
pub fn handle_plugin_message(&mut self, _server: &mut Server, plugin_message: SPluginMessage) {
if plugin_message.channel.starts_with("minecraft:brand")
|| plugin_message.channel.starts_with("MC|Brand")
{
Expand All @@ -199,7 +161,7 @@ impl ClientPacketProcessor for Client {
}
}

fn handle_known_packs(&mut self, _server: &mut Server, _config_acknowledged: SKnownPacks) {
pub fn handle_known_packs(&mut self, _server: &mut Server, _config_acknowledged: SKnownPacks) {
for registry in Registry::get_static() {
self.send_packet(CRegistryData::new(
&registry.registry_id,
Expand All @@ -214,7 +176,7 @@ impl ClientPacketProcessor for Client {
.unwrap_or_else(|e| self.kick(&e.to_string()));
}

fn handle_config_acknowledged(
pub fn handle_config_acknowledged(
&mut self,
server: &mut Server,
_config_acknowledged: SAcknowledgeFinishConfig,
Expand Down
Loading

0 comments on commit 9e63429

Please sign in to comment.