aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/mod.rs
blob: e803e85fe17b542ef9a492e93f4ea7a64f13867c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

use crate::read::ReadPacketError;
use azalea_buf::{BufReadError, McBufVarReadable, McBufVarWritable, McBufWritable};
use std::io::{Cursor, Write};

// TODO: rename the packet files to just like clientbound_add_entity instead of
// clientbound_add_entity_packet

pub const PROTOCOL_VERSION: u32 = 761;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ConnectionProtocol {
    Handshake = -1,
    Game = 0,
    Status = 1,
    Login = 2,
}

impl ConnectionProtocol {
    #[must_use]
    pub fn from_i32(i: i32) -> Option<Self> {
        match i {
            -1 => Some(ConnectionProtocol::Handshake),
            0 => Some(ConnectionProtocol::Game),
            1 => Some(ConnectionProtocol::Status),
            2 => Some(ConnectionProtocol::Login),
            _ => None,
        }
    }
}

/// An enum of packets for a certain protocol
pub trait ProtocolPacket
where
    Self: Sized,
{
    fn id(&self) -> u32;

    /// Read a packet by its id, `ConnectionProtocol`, and flow
    fn read(id: u32, buf: &mut Cursor<&[u8]>) -> Result<Self, Box<ReadPacketError>>;

    fn write(&self, buf: &mut impl Write) -> Result<(), std::io::Error>;
}

impl azalea_buf::McBufReadable for ConnectionProtocol {
    fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
        let id = i32::var_read_from(buf)?;
        ConnectionProtocol::from_i32(id).ok_or(BufReadError::UnexpectedEnumVariant { id })
    }
}

impl McBufWritable for ConnectionProtocol {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        (*self as i32).var_write_into(buf)
    }
}