Skip to main content

azalea_protocol/packets/
mod.rs

1pub mod common;
2pub mod config;
3pub mod game;
4pub mod handshake;
5pub mod login;
6pub mod status;
7
8use std::io::{self, Cursor, Write};
9
10use azalea_buf::{AzBuf, AzBufVar, BufReadError, BufReadErrorRepr};
11
12use crate::read::ReadPacketError;
13
14pub const PROTOCOL_VERSION: i32 = 777;
15pub const VERSION_NAME: &str = "26.3";
16
17#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
18pub enum ConnectionProtocol {
19    Handshake = -1,
20    Game = 0,
21    Status = 1,
22    Login = 2,
23    Configuration = 3,
24}
25
26impl ConnectionProtocol {
27    #[must_use]
28    pub fn from_i32(i: i32) -> Option<Self> {
29        match i {
30            -1 => Some(ConnectionProtocol::Handshake),
31            0 => Some(ConnectionProtocol::Game),
32            1 => Some(ConnectionProtocol::Status),
33            2 => Some(ConnectionProtocol::Login),
34            3 => Some(ConnectionProtocol::Configuration),
35            _ => None,
36        }
37    }
38}
39
40/// An enum of packets for a certain protocol.
41pub trait ProtocolPacket
42where
43    Self: Sized,
44{
45    fn id(&self) -> u32;
46
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
53    /// Read a packet by its ID, `ConnectionProtocol`, and flow.
54    fn read(
55        id: u32,
56        ctx: azalea_buf::Ctx,
57        buf: &mut Cursor<&[u8]>,
58    ) -> Result<Self, Box<ReadPacketError>>;
59
60    fn write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()>;
61}
62
63pub trait Packet<Protocol> {
64    fn into_variant(self) -> Protocol;
65}
66
67#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
68pub enum ClientIntention {
69    Status = 1,
70    Login = 2,
71    Transfer = 3,
72}
73
74impl TryFrom<i32> for ClientIntention {
75    type Error = ();
76
77    fn try_from(value: i32) -> Result<Self, Self::Error> {
78        match value {
79            1 => Ok(ClientIntention::Status),
80            2 => Ok(ClientIntention::Login),
81            3 => Ok(ClientIntention::Transfer),
82            _ => Err(()),
83        }
84    }
85}
86
87impl From<ClientIntention> for ConnectionProtocol {
88    fn from(intention: ClientIntention) -> Self {
89        match intention {
90            ClientIntention::Status => ConnectionProtocol::Status,
91            ClientIntention::Login | ClientIntention::Transfer => ConnectionProtocol::Login,
92        }
93    }
94}
95
96impl AzBuf for ClientIntention {
97    fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
98        let id = i32::azalea_read_var(ctx, buf)?;
99        id.try_into()
100            .map_err(|_| BufReadErrorRepr::UnexpectedEnumVariant { id }.into())
101    }
102    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
103        (*self as i32).azalea_write_var(ctx, buf)
104    }
105}