owengage / fastnbt

Fast serde serializer and deserializer for Minecraft's NBT and Anvil formats
MIT License
185 stars 35 forks source link

Error while iterating over chunk blocks #99

Closed jotalanusse closed 7 months ago

jotalanusse commented 7 months ago

Hello, I'm trying to manually iterate over chunk blocks using the following example:

let complete_chunk = complete::Chunk::from_bytes(&data).unwrap();

for chunk_x in 0..16 {
    for chunk_z in 0..16 {
        for chunk_y in complete_chunk.y_range() {
            let block = complete_chunk.block(chunk_x, chunk_y, chunk_z);

            match block {
                Some(block) => {}
                None => {}
            }
        }
    }
}

But at certain points it fails with the following error:

thread 'main' panicked at /home/jotalanusse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/fastanvil-0.30.0/src/complete/section.rs:26:52:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Am I doing something wrong? Iterating over the blocks with complete_chunk.iter_blocks() seems to work just fine, but I couldn't find how it is implemented.

You can find the .mca file im using here.

owengage commented 7 months ago

This does look like a bug in fastanvil, thanks for the mca file. I will have to investigate at some point.

owengage commented 7 months ago

Just as an update, I've not had much time to look into this. The MCA file you provided has some chunks in it that I don't have any test coverage for. Some of your chunks have a 'Y=-5' section. A section is a 16x16x16 collection of blocks, a tower of sections make up a chunk.

You have a section for the -80 to -65 Y region for some reason, and that section contains no information at all. Usually there's a block palette at least.

I need to investigate more to know the best way to handle it, and it's definitely surfaced a very real bug in fastanvil, so thanks. :)