Skip to content

Commit b6ddde9

Browse files
committed
add ProtocolPacket::name function
1 parent f2d8d42 commit b6ddde9

File tree

2 files changed

+34
-2
lines changed
  • azalea-protocol

2 files changed

+34
-2
lines changed

azalea-protocol/azalea-protocol-macros/src/lib.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
196196
let mut serverbound_enum_contents = quote!();
197197
let mut clientbound_id_match_contents = quote!();
198198
let mut serverbound_id_match_contents = quote!();
199+
let mut clientbound_name_match_contents = quote!();
200+
let mut serverbound_name_match_contents = quote!();
199201
let mut clientbound_write_match_contents = quote!();
200202
let mut serverbound_write_match_contents = quote!();
201203
let mut clientbound_read_match_contents = quote!();
@@ -218,7 +220,10 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
218220
#variant_name(#module_name::#struct_name),
219221
});
220222
clientbound_id_match_contents.extend(quote! {
221-
#clientbound_state_name::#variant_name(_packet) => #id,
223+
#clientbound_state_name::#variant_name(..) => #id,
224+
});
225+
clientbound_name_match_contents.extend(quote! {
226+
#clientbound_state_name::#variant_name(..) => #packet_name_litstr,
222227
});
223228
clientbound_write_match_contents.extend(quote! {
224229
#clientbound_state_name::#variant_name(packet) => packet.write(buf),
@@ -267,7 +272,10 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
267272
#variant_name(#module_name::#struct_name),
268273
});
269274
serverbound_id_match_contents.extend(quote! {
270-
#serverbound_state_name::#variant_name(_packet) => #id,
275+
#serverbound_state_name::#variant_name(..) => #id,
276+
});
277+
serverbound_name_match_contents.extend(quote! {
278+
#serverbound_state_name::#variant_name(..) => #packet_name_litstr,
271279
});
272280
serverbound_write_match_contents.extend(quote! {
273281
#serverbound_state_name::#variant_name(packet) => packet.write(buf),
@@ -297,6 +305,9 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
297305
serverbound_id_match_contents.extend(quote! {
298306
_ => unreachable!("This enum is empty and can't exist.")
299307
});
308+
serverbound_name_match_contents.extend(quote! {
309+
_ => unreachable!("This enum is empty and can't exist.")
310+
});
300311
serverbound_write_match_contents.extend(quote! {
301312
_ => unreachable!("This enum is empty and can't exist.")
302313
});
@@ -305,6 +316,9 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
305316
clientbound_id_match_contents.extend(quote! {
306317
_ => unreachable!("This enum is empty and can't exist.")
307318
});
319+
clientbound_name_match_contents.extend(quote! {
320+
_ => unreachable!("This enum is empty and can't exist.")
321+
});
308322
clientbound_write_match_contents.extend(quote! {
309323
_ => unreachable!("This enum is empty and can't exist.")
310324
});
@@ -338,6 +352,12 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
338352
}
339353
}
340354

355+
fn name(&self) -> &'static str {
356+
match self {
357+
#serverbound_name_match_contents
358+
}
359+
}
360+
341361
fn write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
342362
match self {
343363
#serverbound_write_match_contents
@@ -376,6 +396,12 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
376396
}
377397
}
378398

399+
fn name(&self) -> &'static str {
400+
match self {
401+
#clientbound_name_match_contents
402+
}
403+
}
404+
379405
fn write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
380406
match self {
381407
#clientbound_write_match_contents

azalea-protocol/src/packets/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ where
4444
{
4545
fn id(&self) -> u32;
4646

47+
/// Returns Mojang's resource name for the packet.
48+
///
49+
/// This doesn't include the "minecraft:" prefix, it just returns a string
50+
/// like `pong`.
51+
fn name(&self) -> &'static str;
52+
4753
/// Read a packet by its id, `ConnectionProtocol`, and flow
4854
fn read(id: u32, buf: &mut Cursor<&[u8]>) -> Result<Self, Box<ReadPacketError>>;
4955

0 commit comments

Comments
 (0)