1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

keep doing packet stuff

This commit is contained in:
mat 2021-12-07 22:12:16 +00:00
parent 9c14b3f323
commit 4a44c58444
12 changed files with 65 additions and 3 deletions

4
Cargo.lock generated
View file

@ -278,6 +278,10 @@ version = "2.4.1"
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"

View file

@ -4,4 +4,5 @@ members = [
"bot",
"minecraft-client",
"minecraft-protocol",
"minecraft-chat",
]

View file

@ -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]

View file

@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View file

@ -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:

View file

@ -1,3 +1,5 @@
//! This lib is responsible for parsing Minecraft packets.
use std::net::IpAddr;
use std::str::FromStr;

View file

@ -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) -> () {}
}

View file

@ -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);
}
}

View file

@ -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) -> ();
}

View file

@ -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) -> () {}
}

View file

@ -0,0 +1,6 @@
struct ServerStatus {
description: Component,
players: Players,
version: Version,
favicon: String,
}

View file

@ -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();