#minecraft #packets #protocols #light-weight #packet

rust_mc_proto

纯 Rust 实现的轻量级 Minecraft 协议支持

17 个版本

新版本 0.1.16 2024 年 8 月 22 日
0.1.15 2024 年 7 月 15 日
0.1.8 2024 年 5 月 24 日

网络编程 中排名第 976

Download history 340/week @ 2024-05-10 200/week @ 2024-05-17 201/week @ 2024-05-24 5/week @ 2024-05-31 1/week @ 2024-06-07 747/week @ 2024-07-12 48/week @ 2024-07-19 126/week @ 2024-07-26 11/week @ 2024-08-02

每月 213 次下载

自定义许可证

36KB
814

rust_mc_proto

纯 Rust 实现的轻量级 Minecraft 数据包协议支持
支持压缩 (MinecraftConnection::set_compression)
所有可以在 wiki.vg 上找到的包类型
crates github

设置

稳定

rust_mc_proto = "0.1.15"

不稳定

rust_mc_proto = { git = "https://github.com/MeexReay/rust_mc_proto" }

功能

  • 原子压缩(默认)

如何使用它

参考

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

MOTD 接收示例

use rust_mc_proto::{Packet, ProtocolError, MCConnTcp, DataBufferReader, DataBufferWriter};

/*

    Example of receiving motd from the server
    Sends handshake, status request and receiving one

*/

fn send_handshake(conn: &mut MCConnTcp,
                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)?;
    packet.write_unsigned_short(server_port)?;
    packet.write_u8_varint(next_state)?;

    conn.write_packet(&packet)?;

    Ok(())
}

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

    Ok(())
}

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

    packet.read_string()
}

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

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

    let motd = read_status_response(&mut conn).unwrap();

    dbg!(motd);
}

更多示例

这个 crate 可以用于 Rust 服务器 -_-

依赖

~710KB
~12K SLoC