aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bot/src/main.rs3
-rw-r--r--minecraft-chat/src/component.rs2
-rw-r--r--minecraft-protocol/src/connection.rs5
-rw-r--r--minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs1
-rw-r--r--minecraft-protocol/src/server_status_pinger.rs49
5 files changed, 17 insertions, 43 deletions
diff --git a/bot/src/main.rs b/bot/src/main.rs
index 47af37b4..ec2f5954 100644
--- a/bot/src/main.rs
+++ b/bot/src/main.rs
@@ -3,9 +3,10 @@ use tokio::runtime::Runtime;
async fn bot() {
let address = ServerAddress::parse(&"mc.hypixel.net".to_string()).unwrap();
- minecraft_protocol::server_status_pinger::ping_server(&address)
+ let response = minecraft_protocol::server_status_pinger::ping_server(&address)
.await
.unwrap();
+ println!("{}", response.description.to_ansi(None));
}
fn main() {
diff --git a/minecraft-chat/src/component.rs b/minecraft-chat/src/component.rs
index 2ff6111a..22e803bd 100644
--- a/minecraft-chat/src/component.rs
+++ b/minecraft-chat/src/component.rs
@@ -111,9 +111,7 @@ impl<'de> Deserialize<'de> for Component {
where
D: Deserializer<'de>,
{
- println!("deserializing component");
let json: serde_json::Value = serde::Deserialize::deserialize(de)?;
- println!("made json");
// we create a component that we might add siblings to
let mut component: Component;
diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs
index 2fe03dfb..5b750802 100644
--- a/minecraft-protocol/src/connection.rs
+++ b/minecraft-protocol/src/connection.rs
@@ -50,13 +50,14 @@ impl Connection {
// 1. reads the first 5 bytes, probably only some of this will be used to get the packet length
// 2. how much we should read = packet length - 5
// 3. read the rest of the packet and add it to the cursor
+ // 4. figure out what packet this is and parse it
// the first thing minecraft sends us is the length as a varint, which can be up to 5 bytes long
let mut buf = BufReader::with_capacity(4 * 1024 * 1024, &mut self.stream);
- println!("reading length varint");
+
let (_packet_size, _packet_size_varint_size) = mc_buf::read_varint(&mut buf).await?;
+
// then, minecraft tells us the packet id as a varint
- println!("reading id varint");
let (packet_id, _packet_id_size) = mc_buf::read_varint(&mut buf).await?;
// if we recognize the packet id, parse it
diff --git a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs
index 0868a062..99bef586 100644
--- a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs
+++ b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs
@@ -28,6 +28,7 @@ pub struct Players {
pub sample: Vec<SamplePlayer>,
}
+// the entire packet is just json, which is why it has deserialize
#[derive(Clone, Debug, Deserialize)]
pub struct ClientboundStatusResponsePacket {
pub version: Version,
diff --git a/minecraft-protocol/src/server_status_pinger.rs b/minecraft-protocol/src/server_status_pinger.rs
index ae41ed51..0c1f2076 100644
--- a/minecraft-protocol/src/server_status_pinger.rs
+++ b/minecraft-protocol/src/server_status_pinger.rs
@@ -2,20 +2,22 @@ use crate::{
connection::Connection,
packets::{
handshake::client_intention_packet::ClientIntentionPacket,
- status::serverbound_status_request_packet::ServerboundStatusRequestPacket,
+ status::{
+ clientbound_status_response_packet::ClientboundStatusResponsePacket,
+ serverbound_status_request_packet::ServerboundStatusRequestPacket,
+ },
ConnectionProtocol, Packet, PacketTrait,
},
resolver, ServerAddress,
};
-pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
+pub async fn ping_server(
+ address: &ServerAddress,
+) -> Result<ClientboundStatusResponsePacket, String> {
let resolved_address = resolver::resolve_address(address).await?;
let mut conn = Connection::new(&resolved_address).await?;
- println!("resolved_address {}", &resolved_address.ip);
- println!("writing intention packet {}", address.host);
-
// send the client intention packet and switch to the status state
conn.send_packet(
ClientIntentionPacket {
@@ -35,37 +37,8 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
let packet = conn.read_packet().await.unwrap();
- match packet {
- Packet::ClientboundStatusResponsePacket(p) => {
- println!("{:?}", p);
- println!("{}", p.description.to_ansi(None));
- }
- _ => {
- println!("unexpected packet {:?}", packet);
- }
- }
-
- Ok(())
-
- // let data = mc_buf::read_varint(conn.stream);
- // println!("data {}", data);
-
- // // log what the server sends back
- // loop {
- // if 0 == conn.stream.read_buf(&mut conn.buffer).await.unwrap() {
- // // The remote closed the connection. For this to be a clean
- // // shutdown, there should be no data in the read buffer. If
- // // there is, this means that the peer closed the socket while
- // // sending a frame.
-
- // // log conn.buffer
- // println!("{:?}", conn.buffer);
- // if conn.buffer.is_empty() {
- // println!("buffer is empty ok");
- // return Ok(());
- // } else {
- // return Err("connection reset by peer".into());
- // }
- // }
- // }
+ Ok(match packet {
+ Packet::ClientboundStatusResponsePacket(p) => p,
+ _ => Err("Invalid packet type".to_string())?,
+ })
}