#minecraft #protocols #helper #nbt #packet #version

mclib

Utils and helpers for Minecraft protocol

1 unstable release

0.0.1 Feb 24, 2024

#40 in #nbt

Apache-2.0

66KB
2K SLoC

MCLib MineCraft Library for Rust

Utils and helpers for Minecraft Protocol


lib.rs:

Minecraft Java Edition protocol helpers

Current version of crate supports 763 protocol version, Minecraft version 1.20.2

Examples

Writing to packet

use mclib::MCPacket;
use mclib::packets::server::PingRequest;
use mclib::types::MCLong;

let packet = PingRequest {
    payload: MCLong::from(1337),
};

// Struct will be packed into Minecraft protocol format with packet id prefixed
let binary: Vec<u8> = packet.pack();

Reading from binary data

use mclib::MCPacket;
use mclib::packets::server::LoginStart;

// You can use any data type that implements std::io::Read
let raw_data: Vec<u8> = vec![0x01, 0x03, 0x03, 0x07];
let mut binary_data = std::io::Cursor::new(raw_data);

let login_start = LoginStart::unpack(&mut binary_data);

Writing new packet

If your want to write new packet, you need to create structure with derive [MCPacket] trait and with Minecraft data types: you can use existing [types] or create new by implementing [MCType] trait. Derived trait will automatically add methods like pack() and unpack()

use mclib::types::{MCShort, MCVarInt};
use mclib::{MCPacket, MCType};

#[derive(MCPacket, Debug, Clone)]
#[packet(packet_id = 0x00)] // Specify packet_id here
pub struct NewMinecraftPacket {
    field_1: MCShort
}

NBT

TAG_Compound('hello world'): 1 entry
  {
    TAG_String('name'): 'Bananrama'
  }

Code equivalent for this NBT will be

use mclib::nbt::{NBT, IntoNBTTag};

let nbt = NBT(
    Some("hello world".to_string()),
    vec![("name", "Bananrama".to_nbt())].to_nbt(),
);

For more information about NBT tags their pages

Dependencies

~1.3–1.9MB
~38K SLoC