diff --git a/crates/horizon-world/src/datafix/files.rs b/crates/horizon-world/src/datafix/files.rs index 5125fe1d2..6237f2c64 100644 --- a/crates/horizon-world/src/datafix/files.rs +++ b/crates/horizon-world/src/datafix/files.rs @@ -38,6 +38,23 @@ pub fn read_dat(path: &Path) -> io::Result> { Ok(Some(root.tag)) } +/// `SavedDataStorage.readTagFromDisk`: a file without the gzip header is plain NBT. +pub fn read_saved_dat(path: &Path) -> io::Result> { + let bytes = match fs::read(path) { + Ok(b) => b, + Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(e), + }; + let limits = Limits::default(); + let raw = if bytes.starts_with(&[0x1f, 0x8b]) { + Compression::Gzip.decode(&bytes, limits.max_bytes)? + } else { + bytes + }; + let (root, _) = nbt::read_named(&raw, limits).map_err(|e| invalid(e.to_string()))?; + Ok(Some(root.tag)) +} + /// `NbtIo.writeCompressed`, creating the parent folders. pub fn write_dat(path: &Path, tag: &Tag) -> io::Result<()> { if let Some(parent) = path.parent() { @@ -707,7 +724,7 @@ fn upgrade_saved_data(data: &Path) -> io::Result<()> { let Some(ty) = super::saved_data_type(&relative) else { continue; }; - if let Some(mut tag) = read_dat(&file)? { + if let Some(mut tag) = read_saved_dat(&file)? { super::update_saved_data(ty, &mut tag); set(&mut tag, "DataVersion", Tag::Int(CURRENT)); write_dat(&file, &tag)?; diff --git a/crates/horizon-world/src/datafix/fixes_a.rs b/crates/horizon-world/src/datafix/fixes_a.rs index 2bfe43af3..3d89ec990 100644 --- a/crates/horizon-world/src/datafix/fixes_a.rs +++ b/crates/horizon-world/src/datafix/fixes_a.rs @@ -659,7 +659,10 @@ fn block_pos_format(cx: &mut Cx) { // ------------------------------------------------------------------ 3814 -fn attributes_rename_legacy(cx: &mut Cx, rename_fn: &dyn Fn(&str) -> Option<&'static str>) { +pub(super) fn attributes_rename_legacy( + cx: &mut Cx, + rename_fn: &dyn Fn(&str) -> Option<&'static str>, +) { let fix_name = |v: &mut Tag, field: &str| { if let Some(name) = as_str(v.get(field)) && let Some(new) = rename_fn(&name) diff --git a/crates/horizon-world/src/datafix/fixes_cliffs.rs b/crates/horizon-world/src/datafix/fixes_cliffs.rs index 9a4dfa402..9c2d55a88 100644 --- a/crates/horizon-world/src/datafix/fixes_cliffs.rs +++ b/crates/horizon-world/src/datafix/fixes_cliffs.rs @@ -21,10 +21,16 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { })); out.push((key(2690, 0), |cx| cx.rename(Ty::ItemName, renamer(COPPER)))); out.push((key(2690, 0), |cx| block_rename(cx, COPPER))); - out.push((key(2691, 0), |cx| cx.rename(Ty::ItemName, renamer(WAXED_COPPER)))); + out.push((key(2691, 0), |cx| { + cx.rename(Ty::ItemName, renamer(WAXED_COPPER)) + })); out.push((key(2691, 0), |cx| block_rename(cx, WAXED_COPPER))); - out.push((key(2693, 0), |cx| add_flag(cx, "has_increased_height_already", false))); - out.push((key(2696, 0), |cx| cx.rename(Ty::ItemName, renamer(GRIMSTONE)))); + out.push((key(2693, 0), |cx| { + add_flag(cx, "has_increased_height_already", false) + })); + out.push((key(2696, 0), |cx| { + cx.rename(Ty::ItemName, renamer(GRIMSTONE)) + })); out.push((key(2696, 0), |cx| block_rename(cx, GRIMSTONE))); out.push((key(2700, 0), |cx| { block_rename( @@ -37,20 +43,28 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { })); out.push((key(2701, 0), feature_pool_elements)); out.push((key(2702, 0), arrow_pickup)); - out.push((key(2707, 0), |cx| add_flag(cx, "has_increased_height_already", true))); + out.push((key(2707, 0), |cx| { + add_flag(cx, "has_increased_height_already", true) + })); out.push((key(2710, 0), |cx| { stats_rename(cx, &[("minecraft:play_one_minute", "minecraft:play_time")]) })); out.push((key(2717, 0), |cx| { cx.rename( Ty::ItemName, - renamer(&[("minecraft:azalea_leaves_flowers", "minecraft:flowering_azalea_leaves")]), + renamer(&[( + "minecraft:azalea_leaves_flowers", + "minecraft:flowering_azalea_leaves", + )]), ) })); out.push((key(2717, 0), |cx| { block_rename( cx, - &[("minecraft:azalea_leaves_flowers", "minecraft:flowering_azalea_leaves")], + &[( + "minecraft:azalea_leaves_flowers", + "minecraft:flowering_azalea_leaves", + )], ) })); out.push((key(2825, 0), |cx| { @@ -372,53 +386,161 @@ fn cauldron_rename(cx: &mut Cx) { } const COPPER: &[(&str, &str)] = &[ - ("minecraft:weathered_copper_block", "minecraft:oxidized_copper_block"), - ("minecraft:semi_weathered_copper_block", "minecraft:weathered_copper_block"), - ("minecraft:lightly_weathered_copper_block", "minecraft:exposed_copper_block"), - ("minecraft:weathered_cut_copper", "minecraft:oxidized_cut_copper"), - ("minecraft:semi_weathered_cut_copper", "minecraft:weathered_cut_copper"), - ("minecraft:lightly_weathered_cut_copper", "minecraft:exposed_cut_copper"), - ("minecraft:weathered_cut_copper_stairs", "minecraft:oxidized_cut_copper_stairs"), - ("minecraft:semi_weathered_cut_copper_stairs", "minecraft:weathered_cut_copper_stairs"), - ("minecraft:lightly_weathered_cut_copper_stairs", "minecraft:exposed_cut_copper_stairs"), - ("minecraft:weathered_cut_copper_slab", "minecraft:oxidized_cut_copper_slab"), - ("minecraft:semi_weathered_cut_copper_slab", "minecraft:weathered_cut_copper_slab"), - ("minecraft:lightly_weathered_cut_copper_slab", "minecraft:exposed_cut_copper_slab"), - ("minecraft:waxed_semi_weathered_copper", "minecraft:waxed_weathered_copper"), - ("minecraft:waxed_lightly_weathered_copper", "minecraft:waxed_exposed_copper"), - ("minecraft:waxed_semi_weathered_cut_copper", "minecraft:waxed_weathered_cut_copper"), - ("minecraft:waxed_lightly_weathered_cut_copper", "minecraft:waxed_exposed_cut_copper"), - ("minecraft:waxed_semi_weathered_cut_copper_stairs", "minecraft:waxed_weathered_cut_copper_stairs"), - ("minecraft:waxed_lightly_weathered_cut_copper_stairs", "minecraft:waxed_exposed_cut_copper_stairs"), - ("minecraft:waxed_semi_weathered_cut_copper_slab", "minecraft:waxed_weathered_cut_copper_slab"), - ("minecraft:waxed_lightly_weathered_cut_copper_slab", "minecraft:waxed_exposed_cut_copper_slab"), + ( + "minecraft:weathered_copper_block", + "minecraft:oxidized_copper_block", + ), + ( + "minecraft:semi_weathered_copper_block", + "minecraft:weathered_copper_block", + ), + ( + "minecraft:lightly_weathered_copper_block", + "minecraft:exposed_copper_block", + ), + ( + "minecraft:weathered_cut_copper", + "minecraft:oxidized_cut_copper", + ), + ( + "minecraft:semi_weathered_cut_copper", + "minecraft:weathered_cut_copper", + ), + ( + "minecraft:lightly_weathered_cut_copper", + "minecraft:exposed_cut_copper", + ), + ( + "minecraft:weathered_cut_copper_stairs", + "minecraft:oxidized_cut_copper_stairs", + ), + ( + "minecraft:semi_weathered_cut_copper_stairs", + "minecraft:weathered_cut_copper_stairs", + ), + ( + "minecraft:lightly_weathered_cut_copper_stairs", + "minecraft:exposed_cut_copper_stairs", + ), + ( + "minecraft:weathered_cut_copper_slab", + "minecraft:oxidized_cut_copper_slab", + ), + ( + "minecraft:semi_weathered_cut_copper_slab", + "minecraft:weathered_cut_copper_slab", + ), + ( + "minecraft:lightly_weathered_cut_copper_slab", + "minecraft:exposed_cut_copper_slab", + ), + ( + "minecraft:waxed_semi_weathered_copper", + "minecraft:waxed_weathered_copper", + ), + ( + "minecraft:waxed_lightly_weathered_copper", + "minecraft:waxed_exposed_copper", + ), + ( + "minecraft:waxed_semi_weathered_cut_copper", + "minecraft:waxed_weathered_cut_copper", + ), + ( + "minecraft:waxed_lightly_weathered_cut_copper", + "minecraft:waxed_exposed_cut_copper", + ), + ( + "minecraft:waxed_semi_weathered_cut_copper_stairs", + "minecraft:waxed_weathered_cut_copper_stairs", + ), + ( + "minecraft:waxed_lightly_weathered_cut_copper_stairs", + "minecraft:waxed_exposed_cut_copper_stairs", + ), + ( + "minecraft:waxed_semi_weathered_cut_copper_slab", + "minecraft:waxed_weathered_cut_copper_slab", + ), + ( + "minecraft:waxed_lightly_weathered_cut_copper_slab", + "minecraft:waxed_exposed_cut_copper_slab", + ), ]; const WAXED_COPPER: &[(&str, &str)] = &[ ("minecraft:waxed_copper", "minecraft:waxed_copper_block"), - ("minecraft:oxidized_copper_block", "minecraft:oxidized_copper"), - ("minecraft:weathered_copper_block", "minecraft:weathered_copper"), + ( + "minecraft:oxidized_copper_block", + "minecraft:oxidized_copper", + ), + ( + "minecraft:weathered_copper_block", + "minecraft:weathered_copper", + ), ("minecraft:exposed_copper_block", "minecraft:exposed_copper"), ]; const GRIMSTONE: &[(&str, &str)] = &[ ("minecraft:grimstone", "minecraft:deepslate"), - ("minecraft:grimstone_slab", "minecraft:cobbled_deepslate_slab"), - ("minecraft:grimstone_stairs", "minecraft:cobbled_deepslate_stairs"), - ("minecraft:grimstone_wall", "minecraft:cobbled_deepslate_wall"), - ("minecraft:polished_grimstone", "minecraft:polished_deepslate"), - ("minecraft:polished_grimstone_slab", "minecraft:polished_deepslate_slab"), - ("minecraft:polished_grimstone_stairs", "minecraft:polished_deepslate_stairs"), - ("minecraft:polished_grimstone_wall", "minecraft:polished_deepslate_wall"), + ( + "minecraft:grimstone_slab", + "minecraft:cobbled_deepslate_slab", + ), + ( + "minecraft:grimstone_stairs", + "minecraft:cobbled_deepslate_stairs", + ), + ( + "minecraft:grimstone_wall", + "minecraft:cobbled_deepslate_wall", + ), + ( + "minecraft:polished_grimstone", + "minecraft:polished_deepslate", + ), + ( + "minecraft:polished_grimstone_slab", + "minecraft:polished_deepslate_slab", + ), + ( + "minecraft:polished_grimstone_stairs", + "minecraft:polished_deepslate_stairs", + ), + ( + "minecraft:polished_grimstone_wall", + "minecraft:polished_deepslate_wall", + ), ("minecraft:grimstone_tiles", "minecraft:deepslate_tiles"), - ("minecraft:grimstone_tile_slab", "minecraft:deepslate_tile_slab"), - ("minecraft:grimstone_tile_stairs", "minecraft:deepslate_tile_stairs"), - ("minecraft:grimstone_tile_wall", "minecraft:deepslate_tile_wall"), + ( + "minecraft:grimstone_tile_slab", + "minecraft:deepslate_tile_slab", + ), + ( + "minecraft:grimstone_tile_stairs", + "minecraft:deepslate_tile_stairs", + ), + ( + "minecraft:grimstone_tile_wall", + "minecraft:deepslate_tile_wall", + ), ("minecraft:grimstone_bricks", "minecraft:deepslate_bricks"), - ("minecraft:grimstone_brick_slab", "minecraft:deepslate_brick_slab"), - ("minecraft:grimstone_brick_stairs", "minecraft:deepslate_brick_stairs"), - ("minecraft:grimstone_brick_wall", "minecraft:deepslate_brick_wall"), - ("minecraft:chiseled_grimstone", "minecraft:chiseled_deepslate"), + ( + "minecraft:grimstone_brick_slab", + "minecraft:deepslate_brick_slab", + ), + ( + "minecraft:grimstone_brick_stairs", + "minecraft:deepslate_brick_stairs", + ), + ( + "minecraft:grimstone_brick_wall", + "minecraft:deepslate_brick_wall", + ), + ( + "minecraft:chiseled_grimstone", + "minecraft:chiseled_deepslate", + ), ]; /// `SavedDataFeaturePoolElementFix`: the feature of jigsaw pieces' feature pool @@ -446,7 +568,9 @@ fn feature_pool_elements(cx: &mut Cx) { return child; } let element = child.get("pool_element"); - let is_feature = element.and_then(|e| as_str(e.get("element_type"))).as_deref() + let is_feature = element + .and_then(|e| as_str(e.get("element_type"))) + .as_deref() == Some("minecraft:feature_pool_element"); if is_feature && let Some(f) = child diff --git a/crates/horizon-world/src/datafix/fixes_mid.rs b/crates/horizon-world/src/datafix/fixes_mid.rs new file mode 100644 index 000000000..54a4f51c0 --- /dev/null +++ b/crates/horizon-world/src/datafix/fixes_mid.rs @@ -0,0 +1,1815 @@ +//! Fixes of 1.15 and 1.16 (keys 2100 to 2571): what worlds saved by 1.14.4 to +//! 1.16.5 need before the fixes of [`super::fixes_cliffs`]. +use super::dynamic::{ + as_bool, as_double, as_int, as_long, as_long_stream, as_number, as_str, ceillog2, namespaced, + remove, set, stream, string, +}; +use super::fixes_a::attributes_rename_legacy; +use super::fixes_b::advancements_rename; +use super::schema::{Key, Ty, key}; +use super::{Cx, FixFn, id_is, renamer}; +use crate::nbt::{Compound, JavaString, Tag}; + +pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + out.push((key(1800, 0), |cx| { + cx.rename( + Ty::ItemName, + renamer(&[ + ("minecraft:cactus_green", "minecraft:green_dye"), + ("minecraft:rose_red", "minecraft:red_dye"), + ("minecraft:dandelion_yellow", "minecraft:yellow_dye"), + ]), + ) + })); + out.push((key(1802, 0), |cx| { + super::fixes_a::block_rename( + cx, + &[ + ("minecraft:stone_slab", "minecraft:smooth_stone_slab"), + ("minecraft:sign", "minecraft:oak_sign"), + ("minecraft:wall_sign", "minecraft:oak_wall_sign"), + ], + ) + })); + out.push((key(1802, 0), |cx| { + cx.rename( + Ty::ItemName, + renamer(&[ + ("minecraft:stone_slab", "minecraft:smooth_stone_slab"), + ("minecraft:sign", "minecraft:oak_sign"), + ]), + ) + })); + out.push((key(1803, 0), item_lore)); + out.push((key(1904, 0), cat_split)); + out.push((key(1905, 0), |cx| { + level_status(cx, |status| { + (status == "postprocessed").then_some("fullchunk") + }) + })); + out.push((key(1911, 0), |cx| { + level_status(cx, |status| { + Some(match status { + "structure_references" | "biomes" => "empty", + "base" => "surface", + "carved" => "carvers", + "liquid_carved" => "liquid_carvers", + "decorated" => "features", + "lighted" => "light", + "mobs_spawned" => "spawn", + "finalized" => "heightmaps", + "fullchunk" => "full", + _ => "empty", + }) + }) + })); + out.push((key(1914, 0), |cx| { + cx.block_entity("minecraft:chest", |be| { + if as_str(be.get("LootTable")).as_deref() == Some("minecraft:chests/village_blacksmith") + { + set( + be, + "LootTable", + string("minecraft:chests/village/village_weaponsmith"), + ); + } + }) + })); + out.push((key(1917, 0), |cx| { + cx.entity("minecraft:cat", |e| { + if as_int(e.get("CatType"), 0) == 9 { + set(e, "CatType", Tag::Int(10)); + } + }) + })); + out.push((key(1918, 0), |cx| villager_data(cx, "minecraft:villager"))); + out.push((key(1918, 0), |cx| { + villager_data(cx, "minecraft:zombie_villager") + })); + out.push((key(1920, 0), new_village)); + out.push((key(1925, 0), |cx| { + cx.each(Ty::SavedMapIndex, |root| { + let old = std::mem::replace(root, Tag::End); + *root = Tag::compound([("data", old)]); + }) + })); + out.push((key(1928, 0), |cx| { + entity_rename(cx, "minecraft:illager_beast", "minecraft:ravager") + })); + out.push((key(1928, 0), |cx| { + cx.rename( + Ty::ItemName, + renamer(&[( + "minecraft:illager_beast_spawn_egg", + "minecraft:ravager_spawn_egg", + )]), + ) + })); + out.push((key(1946, 0), reorganize_poi)); + out.push((key(1948, 0), ominous_banner_item)); + out.push((key(1953, 0), ominous_banner_block_entity)); + out.push((key(1955, 0), villager_level_and_xp)); + out.push((key(1955, 0), zombie_villager_xp)); + out.push((key(1961, 0), |cx| { + cx.each(Ty::Chunk, |c| { + if let Some(level) = c.get_mut("Level") { + remove(level, "isLightOn"); + } + }) + })); + out.push((key(1963, 0), |cx| { + cx.entity("minecraft:villager", |e| { + map_elements_filter(e, "Gossips", |g| { + as_str(g.get("Type")).as_deref() != Some("golem") + }); + }) + })); + out.push((key(2100, 0), |cx| { + cx.rename( + Ty::Recipe, + renamer(&[("minecraft:sugar", "minecraft:sugar_from_sugar_cane")]), + ) + })); + out.push((key(2100, 0), |cx| { + advancements_rename_table( + cx, + &[( + "minecraft:recipes/misc/sugar", + "minecraft:recipes/misc/sugar_from_sugar_cane", + )], + ) + })); + out.push((key(2202, 0), chunk_biomes)); + out.push((key(2209, 0), |cx| { + let t: &[(&str, &'static str)] = &[("minecraft:bee_hive", "minecraft:beehive")]; + cx.rename(Ty::ItemName, renamer(t)); + poi_type_rename(cx, t); + })); + out.push((key(2209, 0), |cx| { + super::fixes_a::block_rename(cx, &[("minecraft:bee_hive", "minecraft:beehive")]) + })); + out.push((key(2211, 0), structure_reference_count)); + out.push((key(2218, 0), force_poi_rebuild)); + out.push((key(2501, 0), furnace_recipes)); + out.push((key(2503, 0), wall_property)); + out.push((key(2503, 0), |cx| { + advancements_rename_table( + cx, + &[( + "minecraft:recipes/misc/composter", + "minecraft:recipes/decorations/composter", + )], + ) + })); + out.push((key(2505, 0), |cx| memory_expiry(cx, "minecraft:villager"))); + out.push((key(2508, 0), |cx| cx.rename(Ty::ItemName, renamer(FUNGUS)))); + out.push((key(2508, 0), |cx| super::fixes_a::block_rename(cx, FUNGUS))); + out.push((key(2509, 0), |cx| { + entity_rename(cx, "minecraft:zombie_pigman", "minecraft:zombified_piglin") + })); + out.push((key(2509, 0), |cx| { + cx.rename( + Ty::ItemName, + renamer(&[( + "minecraft:zombie_pigman_spawn_egg", + "minecraft:zombified_piglin_spawn_egg", + )]), + ) + })); + out.push((key(2511, 0), projectile_owner)); + // `SplashPotionItemFieldRenameFix` casts the thrown potion to the type whose + // item stack field is `Item`: the typed `Potion` value takes its place, and + // writing the entity gives `Potion` back from the map it was read from. + out.push((key(2511, 1), |cx| { + cx.entity("minecraft:potion", |e| { + if let Some(item) = e.get("Potion").cloned() { + set(e, "Item", item); + } + super::drop_typed(e, "Potion"); + }) + })); + out.push((key(2514, 0), entity_uuids)); + out.push((key(2514, 0), block_entity_uuids)); + out.push((key(2514, 0), player_uuids)); + out.push((key(2514, 0), level_uuids)); + out.push((key(2514, 0), raids_uuids)); + out.push((key(2514, 0), item_stack_uuids)); + out.push((key(2516, 0), |cx| gossip_uuids(cx, "minecraft:villager"))); + out.push((key(2516, 0), |cx| { + gossip_uuids(cx, "minecraft:zombie_villager") + })); + out.push((key(2518, 0), jigsaw_properties)); + out.push((key(2518, 0), jigsaw_rotation)); + out.push((key(2523, 0), |cx| { + attributes_rename_legacy(cx, &|name| { + ATTRIBUTES + .iter() + .find(|(from, _)| *from == name) + .map(|(_, to)| *to) + }) + })); + out.push((key(2527, 0), bit_storage_align)); + out.push((key(2528, 0), |cx| { + cx.rename( + Ty::ItemName, + renamer(&[ + ("minecraft:soul_fire_torch", "minecraft:soul_torch"), + ("minecraft:soul_fire_lantern", "minecraft:soul_lantern"), + ]), + ) + })); + out.push((key(2528, 0), |cx| { + super::fixes_a::block_rename( + cx, + &[ + ("minecraft:soul_fire_torch", "minecraft:soul_torch"), + ( + "minecraft:soul_fire_wall_torch", + "minecraft:soul_wall_torch", + ), + ("minecraft:soul_fire_lantern", "minecraft:soul_lantern"), + ], + ) + })); + out.push((key(2529, 0), strider_gravity)); + out.push((key(2531, 0), redstone_wire)); + out.push((key(2533, 0), villager_follow_range)); + out.push((key(2535, 0), shulker_rotation)); + out.push((key(2537, 0), legacy_dimension_id)); + out.push((key(2538, 0), level_legacy_world_gen_settings)); + out.push((key(2550, 0), world_gen_settings)); + out.push((key(2552, 0), |cx| { + cx.rename( + Ty::Biome, + renamer(&[("minecraft:nether", "minecraft:nether_wastes")]), + ) + })); + out.push((key(2553, 0), |cx| cx.rename(Ty::Biome, renamer(BIOMES)))); +} + +const FUNGUS: &[(&str, &str)] = &[ + ("minecraft:warped_fungi", "minecraft:warped_fungus"), + ("minecraft:crimson_fungi", "minecraft:crimson_fungus"), +]; + +/// `AdvancementsRenameFix` with a `createRenamer` table. +fn advancements_rename_table(cx: &mut Cx, table: &'static [(&str, &str)]) { + advancements_rename(cx, &|id| { + let id = namespaced(id); + table + .iter() + .find(|(from, _)| *from == id) + .map(|(_, to)| (*to).to_owned()) + }); +} + +/// `SimplestEntityRenameFix`: the id of the entities and the entity names. +fn entity_rename(cx: &mut Cx, from: &str, to: &'static str) { + cx.each(Ty::Entity, |e| { + if id_is(e, from) { + set(e, "id", string(to)); + } + }); + cx.rename(Ty::EntityName, |name| { + (namespaced(name) == from).then_some(to) + }); +} + +/// `PoiTypeRenameFix`. +fn poi_type_rename(cx: &mut Cx, table: &[(&str, &'static str)]) { + let r = renamer(table); + cx.each(Ty::PoiChunk, |poi| { + let Some(Tag::Compound(sections)) = poi.get_mut("Sections") else { + return; + }; + for section in sections.values_mut() { + let Some(Tag::List(records)) = section.get_mut("Records") else { + continue; + }; + for record in records { + if let Some(t) = as_str(record.get("type")) + && let Some(new) = r(&t) + { + set(record, "type", string(new)); + } + } + } + }); +} + +/// `ChunkBiomeFix`: the 256 biomes of a column become 1024 (a 4x4x64 grid). +fn chunk_biomes(cx: &mut Cx) { + cx.each(Ty::Chunk, |c| { + let Some(level) = c.get_mut("Level") else { + return; + }; + let Some(old) = super::dynamic::as_int_stream(level.get("Biomes")) else { + return; + }; + if old.len() != 256 { + return; + } + let mut new = vec![0i32; 1024]; + for z in 0..4 { + for x in 0..4 { + let (ox, oz) = ((x << 2) + 2, (z << 2) + 2); + new[z << 2 | x] = old[oz << 4 | ox]; + } + } + for y in 1..64 { + new.copy_within(0..16, y * 16); + } + set(level, "Biomes", Tag::IntArray(new)); + }); +} + +/// `StructureReferenceCountFix`. +fn structure_reference_count(cx: &mut Cx) { + cx.each(Ty::StructureFeature, |s| { + if let Some(r) = s.get("references") { + let n = as_number(r) + .map(|n| n.int_value()) + .filter(|n| *n > 0) + .unwrap_or(1); + set(s, "references", Tag::Int(n)); + } + }); +} + +/// `ForcePoiRebuild`: every POI section loses its `Valid` flag. +fn force_poi_rebuild(cx: &mut Cx) { + cx.each(Ty::PoiChunk, |poi| { + if let Some(Tag::Compound(sections)) = poi.get_mut("Sections") { + for section in sections.values_mut() { + remove(section, "Valid"); + } + } + }); +} + +/// `FurnaceRecipeFix`: `RecipeLocation`/`RecipeAmount` pairs become the +/// `RecipesUsed` map. +fn furnace_recipes(cx: &mut Cx) { + cx.each(Ty::BlockEntity, |be| { + if ![ + "minecraft:furnace", + "minecraft:blast_furnace", + "minecraft:smoker", + ] + .iter() + .any(|id| id_is(be, id)) + { + return; + } + let size = as_int(be.get("RecipesUsedSize"), 0); + remove(be, "RecipesUsedSize"); + let mut used = Compound::new(); + for i in 0..size { + let location = remove(be, &format!("RecipeLocation{i}")); + let amount = remove(be, &format!("RecipeAmount{i}")); + let amount = as_int(amount.as_ref(), 0); + if amount > 0 + && let Some(location) = location.as_ref().and_then(|l| as_str(Some(l))) + { + used.insert( + JavaString::from(namespaced(&location).as_str()), + Tag::Int(amount), + ); + } + } + set(be, "RecipesUsed", Tag::Compound(used)); + }); +} + +const WALLS: [&str; 14] = [ + "minecraft:andesite_wall", + "minecraft:brick_wall", + "minecraft:cobblestone_wall", + "minecraft:diorite_wall", + "minecraft:end_stone_brick_wall", + "minecraft:granite_wall", + "minecraft:mossy_cobblestone_wall", + "minecraft:mossy_stone_brick_wall", + "minecraft:nether_brick_wall", + "minecraft:prismarine_wall", + "minecraft:red_nether_brick_wall", + "minecraft:red_sandstone_wall", + "minecraft:sandstone_wall", + "minecraft:stone_brick_wall", +]; + +/// `WallPropertyFix`: `true`/`false` sides become `low`/`none`. +fn wall_property(cx: &mut Cx) { + cx.each(Ty::BlockState, |state| { + let Some(name) = as_str(state.get("Name")) else { + return; + }; + if !WALLS.contains(&name.as_str()) { + return; + } + if let Some(props) = state.get_mut("Properties") { + for side in ["east", "west", "north", "south"] { + if let Some(v) = props.get(side) + && let Some(s) = as_str(Some(v)) + { + let new = if s == "true" { "low" } else { "none" }; + set(props, side, string(new)); + } + } + } + }); +} + +/// `MemoryExpiryDataFix`: every memory of a brain is wrapped as `{value: ...}`. +fn memory_expiry(cx: &mut Cx, entity: &str) { + cx.entity(entity, |e| { + let Some(brain) = e.get_mut("Brain") else { + return; + }; + if let Some(Tag::Compound(memories)) = brain.get_mut("memories") { + for value in memories.values_mut() { + let inner = std::mem::replace(value, Tag::End); + *value = Tag::compound([("value", inner)]); + } + } + }); +} + +/// `EntityProjectileOwnerFix`: the owner of a projectile as a UUID int array. +fn projectile_owner(cx: &mut Cx) { + let uuid = |most: i64, least: i64| -> Option { + (most != 0 && least != 0).then(|| { + Tag::IntArray(vec![ + (most >> 32) as i32, + most as i32, + (least >> 32) as i32, + least as i32, + ]) + }) + }; + cx.each(Ty::Entity, |e| { + let Some(id) = as_str(e.get("id")).map(|i| namespaced(&i)) else { + return; + }; + match id.as_str() { + "minecraft:egg" + | "minecraft:ender_pearl" + | "minecraft:experience_bottle" + | "minecraft:snowball" + | "minecraft:potion" => { + let (m, l) = e + .get("owner") + .map_or((0, 0), |o| (as_long(o.get("M"), 0), as_long(o.get("L"), 0))); + if let Some(u) = uuid(m, l) { + set(e, "OwnerUUID", u); + } + remove(e, "owner"); + } + "minecraft:llama_spit" => { + let (m, l) = e.get("Owner").map_or((0, 0), |o| { + ( + as_long(o.get("OwnerUUIDMost"), 0), + as_long(o.get("OwnerUUIDLeast"), 0), + ) + }); + if let Some(u) = uuid(m, l) { + set(e, "OwnerUUID", u); + } + remove(e, "Owner"); + } + "minecraft:arrow" | "minecraft:spectral_arrow" | "minecraft:trident" => { + let m = as_long(e.get("OwnerUUIDMost"), 0); + let l = as_long(e.get("OwnerUUIDLeast"), 0); + if let Some(u) = uuid(m, l) { + set(e, "OwnerUUID", u); + } + remove(e, "OwnerUUIDMost"); + remove(e, "OwnerUUIDLeast"); + } + _ => {} + } + }); +} + +// ------------------------------------------------------------------ 2514, 2516 + +fn uuid_tag(most: i64, least: i64) -> Tag { + Tag::IntArray(vec![ + (most >> 32) as i32, + most as i32, + (least >> 32) as i32, + least as i32, + ]) +} + +/// `AbstractUUIDFix.createUUIDFromLongs`. +fn uuid_from_longs(tag: &Tag, most: &str, least: &str) -> Option { + let m = as_long(tag.get(most), 0); + let l = as_long(tag.get(least), 0); + (m != 0 && l != 0).then(|| uuid_tag(m, l)) +} + +/// `AbstractUUIDFix.replaceUUIDLeastMost`. +fn replace_least_most(tag: &mut Tag, old: &str, new: &str) { + let (most, least) = (format!("{old}Most"), format!("{old}Least")); + if let Some(uuid) = uuid_from_longs(tag, &most, &least) { + remove(tag, &most); + remove(tag, &least); + set(tag, new, uuid); + } +} + +/// `AbstractUUIDFix.replaceUUIDMLTag`. +fn replace_ml(tag: &mut Tag, old: &str, new: &str) { + if let Some(uuid) = tag.get(old).and_then(|t| uuid_from_longs(t, "M", "L")) { + remove(tag, old); + set(tag, new, uuid); + } +} + +/// `UUID.fromString`, which takes five hexadecimal groups of any length. +fn parse_uuid(s: &str) -> Option<(i64, i64)> { + if s.len() > 36 { + return None; + } + let mut parts = [0i64; 5]; + let mut count = 0; + for part in s.split('-') { + if count == 5 || part.is_empty() || !part.bytes().all(|b| b.is_ascii_hexdigit()) { + return None; + } + parts[count] = i64::from_str_radix(part, 16).ok()?; + count += 1; + } + (count == 5).then(|| { + let most = (parts[0] & 0xffff_ffff) << 32 | (parts[1] & 0xffff) << 16 | (parts[2] & 0xffff); + let least = (parts[3] & 0xffff) << 48 | (parts[4] & 0xffff_ffff_ffff); + (most, least) + }) +} + +/// `AbstractUUIDFix.replaceUUIDString`. +fn replace_uuid_string(tag: &mut Tag, old: &str, new: &str) -> bool { + let Some((m, l)) = as_str(tag.get(old)).as_deref().and_then(parse_uuid) else { + return false; + }; + remove(tag, old); + set(tag, new, uuid_tag(m, l)); + true +} + +/// A field that is present becomes the list of its elements mapped by `f`. +fn map_elements(tag: &mut Tag, field: &str, mut f: impl FnMut(Tag) -> Tag) { + if let Some(v) = tag.get_mut(field) { + *v = Tag::List(stream(Some(v)).into_iter().map(&mut f).collect()); + } +} + +/// `EntityUUIDFix.updateLivingEntity`. +fn living_entity_uuids(tag: &mut Tag) { + map_elements(tag, "Attributes", |mut attribute| { + map_elements(&mut attribute, "Modifiers", |mut modifier| { + replace_least_most(&mut modifier, "UUID", "UUID"); + modifier + }); + attribute + }); +} + +fn mob_uuids(tag: &mut Tag) { + living_entity_uuids(tag); + if let Some(leash) = tag.get_mut("Leash") { + replace_least_most(leash, "UUID", "UUID"); + } +} + +fn animal_uuids(tag: &mut Tag) { + mob_uuids(tag); + replace_least_most(tag, "LoveCause", "LoveCause"); +} + +const ABSTRACT_HORSES: &[&str] = &[ + "minecraft:donkey", + "minecraft:horse", + "minecraft:llama", + "minecraft:mule", + "minecraft:skeleton_horse", + "minecraft:trader_llama", + "minecraft:zombie_horse", +]; +const TAMEABLE_ANIMALS: &[&str] = &["minecraft:cat", "minecraft:parrot", "minecraft:wolf"]; +const ANIMALS: &[&str] = &[ + "minecraft:bee", + "minecraft:chicken", + "minecraft:cow", + "minecraft:fox", + "minecraft:mooshroom", + "minecraft:ocelot", + "minecraft:panda", + "minecraft:pig", + "minecraft:polar_bear", + "minecraft:rabbit", + "minecraft:sheep", + "minecraft:turtle", + "minecraft:hoglin", +]; +const MOBS: &[&str] = &[ + "minecraft:bat", + "minecraft:blaze", + "minecraft:cave_spider", + "minecraft:cod", + "minecraft:creeper", + "minecraft:dolphin", + "minecraft:drowned", + "minecraft:elder_guardian", + "minecraft:ender_dragon", + "minecraft:enderman", + "minecraft:endermite", + "minecraft:evoker", + "minecraft:ghast", + "minecraft:giant", + "minecraft:guardian", + "minecraft:husk", + "minecraft:illusioner", + "minecraft:magma_cube", + "minecraft:pufferfish", + "minecraft:zombified_piglin", + "minecraft:salmon", + "minecraft:shulker", + "minecraft:silverfish", + "minecraft:skeleton", + "minecraft:slime", + "minecraft:snow_golem", + "minecraft:spider", + "minecraft:squid", + "minecraft:stray", + "minecraft:tropical_fish", + "minecraft:vex", + "minecraft:villager", + "minecraft:iron_golem", + "minecraft:vindicator", + "minecraft:pillager", + "minecraft:wandering_trader", + "minecraft:witch", + "minecraft:wither", + "minecraft:wither_skeleton", + "minecraft:zombie", + "minecraft:zombie_villager", + "minecraft:phantom", + "minecraft:ravager", + "minecraft:piglin", +]; +const PROJECTILES: &[&str] = &[ + "minecraft:arrow", + "minecraft:dragon_fireball", + "minecraft:firework_rocket", + "minecraft:fireball", + "minecraft:llama_spit", + "minecraft:small_fireball", + "minecraft:snowball", + "minecraft:spectral_arrow", + "minecraft:egg", + "minecraft:ender_pearl", + "minecraft:experience_bottle", + "minecraft:potion", + "minecraft:trident", + "minecraft:wither_skull", +]; + +/// `EntityUUIDFix`. +fn entity_uuids(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + let Some(id) = as_str(e.get("id")).map(|i| namespaced(&i)) else { + // Without an id no choice matches; the UUID is still fixed. + replace_least_most(e, "UUID", "UUID"); + return; + }; + let id = id.as_str(); + replace_least_most(e, "UUID", "UUID"); + if ABSTRACT_HORSES.contains(&id) || TAMEABLE_ANIMALS.contains(&id) { + animal_uuids(e); + replace_uuid_string(e, "OwnerUUID", "Owner"); + } + if ANIMALS.contains(&id) { + animal_uuids(e); + } + if MOBS.contains(&id) { + mob_uuids(e); + } + if id == "minecraft:armor_stand" { + living_entity_uuids(e); + } + if PROJECTILES.contains(&id) + && let Some(owner) = remove(e, "OwnerUUID") + { + set(e, "Owner", owner); + } + match id { + "minecraft:bee" | "minecraft:zombified_piglin" => { + replace_uuid_string(e, "HurtBy", "HurtBy"); + } + "minecraft:fox" => { + if e.get("TrustedUUIDs").is_some() { + let trusted: Vec = stream(e.get("TrustedUUIDs")) + .into_iter() + .map(|t| uuid_from_longs(&t, "M", "L").unwrap_or(t)) + .collect(); + remove(e, "TrustedUUIDs"); + set(e, "Trusted", Tag::List(trusted)); + } + } + "minecraft:item" => { + replace_ml(e, "Owner", "Owner"); + replace_ml(e, "Thrower", "Thrower"); + } + "minecraft:shulker_bullet" => { + replace_ml(e, "Owner", "Owner"); + replace_ml(e, "Target", "Target"); + } + "minecraft:area_effect_cloud" | "minecraft:evoker_fangs" => { + replace_least_most(e, "OwnerUUID", "Owner"); + } + "minecraft:zombie_villager" => { + replace_least_most(e, "ConversionPlayer", "ConversionPlayer") + } + "minecraft:piglin" => { + if let Some(angry) = e + .get_mut("Brain") + .and_then(|b| b.get_mut("memories")) + .and_then(|m| m.get_mut("minecraft:angry_at")) + { + replace_uuid_string(angry, "value", "value"); + } + } + _ => {} + } + }); +} + +/// `BlockEntityUUIDFix`. +fn block_entity_uuids(cx: &mut Cx) { + cx.each(Ty::BlockEntity, |be| { + if id_is(be, "minecraft:conduit") { + replace_ml(be, "target_uuid", "Target"); + } + if id_is(be, "minecraft:skull") + && let Some(mut owner) = remove(be, "Owner") + { + replace_uuid_string(&mut owner, "Id", "Id"); + set(be, "SkullOwner", owner); + } + }); +} + +/// `PlayerUUIDFix`. +fn player_uuids(cx: &mut Cx) { + cx.each(Ty::Player, |p| { + if let Some(vehicle) = p.get_mut("RootVehicle") { + replace_least_most(vehicle, "Attach", "Attach"); + } + living_entity_uuids(p); + replace_least_most(p, "UUID", "UUID"); + }); +} + +/// `LevelUUIDFix`. +fn level_uuids(cx: &mut Cx) { + cx.each(Ty::Level, |level| { + if let Some(Tag::Compound(dimensions)) = level.get_mut("DimensionData") { + for dimension in dimensions.values_mut() { + if let Some(fight) = dimension.get_mut("DragonFight") { + replace_least_most(fight, "DragonUUID", "Dragon"); + } + } + } + replace_uuid_string(level, "WanderingTraderId", "WanderingTraderId"); + if let Some(Tag::Compound(events)) = level.get_mut("CustomBossEvents") { + for event in events.values_mut() { + map_elements(event, "Players", |player| { + uuid_from_longs(&player, "M", "L").unwrap_or(player) + }); + } + } + }); +} + +/// `SavedDataUUIDFix` (raids). +fn raids_uuids(cx: &mut Cx) { + cx.each(Ty::SavedRaids, |saved| { + let Some(data) = saved.get_mut("data") else { + return; + }; + map_elements(data, "Raids", |mut raid| { + map_elements(&mut raid, "HeroesOfTheVillage", |hero| { + uuid_from_longs(&hero, "UUIDMost", "UUIDLeast").unwrap_or(hero) + }); + raid + }); + }); +} + +/// `ItemStackUUIDFix`. +fn item_stack_uuids(cx: &mut Cx) { + cx.each(Ty::ItemStack, |stack| { + let head = + as_str(stack.get("id")).is_some_and(|id| namespaced(&id) == "minecraft:player_head"); + let Some(tag) = stack.get_mut("tag") else { + return; + }; + map_elements(tag, "AttributeModifiers", |mut modifier| { + replace_least_most(&mut modifier, "UUID", "UUID"); + modifier + }); + if head && let Some(owner) = tag.get_mut("SkullOwner") { + replace_uuid_string(owner, "Id", "Id"); + } + }); +} + +/// `GossipUUIDFix`. +fn gossip_uuids(cx: &mut Cx, entity: &str) { + cx.entity(entity, |e| { + if let Some(gossips) = e.get_mut("Gossips") + && crate::datafix::dynamic::is_list(Some(gossips)) + { + *gossips = Tag::List( + stream(Some(gossips)) + .into_iter() + .map(|mut g| { + replace_least_most(&mut g, "Target", "Target"); + g + }) + .collect(), + ); + } + }); +} + +// ------------------------------------------------------------------ 2518 to 2535 + +/// `JigsawPropertiesFix`. +fn jigsaw_properties(cx: &mut Cx) { + cx.block_entity("minecraft:jigsaw", |be| { + let name = super::dynamic::as_string(be.get("attachement_type"), "minecraft:empty"); + let pool = super::dynamic::as_string(be.get("target_pool"), "minecraft:empty"); + set(be, "name", string(&name)); + set(be, "target", string(&name)); + remove(be, "attachement_type"); + set(be, "pool", string(&pool)); + remove(be, "target_pool"); + }); +} + +/// `LegacyJigsawRotationFix`. +fn jigsaw_rotation(cx: &mut Cx) { + cx.each(Ty::BlockState, |state| { + if as_str(state.get("Name")).is_none_or(|n| namespaced(&n) != "minecraft:jigsaw") { + return; + } + if let Some(props) = state.get_mut("Properties") { + let facing = super::dynamic::as_string(props.get("facing"), "north"); + let orientation = match facing.as_str() { + "down" => "down_south", + "up" => "up_north", + "north" => "north_up", + "south" => "south_up", + "west" => "west_up", + "east" => "east_up", + other => other, + }; + remove(props, "facing"); + set(props, "orientation", string(orientation)); + } + }); +} + +const ATTRIBUTES: &[(&str, &str)] = &[ + ("generic.maxHealth", "minecraft:generic.max_health"), + ("Max Health", "minecraft:generic.max_health"), + ( + "zombie.spawnReinforcements", + "minecraft:zombie.spawn_reinforcements", + ), + ( + "Spawn Reinforcements Chance", + "minecraft:zombie.spawn_reinforcements", + ), + ("horse.jumpStrength", "minecraft:horse.jump_strength"), + ("Jump Strength", "minecraft:horse.jump_strength"), + ("generic.followRange", "minecraft:generic.follow_range"), + ("Follow Range", "minecraft:generic.follow_range"), + ( + "generic.knockbackResistance", + "minecraft:generic.knockback_resistance", + ), + ( + "Knockback Resistance", + "minecraft:generic.knockback_resistance", + ), + ("generic.movementSpeed", "minecraft:generic.movement_speed"), + ("Movement Speed", "minecraft:generic.movement_speed"), + ("generic.flyingSpeed", "minecraft:generic.flying_speed"), + ("Flying Speed", "minecraft:generic.flying_speed"), + ("generic.attackDamage", "minecraft:generic.attack_damage"), + ( + "generic.attackKnockback", + "minecraft:generic.attack_knockback", + ), + ("generic.attackSpeed", "minecraft:generic.attack_speed"), + ( + "generic.armorToughness", + "minecraft:generic.armor_toughness", + ), +]; + +/// `BitStorageAlignFix.addPadding`: values stored without crossing longs. +pub(super) fn add_padding(size: usize, bits: usize, data: &[i64]) -> Vec { + let length = data.len(); + if length == 0 { + return Vec::new(); + } + let mask = (1i64 << bits) - 1; + let per_long = 64 / bits; + let required = size.div_ceil(per_long); + let mut result = vec![0i64; required]; + let mut out_index = 0; + let mut out_start = 0; + let mut out_data = 0i64; + let mut current_index = 0; + let mut current = data[0]; + let mut next = if length > 1 { data[1] } else { 0 }; + for index in 0..size { + let position = index * bits; + let start_data = position >> 6; + let end_data = ((index + 1) * bits - 1) >> 6; + let start_bit = position ^ (start_data << 6); + if start_data != current_index { + current = next; + next = if start_data + 1 < length { + data[start_data + 1] + } else { + 0 + }; + current_index = start_data; + } + let value = if start_data == end_data { + ((current as u64) >> start_bit) as i64 & mask + } else { + let shift = 64 - start_bit; + ((((current as u64) >> start_bit) as i64) | next.wrapping_shl(shift as u32)) & mask + }; + let out_end = out_start + bits; + if out_end >= 64 { + result[out_index] = out_data; + out_index += 1; + out_data = value; + out_start = bits; + } else { + out_data |= value << out_start; + out_start = out_end; + } + } + if out_data != 0 { + result[out_index] = out_data; + } + result +} + +fn align_storage(tag: &mut Tag, field: &str, size: usize, bits: usize) { + if let Some(v) = tag.get_mut(field) { + let input = as_long_stream(Some(v)).unwrap_or_default(); + *v = Tag::LongArray(add_padding(size, bits, &input)); + } +} + +/// `BitStorageAlignFix`: block states and heightmaps no longer cross longs. +fn bit_storage_align(cx: &mut Cx) { + cx.each(Ty::Chunk, |c| { + let Some(level) = c.get_mut("Level") else { + return; + }; + if let Some(Tag::List(sections)) = level.get_mut("Sections") { + for section in sections { + let Some(Tag::List(palette)) = section.get("Palette") else { + continue; + }; + let bits = 4.max(ceillog2(palette.len())) as usize; + if bits & (bits - 1) != 0 { + align_storage(section, "BlockStates", 4096, bits); + } + } + } + if let Some(Tag::Compound(heightmaps)) = level.get_mut("Heightmaps") { + for heightmap in heightmaps.values_mut() { + let input = as_long_stream(Some(heightmap)).unwrap_or_default(); + *heightmap = Tag::LongArray(add_padding(256, 9, &input)); + } + } + }); +} + +/// `StriderGravityFix`. +fn strider_gravity(cx: &mut Cx) { + cx.entity("minecraft:strider", |e| { + if as_bool(e.get("NoGravity"), false) { + set(e, "NoGravity", Tag::Byte(0)); + } + }); +} + +/// `RedstoneWireConnectionsFix`. +fn redstone_wire(cx: &mut Cx) { + cx.each(Ty::BlockState, |state| { + if as_str(state.get("Name")).as_deref() != Some("minecraft:redstone_wire") { + return; + } + let Some(props) = state.get_mut("Properties") else { + return; + }; + let side = |props: &Tag, name: &str| super::dynamic::as_string(props.get(name), "none"); + let (east, west, north, south) = ( + side(props, "east"), + side(props, "west"), + side(props, "north"), + side(props, "south"), + ); + let connected = |s: &str| s != "none"; + let east_west = connected(&east) || connected(&west); + let north_south = connected(&north) || connected(&south); + let fixed = [ + ( + "east", + if !connected(&east) && !north_south { + "side" + } else { + &east + }, + ), + ( + "west", + if !connected(&west) && !north_south { + "side" + } else { + &west + }, + ), + ( + "north", + if !connected(&north) && !east_west { + "side" + } else { + &north + }, + ), + ( + "south", + if !connected(&south) && !east_west { + "side" + } else { + &south + }, + ), + ]; + for (name, value) in fixed { + if props.get(name).is_some() { + set(props, name, string(value)); + } + } + }); +} + +/// `VillagerFollowRangeFix`. +fn villager_follow_range(cx: &mut Cx) { + cx.entity("minecraft:villager", |e| { + map_elements(e, "Attributes", |mut attribute| { + if super::dynamic::as_string(attribute.get("Name"), "") == "generic.follow_range" + && as_double(attribute.get("Base"), 0.0) == 16.0 + { + set(&mut attribute, "Base", Tag::Double(48.0f64.to_bits())); + } + attribute + }); + }); +} + +/// `EntityShulkerRotationFix`. +fn shulker_rotation(cx: &mut Cx) { + cx.entity("minecraft:shulker", |e| { + let mut rotation: Vec = stream(e.get("Rotation")) + .iter() + .map(|d| as_double(Some(d), 180.0)) + .collect(); + if !rotation.is_empty() { + rotation[0] -= 180.0; + set( + e, + "Rotation", + Tag::List( + rotation + .into_iter() + .map(|d| Tag::Double(d.to_bits())) + .collect(), + ), + ); + } + }); +} + +// ------------------------------------------------------------------ 2537 to 2553 + +/// `LegacyDimensionIdFix`: numeric dimensions of players and maps become names. +fn legacy_dimension_id(cx: &mut Cx) { + let fix = |v: &mut Tag, field: &str| { + if let Some(id) = v.get(field) + && let Some(n) = as_number(id) + { + let name = match n.int_value() { + -1 => "minecraft:the_nether", + 1 => "minecraft:the_end", + _ => "minecraft:overworld", + }; + set(v, field, string(name)); + } + }; + cx.each(Ty::Player, |p| fix(p, "Dimension")); + cx.each(Ty::SavedMapData, |m| { + if let Some(data) = m.get_mut("data") { + fix(data, "dimension"); + } + }); +} + +/// `LevelLegacyWorldGenSettingsFix`: the generator settings move into `WorldGenSettings`. +fn level_legacy_world_gen_settings(cx: &mut Cx) { + cx.each(Ty::Level, |data| { + let mut settings = data + .get("WorldGenSettings") + .cloned() + .unwrap_or_else(super::dynamic::empty_map); + for key in [ + "RandomSeed", + "generatorName", + "generatorOptions", + "generatorVersion", + "legacy_custom_options", + "MapFeatures", + "BonusChest", + ] { + if let Some(old) = remove(data, key) { + set(&mut settings, key, old); + } + } + set(data, "WorldGenSettings", settings); + }); +} + +fn map_of(entries: impl IntoIterator) -> Tag { + Tag::compound(entries) +} + +fn noise_generator(seed: i64, settings: Tag, biome_source: Tag) -> Tag { + map_of([ + ("type", string("minecraft:noise")), + ("biome_source", biome_source), + ("seed", Tag::Long(seed)), + ("settings", settings), + ]) +} + +fn vanilla_biome_source(seed: i64, legacy_biome_init_layer: bool, large_biomes: bool) -> Tag { + let mut source = map_of([ + ("type", string("minecraft:vanilla_layered")), + ("seed", Tag::Long(seed)), + ("large_biomes", Tag::Byte(i8::from(large_biomes))), + ]); + if legacy_biome_init_layer { + set(&mut source, "legacy_biome_init_layer", Tag::Byte(1)); + } + source +} + +fn default_overworld(seed: i64) -> Tag { + noise_generator( + seed, + string("minecraft:overworld"), + vanilla_biome_source(seed, false, false), + ) +} + +/// `WorldGenSettingsFix.vanillaLevels`. +pub(super) fn vanilla_levels(seed: i64, overworld: Tag, caves: bool) -> Tag { + let overworld_type = if caves { + "minecraft:overworld_caves" + } else { + "minecraft:overworld" + }; + map_of([ + ( + "minecraft:overworld", + map_of([("type", string(overworld_type)), ("generator", overworld)]), + ), + ( + "minecraft:the_nether", + map_of([ + ("type", string("minecraft:the_nether")), + ( + "generator", + noise_generator( + seed, + string("minecraft:nether"), + map_of([ + ("type", string("minecraft:multi_noise")), + ("seed", Tag::Long(seed)), + ("preset", string("minecraft:nether")), + ]), + ), + ), + ]), + ), + ( + "minecraft:the_end", + map_of([ + ("type", string("minecraft:the_end")), + ( + "generator", + noise_generator( + seed, + string("minecraft:end"), + map_of([ + ("type", string("minecraft:the_end")), + ("seed", Tag::Long(seed)), + ]), + ), + ), + ]), + ), + ]) +} + +/// The default `StructureFeatureConfiguration` of a structure: spacing, separation, salt. +fn structure_default(name: &str) -> (i32, i32, i32) { + match name { + "minecraft:village" => (32, 8, 10387312), + "minecraft:desert_pyramid" => (32, 8, 14357617), + "minecraft:igloo" => (32, 8, 14357618), + "minecraft:jungle_pyramid" => (32, 8, 14357619), + "minecraft:swamp_hut" => (32, 8, 14357620), + "minecraft:pillager_outpost" => (32, 8, 165745296), + "minecraft:monument" => (32, 5, 10387313), + "minecraft:endcity" => (20, 11, 10387313), + _ => (80, 20, 10387319), // minecraft:mansion + } +} + +/// `NumberUtils.toInt(s, default)` with a minimum. +fn option_int(value: &str, default: i32, min: i32) -> i32 { + value.parse::().unwrap_or(default).max(min) +} + +fn set_spacing( + config: &mut Vec<(&'static str, (i32, i32, i32))>, + structure: &'static str, + value: &str, + min: i32, +) { + let current = config + .iter() + .find(|(n, _)| *n == structure) + .map_or_else(|| structure_default(structure), |(_, c)| *c); + let spacing = option_int(value, current.0, min); + let updated = (spacing, current.1, current.2); + match config.iter_mut().find(|(n, _)| *n == structure) { + Some(entry) => entry.1 = updated, + None => config.push((structure, updated)), + } +} + +/// `WorldGenSettingsFix.fixFlatStructures`. +fn fix_flat_structures(settings: Option<&Tag>) -> Tag { + let (mut distance, mut spread, mut count) = (32, 3, 128); + let mut has_stronghold = false; + let mut config: Vec<(&'static str, (i32, i32, i32))> = Vec::new(); + if settings.is_none() { + has_stronghold = true; + config.push(("minecraft:village", structure_default("minecraft:village"))); + } + if let Some(Tag::Compound(structures)) = settings.and_then(|s| s.get("structures")) { + for (structure_key, options) in structures { + let Tag::Compound(options) = options else { + continue; + }; + let structure = structure_key.to_string_lossy(); + for (option_key, option_value) in options { + let option = option_key.to_string_lossy(); + let value = as_str(Some(option_value)).unwrap_or_default(); + if structure == "stronghold" { + has_stronghold = true; + match option.as_str() { + "distance" => distance = option_int(&value, distance, 1), + "spread" => spread = option_int(&value, spread, 1), + "count" => count = option_int(&value, count, 1), + _ => {} + } + } else { + match (option.as_str(), structure.as_str()) { + ("distance", "village") => { + set_spacing(&mut config, "minecraft:village", &value, 9) + } + ("distance", "biome_1") => { + for s in [ + "minecraft:desert_pyramid", + "minecraft:igloo", + "minecraft:jungle_pyramid", + "minecraft:swamp_hut", + "minecraft:pillager_outpost", + ] { + set_spacing(&mut config, s, &value, 9); + } + } + ("distance", "endcity") => { + set_spacing(&mut config, "minecraft:endcity", &value, 1) + } + ("distance", "mansion") => { + set_spacing(&mut config, "minecraft:mansion", &value, 1) + } + ("separation", "oceanmonument") => { + let current = config + .iter() + .find(|(n, _)| *n == "minecraft:monument") + .map_or_else( + || structure_default("minecraft:monument"), + |(_, c)| *c, + ); + let spacing = option_int(&value, current.1, 1); + let updated = (spacing, current.1, current.2); + match config.iter_mut().find(|(n, _)| *n == "minecraft:monument") { + Some(entry) => entry.1 = updated, + None => config.push(("minecraft:monument", updated)), + } + } + ("spacing", "oceanmonument") => { + set_spacing(&mut config, "minecraft:monument", &value, 1) + } + _ => {} + } + } + } + } + } + let mut structures = Compound::new(); + for (name, (spacing, separation, salt)) in config { + structures.insert( + JavaString::from(name), + map_of([ + ("spacing", Tag::Int(spacing)), + ("separation", Tag::Int(separation)), + ("salt", Tag::Int(salt)), + ]), + ); + } + let mut out = map_of([("structures", Tag::Compound(structures))]); + if has_stronghold { + set( + &mut out, + "stronghold", + map_of([ + ("distance", Tag::Int(distance)), + ("spread", Tag::Int(spread)), + ("count", Tag::Int(count)), + ]), + ); + } + out +} + +/// `WorldGenSettingsFix`: the legacy generator settings become dimensions. +fn world_gen_settings(cx: &mut Cx) { + cx.each(Ty::WorldGenSettings, |input| { + let seed = as_long(input.get("RandomSeed"), 0); + let name = as_str(input.get("generatorName")).map(|n| n.to_lowercase()); + let legacy_custom_options = as_str(input.get("legacy_custom_options")).or_else(|| { + (name.as_deref() == Some("customized")) + .then(|| as_str(input.get("generatorOptions"))) + .flatten() + }); + let mut caves = false; + let generator = match name.as_deref() { + None | Some("customized") => default_overworld(seed), + Some("flat") => { + let settings = input.get("generatorOptions"); + let layers = settings + .and_then(|s| s.get("layers")) + .cloned() + .unwrap_or_else(|| { + Tag::List( + [ + (1, "minecraft:bedrock"), + (2, "minecraft:dirt"), + (1, "minecraft:grass_block"), + ] + .into_iter() + .map(|(h, b)| map_of([("height", Tag::Int(h)), ("block", string(b))])) + .collect(), + ) + }); + map_of([ + ("type", string("minecraft:flat")), + ( + "settings", + map_of([ + ("structures", fix_flat_structures(settings)), + ("layers", layers), + ( + "biome", + string(&super::dynamic::as_string( + settings.and_then(|s| s.get("biome")), + "minecraft:plains", + )), + ), + ]), + ), + ]) + } + Some("debug_all_block_states") => map_of([("type", string("minecraft:debug"))]), + Some("buffet") => { + let settings = input.get("generatorOptions"); + let kind = settings + .and_then(|s| s.get("chunk_generator")) + .and_then(|g| as_str(g.get("type"))); + let noise_settings = match kind.as_deref() { + Some("minecraft:caves") => { + caves = true; + "minecraft:caves" + } + Some("minecraft:floating_islands") => "minecraft:floating_islands", + _ => "minecraft:overworld", + }; + let source = settings + .and_then(|s| s.get("biome_source")) + .cloned() + .unwrap_or_else(|| map_of([("type", string("minecraft:fixed"))])); + let fixed = if as_str(source.get("type")).as_deref() == Some("minecraft:fixed") { + let biome = stream(source.get("options").and_then(|o| o.get("biomes"))) + .first() + .and_then(|b| as_str(Some(b))) + .unwrap_or_else(|| "minecraft:ocean".to_owned()); + let mut fixed = source; + remove(&mut fixed, "options"); + set(&mut fixed, "biome", string(&biome)); + fixed + } else { + source + }; + noise_generator(seed, string(noise_settings), fixed) + } + Some(other) => { + let normal = other == "default"; + let legacy = other == "default_1_1" + || normal && as_int(input.get("generatorVersion"), 0) == 0; + noise_generator( + seed, + string(if other == "amplified" { + "minecraft:amplified" + } else { + "minecraft:overworld" + }), + vanilla_biome_source(seed, legacy, other == "largebiomes"), + ) + } + }; + let mut out = map_of([ + ("seed", Tag::Long(seed)), + ( + "generate_features", + Tag::Byte(i8::from(as_bool(input.get("MapFeatures"), true))), + ), + ( + "bonus_chest", + Tag::Byte(i8::from(as_bool(input.get("BonusChest"), false))), + ), + ("dimensions", vanilla_levels(seed, generator, caves)), + ]); + if let Some(options) = legacy_custom_options { + set(&mut out, "legacy_custom_options", string(&options)); + } + *input = out; + }); +} + +/// `BiomeFix.BIOMES`. +const BIOMES: &[(&str, &str)] = &[ + ("minecraft:extreme_hills", "minecraft:mountains"), + ("minecraft:swampland", "minecraft:swamp"), + ("minecraft:hell", "minecraft:nether_wastes"), + ("minecraft:sky", "minecraft:the_end"), + ("minecraft:ice_flats", "minecraft:snowy_tundra"), + ("minecraft:ice_mountains", "minecraft:snowy_mountains"), + ("minecraft:mushroom_island", "minecraft:mushroom_fields"), + ( + "minecraft:mushroom_island_shore", + "minecraft:mushroom_field_shore", + ), + ("minecraft:beaches", "minecraft:beach"), + ("minecraft:forest_hills", "minecraft:wooded_hills"), + ("minecraft:smaller_extreme_hills", "minecraft:mountain_edge"), + ("minecraft:stone_beach", "minecraft:stone_shore"), + ("minecraft:cold_beach", "minecraft:snowy_beach"), + ("minecraft:roofed_forest", "minecraft:dark_forest"), + ("minecraft:taiga_cold", "minecraft:snowy_taiga"), + ("minecraft:taiga_cold_hills", "minecraft:snowy_taiga_hills"), + ("minecraft:redwood_taiga", "minecraft:giant_tree_taiga"), + ( + "minecraft:redwood_taiga_hills", + "minecraft:giant_tree_taiga_hills", + ), + ( + "minecraft:extreme_hills_with_trees", + "minecraft:wooded_mountains", + ), + ("minecraft:savanna_rock", "minecraft:savanna_plateau"), + ("minecraft:mesa", "minecraft:badlands"), + ("minecraft:mesa_rock", "minecraft:wooded_badlands_plateau"), + ("minecraft:mesa_clear_rock", "minecraft:badlands_plateau"), + ("minecraft:sky_island_low", "minecraft:small_end_islands"), + ("minecraft:sky_island_medium", "minecraft:end_midlands"), + ("minecraft:sky_island_high", "minecraft:end_highlands"), + ("minecraft:sky_island_barren", "minecraft:end_barrens"), + ("minecraft:void", "minecraft:the_void"), + ("minecraft:mutated_plains", "minecraft:sunflower_plains"), + ("minecraft:mutated_desert", "minecraft:desert_lakes"), + ( + "minecraft:mutated_extreme_hills", + "minecraft:gravelly_mountains", + ), + ("minecraft:mutated_forest", "minecraft:flower_forest"), + ("minecraft:mutated_taiga", "minecraft:taiga_mountains"), + ("minecraft:mutated_swampland", "minecraft:swamp_hills"), + ("minecraft:mutated_ice_flats", "minecraft:ice_spikes"), + ("minecraft:mutated_jungle", "minecraft:modified_jungle"), + ( + "minecraft:mutated_jungle_edge", + "minecraft:modified_jungle_edge", + ), + ( + "minecraft:mutated_birch_forest", + "minecraft:tall_birch_forest", + ), + ( + "minecraft:mutated_birch_forest_hills", + "minecraft:tall_birch_hills", + ), + ( + "minecraft:mutated_roofed_forest", + "minecraft:dark_forest_hills", + ), + ( + "minecraft:mutated_taiga_cold", + "minecraft:snowy_taiga_mountains", + ), + ( + "minecraft:mutated_redwood_taiga", + "minecraft:giant_spruce_taiga", + ), + ( + "minecraft:mutated_redwood_taiga_hills", + "minecraft:giant_spruce_taiga_hills", + ), + ( + "minecraft:mutated_extreme_hills_with_trees", + "minecraft:modified_gravelly_mountains", + ), + ("minecraft:mutated_savanna", "minecraft:shattered_savanna"), + ( + "minecraft:mutated_savanna_rock", + "minecraft:shattered_savanna_plateau", + ), + ("minecraft:mutated_mesa", "minecraft:eroded_badlands"), + ( + "minecraft:mutated_mesa_rock", + "minecraft:modified_wooded_badlands_plateau", + ), + ( + "minecraft:mutated_mesa_clear_rock", + "minecraft:modified_badlands_plateau", + ), + ("minecraft:warm_deep_ocean", "minecraft:deep_warm_ocean"), + ( + "minecraft:lukewarm_deep_ocean", + "minecraft:deep_lukewarm_ocean", + ), + ("minecraft:cold_deep_ocean", "minecraft:deep_cold_ocean"), + ("minecraft:frozen_deep_ocean", "minecraft:deep_frozen_ocean"), +]; + +// ------------------------------------------------------------------ 1800 to 1963 + +/// `ItemLoreFix`: the lore lines become JSON text components. +fn item_lore(cx: &mut Cx) { + cx.each(Ty::ItemStack, |stack| { + let Some(lore) = stack + .get_mut("tag") + .and_then(|t| t.get_mut("display")) + .and_then(|d| d.get_mut("Lore")) + else { + return; + }; + if let Tag::List(lines) = lore { + for line in lines { + if let Tag::String(text) = line { + *line = Tag::String(JavaString(super::json::text_component_json(&text.0))); + } + } + } + }); +} + +/// `EntityCatSplitFix`: tame ocelots become cats. (The vanilla fix sets +/// `Trusting` on a copy it then drops.) +fn cat_split(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + if !id_is(e, "minecraft:ocelot") { + return; + } + let kind = as_int(e.get("CatType"), 0); + if kind > 0 && kind < 4 { + set(e, "CatType", Tag::Int(kind)); + let owner = super::dynamic::as_string(e.get("OwnerUUID"), ""); + set(e, "OwnerUUID", string(&owner)); + set(e, "id", string("minecraft:cat")); + } + }); +} + +/// The `Status` of a chunk, mapped (`ChunkStatusFix`, `ChunkStatusFix2`). +fn level_status(cx: &mut Cx, f: impl Fn(&str) -> Option<&'static str>) { + cx.each(Ty::Chunk, |c| { + let Some(level) = c.get_mut("Level") else { + return; + }; + let status = super::dynamic::as_string(level.get("Status"), "empty"); + if let Some(new) = f(&status) + && new != status + { + set(level, "Status", string(new)); + } + }); +} + +/// A list field filtered by `keep`; a present field that is no list becomes empty. +fn map_elements_filter(tag: &mut Tag, field: &str, keep: impl Fn(&Tag) -> bool) { + if let Some(v) = tag.get_mut(field) { + *v = Tag::List(stream(Some(v)).into_iter().filter(|e| keep(e)).collect()); + } +} + +/// `VillagerDataFix`: profession and career become `VillagerData`. +fn villager_data(cx: &mut Cx, entity: &str) { + cx.entity(entity, |e| { + let profession = as_int(e.get("Profession"), 0); + let career = as_int(e.get("Career"), 0); + let profession = match (profession, career) { + (0, 2) => "minecraft:fisherman", + (0, 3) => "minecraft:shepherd", + (0, 4) => "minecraft:fletcher", + (0, _) => "minecraft:farmer", + (1, 2) => "minecraft:cartographer", + (1, _) => "minecraft:librarian", + (2, _) => "minecraft:cleric", + (3, 2) => "minecraft:weaponsmith", + (3, 3) => "minecraft:toolsmith", + (3, _) => "minecraft:armorer", + (4, 2) => "minecraft:leatherworker", + (4, _) => "minecraft:butcher", + (5, _) => "minecraft:nitwit", + _ => "minecraft:none", + }; + let level = e.get("CareerLevel").cloned().unwrap_or(Tag::Int(1)); + remove(e, "Profession"); + remove(e, "Career"); + remove(e, "CareerLevel"); + set( + e, + "VillagerData", + Tag::compound([ + ("type", string("minecraft:plains")), + ("profession", string(profession)), + ("level", level), + ]), + ); + }); +} + +/// `NewVillageFix`: the new village structure takes the old one's name. +fn new_village(cx: &mut Cx) { + cx.each(Ty::Chunk, |c| { + let Some(structures) = c.get_mut("Level").and_then(|l| l.get_mut("Structures")) else { + return; + }; + if let Some(Tag::Compound(starts)) = structures.get_mut("Starts") { + starts.remove(&JavaString::from("Village")); + if let Some(v) = starts.remove(&JavaString::from("New_Village")) { + starts.insert(JavaString::from("Village"), v); + } + } + if let Some(references) = structures.get_mut("References") { + if let Some(v) = remove(references, "New_Village") { + set(references, "Village", v); + } + remove(references, "Village"); + } + }); + cx.each(Ty::StructureFeature, |s| { + if let Some(id) = s.get("id") { + let id = as_str(Some(id)).unwrap_or_default(); + if namespaced(&id) == "minecraft:new_village" { + set(s, "id", string("minecraft:village")); + } + } + }); +} + +/// `ReorganizePoi`: the sections `0` to `15` move below `Sections`. +fn reorganize_poi(cx: &mut Cx) { + cx.each(Ty::PoiChunk, |poi| { + let mut sections = Compound::new(); + for i in 0..16 { + let name = i.to_string(); + if let Some(records) = remove(poi, &name) { + sections.insert( + JavaString::from(name.as_str()), + Tag::compound([("Records", records)]), + ); + } + } + set(poi, "Sections", Tag::Compound(sections)); + }); +} + +const ILLAGER_BANNER: &str = "\"translate\":\"block.minecraft.illager_banner\""; +const OMINOUS_BANNER: &str = "\"translate\":\"block.minecraft.ominous_banner\""; + +/// `OminousBannerRenameFix`. +fn ominous_banner_item(cx: &mut Cx) { + cx.item( + |id| id == "minecraft:white_banner", + |stack| { + if let Some(name) = stack + .get_mut("tag") + .and_then(|t| t.get_mut("display")) + .and_then(|d| d.get_mut("Name")) + && let Some(text) = as_str(Some(name)) + { + *name = string(&text.replace(ILLAGER_BANNER, OMINOUS_BANNER)); + } + }, + ); +} + +/// `OminousBannerBlockEntityRenameFix`. +fn ominous_banner_block_entity(cx: &mut Cx) { + cx.block_entity("minecraft:banner", |be| { + if let Some(name) = be.get_mut("CustomName") + && let Some(text) = as_str(Some(name)) + { + *name = string(&text.replace(ILLAGER_BANNER, OMINOUS_BANNER)); + } + }); +} + +const LEVEL_XP_THRESHOLDS: [i32; 5] = [0, 10, 50, 100, 150]; + +fn min_xp_per_level(level: i32) -> i32 { + LEVEL_XP_THRESHOLDS[(level - 1).clamp(0, 4) as usize] +} + +/// `VillagerRebuildLevelAndXpFix`. +fn villager_level_and_xp(cx: &mut Cx) { + cx.entity("minecraft:villager", |e| { + let mut level = as_int(e.get("VillagerData").and_then(|d| d.get("level")), 0); + if level == 0 || level == 1 { + let offers = e + .get("Offers") + .and_then(|o| o.get("Recipes")) + .map_or(0, |r| stream(Some(r)).len() as i32); + level = (offers / 2).clamp(1, 5); + if level > 1 + && let Some(data) = e.get_mut("VillagerData") + { + set(data, "level", Tag::Int(level)); + } + } + if e.get("Xp").and_then(as_number).is_none() { + set(e, "Xp", Tag::Int(min_xp_per_level(level))); + } + }); +} + +/// `ZombieVillagerRebuildXpFix`. +fn zombie_villager_xp(cx: &mut Cx) { + cx.entity("minecraft:zombie_villager", |e| { + if e.get("Xp").and_then(as_number).is_none() { + let level = as_int(e.get("VillagerData").and_then(|d| d.get("level")), 1); + set(e, "Xp", Tag::Int(min_xp_per_level(level))); + } + }); +} diff --git a/crates/horizon-world/src/datafix/mod.rs b/crates/horizon-world/src/datafix/mod.rs index 5ebcfbc9c..eca0de51b 100644 --- a/crates/horizon-world/src/datafix/mod.rs +++ b/crates/horizon-world/src/datafix/mod.rs @@ -19,6 +19,7 @@ mod fixes_b; mod fixes_c; mod fixes_cliffs; mod fixes_flat; +mod fixes_mid; mod fixes_old; mod flat_tables; mod items; @@ -183,6 +184,7 @@ type FixFn = fn(&mut Cx); fn fixes() -> Vec<(Key, FixFn)> { let mut out = Vec::new(); fixes_flat::register(&mut out); + fixes_mid::register(&mut out); fixes_cliffs::register(&mut out); fixes_old::register(&mut out); fixes_a::register(&mut out); diff --git a/crates/horizon-world/src/datafix/schema.rs b/crates/horizon-world/src/datafix/schema.rs index f3c4160f4..b8a7f67fb 100644 --- a/crates/horizon-world/src/datafix/schema.rs +++ b/crates/horizon-world/src/datafix/schema.rs @@ -426,7 +426,11 @@ impl Walker<'_> { "minecraft:item" | "minecraft:item_frame" | "minecraft:glow_item_frame" => { self.field(Ty::ItemStack, v, "Item"); } - "minecraft:potion" if k < key(4306, 0) => self.field(Ty::ItemStack, v, "Item"), + "minecraft:potion" if k < key(4306, 0) => { + // V2511_1 renamed the typed field. + let field = if k < key(2511, 1) { "Potion" } else { "Item" }; + self.field(Ty::ItemStack, v, field); + } "minecraft:splash_potion" | "minecraft:lingering_potion" if k >= key(4306, 0) => { self.field(Ty::ItemStack, v, "Item"); } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java index d8a0a235a..a489ccbe6 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java @@ -266,7 +266,11 @@ public final class VanillaDataFixOracle { if (type == null) { continue; } - CompoundTag tag = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap()); + // SavedDataStorage.readTagFromDisk: a file that does not start with the gzip header is plain NBT. + byte[] bytes = Files.readAllBytes(file); + boolean gzip = bytes.length >= 2 && (bytes[0] & 255) == 0x1f && (bytes[1] & 255) == 0x8b; + CompoundTag tag = gzip ? NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap()) + : NbtIo.read(new java.io.DataInputStream(new java.io.ByteArrayInputStream(bytes))); int version = NbtUtils.getDataVersion(tag, 1343); tag = type.update(fixer, tag, version, current); NbtIo.writeCompressed(NbtUtils.addDataVersion(tag, current), file); diff --git a/tools/datafix_scene.py b/tools/datafix_scene.py index 0f1861487..7f62510bc 100644 --- a/tools/datafix_scene.py +++ b/tools/datafix_scene.py @@ -138,6 +138,9 @@ def experiments(version): def items(e, version=None): """The sample stacks of an era, in the exact component formats `version` accepts.""" stacks = ITEMS_A if e == A else ITEMS_B if e == B else ITEMS_C + if version is not None and version < 2586: # 1.13.2 to 1.15 + import datafix_scene_old + stacks = stacks + datafix_scene_old.items(version) if version is None: return stacks return [v for v in (versioned(s, version) for s in stacks) if available(v, version)] @@ -237,6 +240,9 @@ def block_entities(e, version): out = [(b, '{SpawnData:{id:"minecraft:zombie",ArmorItems:[{},{},{},{id:"minecraft:iron_helmet",Count:1b}]},' 'SpawnPotentials:[{Weight:1,Entity:{id:"minecraft:skeleton"}}]}') if b == 'spawner' else (b, nbt) for b, nbt in out] + if version < 2586: + import datafix_scene_old + out += datafix_scene_old.block_entities(version) if version >= 3682: out.append(('crafter', '{Items:[{Slot:0b,id:"minecraft:stone",Count:1b}],triggered:1,crafting_ticks_remaining:2}')) # Blocks the 1.20 experiment brought in 1.19.3 and 1.19.4. @@ -312,6 +318,9 @@ def entities(e, version): ] since = {'allay': 3105, 'chest_boat': 3105, 'text_display': 3325, 'item_display': 3325, 'goat': 2700} out = [(k, x) for k, x in out if version >= since.get(k, 0)] + if version < 2514: # 1.13.2 to 1.15: entities in the formats those versions write + import datafix_scene_old + out += datafix_scene_old.entities(version) if version < 3086: # cats before 1.19 have a numeric CatType out = [(k, 'CatType:1' if k == 'cat' else x) for k, x in out] if 3105 <= version < 3322: # darkness counts its factor from a timestamp before 1.19.4 @@ -363,6 +372,34 @@ def entities(e, version): return out +def world_commands(version): + """(command, seconds to wait) for the scoreboard, a boss bar and a trip through the other + dimensions, before 1.17: what makes level.dat, the scoreboard and the other dimension folders carry data.""" + if version >= 2586: + return [] + return [ + ('bossbar add upgrade:boss {"text":"Boss"}', 0), + ('bossbar set upgrade:boss players @a', 0), + ('scoreboard objectives add kills dummy {"text":"Kills"}', 0), + ('scoreboard objectives add zombies minecraft.killed:minecraft.zombie', 0), + ('scoreboard players set Upgrader kills 3', 0), + ('team add red {"text":"Red"}', 0), + ('team modify red prefix {"text":"[R]"}', 0), + ('team join red Upgrader', 0), + ('advancement grant @a only minecraft:story/root', 0), + ('execute in minecraft:the_end run tp Upgrader 100 60 0', 20), + ('execute in minecraft:the_nether run tp Upgrader 0 80 0', 15), + ] + + +def blocks(version): + """Plain blocks whose states changed over the versions.""" + if version < 2586: + import datafix_scene_old + return datafix_scene_old.blocks(version) + return [] + + def player_commands(e, name, version=None): """Give the joined bot a varied inventory and ender chest.""" out = [] diff --git a/tools/datafix_scene_old.py b/tools/datafix_scene_old.py new file mode 100644 index 000000000..fe670c50d --- /dev/null +++ b/tools/datafix_scene_old.py @@ -0,0 +1,119 @@ +"""Scene additions for worlds saved by 1.13.2 to 1.16.5 (DataVersion 1631 to 2586): the entities, block +entities and items whose save format those versions changed, in the syntax and format of the version +that writes them (numeric UUIDMost/UUIDLeast, string owner UUIDs, Profession/Career villagers, ...). + +Every entry is (minimum DataVersion, maximum DataVersion exclusive, value); datafix_scene.py adds the +ones valid for the world's version to its general scene. +""" + +U1 = '11111111-2222-3333-4444-555555555555' +U2 = '66666666-7777-8888-9999-aaaaaaaaaaaa' +MODIFIERS = ('[{Name:"horse.jumpStrength",Base:0.7d},{Name:"generic.maxHealth",Base:20.0d,' + 'Modifiers:[{Name:"m",Amount:1.0d,Operation:0,UUIDMost:5L,UUIDLeast:6L}]}]') +TRADES = ('{buy:{id:"minecraft:wheat",Count:20b},sell:{id:"minecraft:emerald",Count:1b},maxUses:16,uses:1,rewardExp:1b},' + '{buy:{id:"minecraft:paper",Count:24b},sell:{id:"minecraft:emerald",Count:1b},maxUses:16,uses:0},' + '{buy:{id:"minecraft:book",Count:4b},sell:{id:"minecraft:emerald",Count:1b},maxUses:12,uses:0},' + '{buy:{id:"minecraft:ink_sac",Count:5b},sell:{id:"minecraft:emerald",Count:1b},maxUses:12,uses:0}') + +# (since, until, entity, fields) +ENTITIES = [ + # 1.13.2: professions are numbers + (1631, 1900, 'villager', 'Profession:1,Career:2,CareerLevel:2,Offers:{Recipes:[%s]},Inventory:[{id:"minecraft:bread",Count:3b}]' % TRADES), + (1631, 1900, 'villager', 'Profession:3,Career:3,CareerLevel:1'), + (1631, 1900, 'zombie_villager', 'Profession:4,Career:2'), + (1631, 1900, 'ocelot', 'CatType:2,OwnerUUID:"%s"' % U1), + (1631, 1900, 'ocelot', 'CatType:0'), + (1631, 1900, 'ocelot', 'CatType:0,OwnerUUID:"%s"' % U2), + # 1.14: VillagerData and Xp; some without them + (1900, 2100, 'villager', 'VillagerData:{profession:"minecraft:farmer",level:1,type:"minecraft:plains"},' + 'Offers:{Recipes:[%s]},Gossips:[{Type:"golem",Value:5},{Type:"major_positive",Value:2}]' % TRADES), + (1900, 2100, 'villager', 'VillagerData:{profession:"minecraft:armorer",level:3,type:"minecraft:desert"},Xp:70'), + (1900, 2100, 'zombie_villager', 'VillagerData:{profession:"minecraft:cleric",level:2,type:"minecraft:plains"}'), + (1900, 2100, 'cat', 'CatType:9'), + (1900, 2100, 'cat', 'CatType:3,CollarColor:5b'), + (1900, 2100, 'pillager', 'Inventory:[{id:"minecraft:arrow",Count:2b}]'), + (1900, 2100, 'fox', 'Type:"red",TrustedUUIDs:[{M:3L,L:4L}]'), + (1631, 2514, 'wolf', 'Owner:"%s",OwnerUUID:"%s",CollarColor:3b,Health:15.0f' % ('Steve', U1)), + (1631, 2514, 'horse', 'Tame:1b,OwnerUUID:"%s",SaddleItem:{id:"minecraft:saddle",Count:1b},' + 'ArmorItem:{id:"minecraft:iron_horse_armor",Count:1b},Attributes:%s' % (U2, MODIFIERS)), + (1631, 2514, 'armor_stand', 'Attributes:%s' % MODIFIERS), + (1631, 2514, 'cow', 'Leash:{UUIDMost:7L,UUIDLeast:8L}'), + (1631, 2509, 'zombie_pigman', 'Anger:100s,HurtBy:"%s"' % U1), + (1631, 2514, 'item', 'Item:{id:"minecraft:diamond",Count:3b},Owner:{M:9L,L:10L},Thrower:{M:11L,L:12L}'), + (1631, 2514, 'area_effect_cloud', 'OwnerUUIDMost:13L,OwnerUUIDLeast:14L,Particle:"entity_effect",Radius:2.0f,Duration:600'), + (1631, 2514, 'arrow', 'OwnerUUIDMost:15L,OwnerUUIDLeast:16L,inGround:1b'), + (1631, 2514, 'trident', 'OwnerUUIDMost:17L,OwnerUUIDLeast:18L,Trident:{id:"minecraft:trident",Count:1b}'), + (1631, 2514, 'potion', 'Potion:{id:"minecraft:splash_potion",Count:1b,tag:{Potion:"minecraft:swiftness"}},owner:{M:19L,L:20L}'), + (1631, 2514, 'snowball', 'owner:{M:21L,L:22L}'), + (1631, 2514, 'egg', 'owner:{M:23L,L:24L}'), + (1631, 2514, 'ender_pearl', 'owner:{M:25L,L:26L}'), + (1631, 2514, 'experience_bottle', 'owner:{M:27L,L:28L}'), + (1631, 2514, 'llama_spit', 'Owner:{OwnerUUIDMost:29L,OwnerUUIDLeast:30L}'), + (1631, 2514, 'shulker_bullet', 'Owner:{M:31L,L:32L},Target:{M:33L,L:34L}'), + (1631, 2514, 'evoker_fangs', 'OwnerUUIDMost:35L,OwnerUUIDLeast:36L'), + (1631, 2514, 'shulker', 'Rotation:[40.0f,0.0f],Peek:0b'), + (1631, 2514, 'bat', ''), + (2100, 2514, 'bee', 'HurtBy:"%s",HasNectar:1b,Anger:50' % U1), + (2100, 2514, 'villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"},Xp:20,' + 'Brain:{memories:{"minecraft:job_site":{pos:[I;1,2,3],dimension:"minecraft:overworld"}}}'), +] + +# (since, until, block, nbt) +BLOCK_ENTITIES = [ + (1631, 2514, 'white_banner', '{CustomName:\'{"translate":"block.minecraft.illager_banner","color":"gold"}\',' + 'Patterns:[{Pattern:"cre",Color:15}]}'), + (1631, 2514, 'chest', '{LootTable:"minecraft:chests/village_blacksmith"}'), + (1631, 2514, 'furnace', '{BurnTime:100s,CookTime:20s,CookTimeTotal:200s,RecipesUsedSize:2,RecipeLocation0:"minecraft:iron_ingot",' + 'RecipeAmount0:3,RecipeLocation1:"minecraft:glass",RecipeAmount1:1}'), + (1631, 2514, 'blast_furnace', '{RecipesUsedSize:1,RecipeLocation0:"minecraft:iron_ingot",RecipeAmount0:2}') if False else None, + (1631, 2514, 'player_head', '{Owner:{Id:"%s",Name:"Notch"}}' % U1), + (1631, 2514, 'conduit', '{target_uuid:{M:5L,L:6L},Target:{M:5L,L:6L}}'), + (1631, 2514, 'oak_sign', '{Text1:\'{"text":"one"}\',Text2:\'{"text":"two"}\'}'), + (1900, 2514, 'jigsaw[facing=up]', '{attachement_type:"minecraft:bottom",target_pool:"minecraft:village/plains/houses",' + 'final_state:"minecraft:air"}'), + (1900, 2514, 'jigsaw[facing=east]', '{attachement_type:"minecraft:empty",target_pool:"minecraft:empty"}'), + (1900, 2514, 'lectern', '{Book:{id:"minecraft:written_book",Count:1b,tag:{title:"L",author:"A",pages:[\'"x"\']}},Page:0}'), + (2100, 2514, 'beehive', '{Bees:[{EntityData:{id:"minecraft:bee"},TicksInHive:3,MinOccupationTicks:600}]}'), +] +BLOCK_ENTITIES = [b for b in BLOCK_ENTITIES if b] + +# Plain blocks whose states changed: (since, until, command tail after "setblock x y z") +BLOCKS = [ + (1900, 2500, 'cobblestone_wall[east=true,west=false,north=true,south=false]'), + (1900, 2500, 'andesite_wall[east=true,west=true,north=false,south=false,up=true]'), + (1631, 2500, 'redstone_wire[east=none,west=none,north=none,south=none,power=0]'), + (1631, 2500, 'redstone_wire[east=up,west=none,north=none,south=none,power=3]'), + (1631, 2500, 'redstone_wire[east=none,west=none,north=side,south=none,power=1]'), + (1631, 2500, 'oak_sign[rotation=3]'), + (1631, 1900, 'stone_slab[type=bottom]'), + (1631, 2100, 'bee_nest') if False else None, +] +BLOCKS = [b for b in BLOCKS if b] + +ITEMS = [ + (1631, 2514, '{id:"minecraft:white_banner",Count:1b,tag:{display:{Name:\'{"translate":"block.minecraft.illager_banner","color":"gold"}\',' + 'Lore:[\'first\',\'{"text":"second"}\']},BlockEntityTag:{Patterns:[{Pattern:"cre",Color:15}]}}}'), + (1631, 2514, '{id:"minecraft:player_head",Count:1b,tag:{SkullOwner:{Id:"%s",Name:"Notch"}}}' % U2), + (1631, 2514, '{id:"minecraft:diamond_sword",Count:1b,tag:{AttributeModifiers:[{AttributeName:"generic.attackDamage",Name:"x",Amount:2.5d,' + 'Operation:0,UUIDMost:41L,UUIDLeast:42L,Slot:"mainhand"}]}}'), + (1631, 2514, '{id:"minecraft:filled_map",Count:1b,tag:{map:0}}'), + (1631, 2514, '{id:"minecraft:cactus_green",Count:1b}') if False else None, + (1631, 2514, '{id:"minecraft:stone_slab",Count:2b}') if False else None, +] +ITEMS = [i for i in ITEMS if i] + + +def entities(version): + return [(kind, fields) for since, until, kind, fields in ENTITIES if since <= version < until] + + +def block_entities(version): + return [(block, nbt) for since, until, block, nbt in BLOCK_ENTITIES if since <= version < until] + + +def blocks(version): + return [block for since, until, block in BLOCKS if since <= version < until] + + +def items(version): + return [stack for since, until, stack in ITEMS if since <= version < until] diff --git a/tools/datafix_worlds.py b/tools/datafix_worlds.py index 1b944a0c0..3c1f0b027 100644 --- a/tools/datafix_worlds.py +++ b/tools/datafix_worlds.py @@ -38,8 +38,23 @@ LOCAL = ROOT / '.local' / 'datafix' # Old server jars and their worlds are inputs, not outputs: keep them beside the # vanilla data, which parallel worktrees share (.local/minecraft links there). SHARED = (ROOT / '.local' / 'minecraft').resolve().parent / 'datafix' -VERSIONS = ['1.12.2', '1.13.2', '1.14.4', '1.16.5', '1.17.1', '1.18.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2', '1.20.4', '1.20.5', '1.20.6', '1.21.1', +VERSIONS = ['1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.1', '1.16.5', '1.17.1', '1.18.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2', '1.20.4', '1.20.5', '1.20.6', '1.21.1', '1.21.3', '1.21.4', '1.21.5', '1.21.6', '1.21.8', '1.21.9', '1.21.10', '1.21.11', '26.1', '26.1.2', '26.2'] +# Extra worlds of one version with another world type, for the level.dat generator settings: +# name -> (version, server.properties). They get no scene, only a joined player. +VARIANTS = { + '1.13.2-flat': ('1.13.2', {'level-type': 'flat', 'generator-settings': + '3;minecraft:bedrock,2*minecraft:dirt,minecraft:grass_block;1;village,oceanmonument,mansion(distance=30)'}), + '1.14.4-flat': ('1.14.4', {'level-type': 'flat', 'generator-settings': + '3;minecraft:bedrock,3*minecraft:stone,minecraft:grass_block;2;stronghold(distance=20 count=5),village(distance=12),' + 'oceanmonument(spacing=10 separation=4)'}), + '1.14.4-largebiomes': ('1.14.4', {'level-type': 'largebiomes'}), + '1.14.4-amplified': ('1.14.4', {'level-type': 'amplified'}), + '1.13.2-debug': ('1.13.2', {'level-type': 'debug_all_block_states'}), + '1.14.4-buffet': ('1.14.4', {'level-type': 'buffet', 'generator-settings': + '{"chunk_generator":{"type":"minecraft:surface"},"biome_source":{"type":"minecraft:fixed","options":' + '{"biomes":["minecraft:desert"]}}}'}), +} MANIFEST = 'https://piston-meta.mojang.com/mc/game/version_manifest_v2.json' LOCK = json.loads((ROOT / 'research-lock.json').read_text())['java_runtimes'] BOT = 'Upgrader' @@ -137,7 +152,7 @@ def install_runtime(pin, home): class Server: - def __init__(self, directory, jar, port, packs=(), java='java'): + def __init__(self, directory, jar, port, packs=(), java='java', props_override=None): directory.mkdir(parents=True, exist_ok=True) (directory / 'eula.txt').write_text('eula=true\n') props = {'server-port': port, 'online-mode': 'false', 'level-name': 'world', 'level-seed': '4242', @@ -145,6 +160,7 @@ class Server: 'enable-rcon': 'false', 'enable-query': 'false', 'max-tick-time': '-1'} if packs: props['initial-enabled-packs'] = ','.join(['vanilla', *packs]) + props.update(props_override or {}) (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) self.process = subprocess.Popen([java, '-Xmx2G', '-jar', str(jar), 'nogui'], cwd=directory, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) @@ -349,17 +365,55 @@ def uuid_nbt(i): return f'[I;{UUID_BASE},0,0,{i}]' -def build(version, force=False): +def uuid_fields(data_version, i): + """The UUID of a summoned entity: an int array since 1.16, two longs before.""" + if data_version >= 2514: + return f'UUID:{uuid_nbt(i)}' + return f'UUIDMost:{UUID_BASE << 32}L,UUIDLeast:{i + 1}L' + + +def build_scene(server, run, Bot, port, protocol, props, era, data_version): + """The scene of the generic generator; returns the bot that stays joined.""" + if not props: # variants only differ in their world type + run('forceload add -48 -48 79 79') + run('fill 0 99 0 47 99 47 minecraft:stone') + run('fill 0 100 0 47 112 47 minecraft:air') + for i, (block, nbt) in enumerate(scene.block_entities(era, data_version)): + x, z = 1 + 3 * (i % 15), 1 + 3 * (i // 15) + run(f'setblock {x} 100 {z} minecraft:{block}{nbt}') + for i, (kind, extra) in enumerate(scene.entities(era, data_version)): + x, z = 1.5 + 3 * (i % 15), 25.5 + 3 * (i // 15) + fields = f'{uuid_fields(data_version, i)},NoAI:1b,Silent:1b,NoGravity:1b,Invulnerable:1b,PersistenceRequired:1b' + run(f'summon minecraft:{kind} {x} 100 {z} {{{fields}' + (',' + extra if extra else '') + '}') + for i, block in enumerate(scene.blocks(data_version)): + run(f'setblock {1 + 3 * (i % 15)} 100 {19 + 3 * (i // 15)} minecraft:{block}') + if not props: + run('setworldspawn 20 101 20') + bot = Bot(port, protocol, BOT) + time.sleep(3) + for command in scene.player_commands(era, BOT, data_version): + run(command) + time.sleep(3) + for command, wait in ([] if props else scene.world_commands(data_version)): + run(command) + time.sleep(wait) + run('save-all flush') + time.sleep(2) + return bot + + +def build(name, force=False): + version, props = VARIANTS.get(name, (name, None)) jar, data_version, protocol = server_jar(version) - target = SHARED / 'worlds' / version + target = SHARED / 'worlds' / name if target.exists() and not force: print(f'{version}: kept {target}') return [] era = scene.era(data_version) - work = LOCAL / 'gen' / version + work = LOCAL / 'gen' / name shutil.rmtree(work, ignore_errors=True) port = 30000 + (os.getpid() + data_version) % 20000 - server = Server(work, jar, port, scene.experiments(data_version), java(version)) + server = Server(work, jar, port, scene.experiments(data_version), java(version), props) problems = [] def run(command): @@ -369,31 +423,14 @@ def build(version, force=False): problems.append((command[:160], bad[:2])) return out - if data_version < 1631: # 1.12 and older: tools/datafix_scene_12.py builds the scene and saves the world itself + if data_version < 1631 and not props: # 1.12 and older: tools/datafix_scene_12.py builds the scene and saves the world itself bot = scene12.populate(server, Bot, port, protocol, BOT, problems) else: - run('forceload add -48 -48 79 79') - run('fill 0 99 0 47 99 47 minecraft:stone') - run('fill 0 100 0 47 112 47 minecraft:air') - for i, (block, nbt) in enumerate(scene.block_entities(era, data_version)): - x, z = 1 + 3 * (i % 15), 1 + 3 * (i // 15) - run(f'setblock {x} 100 {z} minecraft:{block}{nbt}') - for i, (kind, extra) in enumerate(scene.entities(era, data_version)): - x, z = 1.5 + 3 * (i % 15), 25.5 + 3 * (i // 15) - fields = f'UUID:{uuid_nbt(i)},NoAI:1b,Silent:1b,NoGravity:1b,Invulnerable:1b,PersistenceRequired:1b' - run(f'summon minecraft:{kind} {x} 100 {z} {{{fields}' + (',' + extra if extra else '') + '}') - run('setworldspawn 20 101 20') - bot = Bot(port, protocol, BOT) - time.sleep(3) - for command in scene.player_commands(era, BOT, data_version): - run(command) - time.sleep(3) - run('save-all flush') - time.sleep(2) + bot = build_scene(server, run, Bot, port, protocol, props, era, data_version) bot.leave() time.sleep(2) server.stop() - if data_version < 1631: + if data_version < 1631 and not props: scene12.fixtures(work / 'world') # the few statistics a passive bot cannot cause, written by hand # Copy aside, then rename: an interrupted copy must not look like a finished world. partial = target.with_name(target.name + '.partial') @@ -415,7 +452,7 @@ def main(): p.add_argument('versions', nargs='*') p.add_argument('--force', action='store_true') args = p.parse_args() - for version in args.versions or VERSIONS: + for version in args.versions or [*VERSIONS, *VARIANTS]: build(version, args.force) if version == '1.12.2': scene12.build_flat(sys.modules[__name__], args.force) # and the flat world 1.12.2-flat diff --git a/tools/test_datafix_parity.py b/tools/test_datafix_parity.py index 471fccaea..a07bf5557 100644 --- a/tools/test_datafix_parity.py +++ b/tools/test_datafix_parity.py @@ -214,7 +214,7 @@ def main(): WORLDS.parent.mkdir(parents=True, exist_ok=True) with open(WORLDS.parent / 'generate.lock', 'w') as lock: fcntl.flock(lock, fcntl.LOCK_EX) # another worktree may be generating them - missing = [v for v in datafix_worlds.VERSIONS if not (WORLDS / v / 'level.dat').exists()] + missing = [v for v in [*datafix_worlds.VERSIONS, *datafix_worlds.VARIANTS] if not (WORLDS / v / 'level.dat').exists()] if missing: print('generating sample worlds:', ' '.join(missing), flush=True) subprocess.run([sys.executable, str(ROOT / 'tools' / 'datafix_worlds.py'), *missing], cwd=ROOT, check=True)