Update serializer for more types

This commit is contained in:
lukas0008 2024-08-05 14:12:56 +02:00
parent a5110eb939
commit 1fed6dca5f
29 changed files with 157 additions and 136 deletions

1
Cargo.lock generated
View File

@ -1185,6 +1185,7 @@ dependencies = [
"cfb8", "cfb8",
"fastnbt 2.5.0 (git+https://github.com/owengage/fastnbt.git)", "fastnbt 2.5.0 (git+https://github.com/owengage/fastnbt.git)",
"log", "log",
"pumpkin-macros",
"serde", "serde",
"serde_json", "serde_json",
"take_mut", "take_mut",

View File

@ -1,6 +1,6 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = [ "pumpkin-macros", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/"] members = [ "pumpkin-macros/", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/" ]
[workspace.package] [workspace.package]
version = "0.1.0" version = "0.1.0"
@ -8,4 +8,3 @@ edition = "2021"
[profile.release] [profile.release]
lto = true lto = true

View File

@ -4,6 +4,9 @@ version.workspace = true
edition.workspace = true edition.workspace = true
[dependencies] [dependencies]
pumpkin-macros = { path = "../pumpkin-macros" }
bytes = "1.7" bytes = "1.7"
uuid = "1.10" uuid = "1.10"

View File

@ -35,7 +35,6 @@ where
P: Packet + Serialize, P: Packet + Serialize,
{ {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
dbg!(P::PACKET_ID);
take_mut::take(bytebuf, |bytebuf| { take_mut::take(bytebuf, |bytebuf| {
let mut serializer = serializer::Serializer::new(bytebuf); let mut serializer = serializer::Serializer::new(bytebuf);
self.serialize(&mut serializer) self.serialize(&mut serializer)

View File

@ -5,6 +5,7 @@ use thiserror::Error;
use super::ByteBuffer; use super::ByteBuffer;
pub struct Serializer { pub struct Serializer {
output: ByteBuffer, output: ByteBuffer,
} }
@ -66,32 +67,37 @@ impl<'a> ser::Serializer for &'a mut Serializer {
Ok(()) Ok(())
} }
fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> { fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> { fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_f32(v);
Ok(())
} }
fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> { fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_f64(v);
Ok(())
} }
fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> { fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> { fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_i16(v);
Ok(())
} }
fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> { fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_i32(v);
Ok(())
} }
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
self.output.put_i64(v); self.output.put_i64(v);
Ok(()) Ok(())
} }
fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> { fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_i8(v);
Ok(())
} }
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_newtype_struct<T>( fn serialize_newtype_struct<T>(
self, self,
@ -101,7 +107,7 @@ impl<'a> ser::Serializer for &'a mut Serializer {
where where
T: ?Sized + Serialize, T: ?Sized + Serialize,
{ {
todo!() unimplemented!()
} }
fn serialize_newtype_variant<T>( fn serialize_newtype_variant<T>(
self, self,
@ -113,19 +119,19 @@ impl<'a> ser::Serializer for &'a mut Serializer {
where where
T: ?Sized + Serialize, T: ?Sized + Serialize,
{ {
todo!() unimplemented!()
} }
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error> fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error>
where where
T: ?Sized + Serialize, T: ?Sized + Serialize,
{ {
todo!() unimplemented!()
} }
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
self.output.put_string(v); self.output.put_string(v);
@ -145,17 +151,17 @@ impl<'a> ser::Serializer for &'a mut Serializer {
_variant: &'static str, _variant: &'static str,
_len: usize, _len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> { ) -> Result<Self::SerializeStructVariant, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_tuple_struct( fn serialize_tuple_struct(
self, self,
_name: &'static str, _name: &'static str,
_len: usize, _len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> { ) -> Result<Self::SerializeTupleStruct, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_tuple_variant( fn serialize_tuple_variant(
self, self,
@ -164,25 +170,29 @@ impl<'a> ser::Serializer for &'a mut Serializer {
_variant: &'static str, _variant: &'static str,
_len: usize, _len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> { ) -> Result<Self::SerializeTupleVariant, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> { fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> { fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_u16(v);
Ok(())
} }
fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> { fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_u32(v);
Ok(())
} }
fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> { fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_u64(v);
Ok(())
} }
fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> { fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
todo!() self.output.put_u8(v);
Ok(())
} }
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
todo!() unimplemented!()
} }
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
todo!() todo!()

View File

@ -1,14 +1,11 @@
use crate::bytebuf::packet_id::Packet; use pumpkin_macros::packet;
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
#[packet(0x02)]
pub struct CConfigDisconnect<'a> { pub struct CConfigDisconnect<'a> {
reason: &'a str, reason: &'a str,
} }
impl<'a> Packet for CConfigDisconnect<'a> {
const PACKET_ID: i32 = 0x02;
}
impl<'a> CConfigDisconnect<'a> { impl<'a> CConfigDisconnect<'a> {
pub fn new(reason: &'a str) -> Self { pub fn new(reason: &'a str) -> Self {
Self { reason } Self { reason }

View File

@ -1,18 +1,12 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, Identifier, VarInt, ClientPacket, Identifier, VarInt,
}; };
#[derive(serde::Serialize)]
#[packet(0x00)]
pub struct CCookieRequest { pub struct CCookieRequest {
key: Identifier, 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);
}
}

View File

@ -1,6 +1,9 @@
use pumpkin_macros::packet;
use crate::{bytebuf::packet_id::Packet, VarInt}; use crate::{bytebuf::packet_id::Packet, VarInt};
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
#[packet(0x03)]
pub struct CFinishConfig {} pub struct CFinishConfig {}
impl Default for CFinishConfig { impl Default for CFinishConfig {
@ -14,7 +17,3 @@ impl CFinishConfig {
Self {} Self {}
} }
} }
impl Packet for CFinishConfig {
const PACKET_ID: VarInt = 0x03;
}

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, KnownPack, VarInt, ClientPacket, KnownPack, VarInt,
}; };
#[packet(0x0E)]
pub struct CKnownPacks<'a> { pub struct CKnownPacks<'a> {
known_packs: &'a [KnownPack<'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> { impl<'a> ClientPacket for CKnownPacks<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_list::<KnownPack>(self.known_packs, |p, v| { bytebuf.put_list::<KnownPack>(self.known_packs, |p, v| {

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x01)]
pub struct CPluginMessage<'a> { pub struct CPluginMessage<'a> {
channel: &'a str, channel: &'a str,
data: &'a [u8], 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> { impl<'a> ClientPacket for CPluginMessage<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_string(self.channel); bytebuf.put_string(self.channel);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x07)]
pub struct CRegistryData<'a> { pub struct CRegistryData<'a> {
registry_id: &'a str, registry_id: &'a str,
entries: &'a [RegistryEntry<'a>], entries: &'a [RegistryEntry<'a>],
@ -22,10 +25,6 @@ pub struct RegistryEntry<'a> {
pub data: Vec<u8>, pub data: Vec<u8>,
} }
impl<'a> Packet for CRegistryData<'a> {
const PACKET_ID: VarInt = 0x07;
}
impl<'a> ClientPacket for CRegistryData<'a> { impl<'a> ClientPacket for CRegistryData<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_string(self.registry_id); bytebuf.put_string(self.registry_id);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x01)]
pub struct CEncryptionRequest<'a> { pub struct CEncryptionRequest<'a> {
server_id: &'a str, // 20 server_id: &'a str, // 20
public_key: &'a [u8], 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> { impl<'a> ClientPacket for CEncryptionRequest<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_string(self.server_id); bytebuf.put_string(self.server_id);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x00)]
pub struct CLoginDisconnect<'a> { pub struct CLoginDisconnect<'a> {
reason: &'a str, 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> { impl<'a> ClientPacket for CLoginDisconnect<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_string(&serde_json::to_string_pretty(&self.reason).unwrap()); bytebuf.put_string(&serde_json::to_string_pretty(&self.reason).unwrap());

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, Property, VarInt, ClientPacket, Property, VarInt,
}; };
#[packet(0x02)]
pub struct CLoginSuccess<'a> { pub struct CLoginSuccess<'a> {
pub uuid: uuid::Uuid, pub uuid: uuid::Uuid,
pub username: String, // 16 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> { impl<'a> ClientPacket for CLoginSuccess<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_uuid(self.uuid); bytebuf.put_uuid(self.uuid);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x03)]
pub struct CSetCompression { pub struct CSetCompression {
threshold: VarInt, threshold: VarInt,
} }
@ -13,10 +16,6 @@ impl CSetCompression {
} }
} }
impl Packet for CSetCompression {
const PACKET_ID: VarInt = 0x03;
}
impl ClientPacket for CSetCompression { impl ClientPacket for CSetCompression {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_var_int(self.threshold); bytebuf.put_var_int(self.threshold);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x0B)]
pub struct CChangeDifficulty { pub struct CChangeDifficulty {
difficulty: u8, difficulty: u8,
locked: bool, locked: bool,
@ -14,10 +17,6 @@ impl CChangeDifficulty {
} }
} }
impl Packet for CChangeDifficulty {
const PACKET_ID: VarInt = 0x0B;
}
impl ClientPacket for CChangeDifficulty { impl ClientPacket for CChangeDifficulty {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_u8(self.difficulty); bytebuf.put_u8(self.difficulty);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
BitSet, ClientPacket, VarInt, BitSet, ClientPacket, VarInt,
}; };
#[packet(0x27)]
pub struct CChunkDataUpdateLight { pub struct CChunkDataUpdateLight {
chunk_x: i32, chunk_x: i32,
chunk_y: i32, chunk_y: i32,
@ -45,10 +48,6 @@ impl CChunkDataUpdateLight {
} }
} }
impl Packet for CChunkDataUpdateLight {
const PACKET_ID: VarInt = 0x27;
}
impl ClientPacket for CChunkDataUpdateLight { impl ClientPacket for CChunkDataUpdateLight {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_i32(self.chunk_x); bytebuf.put_i32(self.chunk_x);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x22)]
pub struct CGameEvent { pub struct CGameEvent {
event: u8, event: u8,
value: f32, value: f32,
@ -14,10 +17,6 @@ impl CGameEvent {
} }
} }
impl Packet for CGameEvent {
const PACKET_ID: VarInt = 0x22;
}
impl ClientPacket for CGameEvent { impl ClientPacket for CGameEvent {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_u8(self.event); bytebuf.put_u8(self.event);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x2B)]
pub struct CLogin { pub struct CLogin {
entity_id: i32, entity_id: i32,
is_hardcore: bool, is_hardcore: bool,
@ -78,10 +81,6 @@ impl CLogin {
} }
} }
impl Packet for CLogin {
const PACKET_ID: VarInt = 0x2B;
}
impl ClientPacket for CLogin { impl ClientPacket for CLogin {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_i32(self.entity_id); bytebuf.put_i32(self.entity_id);

View File

@ -1,23 +1,22 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
text::Text, text::TextComponent,
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x1D)]
pub struct CPlayDisconnect { pub struct CPlayDisconnect {
reason: Text, reason: TextComponent,
} }
impl CPlayDisconnect { impl CPlayDisconnect {
pub fn new(reason: Text) -> Self { pub fn new(reason: TextComponent) -> Self {
Self { reason } Self { reason }
} }
} }
impl Packet for CPlayDisconnect {
const PACKET_ID: VarInt = 0x1D;
}
impl ClientPacket for CPlayDisconnect { impl ClientPacket for CPlayDisconnect {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_slice(&self.reason.encode()); bytebuf.put_slice(&self.reason.encode());

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x38)]
pub struct CPlayerAbilities { pub struct CPlayerAbilities {
flags: i8, flags: i8,
flying_speed: f32, flying_speed: f32,
@ -19,10 +22,6 @@ impl CPlayerAbilities {
} }
} }
impl Packet for CPlayerAbilities {
const PACKET_ID: VarInt = 0x38;
}
impl ClientPacket for CPlayerAbilities { impl ClientPacket for CPlayerAbilities {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_i8(self.flags); bytebuf.put_i8(self.flags);

View File

@ -1,3 +1,5 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, Property, VarInt, ClientPacket, Property, VarInt,
@ -5,15 +7,12 @@ use crate::{
use super::PlayerAction; use super::PlayerAction;
#[packet(0x3E)]
pub struct CPlayerInfoUpdate<'a> { pub struct CPlayerInfoUpdate<'a> {
pub actions: i8, pub actions: i8,
pub players: &'a [Player<'a>], pub players: &'a [Player<'a>],
} }
impl<'a> Packet for CPlayerInfoUpdate<'a> {
const PACKET_ID: VarInt = 0x3E;
}
pub struct Player<'a> { pub struct Player<'a> {
pub uuid: uuid::Uuid, pub uuid: uuid::Uuid,
pub actions: &'a [PlayerAction], pub actions: &'a [PlayerAction],

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x53)]
pub struct CSetHeldItem { pub struct CSetHeldItem {
slot: i8, slot: i8,
} }
@ -13,10 +16,6 @@ impl CSetHeldItem {
} }
} }
impl Packet for CSetHeldItem {
const PACKET_ID: VarInt = 0x53;
}
impl ClientPacket for CSetHeldItem { impl ClientPacket for CSetHeldItem {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_i8(self.slot); bytebuf.put_i8(self.slot);

View File

@ -1,8 +1,11 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[packet(0x40)]
pub struct CSyncPlayerPostion { pub struct CSyncPlayerPostion {
x: f64, x: f64,
y: f64, y: f64,
@ -35,10 +38,6 @@ impl CSyncPlayerPostion {
} }
} }
impl Packet for CSyncPlayerPostion {
const PACKET_ID: VarInt = 0x40;
}
impl ClientPacket for CSyncPlayerPostion { impl ClientPacket for CSyncPlayerPostion {
fn write(&self, bytebuf: &mut ByteBuffer) { fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_f64(self.x); bytebuf.put_f64(self.x);

View File

@ -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);
}
}

View File

@ -7,6 +7,7 @@ mod c_player_abilities;
mod c_player_info_update; mod c_player_info_update;
mod c_set_held_item; mod c_set_held_item;
mod c_sync_player_position; mod c_sync_player_position;
mod c_system_chat_message;
mod player_action; mod player_action;
pub use c_change_difficulty::*; pub use c_change_difficulty::*;
@ -18,4 +19,5 @@ pub use c_player_abilities::*;
pub use c_player_info_update::*; pub use c_player_info_update::*;
pub use c_set_held_item::*; pub use c_set_held_item::*;
pub use c_sync_player_position::*; pub use c_sync_player_position::*;
pub use c_system_chat_message::*;
pub use player_action::*; pub use player_action::*;

View File

@ -1,14 +1,13 @@
use pumpkin_macros::packet;
use crate::{bytebuf::packet_id::Packet, VarInt}; use crate::{bytebuf::packet_id::Packet, VarInt};
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
#[packet(0x01)]
pub struct CPingResponse { pub struct CPingResponse {
payload: i64, // must responde with the same as in `SPingRequest` payload: i64, // must responde with the same as in `SPingRequest`
} }
impl Packet for CPingResponse {
const PACKET_ID: VarInt = 0x01;
}
impl CPingResponse { impl CPingResponse {
pub fn new(payload: i64) -> Self { pub fn new(payload: i64) -> Self {
Self { payload } Self { payload }

View File

@ -1,8 +1,12 @@
use pumpkin_macros::packet;
use crate::{ use crate::{
bytebuf::{packet_id::Packet, ByteBuffer}, bytebuf::{packet_id::Packet, ByteBuffer},
ClientPacket, VarInt, ClientPacket, VarInt,
}; };
#[derive(serde::Serialize)]
#[packet(0x00)]
pub struct CStatusResponse<'a> { pub struct CStatusResponse<'a> {
json_response: &'a str, // 32767 json_response: &'a str, // 32767
} }
@ -12,12 +16,4 @@ impl<'a> CStatusResponse<'a> {
Self { json_response } 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);
}
}

View File

@ -1,18 +1,33 @@
use core::str; use core::str;
use fastnbt::SerOpts; use fastnbt::SerOpts;
use serde::{Deserialize, Serialize}; use serde::Deserialize;
// represents a text component // represents a text component
// Reference: https://wiki.vg/Text_formatting#Text_components // 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 struct TextComponent {
pub text: String, pub text: String,
} }
impl serde::Serialize for TextComponent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(&self.encode())
}
}
impl TextComponent { impl TextComponent {
pub fn encode(&self) -> Vec<u8> { pub fn encode(&self) -> Vec<u8> {
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()
} }
} }