#nbt #minecraft #crabcraft

crab_nbt

Up-to-date Rust crate for easy and intuitive working with NBT data

4 releases

0.1.0 Apr 7, 2024
0.0.3 Mar 24, 2024
0.0.2 Mar 20, 2024
0.0.1 Mar 20, 2024

#708 in Parser implementations

Download history 226/week @ 2024-03-17 145/week @ 2024-03-24 13/week @ 2024-03-31 88/week @ 2024-04-07

472 downloads per month

GPL-3.0-only

24KB
526 lines

🦀 CrabNBT

Up-to-date Rust crate for easy and intuitive working with NBT data.

Why not other libraries?

CrabNBT combines best features of existing NBT crates, to create perfect solution.
Big thanks to simdnbt and fastnbt for ideas!

Features

🚧 Support for serializing to/from Struct *(soon)*
Java string support
✅ NBT! macro for easy creation
✅ Good system of getting values from NBT
✅ Serializing for single tags
✅ Support of Network NBT

Installing

cargo add crab_nbt

Serializing

use crab_nbt::{nbt, Nbt, NbtCompound};

fn main() {
    // Using NBT macro
    let nbt = nbt!("root nbt_inner name", {
        "float": 1.0,
        "key": "value",
        "long_array": [L; 1, 2],
        "int_array": [Int; 1, 10, 25],
        "byte_array": [B; 0, 1, 0, 0, 1],
        "list": ["a", "b", "c"],
        "nbt_inner": {
            "key": "sub value"
        }
    });

    let nbt = Nbt::new(
        "root".to_owned(),
        NbtCompound::from_iter([
            ("float".to_owned(), 1.0.into()),
            ("key".to_owned(), "value".into()),
            ("nbt_inner".to_owned(), NbtCompound::from_iter([
                ("key".to_owned(), "sub value".into()),
            ]).into())
        ])
    );
}

Deserializing

use bytes::Bytes;
use crab_nbt::{nbt, Nbt, NbtCompound};

fn example(bytes: &mut Bytes) {
    let nbt = Nbt::read(bytes).unwrap();
    let egg_name = nbt
        .get_compound("nbt_inner")
        .and_then(|compound| compound.get_compound("egg"))
        .and_then(|compound| compound.get_string("name"))
        .unwrap();
}

Dependencies

~0.5–1MB
~22K SLoC