From e25aeab0654d00374f3b2218c6c7c6832c93a6c5 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 23 May 2023 20:07:23 +0200 Subject: [PATCH] Fix Broken Schematics Loading --- schemsearch-files/src/lib.rs | 18 ++++++++++++++---- schemsearch-lib/src/pattern_mapper.rs | 4 +--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/schemsearch-files/src/lib.rs b/schemsearch-files/src/lib.rs index 16fdcb3..c1dda3f 100644 --- a/schemsearch-files/src/lib.rs +++ b/schemsearch-files/src/lib.rs @@ -64,7 +64,17 @@ pub struct Entity { impl SpongeSchematic { pub fn load_data(data: &mut R) -> Result where R: Read { let nbt: CompoundTag = nbt::decode::read_gzip_compound_tag(data).map_err(|e| e.to_string())?; - let version = nbt.get_i32("Version").map_err(|e| e.to_string())?; + let version = nbt.get_i32("Version").unwrap_or_else(|_| { + return if nbt.contains_key("Blocks") { + 3 + } else if nbt.contains_key("BlockEntities") { + 2 + } else if nbt.contains_key("TileEntities") { + 1 + } else { + -1 + }; + }); match version { 1 => SpongeSchematic::from_nbt_1(nbt), @@ -90,7 +100,7 @@ impl SpongeSchematic { palette_max: nbt.get_i32("PaletteMax").map_err(|e| e.to_string())?, palette: read_palette(nbt.get_compound_tag("Palette").map_err(|e| e.to_string())?), block_data: read_blocks(nbt.get_i8_vec("BlockData").map_err(|e| e.to_string())?), - block_entities: read_tile_entities(nbt.get_compound_tag_vec("TileEntities").map_err(|e| e.to_string())?)?, + block_entities: read_tile_entities(nbt.get_compound_tag_vec("TileEntities").unwrap_or_else(|_| vec![]))?, entities: None, }) } @@ -106,7 +116,7 @@ impl SpongeSchematic { palette_max: nbt.get_i32("PaletteMax").map_err(|e| e.to_string())?, palette: read_palette(nbt.get_compound_tag("Palette").map_err(|e| e.to_string())?), block_data: read_blocks(nbt.get_i8_vec("BlockData").map_err(|e| e.to_string())?), - block_entities: read_tile_entities(nbt.get_compound_tag_vec("BlockEntities").map_err(|e| e.to_string())?)?, + block_entities: read_tile_entities(nbt.get_compound_tag_vec("BlockEntities").unwrap_or_else(|_| vec![]))?, entities: None, }) } @@ -123,7 +133,7 @@ impl SpongeSchematic { palette_max: compute_palette_max(blocks.get_compound_tag("Palette").map_err(|e| e.to_string())?), palette: read_palette(blocks.get_compound_tag("Palette").map_err(|e| e.to_string())?), block_data: read_blocks(blocks.get_i8_vec("BlockData").map_err(|e| e.to_string())?), - block_entities: read_tile_entities(blocks.get_compound_tag_vec("BlockEntities").map_err(|e| e.to_string())?)?, + block_entities: read_tile_entities(blocks.get_compound_tag_vec("BlockEntities").unwrap_or_else(|_| vec![]))?, entities: None, }) } diff --git a/schemsearch-lib/src/pattern_mapper.rs b/schemsearch-lib/src/pattern_mapper.rs index 2950b10..e554d4c 100644 --- a/schemsearch-lib/src/pattern_mapper.rs +++ b/schemsearch-lib/src/pattern_mapper.rs @@ -37,7 +37,7 @@ pub fn strip_data(schem: &SpongeSchematic) -> SpongeSchematic { let reverse_palette = create_reverse_palette(schem); for block in schem.block_data.iter() { - let block_name = reverse_palette[*block as usize].clone(); + let block_name = reverse_palette[*block as usize]; let block_name = block_name.split('[').next().unwrap().to_string(); let entry = palette.entry(block_name).or_insert_with(|| { @@ -61,8 +61,6 @@ pub fn strip_data(schem: &SpongeSchematic) -> SpongeSchematic { offset: [0; 3], entities: None, } - - } pub fn match_palette_adapt(schem: &SpongeSchematic, matching_palette: &HashMap, ignore_data: bool) -> Vec {