Lightweight Minecraft protocol implementation in pure rust
Go to file
2024-05-12 01:09:23 +03:00
src keywords and hating .unwrap() 2024-05-12 01:09:23 +03:00
.gitignore you dont need to see this 2024-05-12 00:12:24 +03:00
Cargo.toml keywords and hating .unwrap() 2024-05-12 01:09:23 +03:00
LICENSE init commit?? 2024-05-12 00:11:25 +03:00
README.md keywords and hating .unwrap() 2024-05-12 01:09:23 +03:00

rust_mc_proto

lightweight minecraft packets protocol support in pure rust
has compression (MinecraftConnection::set_compression)
all types of packets you can find on wiki.vg
on crates on github

how to use it

for reference:

pub type MCConn<T> = MinecraftConnection<T>;

example how to get motd

use std::net::TcpStream;
use varint_rs::VarintWriter;
use rust_mc_proto::{Packet, ProtocolError, MCConn};

fn send_handshake(conn: &mut MCConn<TcpStream>,
                protocol_version: u16,
                server_address: &str,
                server_port: u16,
                next_state: u8) -> Result<(), ProtocolError> {
    let mut packet = Packet::empty(0x00);

    packet.write_u16_varint(protocol_version)?;
    packet.write_string(server_address.to_string())?;
    packet.write_unsigned_short(server_port)?;
    packet.write_u8_varint(next_state)?;

    conn.write_packet(&packet)?;

    Ok(())
}

fn send_status_request(conn: &mut MCConn<TcpStream>) -> Result<(), ProtocolError> {
    let packet = Packet::empty(0x00);
    conn.write_packet(&packet)?;

    Ok(())
}

fn read_status_response(conn: &mut MCConn<TcpStream>) -> Result<String, ProtocolError> {
    let mut packet = conn.read_packet()?;

    packet.read_string()
}

fn main() {
    let mut conn = MCConn::connect("sloganmc.ru:25565").unwrap();

    send_handshake(&mut conn, 765, "sloganmc.ru", 25565, 1).unwrap();
    send_status_request(&mut conn).unwrap();

    println!("{}", read_status_response(&mut conn).unwrap()); // prints servers motd in json
}

also you can get minecraft connection from any stream: MinecraftConnection::from_stream

I think this crate can be used for a server on rust idk -_-