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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
///! Connect to Minecraft servers.
use azalea_protocol::{
connect::HandshakeConnection,
packets::{
game::GamePacket,
handshake::client_intention_packet::ClientIntentionPacket,
login::{serverbound_hello_packet::ServerboundHelloPacket, LoginPacket},
ConnectionProtocol, PROTOCOL_VERSION,
},
resolver, ServerAddress,
};
pub async fn join_server(address: &ServerAddress) -> Result<(), String> {
let username = "bot".to_string();
let resolved_address = resolver::resolve_address(address).await?;
let mut conn = HandshakeConnection::new(&resolved_address).await?;
// handshake
conn.write(
ClientIntentionPacket {
protocol_version: PROTOCOL_VERSION,
hostname: address.host.clone(),
port: address.port,
intention: ConnectionProtocol::Login,
}
.get(),
)
.await;
let mut conn = conn.login();
// login
conn.write(ServerboundHelloPacket { username }.get()).await;
let mut conn = loop {
let packet_result = conn.read().await;
match packet_result {
Ok(packet) => match packet {
LoginPacket::ClientboundHelloPacket(p) => {
println!("Got encryption request {:?} {:?}", p.nonce, p.public_key);
}
LoginPacket::ClientboundLoginCompressionPacket(p) => {
println!("Got compression request {:?}", p.compression_threshold);
conn.set_compression_threshold(p.compression_threshold);
}
LoginPacket::ClientboundGameProfilePacket(p) => {
println!("Got profile {:?}", p.game_profile);
break conn.game();
}
_ => panic!("unhandled packet"),
},
Err(e) => {
println!("Error: {:?}", e);
}
}
};
// game
loop {
let packet_result = conn.read().await;
match packet_result {
Ok(packet) => match packet {
GamePacket::ClientboundLoginPacket(p) => {
println!("Got login packet {:?}", p);
}
GamePacket::ClientboundUpdateViewDistancePacket(p) => {
println!("Got view distance packet {:?}", p);
}
GamePacket::ClientboundCustomPayloadPacket(p) => {
println!("Got custom payload packet {:?}", p);
}
GamePacket::ClientboundChangeDifficultyPacket(p) => {
println!("Got difficulty packet {:?}", p);
}
},
Err(e) => {
println!("Error: {:?}", e);
}
}
}
Ok(())
}
|