1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00
azalea/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs
mat 567c6f4f2c Reduce usage of AsyncRead
We already receive everything from the server when it tells us the length, so we can actually just treat the stream as a Read instead of an AsyncRead.
2022-05-01 21:54:03 -05:00

59 lines
1.4 KiB
Rust
Executable file

use std::io::Read;
use azalea_chat::component::Component;
use serde::Deserialize;
use serde_json::Value;
use crate::mc_buf::Readable;
use super::StatusPacket;
#[derive(Clone, Debug, Deserialize)]
pub struct Version {
pub name: Component,
pub protocol: u32,
}
#[derive(Clone, Debug, Deserialize)]
pub struct SamplePlayer {
pub id: String,
pub name: String,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Players {
pub max: u32,
pub online: u32,
pub sample: Vec<SamplePlayer>,
}
// the entire packet is just json, which is why it has deserialize
#[derive(Clone, Debug, Deserialize)]
pub struct ClientboundStatusResponsePacket {
pub description: Component,
pub favicon: Option<String>,
pub players: Players,
pub version: Version,
}
impl ClientboundStatusResponsePacket {
pub fn get(self) -> StatusPacket {
StatusPacket::ClientboundStatusResponsePacket(self)
}
pub fn write(&self, _buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
Ok(())
}
pub fn read(buf: &mut impl Read) -> Result<StatusPacket, String> {
let status_string = buf.read_utf()?;
let status_json: Value =
serde_json::from_str(status_string.as_str()).expect("Server status isn't valid JSON");
let packet = ClientboundStatusResponsePacket::deserialize(status_json)
.map_err(|e| e.to_string())?
.get();
Ok(packet)
}
}