#nbt #minecraft #serialization

valence_nbt

A library for Minecraft's Named Binary Tag (NBT) format

7 releases (4 breaking)

0.4.0 Oct 30, 2022
0.3.0 Oct 18, 2022
0.2.0 Oct 1, 2022
0.1.2 Sep 23, 2022
0.0.1 Sep 20, 2022

#7 in #nbt

41 downloads per month
Used in azalea-nbt

MIT license

52KB
1.5K SLoC

valence_nbt

A library for encoding and decoding Minecraft's Named Binary Tag (NBT) format.

See the documentation for more information.


lib.rs:

A library for encoding and decoding Minecraft's Named Binary Tag (NBT) format.

Examples

Encode NBT data to its binary form. We are using the [compound!] macro to conveniently construct [Compound] values.

use valence_nbt::{compound, to_binary_writer, List};

let c = compound! {
    "byte" => 5_i8,
    "string" => "hello",
    "list_of_float" => List::Float(vec![
        3.1415,
        2.7182,
        1.4142
    ]),
};

let mut buf = Vec::new();

to_binary_writer(&mut buf, &c, "").unwrap();

Decode NBT data from its binary form.

use valence_nbt::{compound, from_binary_slice};

let some_bytes = [10, 0, 0, 3, 0, 3, 105, 110, 116, 0, 0, 222, 173, 0];

let expected_value = compound! {
    "int" => 0xdead
};

let (nbt, root_name) = from_binary_slice(&mut some_bytes.as_slice()).unwrap();

assert_eq!(nbt, expected_value);
assert_eq!(root_name, "");

Features

  • preserve_order: Causes the order of fields in [Compound]s to be preserved during insertion and deletion at a slight cost to performance. The iterators on Compound can then implement [DoubleEndedIterator].

Dependencies

~1.2–1.7MB
~36K SLoC