minecraft_net/
lib.rs

1pub mod fields;
2mod errors;
3mod net;
4pub mod packets;
5
6use std::io::{Read, Write};
7use crate::errors::Result;
8pub use crate::errors::Errors;
9pub use crate::fields::PacketReader;
10pub use crate::net::{send_packet, receive_packet, receive_unknown_packet, UnknownPacket, EncryptedTcp};
11
12pub trait Packet: Sized {
13    const ID: i32;
14    /// Serializes the packet to a Vec\<u8> in accordance with the minecraft protocol so that it can be sent via the network.
15    ///
16    /// # Arguments
17    /// returns: Vec\<u8>
18    fn to_bytes(&self) -> Vec<u8>;
19    /// Allows the reading of packets from a PacketReader.
20    ///
21    /// # Arguments
22    ///
23    /// * `reader`: The reader the packet will be read from.
24    ///
25    /// returns: Result<Self, Errors>
26    fn from_reader(reader: &mut PacketReader) -> Result<Self>;
27}
28pub trait Field: Sized + Clone {
29    /// Serializes the field into an array of bytes in accordance with the minecraft protocol.
30    ///
31    /// # Arguments
32    /// returns: Vec\<u8>
33    fn to_bytes(&self) -> Vec<u8>;
34    /// Allows a field to be read from a PacketReader, which is a mostly internal struct.
35    ///
36    /// # Arguments
37    ///
38    /// * `reader`: The reader which field packets will be read from
39    ///
40    /// returns: Result<Self, Errors>
41    fn from_reader(reader: &mut PacketReader) -> Result<Self>;
42}
43impl<T: Field> Field for Box<T> {
44    fn to_bytes(&self) -> Vec<u8> {
45        Field::to_bytes(self.as_ref())
46    }
47
48    fn from_reader(reader: &mut PacketReader) -> Result<Self> {
49        Ok(Self::new(reader.read::<T>()?))
50    }
51}
52pub trait Stream: Read + Write {}
53impl<T> Stream for T where T: Read + Write {}