mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 23:44:38 +00:00
* 23w40a * 23w41a * 23w42a * 23w43a * 23w44a * serialize FormattedText as nbt in network * use azalea-nbt/serde in azalea-chat * 23w45a * fix 23w45a to compile * handle Object in codegen * 1.20.3-pre2 * remove unused clientbound_resource_pack_packet.rs * merge main and make azalea-chat use simdnbt * 1.20.3-rc1 * fix tests * use simdnbt 0.3 * fix ServerboundSetJigsawBlockPacket * 1.20.3
64 lines
1.7 KiB
Rust
Executable file
64 lines
1.7 KiB
Rust
Executable file
pub mod common;
|
|
pub mod configuration;
|
|
pub mod game;
|
|
pub mod handshaking;
|
|
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: i32 = 765;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
pub enum ConnectionProtocol {
|
|
Handshake = -1,
|
|
Game = 0,
|
|
Status = 1,
|
|
Login = 2,
|
|
Configuration = 3,
|
|
}
|
|
|
|
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),
|
|
3 => Some(ConnectionProtocol::Configuration),
|
|
_ => 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)
|
|
}
|
|
}
|