diff options
| -rw-r--r-- | Cargo.lock | 4 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | minecraft-chat/Cargo.toml | 8 | ||||
| -rw-r--r-- | minecraft-chat/src/lib.rs | 7 | ||||
| -rw-r--r-- | minecraft-protocol/src/connection.rs | 8 | ||||
| -rw-r--r-- | minecraft-protocol/src/lib.rs | 2 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/client_intention_packet.rs | 2 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/clientbound_status_response_packet.rs | 21 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/mod.rs | 4 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/serverbound_status_request_packet.rs | 1 | ||||
| -rw-r--r-- | minecraft-protocol/src/server_status.rs | 6 | ||||
| -rw-r--r-- | minecraft-protocol/src/server_status_pinger.rs | 4 |
12 files changed, 65 insertions, 3 deletions
@@ -279,6 +279,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] +name = "minecraft-chat" +version = "0.1.0" + +[[package]] name = "minecraft-client" version = "0.1.0" dependencies = [ @@ -4,4 +4,5 @@ members = [ "bot", "minecraft-client", "minecraft-protocol", + "minecraft-chat", ]
\ No newline at end of file diff --git a/minecraft-chat/Cargo.toml b/minecraft-chat/Cargo.toml new file mode 100644 index 00000000..4607dc99 --- /dev/null +++ b/minecraft-chat/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "minecraft-chat" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/minecraft-chat/src/lib.rs b/minecraft-chat/src/lib.rs new file mode 100644 index 00000000..31e1bb20 --- /dev/null +++ b/minecraft-chat/src/lib.rs @@ -0,0 +1,7 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs index ee03b5e5..a162bb6b 100644 --- a/minecraft-protocol/src/connection.rs +++ b/minecraft-protocol/src/connection.rs @@ -1,4 +1,4 @@ -//! Handle sending and receiving packets with a server. +//! parse sending and receiving packets with a server. use crate::packets::ConnectionProtocol; use crate::{mc_buf, packets::Packet, ServerIpAddress}; @@ -41,6 +41,10 @@ impl Connection { }) } + pub fn switch_state(&mut self, state: ConnectionProtocol) { + self.state = state; + } + pub async fn read_packet(&mut self) -> Result<(), String> { // what this does: // 1. reads the first 5 bytes, probably only some of this will be used to get the packet length @@ -68,7 +72,7 @@ impl Connection { } /// Write a packet to the server - pub async fn send_packet(&mut self, packet: &dyn Packet) { + pub async fn send_packet(&mut self, packet: &impl Packet) { // TODO: implement compression // packet structure: diff --git a/minecraft-protocol/src/lib.rs b/minecraft-protocol/src/lib.rs index 0e2b1c45..88b3603f 100644 --- a/minecraft-protocol/src/lib.rs +++ b/minecraft-protocol/src/lib.rs @@ -1,3 +1,5 @@ +//! This lib is responsible for parsing Minecraft packets. + use std::net::IpAddr; use std::str::FromStr; diff --git a/minecraft-protocol/src/packets/client_intention_packet.rs b/minecraft-protocol/src/packets/client_intention_packet.rs index 90574038..a35e65dc 100644 --- a/minecraft-protocol/src/packets/client_intention_packet.rs +++ b/minecraft-protocol/src/packets/client_intention_packet.rs @@ -26,4 +26,6 @@ impl<'a> Packet for ClientIntentionPacket<'a> { mc_buf::write_short(buf, self.port); mc_buf::write_varint(buf, self.intention.clone() as u32); } + + fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {} } diff --git a/minecraft-protocol/src/packets/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/clientbound_status_response_packet.rs new file mode 100644 index 00000000..8baa2a4d --- /dev/null +++ b/minecraft-protocol/src/packets/clientbound_status_response_packet.rs @@ -0,0 +1,21 @@ +use std::hash::Hash; + +use super::Packet; + +#[derive(Hash)] +pub struct ServerboundStatusRequestPacket { + status: ServerStatus, +} + +// implement "Packet" for "ClientIntentionPacket" +impl Packet for ServerboundStatusRequestPacket { + fn get_id(&self) -> u32 { + 0x00 + } + + // implement "from_reader" for "ClientIntentionPacket" + fn write(&self, _buf: &mut Vec<u8>) {} + fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () { + // this.status = GsonHelper.fromJson(GSON, friendlyByteBuf.readUtf(32767), ServerStatus.class); + } +} diff --git a/minecraft-protocol/src/packets/mod.rs b/minecraft-protocol/src/packets/mod.rs index 6a053124..8d943be0 100644 --- a/minecraft-protocol/src/packets/mod.rs +++ b/minecraft-protocol/src/packets/mod.rs @@ -2,6 +2,7 @@ mod client_intention_packet; pub use client_intention_packet::ClientIntentionPacket; mod serverbound_status_request_packet; pub use serverbound_status_request_packet::ServerboundStatusRequestPacket; +use tokio::io::AsyncRead; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ConnectionProtocol { @@ -15,5 +16,6 @@ pub trait Packet { /// Get the id of the packet, this is always a byte. fn get_id(&self) -> u32; - fn write(&self, friendly_byte_buf: &mut Vec<u8>) -> (); + fn write(&self, buf: &mut Vec<u8>) -> (); + fn parse<T: AsyncRead + std::marker::Unpin>(&self, buf: T) -> (); } diff --git a/minecraft-protocol/src/packets/serverbound_status_request_packet.rs b/minecraft-protocol/src/packets/serverbound_status_request_packet.rs index 94b7bbee..ab9001f4 100644 --- a/minecraft-protocol/src/packets/serverbound_status_request_packet.rs +++ b/minecraft-protocol/src/packets/serverbound_status_request_packet.rs @@ -13,4 +13,5 @@ impl Packet for ServerboundStatusRequestPacket { // implement "from_reader" for "ClientIntentionPacket" fn write(&self, _buf: &mut Vec<u8>) {} + fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {} } diff --git a/minecraft-protocol/src/server_status.rs b/minecraft-protocol/src/server_status.rs new file mode 100644 index 00000000..93480aaf --- /dev/null +++ b/minecraft-protocol/src/server_status.rs @@ -0,0 +1,6 @@ +struct ServerStatus { + description: Component, + players: Players, + version: Version, + favicon: String, +} diff --git a/minecraft-protocol/src/server_status_pinger.rs b/minecraft-protocol/src/server_status_pinger.rs index df53b897..342c4f44 100644 --- a/minecraft-protocol/src/server_status_pinger.rs +++ b/minecraft-protocol/src/server_status_pinger.rs @@ -13,6 +13,7 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { 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 { protocol_version: 757, hostname: &address.host, @@ -20,6 +21,9 @@ pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { intention: ConnectionProtocol::Status, }) .await; + conn.switch_state(ConnectionProtocol::Status); + + // send the empty status request packet conn.send_packet(&ServerboundStatusRequestPacket {}).await; conn.read_packet().await.unwrap(); |
