aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs
blob: a88c6cbf21a2bfcd727c89a151f5120f12f019da (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
use std::hash::Hash;

use crate::mc_buf::{Readable, Writable};

use super::LoginPacket;

#[derive(Hash, Clone, Debug)]
pub struct ClientboundLoginCompressionPacket {
    pub compression_threshold: i32,
}

impl ClientboundLoginCompressionPacket {
    pub fn get(self) -> LoginPacket {
        LoginPacket::ClientboundLoginCompressionPacket(self)
    }

    pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
        buf.write_varint(self.compression_threshold).unwrap();
        Ok(())
    }

    pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
        buf: &mut T,
    ) -> Result<LoginPacket, String> {
        let compression_threshold = buf.read_varint().await?;

        Ok(ClientboundLoginCompressionPacket {
            compression_threshold,
        }
        .get())
    }
}