From 1fed6dca5f85e735147e09e4287796e2d8fe78d6 Mon Sep 17 00:00:00 2001 From: lukas0008 Date: Mon, 5 Aug 2024 14:12:56 +0200 Subject: [PATCH] Update serializer for more types --- Cargo.lock | 1 + Cargo.toml | 3 +- pumpkin-protocol/Cargo.toml | 3 + pumpkin-protocol/src/bytebuf/packet_id.rs | 1 - pumpkin-protocol/src/bytebuf/serializer.rs | 74 +++++++++++-------- .../src/client/config/c_config_disconnect.rs | 7 +- .../src/client/config/c_cookie_request.rs | 14 +--- .../src/client/config/c_finish_config.rs | 7 +- .../src/client/config/c_known_packs.rs | 7 +- .../src/client/config/c_plugin_message.rs | 7 +- .../src/client/config/c_registry_data.rs | 7 +- .../src/client/login/c_encryption_request.rs | 7 +- .../src/client/login/c_login_disconnect.rs | 7 +- .../src/client/login/c_login_success.rs | 7 +- .../src/client/login/c_set_compression.rs | 7 +- .../src/client/play/c_change_difficulty.rs | 7 +- .../client/play/c_chunk_data_update_light.rs | 7 +- .../src/client/play/c_game_event.rs | 7 +- pumpkin-protocol/src/client/play/c_login.rs | 7 +- .../src/client/play/c_play_disconnect.rs | 13 ++-- .../src/client/play/c_player_abilities.rs | 7 +- .../src/client/play/c_player_info_update.rs | 7 +- .../src/client/play/c_set_held_item.rs | 7 +- .../src/client/play/c_sync_player_position.rs | 7 +- .../src/client/play/c_system_chat_message.rs | 23 ++++++ pumpkin-protocol/src/client/play/mod.rs | 2 + .../src/client/status/c_ping_response.rs | 7 +- .../src/client/status/c_status_response.rs | 12 +-- pumpkin-protocol/src/text.rs | 21 +++++- 29 files changed, 157 insertions(+), 136 deletions(-) create mode 100644 pumpkin-protocol/src/client/play/c_system_chat_message.rs diff --git a/Cargo.lock b/Cargo.lock index 94927e800..21ed84150 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1185,6 +1185,7 @@ dependencies = [ "cfb8", "fastnbt 2.5.0 (git+https://github.com/owengage/fastnbt.git)", "log", + "pumpkin-macros", "serde", "serde_json", "take_mut", diff --git a/Cargo.toml b/Cargo.toml index b9c8a2944..c2a5b77d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = [ "pumpkin-macros", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/"] +members = [ "pumpkin-macros/", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/" ] [workspace.package] version = "0.1.0" @@ -8,4 +8,3 @@ edition = "2021" [profile.release] lto = true - diff --git a/pumpkin-protocol/Cargo.toml b/pumpkin-protocol/Cargo.toml index 34a650b63..1432cb3ba 100644 --- a/pumpkin-protocol/Cargo.toml +++ b/pumpkin-protocol/Cargo.toml @@ -4,6 +4,9 @@ version.workspace = true edition.workspace = true [dependencies] + +pumpkin-macros = { path = "../pumpkin-macros" } + bytes = "1.7" uuid = "1.10" diff --git a/pumpkin-protocol/src/bytebuf/packet_id.rs b/pumpkin-protocol/src/bytebuf/packet_id.rs index 37b7fb8b6..f12b1b378 100644 --- a/pumpkin-protocol/src/bytebuf/packet_id.rs +++ b/pumpkin-protocol/src/bytebuf/packet_id.rs @@ -35,7 +35,6 @@ where P: Packet + Serialize, { fn write(&self, bytebuf: &mut ByteBuffer) { - dbg!(P::PACKET_ID); take_mut::take(bytebuf, |bytebuf| { let mut serializer = serializer::Serializer::new(bytebuf); self.serialize(&mut serializer) diff --git a/pumpkin-protocol/src/bytebuf/serializer.rs b/pumpkin-protocol/src/bytebuf/serializer.rs index e0e63ddff..42e83edb6 100644 --- a/pumpkin-protocol/src/bytebuf/serializer.rs +++ b/pumpkin-protocol/src/bytebuf/serializer.rs @@ -5,6 +5,7 @@ use thiserror::Error; use super::ByteBuffer; + pub struct Serializer { output: ByteBuffer, } @@ -66,32 +67,37 @@ impl<'a> ser::Serializer for &'a mut Serializer { Ok(()) } fn serialize_char(self, _v: char) -> Result { - todo!() + unimplemented!() } - fn serialize_f32(self, _v: f32) -> Result { - todo!() + fn serialize_f32(self, v: f32) -> Result { + self.output.put_f32(v); + Ok(()) } - fn serialize_f64(self, _v: f64) -> Result { - todo!() + fn serialize_f64(self, v: f64) -> Result { + self.output.put_f64(v); + Ok(()) } fn serialize_i128(self, _v: i128) -> Result { - todo!() + unimplemented!() } - fn serialize_i16(self, _v: i16) -> Result { - todo!() + fn serialize_i16(self, v: i16) -> Result { + self.output.put_i16(v); + Ok(()) } - fn serialize_i32(self, _v: i32) -> Result { - todo!() + fn serialize_i32(self, v: i32) -> Result { + self.output.put_i32(v); + Ok(()) } fn serialize_i64(self, v: i64) -> Result { self.output.put_i64(v); Ok(()) } - fn serialize_i8(self, _v: i8) -> Result { - todo!() + fn serialize_i8(self, v: i8) -> Result { + self.output.put_i8(v); + Ok(()) } fn serialize_map(self, _len: Option) -> Result { - todo!() + unimplemented!() } fn serialize_newtype_struct( self, @@ -101,7 +107,7 @@ impl<'a> ser::Serializer for &'a mut Serializer { where T: ?Sized + Serialize, { - todo!() + unimplemented!() } fn serialize_newtype_variant( self, @@ -113,19 +119,19 @@ impl<'a> ser::Serializer for &'a mut Serializer { where T: ?Sized + Serialize, { - todo!() + unimplemented!() } fn serialize_none(self) -> Result { - todo!() + unimplemented!() } fn serialize_seq(self, _len: Option) -> Result { - todo!() + unimplemented!() } fn serialize_some(self, _value: &T) -> Result where T: ?Sized + Serialize, { - todo!() + unimplemented!() } fn serialize_str(self, v: &str) -> Result { self.output.put_string(v); @@ -145,17 +151,17 @@ impl<'a> ser::Serializer for &'a mut Serializer { _variant: &'static str, _len: usize, ) -> Result { - todo!() + unimplemented!() } fn serialize_tuple(self, _len: usize) -> Result { - todo!() + unimplemented!() } fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result { - todo!() + unimplemented!() } fn serialize_tuple_variant( self, @@ -164,25 +170,29 @@ impl<'a> ser::Serializer for &'a mut Serializer { _variant: &'static str, _len: usize, ) -> Result { - todo!() + unimplemented!() } fn serialize_u128(self, _v: u128) -> Result { - todo!() + unimplemented!() } - fn serialize_u16(self, _v: u16) -> Result { - todo!() + fn serialize_u16(self, v: u16) -> Result { + self.output.put_u16(v); + Ok(()) } - fn serialize_u32(self, _v: u32) -> Result { - todo!() + fn serialize_u32(self, v: u32) -> Result { + self.output.put_u32(v); + Ok(()) } - fn serialize_u64(self, _v: u64) -> Result { - todo!() + fn serialize_u64(self, v: u64) -> Result { + self.output.put_u64(v); + Ok(()) } - fn serialize_u8(self, _v: u8) -> Result { - todo!() + fn serialize_u8(self, v: u8) -> Result { + self.output.put_u8(v); + Ok(()) } fn serialize_unit(self) -> Result { - todo!() + unimplemented!() } fn serialize_unit_struct(self, _name: &'static str) -> Result { todo!() diff --git a/pumpkin-protocol/src/client/config/c_config_disconnect.rs b/pumpkin-protocol/src/client/config/c_config_disconnect.rs index 4917fe1b4..2ba67b574 100644 --- a/pumpkin-protocol/src/client/config/c_config_disconnect.rs +++ b/pumpkin-protocol/src/client/config/c_config_disconnect.rs @@ -1,14 +1,11 @@ -use crate::bytebuf::packet_id::Packet; +use pumpkin_macros::packet; #[derive(serde::Serialize)] +#[packet(0x02)] pub struct CConfigDisconnect<'a> { reason: &'a str, } -impl<'a> Packet for CConfigDisconnect<'a> { - const PACKET_ID: i32 = 0x02; -} - impl<'a> CConfigDisconnect<'a> { pub fn new(reason: &'a str) -> Self { Self { reason } diff --git a/pumpkin-protocol/src/client/config/c_cookie_request.rs b/pumpkin-protocol/src/client/config/c_cookie_request.rs index f7e72ed62..52603c657 100644 --- a/pumpkin-protocol/src/client/config/c_cookie_request.rs +++ b/pumpkin-protocol/src/client/config/c_cookie_request.rs @@ -1,18 +1,12 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, Identifier, VarInt, }; +#[derive(serde::Serialize)] +#[packet(0x00)] pub struct CCookieRequest { key: Identifier, } - -impl Packet for CCookieRequest { - const PACKET_ID: VarInt = 0x00; -} - -impl ClientPacket for CCookieRequest { - fn write(&self, bytebuf: &mut ByteBuffer) { - bytebuf.put_string(&self.key); - } -} diff --git a/pumpkin-protocol/src/client/config/c_finish_config.rs b/pumpkin-protocol/src/client/config/c_finish_config.rs index 6c110b8f3..867feaf8d 100644 --- a/pumpkin-protocol/src/client/config/c_finish_config.rs +++ b/pumpkin-protocol/src/client/config/c_finish_config.rs @@ -1,6 +1,9 @@ +use pumpkin_macros::packet; + use crate::{bytebuf::packet_id::Packet, VarInt}; #[derive(serde::Serialize)] +#[packet(0x03)] pub struct CFinishConfig {} impl Default for CFinishConfig { @@ -14,7 +17,3 @@ impl CFinishConfig { Self {} } } - -impl Packet for CFinishConfig { - const PACKET_ID: VarInt = 0x03; -} diff --git a/pumpkin-protocol/src/client/config/c_known_packs.rs b/pumpkin-protocol/src/client/config/c_known_packs.rs index 47f037d65..a30651bc4 100644 --- a/pumpkin-protocol/src/client/config/c_known_packs.rs +++ b/pumpkin-protocol/src/client/config/c_known_packs.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, KnownPack, VarInt, }; +#[packet(0x0E)] pub struct CKnownPacks<'a> { known_packs: &'a [KnownPack<'a>], } @@ -13,10 +16,6 @@ impl<'a> CKnownPacks<'a> { } } -impl<'a> Packet for CKnownPacks<'a> { - const PACKET_ID: VarInt = 0x0E; -} - impl<'a> ClientPacket for CKnownPacks<'a> { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_list::(self.known_packs, |p, v| { diff --git a/pumpkin-protocol/src/client/config/c_plugin_message.rs b/pumpkin-protocol/src/client/config/c_plugin_message.rs index 0b500dd97..1adf0bc68 100644 --- a/pumpkin-protocol/src/client/config/c_plugin_message.rs +++ b/pumpkin-protocol/src/client/config/c_plugin_message.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x01)] pub struct CPluginMessage<'a> { channel: &'a str, data: &'a [u8], @@ -14,10 +17,6 @@ impl<'a> CPluginMessage<'a> { } } -impl<'a> Packet for CPluginMessage<'a> { - const PACKET_ID: VarInt = 0x01; -} - impl<'a> ClientPacket for CPluginMessage<'a> { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_string(self.channel); diff --git a/pumpkin-protocol/src/client/config/c_registry_data.rs b/pumpkin-protocol/src/client/config/c_registry_data.rs index ba2b9d92e..502622004 100644 --- a/pumpkin-protocol/src/client/config/c_registry_data.rs +++ b/pumpkin-protocol/src/client/config/c_registry_data.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x07)] pub struct CRegistryData<'a> { registry_id: &'a str, entries: &'a [RegistryEntry<'a>], @@ -22,10 +25,6 @@ pub struct RegistryEntry<'a> { pub data: Vec, } -impl<'a> Packet for CRegistryData<'a> { - const PACKET_ID: VarInt = 0x07; -} - impl<'a> ClientPacket for CRegistryData<'a> { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_string(self.registry_id); diff --git a/pumpkin-protocol/src/client/login/c_encryption_request.rs b/pumpkin-protocol/src/client/login/c_encryption_request.rs index edc278ef7..408b1316a 100644 --- a/pumpkin-protocol/src/client/login/c_encryption_request.rs +++ b/pumpkin-protocol/src/client/login/c_encryption_request.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x01)] pub struct CEncryptionRequest<'a> { server_id: &'a str, // 20 public_key: &'a [u8], @@ -26,10 +29,6 @@ impl<'a> CEncryptionRequest<'a> { } } -impl<'a> Packet for CEncryptionRequest<'a> { - const PACKET_ID: VarInt = 0x01; -} - impl<'a> ClientPacket for CEncryptionRequest<'a> { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_string(self.server_id); diff --git a/pumpkin-protocol/src/client/login/c_login_disconnect.rs b/pumpkin-protocol/src/client/login/c_login_disconnect.rs index dc98dffa6..0520a72c4 100644 --- a/pumpkin-protocol/src/client/login/c_login_disconnect.rs +++ b/pumpkin-protocol/src/client/login/c_login_disconnect.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x00)] pub struct CLoginDisconnect<'a> { reason: &'a str, } @@ -13,10 +16,6 @@ impl<'a> CLoginDisconnect<'a> { } } -impl<'a> Packet for CLoginDisconnect<'a> { - const PACKET_ID: VarInt = 0x00; -} - impl<'a> ClientPacket for CLoginDisconnect<'a> { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_string(&serde_json::to_string_pretty(&self.reason).unwrap()); diff --git a/pumpkin-protocol/src/client/login/c_login_success.rs b/pumpkin-protocol/src/client/login/c_login_success.rs index 37788efb9..c1de8d8ba 100644 --- a/pumpkin-protocol/src/client/login/c_login_success.rs +++ b/pumpkin-protocol/src/client/login/c_login_success.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, Property, VarInt, }; +#[packet(0x02)] pub struct CLoginSuccess<'a> { pub uuid: uuid::Uuid, pub username: String, // 16 @@ -26,10 +29,6 @@ impl<'a> CLoginSuccess<'a> { } } -impl<'a> Packet for CLoginSuccess<'a> { - const PACKET_ID: VarInt = 0x02; -} - impl<'a> ClientPacket for CLoginSuccess<'a> { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_uuid(self.uuid); diff --git a/pumpkin-protocol/src/client/login/c_set_compression.rs b/pumpkin-protocol/src/client/login/c_set_compression.rs index f124b0a1a..47f1957ae 100644 --- a/pumpkin-protocol/src/client/login/c_set_compression.rs +++ b/pumpkin-protocol/src/client/login/c_set_compression.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x03)] pub struct CSetCompression { threshold: VarInt, } @@ -13,10 +16,6 @@ impl CSetCompression { } } -impl Packet for CSetCompression { - const PACKET_ID: VarInt = 0x03; -} - impl ClientPacket for CSetCompression { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_var_int(self.threshold); diff --git a/pumpkin-protocol/src/client/play/c_change_difficulty.rs b/pumpkin-protocol/src/client/play/c_change_difficulty.rs index 220aebea2..6e1d7bc0c 100644 --- a/pumpkin-protocol/src/client/play/c_change_difficulty.rs +++ b/pumpkin-protocol/src/client/play/c_change_difficulty.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x0B)] pub struct CChangeDifficulty { difficulty: u8, locked: bool, @@ -14,10 +17,6 @@ impl CChangeDifficulty { } } -impl Packet for CChangeDifficulty { - const PACKET_ID: VarInt = 0x0B; -} - impl ClientPacket for CChangeDifficulty { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_u8(self.difficulty); diff --git a/pumpkin-protocol/src/client/play/c_chunk_data_update_light.rs b/pumpkin-protocol/src/client/play/c_chunk_data_update_light.rs index aeb1feb1e..91b059259 100644 --- a/pumpkin-protocol/src/client/play/c_chunk_data_update_light.rs +++ b/pumpkin-protocol/src/client/play/c_chunk_data_update_light.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, BitSet, ClientPacket, VarInt, }; +#[packet(0x27)] pub struct CChunkDataUpdateLight { chunk_x: i32, chunk_y: i32, @@ -45,10 +48,6 @@ impl CChunkDataUpdateLight { } } -impl Packet for CChunkDataUpdateLight { - const PACKET_ID: VarInt = 0x27; -} - impl ClientPacket for CChunkDataUpdateLight { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_i32(self.chunk_x); diff --git a/pumpkin-protocol/src/client/play/c_game_event.rs b/pumpkin-protocol/src/client/play/c_game_event.rs index 57042ff04..0d78f59fb 100644 --- a/pumpkin-protocol/src/client/play/c_game_event.rs +++ b/pumpkin-protocol/src/client/play/c_game_event.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x22)] pub struct CGameEvent { event: u8, value: f32, @@ -14,10 +17,6 @@ impl CGameEvent { } } -impl Packet for CGameEvent { - const PACKET_ID: VarInt = 0x22; -} - impl ClientPacket for CGameEvent { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_u8(self.event); diff --git a/pumpkin-protocol/src/client/play/c_login.rs b/pumpkin-protocol/src/client/play/c_login.rs index 34073956b..54e92c03f 100644 --- a/pumpkin-protocol/src/client/play/c_login.rs +++ b/pumpkin-protocol/src/client/play/c_login.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x2B)] pub struct CLogin { entity_id: i32, is_hardcore: bool, @@ -78,10 +81,6 @@ impl CLogin { } } -impl Packet for CLogin { - const PACKET_ID: VarInt = 0x2B; -} - impl ClientPacket for CLogin { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_i32(self.entity_id); diff --git a/pumpkin-protocol/src/client/play/c_play_disconnect.rs b/pumpkin-protocol/src/client/play/c_play_disconnect.rs index 10c89240d..a8172d959 100644 --- a/pumpkin-protocol/src/client/play/c_play_disconnect.rs +++ b/pumpkin-protocol/src/client/play/c_play_disconnect.rs @@ -1,23 +1,22 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, - text::Text, + text::TextComponent, ClientPacket, VarInt, }; +#[packet(0x1D)] pub struct CPlayDisconnect { - reason: Text, + reason: TextComponent, } impl CPlayDisconnect { - pub fn new(reason: Text) -> Self { + pub fn new(reason: TextComponent) -> Self { Self { reason } } } -impl Packet for CPlayDisconnect { - const PACKET_ID: VarInt = 0x1D; -} - impl ClientPacket for CPlayDisconnect { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_slice(&self.reason.encode()); diff --git a/pumpkin-protocol/src/client/play/c_player_abilities.rs b/pumpkin-protocol/src/client/play/c_player_abilities.rs index d6404344e..901a57279 100644 --- a/pumpkin-protocol/src/client/play/c_player_abilities.rs +++ b/pumpkin-protocol/src/client/play/c_player_abilities.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x38)] pub struct CPlayerAbilities { flags: i8, flying_speed: f32, @@ -19,10 +22,6 @@ impl CPlayerAbilities { } } -impl Packet for CPlayerAbilities { - const PACKET_ID: VarInt = 0x38; -} - impl ClientPacket for CPlayerAbilities { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_i8(self.flags); diff --git a/pumpkin-protocol/src/client/play/c_player_info_update.rs b/pumpkin-protocol/src/client/play/c_player_info_update.rs index 091577879..b8b6d44f7 100644 --- a/pumpkin-protocol/src/client/play/c_player_info_update.rs +++ b/pumpkin-protocol/src/client/play/c_player_info_update.rs @@ -1,3 +1,5 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, Property, VarInt, @@ -5,15 +7,12 @@ use crate::{ use super::PlayerAction; +#[packet(0x3E)] pub struct CPlayerInfoUpdate<'a> { pub actions: i8, pub players: &'a [Player<'a>], } -impl<'a> Packet for CPlayerInfoUpdate<'a> { - const PACKET_ID: VarInt = 0x3E; -} - pub struct Player<'a> { pub uuid: uuid::Uuid, pub actions: &'a [PlayerAction], diff --git a/pumpkin-protocol/src/client/play/c_set_held_item.rs b/pumpkin-protocol/src/client/play/c_set_held_item.rs index 41fc6c13f..16f59b551 100644 --- a/pumpkin-protocol/src/client/play/c_set_held_item.rs +++ b/pumpkin-protocol/src/client/play/c_set_held_item.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x53)] pub struct CSetHeldItem { slot: i8, } @@ -13,10 +16,6 @@ impl CSetHeldItem { } } -impl Packet for CSetHeldItem { - const PACKET_ID: VarInt = 0x53; -} - impl ClientPacket for CSetHeldItem { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_i8(self.slot); diff --git a/pumpkin-protocol/src/client/play/c_sync_player_position.rs b/pumpkin-protocol/src/client/play/c_sync_player_position.rs index 68aac2413..137476779 100644 --- a/pumpkin-protocol/src/client/play/c_sync_player_position.rs +++ b/pumpkin-protocol/src/client/play/c_sync_player_position.rs @@ -1,8 +1,11 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[packet(0x40)] pub struct CSyncPlayerPostion { x: f64, y: f64, @@ -35,10 +38,6 @@ impl CSyncPlayerPostion { } } -impl Packet for CSyncPlayerPostion { - const PACKET_ID: VarInt = 0x40; -} - impl ClientPacket for CSyncPlayerPostion { fn write(&self, bytebuf: &mut ByteBuffer) { bytebuf.put_f64(self.x); diff --git a/pumpkin-protocol/src/client/play/c_system_chat_message.rs b/pumpkin-protocol/src/client/play/c_system_chat_message.rs new file mode 100644 index 000000000..c1d694603 --- /dev/null +++ b/pumpkin-protocol/src/client/play/c_system_chat_message.rs @@ -0,0 +1,23 @@ +use pumpkin_macros::packet; + +use crate::{bytebuf::ByteBuffer, text::TextComponent, ClientPacket, VarInt}; + + +#[packet(0x6C)] +pub struct CSystemChatMessge { + content: TextComponent, + overlay: bool, +} + +impl CSystemChatMessge { + pub fn new(content: TextComponent, overlay: bool) -> Self { + Self { content, overlay } + } +} + +impl ClientPacket for CSystemChatMessge { + fn write(&self, bytebuf: &mut ByteBuffer) { + bytebuf.put_slice(&self.content.encode()); + bytebuf.put_bool(self.overlay); + } +} diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index 5e4ea9cbc..8e68eb329 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -7,6 +7,7 @@ mod c_player_abilities; mod c_player_info_update; mod c_set_held_item; mod c_sync_player_position; +mod c_system_chat_message; mod player_action; pub use c_change_difficulty::*; @@ -18,4 +19,5 @@ pub use c_player_abilities::*; pub use c_player_info_update::*; pub use c_set_held_item::*; pub use c_sync_player_position::*; +pub use c_system_chat_message::*; pub use player_action::*; diff --git a/pumpkin-protocol/src/client/status/c_ping_response.rs b/pumpkin-protocol/src/client/status/c_ping_response.rs index d667fd075..2aa7e88c4 100644 --- a/pumpkin-protocol/src/client/status/c_ping_response.rs +++ b/pumpkin-protocol/src/client/status/c_ping_response.rs @@ -1,14 +1,13 @@ +use pumpkin_macros::packet; + use crate::{bytebuf::packet_id::Packet, VarInt}; #[derive(serde::Serialize)] +#[packet(0x01)] pub struct CPingResponse { payload: i64, // must responde with the same as in `SPingRequest` } -impl Packet for CPingResponse { - const PACKET_ID: VarInt = 0x01; -} - impl CPingResponse { pub fn new(payload: i64) -> Self { Self { payload } diff --git a/pumpkin-protocol/src/client/status/c_status_response.rs b/pumpkin-protocol/src/client/status/c_status_response.rs index 4896527ef..293f42524 100644 --- a/pumpkin-protocol/src/client/status/c_status_response.rs +++ b/pumpkin-protocol/src/client/status/c_status_response.rs @@ -1,8 +1,12 @@ +use pumpkin_macros::packet; + use crate::{ bytebuf::{packet_id::Packet, ByteBuffer}, ClientPacket, VarInt, }; +#[derive(serde::Serialize)] +#[packet(0x00)] pub struct CStatusResponse<'a> { json_response: &'a str, // 32767 } @@ -12,12 +16,4 @@ impl<'a> CStatusResponse<'a> { Self { json_response } } } -impl<'a> Packet for CStatusResponse<'a> { - const PACKET_ID: VarInt = 0x00; -} -impl<'a> ClientPacket for CStatusResponse<'a> { - fn write(&self, bytebuf: &mut ByteBuffer) { - bytebuf.put_string(self.json_response); - } -} diff --git a/pumpkin-protocol/src/text.rs b/pumpkin-protocol/src/text.rs index 8e9767df2..a9a58fadd 100644 --- a/pumpkin-protocol/src/text.rs +++ b/pumpkin-protocol/src/text.rs @@ -1,18 +1,33 @@ use core::str; use fastnbt::SerOpts; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; // represents a text component // Reference: https://wiki.vg/Text_formatting#Text_components -#[derive(Clone, PartialEq, Default, Debug, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Default, Debug, Deserialize)] pub struct TextComponent { pub text: String, } +impl serde::Serialize for TextComponent { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_bytes(&self.encode()) + } +} + impl TextComponent { pub fn encode(&self) -> Vec { - fastnbt::to_bytes_with_opts(&self, SerOpts::network_nbt()).unwrap() + #[derive(serde::Serialize)] + struct TempStruct<'a> { + text: &'a String, + } + + fastnbt::to_bytes_with_opts(&TempStruct { text: &self.text }, SerOpts::network_nbt()) + .unwrap() } }