basalt_mc_protocol/version.rs
1/// Supported Minecraft protocol versions.
2///
3/// Each variant corresponds to a specific Minecraft release and its
4/// associated protocol version number. The protocol version is sent
5/// in the Handshake packet and determines which packet definitions
6/// and ID mappings to use.
7///
8/// Currently only 1.21.x is supported. Additional versions will be
9/// added as the multi-version delta/overlay model is implemented.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum ProtocolVersion {
12 /// Minecraft 1.21 (protocol version 767).
13 V1_21,
14}
15
16impl ProtocolVersion {
17 /// Returns the numeric protocol version sent in the Handshake packet.
18 ///
19 /// This integer identifies the protocol version to the server and is
20 /// used to select the correct packet registry. The client sends this
21 /// value during the handshake; the server uses it to determine which
22 /// packet definitions apply.
23 pub fn protocol_number(&self) -> i32 {
24 match self {
25 Self::V1_21 => 767,
26 }
27 }
28
29 /// Attempts to resolve a protocol version from its numeric identifier.
30 ///
31 /// Returns `None` if the protocol number doesn't match any supported
32 /// version. This is used during handshake to determine if the server
33 /// supports the client's protocol version.
34 pub fn from_protocol_number(number: i32) -> Option<Self> {
35 match number {
36 767 => Some(Self::V1_21),
37 _ => None,
38 }
39 }
40}
41
42impl std::fmt::Display for ProtocolVersion {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 match self {
45 Self::V1_21 => f.write_str("1.21"),
46 }
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn protocol_number_v1_21() {
56 assert_eq!(ProtocolVersion::V1_21.protocol_number(), 767);
57 }
58
59 #[test]
60 fn from_protocol_number_valid() {
61 assert_eq!(
62 ProtocolVersion::from_protocol_number(767),
63 Some(ProtocolVersion::V1_21)
64 );
65 }
66
67 #[test]
68 fn from_protocol_number_unknown() {
69 assert_eq!(ProtocolVersion::from_protocol_number(999), None);
70 }
71
72 #[test]
73 fn display() {
74 assert_eq!(ProtocolVersion::V1_21.to_string(), "1.21");
75 }
76}