Crates.io | mcnbt |
lib.rs | mcnbt |
version | 2.0.0 |
source | src |
created_at | 2023-07-02 17:02:06.813472 |
updated_at | 2024-12-02 19:16:45.763899 |
description | Read and write NBT |
homepage | |
repository | https://github.com/phoenixr-codes/mcnbt |
max_upload_size | |
id | 906233 |
size | 75,155 |
Read and write NBT files.
cargo add mcnbt
Reading NBT data.
use mcnbt::{ByteOrder, Tag};
let tag = Tag::from_bytes(
include_bytes!("../examples/hello_world.nbt"), // file to read
ByteOrder::BigEndian // Java Edition uses big endian byte order
);
println!("{:#?}", tag);
Writing NBT data.
use mcnbt::{ByteOrder, Tag};
let tag = mcnbt::nbt![
Tag::Int(Some("foo".to_string()), 42),
Tag::List(Some("bar".to_string()), vec![
Tag::String(None, "Hello".to_string()),
Tag::String(None, "World".to_string()),
]),
Tag::ByteArray(Some("baz".to_string()), vec![
-8,
-6,
-4,
-2,
0,
2,
4,
6,
8,
]),
];
println!("{:#?}", tag.to_bytes(ByteOrder::LittleEndian));
cargo install mcnbt
Usage: nbt [OPTIONS] <path>
Arguments:
<path> The path to the NBT file
Options:
-L, --little-endian Use little endian byte order
-h, --help Print help
-V, --version Print version
The web
directory conatains a web interface which uses mcnbt
in the back-end.
See it in action here.
serde
cargo add -F serde mcnbt
use mcnbt::Tag;
use serde_json::Value;
let data = Tag::Compound(
Some("".to_string()),
vec![
Tag::String(
Some("foo".to_string()),
"Hello World".to_string()
),
Tag::List(
Some("bar".to_string()),
vec![
Tag::Byte(None, 1),
Tag::Byte(None, 2),
Tag::Byte(None, 3),
]
)
]
);
assert_eq!(
serde_json::to_string(&data).unwrap(),
serde_json::to_string(
&serde_json::json!({
"type": "compound",
"name": "",
"payload": [
{
"type": "string",
"name": "foo",
"payload": "Hello World"
},
{
"type": "list",
"name": "bar",
"payload": [
{
"type": "byte",
"name": null,
"payload": 1
},
{
"type": "byte",
"name": null,
"payload": 2
},
{
"type": "byte",
"name": null,
"payload": 3
}
]
}
]
})
).unwrap()
);
Here are some websites explaining the NBT file format that have been used for the development of this library.
cargo test --all-features
# ^^^^^^^^^^^^^^ important