world: fix deserialize of unpopulated 1.18+ sections

This commit is contained in:
Matthias Schiffer 2023-08-20 19:11:12 +02:00
parent 42a800e08c
commit 003b48951b
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 51 additions and 28 deletions

View file

@ -112,27 +112,36 @@ impl<'a> Chunk<'a> {
let mut section_map = BTreeMap::new(); let mut section_map = BTreeMap::new();
for section in sections { for section in sections {
section_map.insert( match &section.section {
SectionY(section.y), de::SectionV1_18Variants::V1_18 {
( block_states,
SectionV1_13::new( biomes,
data_version, block_light,
section.block_states.data.as_deref(), } => {
&section.block_states.palette, section_map.insert(
block_types, SectionY(section.y),
) (
.with_context(|| format!("Failed to load section at Y={}", section.y))?, SectionV1_13::new(
BiomesV1_18::new( data_version,
section.biomes.data.as_deref(), block_states.data.as_deref(),
&section.biomes.palette, &block_states.palette,
biome_types, block_types,
) )
.with_context(|| format!("Failed to load section biomes at Y={}", section.y))?, .with_context(|| {
BlockLight::new(section.block_light.as_deref()).with_context(|| { format!("Failed to load section at Y={}", section.y)
format!("Failed to load section block light at Y={}", section.y) })?,
})?, BiomesV1_18::new(biomes.data.as_deref(), &biomes.palette, biome_types)
), .with_context(|| {
); format!("Failed to load section biomes at Y={}", section.y)
})?,
BlockLight::new(block_light.as_deref()).with_context(|| {
format!("Failed to load section block light at Y={}", section.y)
})?,
),
);
}
de::SectionV1_18Variants::Empty {} => {}
};
} }
Ok(Chunk::V1_18 { section_map }) Ok(Chunk::V1_18 { section_map })

View file

@ -28,19 +28,33 @@ pub struct BiomesV1_18 {
pub data: Option<fastnbt::LongArray>, pub data: Option<fastnbt::LongArray>,
} }
/// Variable part of a [SectionV1_18]
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum SectionV1_18Variants {
/// Populated 1.18+ section
V1_18 {
/// Block type data
block_states: BlockStatesV1_18,
/// Biome data
biomes: BiomesV1_18,
/// Block light data
#[serde(rename = "BlockLight")]
block_light: Option<fastnbt::ByteArray>,
},
/// Empty section
Empty {},
}
/// Element of the 1.18+ `sections` list found in a [Chunk] /// Element of the 1.18+ `sections` list found in a [Chunk]
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct SectionV1_18 { pub struct SectionV1_18 {
/// Y coordinate /// Y coordinate
#[serde(rename = "Y")] #[serde(rename = "Y")]
pub y: i32, pub y: i32,
/// Block type data /// Variable part of section
pub block_states: BlockStatesV1_18, #[serde(flatten)]
/// Biome data pub section: SectionV1_18Variants,
pub biomes: BiomesV1_18,
/// Block light data
#[serde(rename = "BlockLight")]
pub block_light: Option<fastnbt::ByteArray>,
} }
/// Version-specific part of a pre-1.18 [Section](SectionV0) /// Version-specific part of a pre-1.18 [Section](SectionV0)