kojacoord_protocol/versions/v1_20_4/status/
mod.rs1use bytes::{Bytes, BytesMut};
2
3use crate::codec::{Decode, Encode, PacketId};
4use crate::error::ProtocolError;
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct ServerboundStatusRequest;
8
9impl PacketId for ServerboundStatusRequest {
10 fn packet_id(_ver: u32) -> u8 {
11 0x00
12 }
13}
14
15impl Encode for ServerboundStatusRequest {
16 fn encode(&self, _dst: &mut BytesMut) -> Result<(), ProtocolError> {
17 Ok(())
18 }
19}
20
21impl Decode for ServerboundStatusRequest {
22 fn decode(_src: &mut Bytes) -> Result<Self, ProtocolError> {
23 Ok(Self)
24 }
25}
26
27#[derive(Debug, Clone, PartialEq)]
28pub struct ClientboundStatusResponse {
29 pub json_response: String,
30}
31
32impl PacketId for ClientboundStatusResponse {
33 fn packet_id(_ver: u32) -> u8 {
34 0x00
35 }
36}
37
38impl Encode for ClientboundStatusResponse {
39 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
40 self.json_response.encode(dst)
41 }
42}
43
44impl Decode for ClientboundStatusResponse {
45 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
46 Ok(Self {
47 json_response: String::decode(src)?,
48 })
49 }
50}
51
52#[derive(Debug, Clone, PartialEq)]
53pub struct ServerboundPingRequest {
54 pub payload: i64,
55}
56
57impl PacketId for ServerboundPingRequest {
58 fn packet_id(_ver: u32) -> u8 {
59 0x01
60 }
61}
62
63impl Encode for ServerboundPingRequest {
64 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
65 self.payload.encode(dst)
66 }
67}
68
69impl Decode for ServerboundPingRequest {
70 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
71 Ok(Self {
72 payload: i64::decode(src)?,
73 })
74 }
75}
76
77#[derive(Debug, Clone, PartialEq)]
78pub struct ClientboundPongResponse {
79 pub payload: i64,
80}
81
82impl PacketId for ClientboundPongResponse {
83 fn packet_id(_ver: u32) -> u8 {
84 0x01
85 }
86}
87
88impl Encode for ClientboundPongResponse {
89 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
90 self.payload.encode(dst)
91 }
92}
93
94impl Decode for ClientboundPongResponse {
95 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
96 Ok(Self {
97 payload: i64::decode(src)?,
98 })
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn response_roundtrip() {
108 let p = ClientboundStatusResponse {
109 json_response: r#"{"version":{"name":"1.20.4"}}"#.to_string(),
110 };
111 let mut buf = BytesMut::new();
112 p.encode(&mut buf).unwrap();
113 let mut b = buf.freeze();
114 assert_eq!(ClientboundStatusResponse::decode(&mut b).unwrap(), p);
115 }
116
117 #[test]
118 fn ping_roundtrip() {
119 let p = ServerboundPingRequest { payload: 9999 };
120 let mut buf = BytesMut::new();
121 p.encode(&mut buf).unwrap();
122 let mut b = buf.freeze();
123 assert_eq!(ServerboundPingRequest::decode(&mut b).unwrap(), p);
124 }
125}