Rusty-Quartz / quartz_nbt

Provides support for encoding and decoding Minecraft's NBT format. This crate supports both zlib and gz compression, and also provides tools for converting NBT data to stringified NBT (SNBT) and vice versa.
https://crates.io/crates/quartz_nbt
MIT License
25 stars 10 forks source link

Help using get method for NbtTag types #1

Closed JMMFL closed 3 years ago

JMMFL commented 3 years ago

Hello! I read your documentation on the get method for NbtCompounds:

let mut compound = NbtCompound::new(); compound.insert("test", 1.0f64); assert_eq!(compound.get::<_, f64>("test"), Ok(1.0f64));

I'm trying to access the value of my 'blocks' tag in my NBT file. What would the type annotation look like? I've tried the following:

let mut file = File::open("1x1x1-dirt.nbt").unwrap(); let compound = read_nbt_gz_compressed(&mut file).unwrap().0; let compound = compound.get::<_, NbtTag>("blocks");

Compound looks like: "{size:[1,1,1],DataVersion:2586,palette:[{Name:\"minecraft:dirt\"}],entities:[],blocks:[{state:0,pos:[0,0,0]}]}"

Appreciate the guidance 👍

Cassy343 commented 3 years ago

Hello, I'd recommend doing the following:

let mut file = File::open("1x1x1-dirt.nbt").unwrap();
let compound = read_nbt_gz_compressed(&mut file).unwrap().0;

// The `?` syntax would also work here when returning a result
let blocks: &NbtList = compound.get("blocks").unwrap();

Note that this works because by specifying that the type of blocks is &NbtList, the compiler can infer the appropriate type parameter for the get method. If you need an owned value, then clone the NBT list.