diff --git a/crates/horizon-world/examples/datafix_world.rs b/crates/horizon-world/examples/datafix_world.rs new file mode 100644 index 000000000..f9e0a1725 --- /dev/null +++ b/crates/horizon-world/examples/datafix_world.rs @@ -0,0 +1,45 @@ +//! Upgrade a copy of a world saved by 1.20 to 26.2 to the 26.3 format, as +//! vanilla 26.3 does when it opens it and reads every file: +//! `datafix_world IN_WORLD OUT_WORLD` (used by tools/test_datafix_parity.py). +use horizon_world::datafix::files; +use std::{env, fs, io, path::Path, process::ExitCode}; + +fn copy(from: &Path, to: &Path) -> io::Result<()> { + fs::create_dir_all(to)?; + for entry in fs::read_dir(from)? { + let entry = entry?; + let target = to.join(entry.file_name()); + if entry.file_type()?.is_dir() { + copy(&entry.path(), &target)?; + } else if entry.file_name() != "session.lock" { + fs::copy(entry.path(), target)?; + } + } + Ok(()) +} + +fn main() -> ExitCode { + let args: Vec = env::args().skip(1).collect(); + let [input, output] = args.as_slice() else { + eprintln!("usage: datafix_world IN_WORLD OUT_WORLD"); + return ExitCode::from(2); + }; + let output = Path::new(output); + let result = (|| { + if output.exists() { + fs::remove_dir_all(output)?; + } + copy(Path::new(input), output)?; + files::upgrade_world(output) + })(); + match result { + Ok(version) => { + println!("upgraded {} from DataVersion {version}", output.display()); + ExitCode::SUCCESS + } + Err(e) => { + eprintln!("datafix_world: {e}"); + ExitCode::FAILURE + } + } +} diff --git a/crates/horizon-world/src/datafix/dynamic.rs b/crates/horizon-world/src/datafix/dynamic.rs new file mode 100644 index 000000000..88e089438 --- /dev/null +++ b/crates/horizon-world/src/datafix/dynamic.rs @@ -0,0 +1,238 @@ +//! What DataFixerUpper's `Dynamic` over `NbtOps` does with values: number and +//! string views, maps and lists, field edits. Fixes use these to stay close to +//! the Java they port. +use crate::nbt::{Compound, JavaString, Tag}; + +pub fn key(s: &str) -> JavaString { + JavaString::from(s) +} +pub fn string(s: &str) -> Tag { + Tag::String(JavaString::from(s)) +} +pub fn empty_map() -> Tag { + Tag::Compound(Compound::new()) +} +pub fn boolean(b: bool) -> Tag { + Tag::Byte(i8::from(b)) +} +pub fn int_list(values: &[i32]) -> Tag { + Tag::IntArray(values.to_vec()) +} + +/// A Java `Number` from a numeric tag (`Tag.asNumber`). +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Number { + Byte(i8), + Short(i16), + Int(i32), + Long(i64), + Float(f32), + Double(f64), +} +impl Number { + pub fn int_value(self) -> i32 { + match self { + Self::Byte(v) => i32::from(v), + Self::Short(v) => i32::from(v), + Self::Int(v) => v, + Self::Long(v) => v as i32, + Self::Float(v) => v as i32, + Self::Double(v) => v as i32, + } + } + pub fn long_value(self) -> i64 { + match self { + Self::Byte(v) => i64::from(v), + Self::Short(v) => i64::from(v), + Self::Int(v) => i64::from(v), + Self::Long(v) => v, + Self::Float(v) => v as i64, + Self::Double(v) => v as i64, + } + } + pub fn double_value(self) -> f64 { + match self { + Self::Byte(v) => f64::from(v), + Self::Short(v) => f64::from(v), + Self::Int(v) => f64::from(v), + Self::Long(v) => v as f64, + Self::Float(v) => f64::from(v), + Self::Double(v) => v, + } + } + pub fn float_value(self) -> f32 { + match self { + Self::Double(v) => v as f32, + Self::Long(v) => v as f32, + other => other.double_value() as f32, + } + } + pub fn byte_value(self) -> i8 { + match self { + Self::Byte(v) => v, + Self::Short(v) => v as i8, + Self::Int(v) => v as i8, + Self::Long(v) => v as i8, + Self::Float(v) => (v as i32) as i8, + Self::Double(v) => (v as i32) as i8, + } + } + /// `Number.toString` of the boxed value. + pub fn java_string(self) -> String { + match self { + Self::Byte(v) => v.to_string(), + Self::Short(v) => v.to_string(), + Self::Int(v) => v.to_string(), + Self::Long(v) => v.to_string(), + Self::Float(v) => crate::datafix::json::java_float_string(v), + Self::Double(v) => crate::datafix::json::java_double_string(v), + } + } +} + +pub fn as_number(tag: &Tag) -> Option { + Some(match tag { + Tag::Byte(v) => Number::Byte(*v), + Tag::Short(v) => Number::Short(*v), + Tag::Int(v) => Number::Int(*v), + Tag::Long(v) => Number::Long(*v), + Tag::Float(v) => Number::Float(f32::from_bits(*v)), + Tag::Double(v) => Number::Double(f64::from_bits(*v)), + _ => return None, + }) +} +pub fn as_int(tag: Option<&Tag>, default: i32) -> i32 { + tag.and_then(as_number).map_or(default, Number::int_value) +} +pub fn as_long(tag: Option<&Tag>, default: i64) -> i64 { + tag.and_then(as_number).map_or(default, Number::long_value) +} +pub fn as_double(tag: Option<&Tag>, default: f64) -> f64 { + tag.and_then(as_number).map_or(default, Number::double_value) +} +pub fn as_float(tag: Option<&Tag>, default: f32) -> f32 { + tag.and_then(as_number).map_or(default, Number::float_value) +} +/// `NbtOps.getBooleanValue`: a number that is not zero. +pub fn as_bool_opt(tag: Option<&Tag>) -> Option { + tag.and_then(as_number).map(|n| n.double_value() != 0.0) +} +pub fn as_bool(tag: Option<&Tag>, default: bool) -> bool { + as_bool_opt(tag).unwrap_or(default) +} +pub fn as_str(tag: Option<&Tag>) -> Option { + match tag { + Some(Tag::String(s)) => Some(s.to_string_lossy()), + _ => None, + } +} +pub fn as_string(tag: Option<&Tag>, default: &str) -> String { + as_str(tag).unwrap_or_else(|| default.to_owned()) +} + +/// `NbtOps.getStream`: the elements of a list or of a number array. +pub fn stream(tag: Option<&Tag>) -> Vec { + match tag { + Some(Tag::List(v)) => v.clone(), + Some(Tag::ByteArray(v)) => v.iter().map(|b| Tag::Byte(*b as i8)).collect(), + Some(Tag::IntArray(v)) => v.iter().map(|i| Tag::Int(*i)).collect(), + Some(Tag::LongArray(v)) => v.iter().map(|l| Tag::Long(*l)).collect(), + _ => Vec::new(), + } +} +pub fn is_list(tag: Option<&Tag>) -> bool { + matches!( + tag, + Some(Tag::List(_) | Tag::ByteArray(_) | Tag::IntArray(_) | Tag::LongArray(_)) + ) +} + +pub fn get<'a>(tag: &'a Tag, field: &str) -> Option<&'a Tag> { + tag.get(field) +} +pub fn map(tag: &Tag) -> Option<&Compound> { + match tag { + Tag::Compound(c) => Some(c), + _ => None, + } +} +pub fn map_mut(tag: &mut Tag) -> Option<&mut Compound> { + match tag { + Tag::Compound(c) => Some(c), + _ => None, + } +} +/// `Dynamic.set`: only maps change. +pub fn set(tag: &mut Tag, field: &str, value: Tag) { + if let Tag::End = tag { + *tag = empty_map(); + } + if let Tag::Compound(c) = tag { + c.insert(key(field), value); + } +} +pub fn remove(tag: &mut Tag, field: &str) -> Option { + match tag { + Tag::Compound(c) => c.remove(&key(field)), + _ => None, + } +} +pub fn has(tag: &Tag, field: &str) -> bool { + tag.get(field).is_some() +} +/// `Dynamic.renameField`. +pub fn rename(tag: &mut Tag, from: &str, to: &str) { + if let Some(v) = remove(tag, from) { + set(tag, to, v); + } +} +/// `Dynamic.update`: apply `f` to a field that is present. +pub fn update(tag: &mut Tag, field: &str, f: impl FnOnce(Tag) -> Tag) { + if let Some(v) = remove(tag, field) { + set(tag, field, f(v)); + } +} +/// `Dynamic.update` in place. +pub fn update_mut(tag: &mut Tag, field: &str, f: impl FnOnce(&mut Tag)) { + if let Some(v) = tag.get_mut(field) { + f(v); + } +} +/// `Dynamic.renameAndFixField`: the fixed value under the new name, removed when +/// the fix gives nothing. +pub fn rename_and_fix(tag: &mut Tag, from: &str, to: &str, f: impl FnOnce(Tag) -> Option) { + if let Some(v) = remove(tag, from) + && let Some(v) = f(v) + { + set(tag, to, v); + } +} +/// `Dynamic.copyField`. +pub fn copy_field(from: &Tag, field: &str, to: &mut Tag, to_field: &str) { + if let Some(v) = from.get(field) { + set(to, to_field, v.clone()); + } +} +pub fn is_empty_map(tag: &Tag) -> bool { + matches!(tag, Tag::Compound(c) if c.is_empty()) +} +/// `Dynamic.orElseEmptyMap` of an optional field. +pub fn or_empty_map(tag: Option<&Tag>) -> Tag { + tag.cloned().unwrap_or_else(empty_map) +} +/// Apply `f` to every element of a list field (`Dynamic.createList(asStream().map(f))`). +pub fn map_list(tag: &mut Tag, field: &str, mut f: impl FnMut(Tag) -> Tag) { + if let Some(v) = tag.get(field) + && is_list(Some(v)) + { + let list = stream(Some(v)).into_iter().map(&mut f).collect(); + set(tag, field, Tag::List(list)); + } +} +/// `NamespacedSchema.ensureNamespaced`. +pub fn namespaced(id: &str) -> String { + match crate::datafix::identifier(id) { + Some((ns, path)) => format!("{ns}:{path}"), + None => id.to_owned(), + } +} diff --git a/crates/horizon-world/src/datafix/files.rs b/crates/horizon-world/src/datafix/files.rs new file mode 100644 index 000000000..c376a695d --- /dev/null +++ b/crates/horizon-world/src/datafix/files.rs @@ -0,0 +1,570 @@ +//! Opening an older world as 26.3 does: the file fixes of `FileFixerUpper` +//! (`net.minecraft.util.filefix`, from DataVersion 4772 on) that move the +//! files into the 26.x layout, then level.dat brought to the current version, +//! and the upgrades vanilla runs as it reads each file ([`upgrade_world`] +//! does them all at once, as a converter). +use super::dynamic::{as_bool, as_int, as_str, int_list, remove, set, string}; +use super::json::{self, Json}; +use super::schema::Ty; +use super::{CURRENT, update}; +use crate::compression::Compression; +use crate::nbt::{self, Compound, JavaString, Limits, NamedTag, Tag}; +use crate::region::{ChunkRecord, RegionFile, RegionWriter}; +use std::fs; +use std::io; +use std::path::{Path, PathBuf}; + +/// The name `SharedConstants.getCurrentVersion().name()` writes in level.dat. +pub const VERSION_NAME: &str = "26.3"; + +const FILE_FIXER_INTRODUCTION: i32 = 4772; +/// The versions of the file fixes, in order. +const FILE_FIXES: [i32; 3] = [4772, 4773, 4899]; + +fn invalid(message: impl Into) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, message.into()) +} + +/// `NbtIo.readCompressed`: the root compound of a gzip NBT file, if it exists. +pub fn read_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 = Compression::Gzip.decode(&bytes, limits.max_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() { + fs::create_dir_all(parent)?; + } + let root = NamedTag { + name: JavaString::default(), + tag: tag.clone(), + }; + let raw = nbt::write_named(&root, Limits::default()).map_err(|e| invalid(e.to_string()))?; + fs::write(path, Compression::Gzip.encode(&raw)?) +} + +/// `NbtUtils.getDataVersion`. +pub fn data_version(tag: &Tag, default: i32) -> i32 { + match tag.get("DataVersion") { + Some(Tag::Int(v)) => *v, + _ => default, + } +} + +/// `FileFixerUpper.requiresFileFixing`. +pub fn requires_file_fixing(level_version: i32) -> bool { + let from = if level_version < FILE_FIXER_INTRODUCTION { 0 } else { level_version }; + from < *FILE_FIXES.last().unwrap() +} + +// ------------------------------------------------------------------ file operations + +fn resolve(base: &Path, relative: &str) -> PathBuf { + base.join(relative.trim_start_matches('/')) +} + +/// `FileFixUtil.moveFile`: nothing when the source is missing, an error when +/// the target exists. +fn move_file(base: &Path, from: &str, to: &str) -> io::Result<()> { + let from = resolve(base, from); + if !from.exists() { + return Ok(()); + } + let to = resolve(base, to); + if to.exists() { + return Err(io::Error::new( + io::ErrorKind::AlreadyExists, + format!("cannot move {} to {}: target already exists", from.display(), to.display()), + )); + } + if let Some(parent) = to.parent() { + fs::create_dir_all(parent)?; + } + fs::rename(from, to) +} + +/// `FileFixUtil.deleteFileOrEmptyDirectory`. +fn delete(base: &Path, relative: &str) -> io::Result<()> { + let path = resolve(base, relative); + if path.is_dir() { + let entries: Vec<_> = fs::read_dir(&path)?.collect::>()?; + if entries.len() == 1 && entries[0].file_name() == ".DS_Store" { + let _ = fs::remove_file(entries[0].path()); + } + if fs::read_dir(&path)?.next().is_some() { + return Ok(()); + } + fs::remove_dir(&path) + } else if path.exists() { + fs::remove_file(&path) + } else { + Ok(()) + } +} + +fn subdirectories(path: &Path) -> Vec { + let mut out: Vec = fs::read_dir(path) + .map(|r| r.filter_map(Result::ok).map(|e| e.path()).filter(|p| p.is_dir()).collect()) + .unwrap_or_default(); + out.sort(); + out +} + +/// `FileRelation.DIMENSIONS`. +fn dimensions(base: &Path) -> Vec { + let root = base.join("dimensions"); + let defaults = || { + ["overworld", "the_nether", "the_end"].map(|d| base.join("dimensions/minecraft").join(d)).to_vec() + }; + if !root.exists() { + return defaults(); + } + let found: Vec = subdirectories(&root).iter().flat_map(|ns| subdirectories(ns)).collect(); + if found.is_empty() { defaults() } else { found } +} + +// ------------------------------------------------------------------ file accessors + +/// `LevelDat.read`: level.dat's `Data` brought to `version`. +fn read_level(base: &Path, version: i32) -> io::Result> { + Ok(read_dat(&base.join("level.dat"))?.map(|root| { + let mut data = root.get("Data").cloned().unwrap_or_else(|| Tag::Compound(Compound::new())); + let from = data_version(&data, -1); + update(Ty::Level, &mut data, from, version); + data + })) +} +fn write_level(base: &Path, mut data: Tag, version: i32) -> io::Result<()> { + set(&mut data, "DataVersion", Tag::Int(version)); + write_dat(&base.join("level.dat"), &Tag::compound([("Data", data)])) +} +/// `SavedDataNbt.write`. +fn write_saved_data(path: &Path, data: Tag, version: i32) -> io::Result<()> { + write_dat(path, &Tag::compound([("data", data), ("DataVersion", Tag::Int(version))])) +} +/// `SavedDataNbt.read`: the `data` of a saved data file brought to `version`. +fn read_saved_data(path: &Path, ty: Ty, version: i32) -> io::Result> { + Ok(read_dat(path)?.map(|mut tag| { + let from = data_version(&tag, -1); + update(ty, &mut tag, from, version); + tag.get("data").cloned().unwrap_or_else(|| Tag::Compound(Compound::new())) + })) +} + +// ------------------------------------------------------------------ the file fixes + +/// 4772: `ResourcePackLocationFileFix`, `DimensionStorageFileFix`, +/// `PlayerStorageFileFix`, `LevelDatToSavedDataFileFix`, `RemoveObsoleteFilesFileFix`. +fn fix_4772(base: &Path) -> io::Result<()> { + move_file(base, "resources.zip", "resourcepacks/resources.zip")?; + + for dim in dimensions(base) { + let data = dim.join("data"); + move_file(&data, "chunks.dat", "minecraft/chunk_tickets.dat")?; + move_file(&data, "raids.dat", "minecraft/raids.dat")?; + move_file(&data, "world_border.dat", "minecraft/world_border.dat")?; + } + for (from, to) in [ + ("data", "dimensions/minecraft/overworld/data/minecraft"), + ("DIM-1/data", "dimensions/minecraft/the_nether/data/minecraft"), + ("DIM1/data", "dimensions/minecraft/the_end/data/minecraft"), + ] { + for (a, b) in [ + ("chunks.dat", "chunk_tickets.dat"), + ("raids.dat", "raids.dat"), + ("raids_end.dat", "raids.dat"), + ("world_border.dat", "world_border.dat"), + ] { + move_file(base, &format!("{from}/{a}"), &format!("{to}/{b}"))?; + } + } + delete(base, "DIM1/data")?; + delete(base, "DIM-1/data")?; + let data = base.join("data"); + for (a, b) in [ + ("scoreboard.dat", "minecraft/scoreboard.dat"), + ("stopwatches.dat", "minecraft/stopwatches.dat"), + ] { + move_file(&data, a, b)?; + } + regex_move(&data, |name| { + let ns = name.strip_prefix("command_storage_")?.strip_suffix(".dat")?; + (!ns.is_empty() && ns.bytes().all(|b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'.' | b'-'))) + .then(|| format!("{ns}/command_storage.dat")) + })?; + move_file(&data, "idcounts.dat", "minecraft/maps/last_id.dat")?; + regex_move(&data, |name| { + let id = name.strip_prefix("map_")?.strip_suffix(".dat")?; + (!id.is_empty() && id.bytes().all(|b| b.is_ascii_digit())).then(|| format!("minecraft/maps/{id}.dat")) + })?; + move_file(&data, "random_sequences.dat", "minecraft/random_sequences.dat")?; + for (from, to) in [ + ("", "dimensions/minecraft/overworld"), + ("DIM-1", "dimensions/minecraft/the_nether"), + ("DIM1", "dimensions/minecraft/the_end"), + ] { + for f in ["region", "entities", "poi"] { + move_file(base, &format!("{from}/{f}"), &format!("{to}/{f}"))?; + } + } + delete(base, "DIM-1")?; + delete(base, "DIM1")?; + + move_file(base, "advancements", "players/advancements")?; + move_file(base, "playerdata", "players/data")?; + move_file(base, "stats", "players/stats")?; + + level_dat_to_saved_data(base)?; + + for f in [ + "data/villages.dat", + "data/villages_end.dat", + "data/villages_nether.dat", + "data/advancements", + "data/functions", + ] { + delete(base, f)?; + } + for s in ["Monument", "Stronghold", "Mineshaft", "Temple", "Mansion", "Village", "Fortress", "EndCity"] { + delete(base, &format!("data/{s}.dat"))?; + delete(base, &format!("data/{s}_index.dat"))?; + } + Ok(()) +} + +/// `RegexMove` over the files of a folder. +fn regex_move(folder: &Path, rename: impl Fn(&str) -> Option) -> io::Result<()> { + if !folder.is_dir() { + return Ok(()); + } + let mut names: Vec = fs::read_dir(folder)? + .filter_map(Result::ok) + .map(|e| e.file_name().to_string_lossy().into_owned()) + .collect(); + names.sort(); + for name in names { + if let Some(new) = rename(&name) { + move_file(folder, &name, &new)?; + } + } + Ok(()) +} + +fn level_dat_to_saved_data(base: &Path) -> io::Result<()> { + const V: i32 = 4772; + let Some(mut content) = read_level(base, V)? else { + return Ok(()); + }; + let data = base.join("data/minecraft"); + let dim_data = |dim: &str, file: &str| base.join(format!("dimensions/minecraft/{dim}/data/minecraft/{file}")); + let extract = |content: &mut Tag, key: &str, path: PathBuf| -> io::Result<()> { + if let Some(tag) = remove(content, key) { + write_saved_data(&path, tag, V)?; + } + Ok(()) + }; + extract(&mut content, "dragon_fight", dim_data("the_end", "ender_dragon_fight.dat"))?; + if let Some(mut player) = remove(&mut content, "Player") { + let from = data_version(&player, -1); + update(Ty::Player, &mut player, from, V); + let uuid = match player.get("UUID") { + Some(u) => u.clone(), + None => { + set(&mut player, "DataVersion", Tag::Int(V)); + write_dat(&base.join("players/data/00000000-0000-0000-0000-000000000000.dat"), &player)?; + int_list(&[0, 0, 0, 0]) + } + }; + set(&mut content, "singleplayer_uuid", uuid); + } + extract(&mut content, "wandering_trader_migration_data", data.join("wandering_trader.dat"))?; + extract(&mut content, "CustomBossEvents", data.join("custom_boss_events.dat"))?; + extract(&mut content, "weather_data", data.join("weather.dat"))?; + extract(&mut content, "scheduled_events", data.join("scheduled_events.dat"))?; + if let Some(border) = content.get("world_border").cloned() { + for (dim, divider) in [("overworld", 1.0), ("the_nether", 8.0), ("the_end", 1.0)] { + let mut b = border.clone(); + for k in ["center_x", "center_z"] { + if let Some(v) = b.get(k) { + let v = super::dynamic::as_double(Some(v), 0.0) / divider; + set(&mut b, k, Tag::Double(v.to_bits())); + } + } + write_saved_data(&dim_data(dim, "world_border.dat"), b, V)?; + } + } + remove(&mut content, "world_border"); + extract(&mut content, "game_rules", data.join("game_rules.dat"))?; + if let Some(mut settings) = remove(&mut content, "world_gen_settings") { + let from = data_version(&content, -1); + update(Ty::WorldGenSettings, &mut settings, from, V); + write_saved_data(&data.join("world_gen_settings.dat"), settings, V)?; + } + extract(&mut content, "world_clocks", data.join("world_clocks.dat"))?; + write_level(base, content, V) +} + +/// 4773: `GeneratedStructuresRenameFileFix`. +fn fix_4773(base: &Path) -> io::Result<()> { + for ns in subdirectories(&base.join("generated")) { + move_file(&ns, "structures", "structure")?; + } + Ok(()) +} + +/// 4899: `ReenableSpectatorsGenerateChunksInHardcoreWorldsFileFix`. +fn fix_4899(base: &Path) -> io::Result<()> { + const V: i32 = 4899; + let Some(level) = read_level(base, V)? else { + return Ok(()); + }; + let hardcore = level.get("difficulty_settings").is_some_and(|d| as_bool(d.get("hardcore"), false)); + if level.get("singleplayer_uuid").is_some() && hardcore { + let path = base.join("data/minecraft/game_rules.dat"); + if let Some(mut rules) = read_saved_data(&path, Ty::SavedGameRules, V)? { + if rules.get("minecraft:spectators_generate_chunks").is_some() { + set(&mut rules, "minecraft:spectators_generate_chunks", Tag::Byte(1)); + } + write_saved_data(&path, rules, V)?; + } + } + Ok(()) +} + +/// `FileFixerUpper.addVersionsToLevelData`. +fn add_versions(data: &mut Tag) { + set(data, "DataVersion", Tag::Int(CURRENT)); + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as i64); + set(data, "LastPlayed", Tag::Long(now)); + set( + data, + "Version", + Tag::compound([ + ("Name", string(VERSION_NAME)), + ("Id", Tag::Int(CURRENT)), + ("Snapshot", Tag::Byte(0)), + ("Series", string("main")), + ]), + ); +} + +/// `FileFixerUpper.fix`: the file fixes a world saved at an older version +/// needs, then its level.dat at the current version. Returns the DataVersion +/// the world had. +pub fn fix_world_files(base: &Path) -> io::Result { + let root = read_dat(&base.join("level.dat"))?.ok_or_else(|| invalid("missing level.dat"))?; + let mut data = root.get("Data").cloned().unwrap_or_else(|| Tag::Compound(Compound::new())); + let loaded = data_version(&data, -1); + if loaded >= CURRENT { + return Ok(loaded); + } + let mut from = loaded; + if requires_file_fixing(loaded) { + let start = if loaded < FILE_FIXER_INTRODUCTION { 0 } else { loaded }; + for version in FILE_FIXES { + if version > start && version <= CURRENT { + match version { + 4772 => fix_4772(base)?, + 4773 => fix_4773(base)?, + _ => fix_4899(base)?, + } + } + } + // `writeUpdatedLevelData`, then the world swapped in and read again. + let root = read_dat(&base.join("level.dat"))?.ok_or_else(|| invalid("missing level.dat"))?; + data = root.get("Data").cloned().unwrap_or_else(|| Tag::Compound(Compound::new())); + from = data_version(&data, -1); + let _ = fs::remove_file(base.join("level.dat_old")); + let _ = fs::remove_file(base.join("session.lock")); + } + update(Ty::Level, &mut data, from, CURRENT); + add_versions(&mut data); + write_dat(&base.join("level.dat"), &Tag::compound([("Data", data)]))?; + Ok(loaded) +} + +// ------------------------------------------------------------------ the read-time upgrades + +/// Chunk generator types per dimension from `world_gen_settings.dat`. +fn generators(base: &Path) -> io::Result> { + let Some(tag) = read_dat(&base.join("data/minecraft/world_gen_settings.dat"))? else { + return Ok(Vec::new()); + }; + let mut out = Vec::new(); + if let Some(Tag::Compound(dims)) = tag.get("data").and_then(|d| d.get("dimensions")) { + for (id, dim) in dims { + if let Some(ty) = dim.get("generator").and_then(|g| as_str(g.get("type"))) { + out.push((id.to_string_lossy(), ty)); + } + } + } + Ok(out) +} + +fn files_with_suffix(folder: &Path, suffix: &str) -> Vec { + let mut out: Vec = fs::read_dir(folder) + .map(|r| { + r.filter_map(Result::ok) + .map(|e| e.path()) + .filter(|p| p.is_file() && p.to_string_lossy().ends_with(suffix)) + .collect() + }) + .unwrap_or_default(); + out.sort(); + out +} + +/// Rewrite every chunk of the region files of a folder with `fix`. +fn upgrade_regions(folder: &Path, mut fix: impl FnMut(&mut Tag)) -> io::Result<()> { + let limits = Limits::default(); + for path in files_with_suffix(folder, ".mca") { + let mut region = RegionFile::open(&path)?; + let positions: Vec<_> = region.chunks().map(|(p, _)| p).collect(); + let mut records = Vec::new(); + for pos in positions { + let Some(record) = region.read(pos)? else { + continue; + }; + let mut root = record.nbt(limits)?; + fix(&mut root.tag); + records.push((pos, ChunkRecord::from_nbt(&root, record.compression, record.timestamp, limits)?)); + } + drop(region); + let staging = folder.join(".datafix"); + fs::create_dir_all(&staging)?; + let temporary = staging.join(path.file_name().unwrap_or_default()); + let _ = fs::remove_file(&temporary); + let mut writer = RegionWriter::create(&temporary)?; + for (pos, record) in &records { + writer.write(*pos, record)?; + } + writer.finish()?; + fs::rename(&temporary, &path)?; + fs::remove_dir(&staging)?; + } + Ok(()) +} + +/// `SavedDataStorage.readTagFromDisk` over every known file of a data folder. +fn upgrade_saved_data(data: &Path) -> io::Result<()> { + if !data.is_dir() { + return Ok(()); + } + let mut files = Vec::new(); + let mut stack = vec![data.to_path_buf()]; + while let Some(dir) = stack.pop() { + for entry in fs::read_dir(&dir)?.filter_map(Result::ok) { + let p = entry.path(); + if p.is_dir() { + stack.push(p); + } else if p.to_string_lossy().ends_with(".dat") { + files.push(p); + } + } + } + files.sort(); + for file in files { + let relative = file.strip_prefix(data).unwrap_or(&file).to_string_lossy().replace('\\', "/"); + let Some(ty) = super::saved_data_type(&relative) else { + continue; + }; + if let Some(mut tag) = read_dat(&file)? { + super::update_saved_data(ty, &mut tag); + set(&mut tag, "DataVersion", Tag::Int(CURRENT)); + write_dat(&file, &tag)?; + } + } + Ok(()) +} + +/// A JSON file of the player folders as NBT for the fixes (`JsonOps` and +/// `NbtOps` see the same structure), booleans kept apart. +fn json_to_tag(j: &Json) -> Tag { + match j { + Json::Bool(b) => Tag::compound([("\u{0}bool", Tag::Byte(i8::from(*b)))]), + Json::Arr(items) => Tag::List(items.iter().map(json_to_tag).collect()), + Json::Obj(entries) => { + Tag::Compound(entries.iter().map(|(k, v)| (JavaString(k.clone()), json_to_tag(v))).collect()) + } + other => json::to_nbt(other).unwrap_or(Tag::End), + } +} +fn tag_to_json(t: &Tag) -> Json { + match t { + Tag::Compound(c) if c.len() == 1 && c.contains_key(&JavaString::from("\u{0}bool")) => { + Json::Bool(as_int(t.get("\u{0}bool"), 0) != 0) + } + Tag::List(items) => Json::Arr(items.iter().map(tag_to_json).collect()), + Tag::Compound(c) => Json::Obj(c.iter().map(|(k, v)| (k.0.clone(), tag_to_json(v))).collect()), + other => json::from_nbt(other), + } +} + +fn upgrade_json(path: &Path, ty: Ty) -> io::Result<()> { + let text = fs::read_to_string(path)?; + let units: Vec = text.encode_utf16().collect(); + let parsed = json::parse_lenient(&units).ok_or_else(|| invalid(format!("bad JSON in {}", path.display())))?; + let mut tag = json_to_tag(&parsed); + let from = super::dynamic::as_int(tag.get("DataVersion"), 1343); + if ty == Ty::Advancements { + // `PlayerAdvancements`: the version field is not part of the data. + remove(&mut tag, "DataVersion"); + } + update(ty, &mut tag, from, CURRENT); + set(&mut tag, "DataVersion", Tag::Int(CURRENT)); + let out = json::to_stable_string(&tag_to_json(&tag)); + fs::write(path, String::from_utf16_lossy(&out)) +} + +/// Everything vanilla upgrades as it reads a world, done at once: every chunk, +/// entity chunk and POI chunk, saved data, player, advancements and +/// statistics file. Run after [`fix_world_files`]. +pub fn upgrade_world_data(base: &Path) -> io::Result<()> { + let generators = generators(base)?; + for ns in subdirectories(&base.join("dimensions")) { + for dim in subdirectories(&ns) { + let id = format!( + "{}:{}", + ns.file_name().unwrap_or_default().to_string_lossy(), + dim.file_name().unwrap_or_default().to_string_lossy() + ); + let generator = generators.iter().find(|(d, _)| *d == id).map(|(_, g)| g.clone()); + upgrade_regions(&dim.join("region"), |tag| super::update_chunk(tag, &id, generator.as_deref()))?; + upgrade_regions(&dim.join("entities"), |tag| super::update_region_tag(Ty::EntityChunk, tag))?; + upgrade_regions(&dim.join("poi"), |tag| super::update_region_tag(Ty::PoiChunk, tag))?; + upgrade_saved_data(&dim.join("data"))?; + } + } + upgrade_saved_data(&base.join("data"))?; + for file in files_with_suffix(&base.join("players/data"), ".dat") { + if let Some(mut tag) = read_dat(&file)? { + super::update_player(&mut tag); + set(&mut tag, "DataVersion", Tag::Int(CURRENT)); + write_dat(&file, &tag)?; + } + } + for file in files_with_suffix(&base.join("players/advancements"), ".json") { + upgrade_json(&file, Ty::Advancements)?; + } + for file in files_with_suffix(&base.join("players/stats"), ".json") { + upgrade_json(&file, Ty::Stats)?; + } + Ok(()) +} + +/// Bring a whole world saved by 1.20 to 26.2 to the 26.3 format in place. +pub fn upgrade_world(base: &Path) -> io::Result { + let version = fix_world_files(base)?; + upgrade_world_data(base)?; + Ok(version) +} diff --git a/crates/horizon-world/src/datafix/fixes_a.rs b/crates/horizon-world/src/datafix/fixes_a.rs new file mode 100644 index 000000000..7781d6820 --- /dev/null +++ b/crates/horizon-world/src/datafix/fixes_a.rs @@ -0,0 +1,937 @@ +//! Fixes of 1.20.2 to 1.20.6 (keys 3564 to 3833). +use super::dynamic::{ + as_double, as_float, as_int, as_number, as_str, as_string, empty_map, int_list, is_list, namespaced, remove, + rename, rename_and_fix, set, stream, string, +}; +use super::json; +use super::schema::{Key, Ty, key}; +use super::{Cx, FixFn, id_is, items, renamer}; +use crate::nbt::Tag; + +pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + out.push((key(3564, 0), |cx| drop_invalid_sign_data(cx, "minecraft:sign"))); + out.push((key(3564, 1), |cx| drop_invalid_sign_data(cx, "minecraft:hanging_sign"))); + out.push((key(3565, 0), random_sequence_settings)); + out.push((key(3566, 0), scoreboard_display_slots)); + out.push((key(3568, 0), mob_effect_ids)); + out.push((key(3683, 0), primed_tnt_block_state)); + out.push((key(3685, 0), projectile_stored_item)); + out.push((key(3692, 0), |cx| block_rename(cx, &[("minecraft:grass", "minecraft:short_grass")]))); + out.push((key(3692, 0), |cx| cx.rename(Ty::ItemName, renamer(&[("minecraft:grass", "minecraft:short_grass")])))); + out.push((key(3800, 0), |cx| cx.rename(Ty::ItemName, renamer(&[("minecraft:scute", "minecraft:turtle_scute")])))); + out.push((key(3803, 0), rename_sweeping)); + out.push((key(3807, 1), map_banner_block_pos)); + out.push((key(3808, 0), |cx| horse_body_armor(cx, "minecraft:horse", "ArmorItem", true))); + out.push((key(3808, 1), |cx| horse_body_armor(cx, "minecraft:llama", "DecorItem", false))); + out.push((key(3808, 2), |cx| horse_body_armor(cx, "minecraft:trader_llama", "DecorItem", false))); + out.push((key(3809, 0), chested_horse_slots)); + out.push((key(3812, 0), wolf_health)); + out.push((key(3813, 0), block_pos_format)); + out.push((key(3814, 0), |cx| { + attributes_rename_legacy(cx, &|id| (namespaced(id) == "minecraft:horse.jump_strength").then_some("minecraft:generic.jump_strength")) + })); + out.push((key(3818, 0), beehive_fields)); + out.push((key(3818, 1), banner_patterns)); + out.push((key(3818, 2), tipped_arrow_potion)); + out.push((key(3818, 4), particle_unflattening)); + out.push((key(3818, 5), |cx| cx.each(Ty::ItemStack, items::componentize))); + out.push((key(3818, 6), area_effect_cloud_potion)); + out.push((key(3820, 0), player_head_profile)); + out.push((key(3820, 0), lodestone_compass_component)); + out.push((key(3825, 0), item_custom_name_to_item_name)); + out.push((key(3825, 0), banner_custom_name_to_item_name)); + out.push((key(3825, 0), trial_spawner_config)); + out.push((key(3828, 0), empty_item_in_villager_trade)); + out.push((key(3833, 0), remove_empty_brushable_item)); +} + +// ------------------------------------------------------------------ 3564 + +/// `BlockEntitySignDoubleSidedEditableTextFix.FIELDS_TO_DROP`. +const SIGN_FIELDS_TO_DROP: [&str; 10] = [ + "Text1", "Text2", "Text3", "Text4", "FilteredText1", "FilteredText2", "FilteredText3", "FilteredText4", "Color", + "GlowingText", +]; + +fn drop_invalid_sign_data(cx: &mut Cx, id: &str) { + cx.block_entity(id, |be| { + if super::dynamic::as_bool(be.get("_filtered_correct"), false) { + remove(be, "_filtered_correct"); + return; + } + for side in ["front_text", "back_text"] { + if let Some(t) = be.get_mut(side) { + fix_sign_text(t); + } + } + for f in SIGN_FIELDS_TO_DROP { + remove(be, f); + } + }); +} + +fn fix_sign_text(t: &mut Tag) { + if !is_list(t.get("filtered_messages")) { + return; + } + let filtered = stream(t.get("filtered_messages")); + let lines = stream(t.get("messages")); + let empty = Tag::String(crate::nbt::JavaString(json::text_component_json(&[]))); + let new: Vec = filtered + .into_iter() + .enumerate() + .map(|(i, line)| { + let fallback = lines.get(i).cloned().unwrap_or_else(|| empty.clone()); + if line == empty { fallback } else { line } + }) + .collect(); + if new == lines { + remove(t, "filtered_messages"); + } else { + set(t, "filtered_messages", Tag::List(new)); + } +} + +// ------------------------------------------------------------------ 3565, 3566 + +fn random_sequence_settings(cx: &mut Cx) { + cx.each(Ty::SavedRandomSequences, |v| { + if let Some(data) = remove(v, "data") { + set(v, "data", Tag::compound([("sequences", data)])); + } + }); +} + +const SLOT_RENAMES: [(&str, &str); 19] = [ + ("slot_0", "list"), + ("slot_1", "sidebar"), + ("slot_2", "below_name"), + ("slot_3", "sidebar.team.black"), + ("slot_4", "sidebar.team.dark_blue"), + ("slot_5", "sidebar.team.dark_green"), + ("slot_6", "sidebar.team.dark_aqua"), + ("slot_7", "sidebar.team.dark_red"), + ("slot_8", "sidebar.team.dark_purple"), + ("slot_9", "sidebar.team.gold"), + ("slot_10", "sidebar.team.gray"), + ("slot_11", "sidebar.team.dark_gray"), + ("slot_12", "sidebar.team.blue"), + ("slot_13", "sidebar.team.green"), + ("slot_14", "sidebar.team.aqua"), + ("slot_15", "sidebar.team.red"), + ("slot_16", "sidebar.team.light_purple"), + ("slot_17", "sidebar.team.yellow"), + ("slot_18", "sidebar.team.white"), +]; + +fn scoreboard_display_slots(cx: &mut Cx) { + cx.each(Ty::SavedScoreboard, |v| { + if let Some(data) = v.get_mut("data") + && let Some(Tag::Compound(slots)) = data.get_mut("DisplaySlots") + { + let old = std::mem::take(slots); + for (k, value) in old { + let name = k.to_string_lossy(); + let k = SLOT_RENAMES.iter().find(|(o, _)| *o == name).map_or(k, |(_, n)| (*n).into()); + slots.insert(k, value); + } + } + }); +} + +// ------------------------------------------------------------------ 3568 + +const EFFECT_IDS: [&str; 33] = [ + "minecraft:speed", + "minecraft:slowness", + "minecraft:haste", + "minecraft:mining_fatigue", + "minecraft:strength", + "minecraft:instant_health", + "minecraft:instant_damage", + "minecraft:jump_boost", + "minecraft:nausea", + "minecraft:regeneration", + "minecraft:resistance", + "minecraft:fire_resistance", + "minecraft:water_breathing", + "minecraft:invisibility", + "minecraft:blindness", + "minecraft:night_vision", + "minecraft:hunger", + "minecraft:weakness", + "minecraft:poison", + "minecraft:wither", + "minecraft:health_boost", + "minecraft:absorption", + "minecraft:saturation", + "minecraft:glowing", + "minecraft:levitation", + "minecraft:luck", + "minecraft:unluck", + "minecraft:slow_falling", + "minecraft:conduit_power", + "minecraft:dolphins_grace", + "minecraft:bad_omen", + "minecraft:hero_of_the_village", + "minecraft:darkness", +]; + +/// `getAndConvertMobEffectId`. +fn effect_id(v: &Tag, field: &str) -> Option { + let n = as_number(v.get(field)?)?.int_value(); + (1..=33).contains(&n).then(|| string(EFFECT_IDS[(n - 1) as usize])) +} +/// `replaceField(old, new, value)`. +fn replace_field(v: &mut Tag, old: &str, new: &str, value: Option) { + remove(v, old); + if let Some(value) = value { + set(v, new, value); + } +} +fn update_effect_id_field(input: &Tag, old: &str, output: &mut Tag, new: &str) { + let mapped = effect_id(input, old); + replace_field(output, old, new, mapped); +} +fn update_effect_instance(mut v: Tag) -> Tag { + let input = v.clone(); + update_effect_id_field(&input, "Id", &mut v, "id"); + rename(&mut v, "Ambient", "ambient"); + rename(&mut v, "Amplifier", "amplifier"); + rename(&mut v, "Duration", "duration"); + rename(&mut v, "ShowParticles", "show_particles"); + rename(&mut v, "ShowIcon", "show_icon"); + let hidden = v.get("HiddenEffect").cloned().map(update_effect_instance); + replace_field(&mut v, "HiddenEffect", "hidden_effect", hidden); + v +} +fn update_effect_list(v: &mut Tag, old: &str, new: &str) { + let value = is_list(v.get(old)).then(|| Tag::List(stream(v.get(old)).into_iter().map(update_effect_instance).collect())); + replace_field(v, old, new, value); +} +fn update_stew_entry(input: &Tag, mut output: Tag) -> Tag { + update_effect_id_field(input, "EffectId", &mut output, "id"); + let duration = input.get("EffectDuration").cloned(); + replace_field(&mut output, "EffectDuration", "duration", duration); + output +} + +fn mob_effect_ids(cx: &mut Cx) { + cx.block_entity("minecraft:beacon", |be| { + let input = be.clone(); + update_effect_id_field(&input, "Primary", be, "primary_effect"); + let input = be.clone(); + update_effect_id_field(&input, "Secondary", be, "secondary_effect"); + }); + cx.each(Ty::Entity, |e| { + if id_is(e, "minecraft:mooshroom") { + let initial = empty_map(); + let entry = update_stew_entry(e, initial.clone()); + if entry != initial { + set(e, "stew_effects", Tag::List(vec![entry])); + } + remove(e, "EffectId"); + remove(e, "EffectDuration"); + } + if id_is(e, "minecraft:arrow") { + update_effect_list(e, "CustomPotionEffects", "custom_potion_effects"); + } + if id_is(e, "minecraft:area_effect_cloud") { + update_effect_list(e, "Effects", "effects"); + } + update_effect_list(e, "ActiveEffects", "active_effects"); + }); + cx.each(Ty::Player, |p| update_effect_list(p, "ActiveEffects", "active_effects")); + cx.each(Ty::ItemStack, |s| { + let Some(id) = as_str(s.get("id")).map(|i| namespaced(&i)) else { + return; + }; + let Some(tag) = s.get_mut("tag") else { + return; + }; + if id == "minecraft:suspicious_stew" { + let value = is_list(tag.get("Effects")).then(|| { + Tag::List(stream(tag.get("Effects")).into_iter().map(|e| update_stew_entry(&e.clone(), e)).collect()) + }); + replace_field(tag, "Effects", "effects", value); + } else if matches!( + id.as_str(), + "minecraft:potion" | "minecraft:splash_potion" | "minecraft:lingering_potion" | "minecraft:tipped_arrow" + ) { + update_effect_list(tag, "CustomPotionEffects", "custom_potion_effects"); + } + }); +} + +// ------------------------------------------------------------------ 3683, 3685 + +fn primed_tnt_block_state(cx: &mut Cx) { + cx.entity("minecraft:tnt", |e| { + set(e, "block_state", Tag::compound([("Name", string("minecraft:tnt"))])); + if let Some(fuse) = e.get("Fuse").cloned() { + set(e, "fuse", fuse); + } + }); +} + +fn projectile_stored_item(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + let item = if id_is(e, "minecraft:arrow") { + let potion = as_string(e.get("Potion"), "minecraft:empty"); + if potion == "minecraft:empty" { "minecraft:arrow" } else { "minecraft:tipped_arrow" } + } else if id_is(e, "minecraft:spectral_arrow") { + "minecraft:spectral_arrow" + } else { + // The trident's value is cast to the new type: its Trident field is `item`. + if id_is(e, "minecraft:trident") { + rename(e, "Trident", "item"); + } + return; + }; + let stack = Tag::compound([("id", string(item)), ("Count", Tag::Int(1))]); + set(e, "item", stack); + // The new stack is read with the output schema (V3685): its hook. + if let Some(s) = e.get_mut("item") { + items::add_names(s); + } + }); +} + +// ------------------------------------------------------------------ 3692 renames + +/// `LegacyBlockRenameFix`: block names, block states and flat block states. +pub(super) fn block_rename(cx: &mut Cx, table: &[(&str, &'static str)]) { + let r = renamer(table); + cx.rename(Ty::BlockName, &r); + cx.each(Ty::BlockState, |v| { + if let Some(name) = as_str(v.get("Name")) + && let Some(new) = r(&name) + { + set(v, "Name", string(new)); + } + }); + cx.each(Ty::FlatBlockState, |v| { + if let Some(s) = as_str(Some(v)) { + let props = s.find('[').filter(|&i| i > 0); + let nbt = s.find('{').filter(|&i| i > 0); + let end = match (props, nbt) { + (Some(a), Some(b)) => a.min(b), + (Some(a), None) => a, + (None, Some(b)) => b, + (None, None) => s.len(), + }; + let name = &s[..end]; + let new = r(name).map_or_else(|| name.to_owned(), str::to_owned); + *v = string(&format!("{new}{}", &s[end..])); + } + }); +} + +// ------------------------------------------------------------------ 3803 + +fn rename_sweeping(cx: &mut Cx) { + cx.each(Ty::ItemStack, |s| { + let Some(tag) = s.get_mut("tag") else { + return; + }; + for field in ["Enchantments", "StoredEnchantments"] { + if let Some(list) = tag.get(field) + && is_list(Some(list)) + { + let fixed = stream(Some(list)) + .into_iter() + .map(|mut e| { + if let Some(id) = as_str(e.get("id")) { + let new = if namespaced(&id) == "minecraft:sweeping" { "minecraft:sweeping_edge".into() } else { id }; + set(&mut e, "id", string(&new)); + } + e + }) + .collect(); + set(tag, field, Tag::List(fixed)); + } + } + }); +} + +// ------------------------------------------------------------------ 3807.1 + +/// `ExtraDataFixUtils.fixBlockPos`. +pub(super) fn fix_block_pos(v: Tag) -> Tag { + let (x, y, z) = (v.get("X").and_then(as_number), v.get("Y").and_then(as_number), v.get("Z").and_then(as_number)); + match (x, y, z) { + (Some(x), Some(y), Some(z)) => int_list(&[x.int_value(), y.int_value(), z.int_value()]), + _ => v, + } +} +/// `ExtraDataFixUtils.fixInlineBlockPos`. +pub(super) fn fix_inline_block_pos(v: &mut Tag, fx: &str, fy: &str, fz: &str, new: &str) { + let (x, y, z) = (v.get(fx).and_then(as_number), v.get(fy).and_then(as_number), v.get(fz).and_then(as_number)); + if let (Some(x), Some(y), Some(z)) = (x, y, z) { + remove(v, fx); + remove(v, fy); + remove(v, fz); + set(v, new, int_list(&[x.int_value(), y.int_value(), z.int_value()])); + } +} + +fn map_banner_block_pos(cx: &mut Cx) { + cx.each(Ty::SavedMapData, |v| { + if let Some(d) = v.get_mut("data") + && let Some(Tag::List(banners)) = d.get_mut("banners") + { + for b in banners { + super::dynamic::update(b, "Pos", fix_block_pos); + } + } + }); +} + +// ------------------------------------------------------------------ 3808, 3809, 3812 + +fn horse_body_armor(cx: &mut Cx, id: &str, field: &str, clear_armor_items: bool) { + cx.entity(id, |e| { + let Some(armor) = e.get(field).cloned() else { + return; + }; + remove(e, field); + if clear_armor_items { + super::dynamic::update(e, "ArmorItems", |list| { + Tag::List( + stream(Some(&list)) + .into_iter() + .enumerate() + .map(|(i, x)| if i == 2 { empty_map() } else { x }) + .collect(), + ) + }); + super::dynamic::update(e, "ArmorDropChances", |list| { + Tag::List( + stream(Some(&list)) + .into_iter() + .enumerate() + .map(|(i, x)| if i == 2 { Tag::Float(0.085f32.to_bits()) } else { x }) + .collect(), + ) + }); + } + set(e, "body_armor_item", armor); + set(e, "body_armor_drop_chance", Tag::Float(2.0f32.to_bits())); + }); +} + +fn chested_horse_slots(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + if !["minecraft:llama", "minecraft:trader_llama", "minecraft:mule", "minecraft:donkey"] + .iter() + .any(|id| id_is(e, id)) + { + return; + } + if let Some(Tag::List(items)) = e.get_mut("Items") { + for item in items { + if let Some(slot) = item.get("Slot") { + let n = as_number(slot).map_or(2, |n| n.int_value()); + set(item, "Slot", Tag::Byte((n - 2) as i8)); + } + } + } + }); +} + +fn wolf_health(cx: &mut Cx) { + cx.entity("minecraft:wolf", |e| { + let mut adjusted = false; + super::dynamic::update(e, "Attributes", |list| { + Tag::List( + stream(Some(&list)) + .into_iter() + .map(|mut a| { + if namespaced(&as_string(a.get("Name"), "")) == "minecraft:generic.max_health" { + super::dynamic::update(&mut a, "Base", |base| { + if as_double(Some(&base), 0.0) == 20.0 { + adjusted = true; + Tag::Double(40.0f64.to_bits()) + } else { + base + } + }); + } + a + }) + .collect(), + ) + }); + if adjusted { + super::dynamic::update(e, "Health", |h| Tag::Float((as_float(Some(&h), 0.0) * 2.0).to_bits())); + } + }); +} + +// ------------------------------------------------------------------ 3813 + +const PATROLLING_MOBS: [&str; 6] = [ + "minecraft:witch", + "minecraft:ravager", + "minecraft:pillager", + "minecraft:illusioner", + "minecraft:evoker", + "minecraft:vindicator", +]; + +fn rename_block_pos(v: &mut Tag, fields: &[(&str, &str)]) { + for (old, new) in fields { + rename_and_fix(v, old, new, |p| Some(fix_block_pos(p))); + } +} + +fn block_pos_format(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + if id_is(e, "minecraft:bee") { + rename_block_pos(e, &[("HivePos", "hive_pos"), ("FlowerPos", "flower_pos")]); + } + if id_is(e, "minecraft:end_crystal") { + rename_block_pos(e, &[("BeamTarget", "beam_target")]); + } + if id_is(e, "minecraft:wandering_trader") { + rename_block_pos(e, &[("WanderTarget", "wander_target")]); + } + if PATROLLING_MOBS.iter().any(|id| id_is(e, id)) { + rename_block_pos(e, &[("PatrolTarget", "patrol_target")]); + } + rename_block_pos(e, &[("Leash", "leash")]); + }); + cx.block_entity("minecraft:beehive", |be| rename_block_pos(be, &[("FlowerPos", "flower_pos")])); + cx.block_entity("minecraft:end_gateway", |be| rename_block_pos(be, &[("ExitPortal", "exit_portal")])); + cx.each(Ty::SavedMapData, |v| { + if let Some(d) = v.get_mut("data") { + super::dynamic::update(d, "frames", |frames| { + Tag::List( + stream(Some(&frames)) + .into_iter() + .map(|mut f| { + rename_and_fix(&mut f, "Pos", "pos", |p| Some(fix_block_pos(p))); + rename(&mut f, "Rotation", "rotation"); + rename(&mut f, "EntityId", "entity_id"); + f + }) + .collect(), + ) + }); + super::dynamic::update(d, "banners", |banners| { + Tag::List( + stream(Some(&banners)) + .into_iter() + .map(|mut b| { + rename(&mut b, "Pos", "pos"); + rename(&mut b, "Color", "color"); + rename(&mut b, "Name", "name"); + b + }) + .collect(), + ) + }); + } + }); + cx.item(|id| id == "minecraft:compass", |s| { + if let Some(tag) = s.get_mut("tag") { + super::dynamic::update(tag, "LodestonePos", fix_block_pos); + } + }); +} + +// ------------------------------------------------------------------ 3814 + +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) + { + set(v, field, string(new)); + } + }; + cx.each(Ty::ItemStack, |s| { + if let Some(tag) = s.get_mut("tag") + && let Some(Tag::List(mods)) = tag.get_mut("AttributeModifiers") + { + for m in mods { + fix_name(m, "AttributeName"); + } + } + }); + let fix_entity = |e: &mut Tag| { + if let Some(Tag::List(attrs)) = e.get_mut("Attributes") { + for a in attrs { + fix_name(a, "Name"); + } + } + }; + cx.each(Ty::Entity, fix_entity); + cx.each(Ty::Player, fix_entity); +} + +// ------------------------------------------------------------------ 3818 + +fn beehive_fields(cx: &mut Cx) { + cx.block_entity("minecraft:beehive", |be| { + // The typed Bees list moves to `bees` (the schema renames it); the + // remainder loses Bees, each bee loses EntityData's old name. + if let Some(Tag::List(bees)) = remove(be, "Bees") { + let bees = bees + .into_iter() + .map(|mut bee| { + let data = remove(&mut bee, "EntityData"); + rename(&mut bee, "TicksInHive", "ticks_in_hive"); + rename(&mut bee, "MinOccupationTicks", "min_ticks_in_hive"); + if let Some(data) = data { + set(&mut bee, "entity_data", data); + } + bee + }) + .collect(); + set(be, "bees", Tag::List(bees)); + } + }); +} + +const PATTERN_IDS: [(&str, &str); 41] = [ + ("b", "minecraft:base"), + ("bl", "minecraft:square_bottom_left"), + ("br", "minecraft:square_bottom_right"), + ("tl", "minecraft:square_top_left"), + ("tr", "minecraft:square_top_right"), + ("bs", "minecraft:stripe_bottom"), + ("ts", "minecraft:stripe_top"), + ("ls", "minecraft:stripe_left"), + ("rs", "minecraft:stripe_right"), + ("cs", "minecraft:stripe_center"), + ("ms", "minecraft:stripe_middle"), + ("drs", "minecraft:stripe_downright"), + ("dls", "minecraft:stripe_downleft"), + ("ss", "minecraft:small_stripes"), + ("cr", "minecraft:cross"), + ("sc", "minecraft:straight_cross"), + ("bt", "minecraft:triangle_bottom"), + ("tt", "minecraft:triangle_top"), + ("bts", "minecraft:triangles_bottom"), + ("tts", "minecraft:triangles_top"), + ("ld", "minecraft:diagonal_left"), + ("rd", "minecraft:diagonal_up_right"), + ("lud", "minecraft:diagonal_up_left"), + ("rud", "minecraft:diagonal_right"), + ("mc", "minecraft:circle"), + ("mr", "minecraft:rhombus"), + ("vh", "minecraft:half_vertical"), + ("hh", "minecraft:half_horizontal"), + ("vhr", "minecraft:half_vertical_right"), + ("hhb", "minecraft:half_horizontal_bottom"), + ("bo", "minecraft:border"), + ("cbo", "minecraft:curly_border"), + ("gra", "minecraft:gradient"), + ("gru", "minecraft:gradient_up"), + ("bri", "minecraft:bricks"), + ("glb", "minecraft:globe"), + ("cre", "minecraft:creeper"), + ("sku", "minecraft:skull"), + ("flo", "minecraft:flower"), + ("moj", "minecraft:mojang"), + ("pig", "minecraft:piglin"), +]; + +/// `ExtraDataFixUtils.dyeColorIdToName`. +pub(super) fn dye_color(id: i32) -> &'static str { + match id { + 1 => "orange", + 2 => "magenta", + 3 => "light_blue", + 4 => "yellow", + 5 => "lime", + 6 => "pink", + 7 => "gray", + 8 => "light_gray", + 9 => "cyan", + 10 => "purple", + 11 => "blue", + 12 => "brown", + 13 => "green", + 14 => "red", + 15 => "black", + _ => "white", + } +} + +pub(super) fn banner_layer(mut v: Tag) -> Tag { + rename_and_fix(&mut v, "Pattern", "pattern", |p| { + Some(match as_str(Some(&p)) { + Some(id) => string(PATTERN_IDS.iter().find(|(k, _)| *k == id).map_or(id.as_str(), |(_, n)| n)), + None => p, + }) + }); + let color = dye_color(as_int(v.get("Color"), 0)); + set(&mut v, "color", string(color)); + remove(&mut v, "Color"); + v +} + +fn banner_patterns(cx: &mut Cx) { + cx.block_entity("minecraft:banner", |be| { + rename_and_fix(be, "Patterns", "patterns", |p| { + Some(Tag::List(stream(Some(&p)).into_iter().map(banner_layer).collect())) + }); + }); +} + +fn tipped_arrow_potion(cx: &mut Cx) { + cx.entity("minecraft:arrow", |e| { + let potion = e.get("Potion").cloned(); + let effects = e.get("custom_potion_effects").cloned(); + let color = e.get("Color").cloned(); + if potion.is_none() && effects.is_none() && color.is_none() { + return; + } + remove(e, "Potion"); + remove(e, "custom_potion_effects"); + remove(e, "Color"); + super::dynamic::update(e, "item", |mut stack| { + let mut tag = super::dynamic::or_empty_map(stack.get("tag")); + if let Some(p) = potion { + set(&mut tag, "Potion", p); + } + if let Some(c) = effects { + set(&mut tag, "custom_potion_effects", c); + } + if let Some(c) = color { + set(&mut tag, "CustomPotionColor", c); + } + set(&mut stack, "tag", tag); + stack + }); + }); +} + +fn particle_unflattening(cx: &mut Cx) { + cx.each(Ty::Particle, |p| { + if let Some(s) = as_str(Some(p)) { + *p = items::unflatten_particle(&s); + } + }); +} + +fn area_effect_cloud_potion(cx: &mut Cx) { + cx.entity("minecraft:area_effect_cloud", |e| { + let color = remove(e, "Color"); + let effects = remove(e, "effects"); + let potion = remove(e, "Potion"); + if color.is_none() && effects.is_none() && potion.is_none() { + return; + } + let mut contents = empty_map(); + if let Some(c) = color { + set(&mut contents, "custom_color", c); + } + if let Some(c) = effects { + set(&mut contents, "custom_effects", c); + } + if let Some(p) = potion { + set(&mut contents, "potion", p); + } + set(e, "potion_contents", contents); + }); +} + +// ------------------------------------------------------------------ 3820 + +fn player_head_profile(cx: &mut Cx) { + cx.block_entity("minecraft:skull", |be| { + let profile = be.get("SkullOwner").or_else(|| be.get("ExtraType")).cloned(); + let Some(profile) = profile else { + return; + }; + remove(be, "SkullOwner"); + remove(be, "ExtraType"); + set(be, "profile", items::fix_profile(&profile)); + }); +} + +/// `DataComponentRemainderFix`: one component of every component map. +pub(super) fn component_fix(cx: &mut Cx, id: &str, new_id: &str, f: impl Fn(Tag) -> Option) { + cx.each(Ty::DataComponents, |c| { + if let Some(component) = remove(c, id) + && let Some(new) = f(component) + { + set(c, new_id, new); + } + }); +} + +fn lodestone_compass_component(cx: &mut Cx) { + component_fix(cx, "minecraft:lodestone_target", "minecraft:lodestone_tracker", |mut v| { + let pos = remove(&mut v, "pos"); + let dimension = remove(&mut v, "dimension"); + if let (Some(pos), Some(dimension)) = (pos, dimension) { + set(&mut v, "target", Tag::compound([("pos", pos), ("dimension", dimension)])); + } + Some(v) + }); +} + +// ------------------------------------------------------------------ 3825 + +const MAP_NAMES: [&str; 11] = [ + "filled_map.buried_treasure", + "filled_map.explorer_jungle", + "filled_map.explorer_swamp", + "filled_map.mansion", + "filled_map.monument", + "filled_map.trial_chambers", + "filled_map.village_desert", + "filled_map.village_plains", + "filled_map.village_savanna", + "filled_map.village_snowy", + "filled_map.village_taiga", +]; + +/// `LegacyComponentDataFixUtils.extractTranslationString`. +pub(super) fn translation_key(component: &Tag) -> Option { + let Tag::String(s) = component else { + return None; + }; + match json::parse_lenient(&s.0)? { + j @ json::Json::Obj(_) => match j.get("translate")? { + json::Json::Str(k) => Some(String::from_utf16_lossy(k)), + json::Json::Num(n) => Some(n.clone()), + json::Json::Bool(b) => Some(b.to_string()), + _ => None, + }, + _ => None, + } +} + +fn item_custom_name_to_item_name(cx: &mut Cx) { + cx.each(Ty::ItemStack, |s| { + let id = namespaced(&as_str(s.get("id")).unwrap_or_default()); + let expected: &dyn Fn(&str) -> bool = match id.as_str() { + "minecraft:white_banner" => &|k: &str| k == "block.minecraft.ominous_banner", + "minecraft:filled_map" => &|k: &str| MAP_NAMES.contains(&k), + _ => return, + }; + if let Some(c) = s.get_mut("components") + && c.get("minecraft:custom_name").and_then(translation_key).is_some_and(|k| expected(&k)) + { + rename(c, "minecraft:custom_name", "minecraft:item_name"); + } + }); +} + +fn banner_custom_name_to_item_name(cx: &mut Cx) { + cx.block_entity("minecraft:banner", |be| { + let Some(name) = be.get("CustomName").cloned() else { + return; + }; + if translation_key(&name).as_deref() != Some("block.minecraft.ominous_banner") { + return; + } + set( + be, + "components", + Tag::compound([("minecraft:item_name", name), ("minecraft:hide_additional_tooltip", empty_map())]), + ); + remove(be, "CustomName"); + }); +} + +fn trial_spawner_config(cx: &mut Cx) { + cx.block_entity("minecraft:trial_spawner", |be| { + let mut config = empty_map(); + let mut moved = false; + for k in [ + "spawn_range", + "total_mobs", + "simultaneous_mobs", + "total_mobs_added_per_player", + "simultaneous_mobs_added_per_player", + "ticks_between_spawn", + "spawn_potentials", + "loot_tables_to_eject", + "items_to_drop_when_ominous", + ] { + if let Some(v) = remove(be, k) { + set(&mut config, k, v); + moved = true; + } + } + if moved { + set(be, "normal_config", config); + } + }); +} + +// ------------------------------------------------------------------ 3828, 3833 + +fn empty_item_in_villager_trade(cx: &mut Cx) { + cx.each(Ty::VillagerTrade, |t| { + let buy_b = super::dynamic::or_empty_map(t.get("buyB")); + let id = namespaced(&as_string(buy_b.get("id"), "minecraft:air")); + let count = as_int(buy_b.get("count"), 0); + if id == "minecraft:air" || count == 0 { + remove(t, "buyB"); + } + }); +} + +fn remove_empty_brushable_item(cx: &mut Cx) { + cx.block_entity("minecraft:brushable_block", |be| { + if let Some(item) = be.get("item") { + let id = namespaced(&as_string(item.get("id"), "minecraft:air")); + if id == "minecraft:air" || as_int(item.get("count"), 0) == 0 { + remove(be, "item"); + } + } + }); +} + +// ------------------------------------------------------------------ scoreboard objective hooks + +/// `V1451_6.UNPACK_OBJECTIVE_ID`. +pub(super) fn unpack_objective(v: &mut Tag) { + let Some(name) = as_str(v.get("CriteriaName")) else { + return; + }; + let (ty, id) = match name.find(':') { + None => ("_special".to_owned(), name.clone()), + Some(colon) => match (by_separator(&name[..colon]), by_separator(&name[colon + 1..])) { + (Some(t), Some(i)) => (t, i), + _ => ("_special".to_owned(), name.clone()), + }, + }; + set(v, "CriteriaType", Tag::compound([("type", string(&ty)), ("id", string(&id))])); +} + +/// `Identifier.bySeparator(s, '.')`: namespace before the first dot. +fn by_separator(s: &str) -> Option { + let (ns, path) = match s.find('.') { + Some(i) => (&s[..i], &s[i + 1..]), + None => ("minecraft", s), + }; + let ns = if ns.is_empty() { "minecraft" } else { ns }; + super::identifier(&format!("{ns}:{path}")).map(|(n, p)| format!("{n}:{p}")) +} + +/// `V1451_6.REPACK_OBJECTIVE_ID`. +pub(super) fn repack_objective(v: &mut Tag) { + let Some(ct) = v.get("CriteriaType") else { + return; + }; + let (Some(ty), Some(id)) = (as_str(ct.get("type")), as_str(ct.get("id"))) else { + return; + }; + let name = if ty == "_special" { id } else { format!("{}:{}", pack_with_dot(&ty), pack_with_dot(&id)) }; + set(v, "CriteriaName", string(&name)); + remove(v, "CriteriaType"); +} +fn pack_with_dot(location: &str) -> String { + match super::identifier(location) { + Some((ns, path)) => format!("{ns}.{path}"), + None => location.to_owned(), + } +} diff --git a/crates/horizon-world/src/datafix/fixes_b.rs b/crates/horizon-world/src/datafix/fixes_b.rs new file mode 100644 index 000000000..656bc5dde --- /dev/null +++ b/crates/horizon-world/src/datafix/fixes_b.rs @@ -0,0 +1,1140 @@ +//! Fixes of 1.21 to 1.21.5 (keys 3938 to 4314), text components apart. +use super::dynamic::{ + as_bool, as_double, as_float, as_int, as_long, as_number, as_str, as_string, empty_map, is_empty_map, is_list, + namespaced, or_empty_map, remove, rename, rename_and_fix, set, stream, string, update, +}; +use super::fixes_a::{component_fix, dye_color, fix_inline_block_pos, translation_key}; +use super::items::java_hash_order; +use super::schema::{Key, Ty, key}; +use super::{Cx, FixFn, id_is}; +use crate::nbt::{Compound, Tag}; + +pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + out.push((key(3939, 0), |cx| feature_flag_remove(cx, &["minecraft:update_1_21"]))); + out.push((key(3945, 0), attribute_modifier_ids)); + out.push((key(3945, 0), jukebox_ticks)); + out.push((key(4054, 0), ominous_banner_rarity)); + out.push((key(4055, 0), |cx| attributes_rename(cx, true, &attribute_id_prefix))); + out.push((key(4059, 0), food_to_consumable)); + out.push((key(4061, 0), trial_spawner_config_in_registry)); + out.push((key(4064, 0), |cx| { + component_fix(cx, "minecraft:fire_resistant", "minecraft:damage_resistant", |_| { + Some(Tag::compound([("types", string("#minecraft:is_fire"))])) + }); + })); + out.push((key(4067, 0), boat_split)); + out.push((key(4067, 0), |cx| feature_flag_remove(cx, &["minecraft:bundle"]))); + out.push((key(4068, 0), |cx| component_fix(cx, "minecraft:lock", "minecraft:lock", fix_lock))); + out.push((key(4068, 0), |cx| { + cx.each(Ty::BlockEntity, |be| rename_and_fix(be, "Lock", "lock", fix_lock)); + })); + out.push((key(4081, 0), |cx| { + cx.entity("minecraft:salmon", |e| { + if as_string(e.get("type"), "medium") != "large" { + set(e, "type", string("medium")); + } + }); + })); + out.push((key(4173, 0), |cx| cx.entity("minecraft:tnt_minecart", |e| rename(e, "TNTFuse", "fuse")))); + out.push((key(4175, 0), |cx| { + cx.each(Ty::DataComponents, |c| { + if let Some(eq) = c.get_mut("minecraft:equippable") { + rename(eq, "model", "asset_id"); + } + }); + })); + out.push((key(4175, 0), |cx| { + cx.each(Ty::DataComponents, |c| { + update(c, "minecraft:custom_model_data", |cmd| { + let value = as_number(&cmd).map_or(0.0, |n| n.float_value()); + Tag::compound([("floats", Tag::List(vec![Tag::Float(value.to_bits())]))]) + }); + }); + })); + out.push((key(4176, 0), |cx| { + cx.each(Ty::BlockEntity, |be| { + if let Some(lock) = remove(be, "lock") + && !broken_lock(&lock) + { + set(be, "lock", lock); + } + }); + })); + out.push((key(4176, 0), |cx| { + component_fix(cx, "minecraft:lock", "minecraft:lock", |l| (!broken_lock(&l)).then_some(l)); + })); + out.push((key(4180, 0), |cx| feature_flag_remove(cx, &["minecraft:winter_drop"]))); + for id in ["minecraft:furnace", "minecraft:smoker", "minecraft:blast_furnace"] { + let _ = id; + } + out.push((key(4181, 0), |cx| furnace_burn_time(cx, "minecraft:furnace"))); + out.push((key(4181, 0), |cx| furnace_burn_time(cx, "minecraft:smoker"))); + out.push((key(4181, 0), |cx| furnace_burn_time(cx, "minecraft:blast_furnace"))); + out.push((key(4187, 0), |cx| base_value(cx, "minecraft:villager", "minecraft:follow_range", 48.0, 16.0))); + out.push((key(4187, 0), |cx| base_value(cx, "minecraft:bee", "minecraft:follow_range", 48.0, 16.0))); + out.push((key(4187, 0), |cx| base_value(cx, "minecraft:allay", "minecraft:follow_range", 48.0, 16.0))); + out.push((key(4187, 0), |cx| base_value(cx, "minecraft:llama", "minecraft:follow_range", 40.0, 16.0))); + out.push((key(4187, 0), |cx| base_value(cx, "minecraft:piglin_brute", "minecraft:follow_range", 16.0, 12.0))); + out.push((key(4187, 0), |cx| base_value(cx, "minecraft:warden", "minecraft:follow_range", 16.0, 24.0))); + super::text::register(out); + out.push((key(4293, 0), drop_chances_format)); + out.push((key(4294, 0), |cx| { + block_property_rename(cx, "minecraft:creaking_heart", "active", "creaking_heart_state", &|v| { + if v == "true" { "awake".into() } else { "uprooted".into() } + }); + })); + out.push((key(4296, 0), |cx| { + cx.entity("minecraft:area_effect_cloud", |e| set(e, "potion_duration_scale", Tag::Float(0.25f32.to_bits()))); + })); + out.push((key(4297, 0), forced_chunk_to_ticket)); + out.push((key(4299, 0), entity_spawner_item_variants)); + out.push((key(4300, 0), saddle_equipment_slot)); + out.push((key(4301, 0), equipment_format)); + out.push((key(4303, 0), |cx| cx.each_out(Ty::Entity, fall_distance_to_double))); + out.push((key(4303, 0), |cx| cx.each_out(Ty::Player, fall_distance_to_double))); + out.push((key(4305, 0), |cx| { + block_property_rename(cx, "minecraft:test_block", "test_block_mode", "mode", &|v| v.to_owned()); + })); + out.push((key(4306, 0), thrown_potion_split)); + out.push((key(4307, 0), tooltip_display)); + out.push((key(4309, 0), raid_renames)); + out.push((key(4309, 0), chunk_ticket_unpack_pos)); + out.push((key(4311, 0), |cx| { + advancements_rename(cx, &|id| { + (namespaced(id) == "minecraft:nether/use_lodestone").then(|| "minecraft:adventure/use_lodestone".into()) + }); + })); + out.push((key(4312, 0), player_equipment)); + out.push((key(4314, 0), inline_block_pos_format)); +} + +// ------------------------------------------------------------------ 3939, 4067, 4180, 4424 + +pub(super) fn feature_flag_remove(cx: &mut Cx, flags: &[&str]) { + cx.each(Ty::LightweightLevel, |level| { + let mut inactive: Vec = stream(level.get("removed_features")); + if let Some(features) = level.get("enabled_features") + && is_list(Some(features)) + { + let kept: Vec = stream(Some(features)) + .into_iter() + .filter(|f| match as_str(Some(f)) { + Some(s) if flags.contains(&s.as_str()) => { + inactive.push(string(&s)); + false + } + _ => true, + }) + .collect(); + set(level, "enabled_features", Tag::List(kept)); + } + if !inactive.is_empty() { + set(level, "removed_features", Tag::List(inactive)); + } + }); +} + +// ------------------------------------------------------------------ 3945 + +fn uuid_text(u: [i32; 4]) -> String { + let most = (i64::from(u[0]) << 32) | (i64::from(u[1]) & 0xFFFF_FFFF); + let least = (i64::from(u[2]) << 32) | (i64::from(u[3]) & 0xFFFF_FFFF); + let (m, l) = (most as u64, least as u64); + format!( + "{:08x}-{:04x}-{:04x}-{:04x}-{:012x}", + m >> 32, + (m >> 16) & 0xFFFF, + m & 0xFFFF, + l >> 48, + l & 0xFFFF_FFFF_FFFF + ) +} + +const MODIFIER_IDS: &[(&str, &str)] = &[ + ("736565d2-e1a7-403d-a3f8-1aeb3e302542", "minecraft:creative_mode_block_range"), + ("98491ef6-97b1-4584-ae82-71a8cc85cf73", "minecraft:creative_mode_entity_range"), + ("91aeaa56-376b-4498-935b-2f7f68070635", "minecraft:effect.speed"), + ("7107de5e-7ce8-4030-940e-514c1f160890", "minecraft:effect.slowness"), + ("af8b6e3f-3328-4c0a-aa36-5ba2bb9dbef3", "minecraft:effect.haste"), + ("55fced67-e92a-486e-9800-b47f202c4386", "minecraft:effect.mining_fatigue"), + ("648d7064-6a60-4f59-8abe-c2c23a6dd7a9", "minecraft:effect.strength"), + ("c0105bf3-aef8-46b0-9ebc-92943757ccbe", "minecraft:effect.jump_boost"), + ("22653b89-116e-49dc-9b6b-9971489b5be5", "minecraft:effect.weakness"), + ("5d6f0ba2-1186-46ac-b896-c61c5cee99cc", "minecraft:effect.health_boost"), + ("eae29cf0-701e-4ed6-883a-96f798f3dab5", "minecraft:effect.absorption"), + ("03c3c89d-7037-4b42-869f-b146bcb64d2e", "minecraft:effect.luck"), + ("cc5af142-2bd2-4215-b636-2605aed11727", "minecraft:effect.unluck"), + ("6555be74-63b3-41f1-a245-77833b3c2562", "minecraft:evil"), + ("1eaf83ff-7207-4596-b37a-d7a07b3ec4ce", "minecraft:powder_snow"), + ("662a6b8d-da3e-4c1c-8813-96ea6097278d", "minecraft:sprinting"), + ("020e0dfb-87ae-4653-9556-831010e291a0", "minecraft:attacking"), + ("766bfa64-11f3-11ea-8d71-362b9e155667", "minecraft:baby"), + ("7e0292f2-9434-48d5-a29f-9583af7df27f", "minecraft:covered"), + ("9e362924-01de-4ddd-a2b2-d0f7a405a174", "minecraft:suffocating"), + ("5cd17e52-a79a-43d3-a529-90fde04b181e", "minecraft:drinking"), + ("b9766b59-9566-4402-bc1f-2ee2a276d836", "minecraft:baby"), + ("49455a49-7ec5-45ba-b886-3b90b23a1718", "minecraft:attacking"), + ("845db27c-c624-495f-8c9f-6020a9a58b6b", "minecraft:armor.boots"), + ("d8499b04-0e66-4726-ab29-64469d734e0d", "minecraft:armor.leggings"), + ("9f3d476d-c118-4544-8365-64846904b48e", "minecraft:armor.chestplate"), + ("2ad3f246-fee1-4e67-b886-69fd380bb150", "minecraft:armor.helmet"), + ("c1c72771-8b8e-ba4a-ace0-81a93c8928b2", "minecraft:armor.body"), + ("b572ecd2-ac0c-4071-abde-9594af072a37", "minecraft:enchantment.fire_protection"), + ("40a9968f-5c66-4e2f-b7f4-2ec2f4b3e450", "minecraft:enchantment.blast_protection"), + ("07a65791-f64d-4e79-86c7-f83932f007ec", "minecraft:enchantment.respiration"), + ("60b1b7db-fffd-4ad0-817c-d6c6a93d8a45", "minecraft:enchantment.aqua_affinity"), + ("11dc269a-4476-46c0-aff3-9e17d7eb6801", "minecraft:enchantment.depth_strider"), + ("87f46a96-686f-4796-b035-22e16ee9e038", "minecraft:enchantment.soul_speed"), + ("b9716dbd-50df-4080-850e-70347d24e687", "minecraft:enchantment.soul_speed"), + ("92437d00-c3a7-4f2e-8f6c-1f21585d5dd0", "minecraft:enchantment.swift_sneak"), + ("5d3d087b-debe-4037-b53e-d84f3ff51f17", "minecraft:enchantment.sweeping_edge"), + ("3ceb37c0-db62-46b5-bd02-785457b01d96", "minecraft:enchantment.efficiency"), + ("cb3f55d3-645c-4f38-a497-9c13a33db5cf", "minecraft:base_attack_damage"), + ("fa233e1c-4180-4865-b01b-bcce9785aca3", "minecraft:base_attack_speed"), +]; +const MODIFIER_NAMES: &[(&str, &str)] = &[ + ("Random spawn bonus", "minecraft:random_spawn_bonus"), + ("Random zombie-spawn bonus", "minecraft:zombie_random_spawn_bonus"), + ("Leader zombie bonus", "minecraft:leader_zombie_bonus"), + ("Zombie reinforcement callee charge", "minecraft:reinforcement_callee_charge"), + ("Zombie reinforcement caller charge", "minecraft:reinforcement_caller_charge"), +]; + +/// `asIntStream` of a UUID field, as `uuidFromIntArray` wants it. +fn uuid_of(v: Option<&Tag>) -> Option<[i32; 4]> { + let ints: Vec = match v { + Some(Tag::IntArray(a)) => a.clone(), + other => stream(other).iter().map(|t| as_number(t).map_or(0, |n| n.int_value())).collect(), + }; + (ints.len() == 4).then(|| [ints[0], ints[1], ints[2], ints[3]]) +} + +/// `AttributeModifierIdFix.fixModifiers`: an insertion-ordered map by id. +fn fix_modifiers(modifiers: Vec) -> Vec { + let mut converted: Vec<(String, Tag)> = Vec::new(); + let put = |c: &mut Vec<(String, Tag)>, id: String, v: Tag| { + if let Some(e) = c.iter_mut().find(|(k, _)| *k == id) { + e.1 = v; + } else { + c.push((id, v)); + } + }; + for mut m in modifiers { + let uuid = uuid_of(m.get("uuid")).map(uuid_text); + let name = as_string(m.get("name"), ""); + let from_uuid = uuid.as_deref().and_then(|u| MODIFIER_IDS.iter().find(|(k, _)| *k == u).map(|(_, v)| *v)); + let from_name = MODIFIER_NAMES.iter().find(|(k, _)| *k == name).map(|(_, v)| *v); + if let Some(id) = from_uuid { + set(&mut m, "id", string(id)); + remove(&mut m, "uuid"); + remove(&mut m, "name"); + put(&mut converted, id.to_owned(), m); + } else if let Some(id) = from_name { + if let Some((_, pre)) = converted.iter_mut().find(|(k, _)| k == id) { + let amount = as_double(pre.get("amount"), 0.0) + as_double(m.get("amount"), 0.0); + set(pre, "amount", Tag::Double(amount.to_bits())); + } else { + set(&mut m, "id", string(id)); + remove(&mut m, "uuid"); + remove(&mut m, "name"); + put(&mut converted, id.to_owned(), m); + } + } else { + let id = format!("minecraft:{}", uuid.as_deref().unwrap_or("unknown")); + set(&mut m, "id", string(&id)); + remove(&mut m, "uuid"); + remove(&mut m, "name"); + put(&mut converted, id, m); + } + } + converted.into_iter().map(|(_, v)| v).collect() +} + +fn attribute_modifier_ids(cx: &mut Cx) { + cx.each(Ty::ItemStack, |s| { + if let Some(c) = s.get_mut("components") + && let Some(a) = c.get_mut("minecraft:attribute_modifiers") + && let Some(m) = a.get("modifiers") + && is_list(Some(m)) + { + let fixed = fix_modifiers(stream(Some(m))); + set(a, "modifiers", Tag::List(fixed)); + } + }); + let fix_entity = |e: &mut Tag| { + rename_and_fix(e, "Attributes", "attributes", |list| { + if !is_list(Some(&list)) { + return Some(list); + } + Some(Tag::List( + stream(Some(&list)) + .into_iter() + .map(|mut a| { + rename(&mut a, "Name", "id"); + rename(&mut a, "Base", "base"); + rename_and_fix(&mut a, "Modifiers", "modifiers", |mods| { + if !is_list(Some(&mods)) { + return Some(mods); + } + let converted = stream(Some(&mods)) + .into_iter() + .map(|mut m| { + rename(&mut m, "UUID", "uuid"); + rename(&mut m, "Name", "name"); + rename(&mut m, "Amount", "amount"); + rename_and_fix(&mut m, "Operation", "operation", |op| { + Some(string(match as_int(Some(&op), 0) { + 0 => "add_value", + 1 => "add_multiplied_base", + 2 => "add_multiplied_total", + _ => "invalid", + })) + }); + m + }) + .collect(); + Some(Tag::List(fix_modifiers(converted))) + }); + a + }) + .collect(), + )) + }); + }; + cx.each(Ty::Entity, fix_entity); + cx.each(Ty::Player, fix_entity); +} + +fn jukebox_ticks(cx: &mut Cx) { + cx.block_entity("minecraft:jukebox", |be| { + let ticks = as_long(be.get("TickCount"), 0).wrapping_sub(as_long(be.get("RecordStartTick"), 0)); + remove(be, "IsPlaying"); + remove(be, "TickCount"); + remove(be, "RecordStartTick"); + if ticks > 0 { + set(be, "ticks_since_song_started", Tag::Long(ticks)); + } + }); +} + +// ------------------------------------------------------------------ 4054, 4055 + +fn ominous_banner_rarity(cx: &mut Cx) { + let fix = |c: &mut Tag| { + if c.get("minecraft:item_name").and_then(translation_key).as_deref() == Some("block.minecraft.ominous_banner") { + set( + c, + "minecraft:item_name", + Tag::String(crate::nbt::JavaString(super::json::translatable_component_json( + "block.minecraft.ominous_banner", + ))), + ); + set(c, "minecraft:rarity", string("uncommon")); + } + }; + cx.block_entity("minecraft:banner", |be| { + if let Some(c) = be.get_mut("components") { + fix(c); + } + }); + cx.item(|id| id == "minecraft:white_banner", |s| { + if let Some(c) = s.get_mut("components") { + fix(c); + } + }); +} + +fn attribute_id_prefix(id: &str) -> Option { + let namespaced_id = namespaced(id); + for prefix in ["generic.", "horse.", "player.", "zombie."] { + let p = namespaced(prefix); + if let Some(rest) = namespaced_id.strip_prefix(&p) { + return Some(format!("minecraft:{rest}")); + } + } + None +} + +/// `AttributesRenameFix`. +pub(super) fn attributes_rename(cx: &mut Cx, old_component_format: bool, rename_fn: &dyn Fn(&str) -> Option) { + let fix_field = |v: &mut Tag, field: &str| { + if let Some(s) = as_str(v.get(field)) + && let Some(new) = rename_fn(&s) + { + set(v, field, string(&new)); + } + }; + cx.each(Ty::DataComponents, |c| { + let Some(a) = c.get_mut("minecraft:attribute_modifiers") else { + return; + }; + let list = if old_component_format { a.get_mut("modifiers") } else { Some(a) }; + if let Some(Tag::List(mods)) = list { + for m in mods { + fix_field(m, "type"); + } + } + }); + let fix_entity = |e: &mut Tag| { + if let Some(Tag::List(attrs)) = e.get_mut("attributes") { + for a in attrs { + fix_field(a, "id"); + } + } + }; + cx.each(Ty::Entity, fix_entity); + cx.each(Ty::Player, fix_entity); +} + +// ------------------------------------------------------------------ 4059, 4061 + +fn food_to_consumable(cx: &mut Cx) { + cx.each(Ty::DataComponents, |c| { + let Some(food) = c.get("minecraft:food").cloned() else { + return; + }; + let eat_seconds = as_float(food.get("eat_seconds"), 1.6); + let effects: Vec = stream(food.get("effects")) + .into_iter() + .map(|e| { + Tag::compound([ + ("type", string("minecraft:apply_effects")), + ("effects", Tag::List(e.get("effect").cloned().into_iter().collect())), + ("probability", Tag::Float(as_float(e.get("probability"), 1.0).to_bits())), + ]) + }) + .collect(); + super::dynamic::copy_field(&food, "using_converts_to", c, "minecraft:use_remainder"); + let mut food = food; + remove(&mut food, "eat_seconds"); + remove(&mut food, "effects"); + remove(&mut food, "using_converts_to"); + set(c, "minecraft:food", food); + set( + c, + "minecraft:consumable", + Tag::compound([ + ("consume_seconds", Tag::Float(eat_seconds.to_bits())), + ("on_consume_effects", Tag::List(effects)), + ]), + ); + }); +} + +const TRIAL_CONFIGS: &[(&str, &str, &str)] = &[ + ( + "trial_chamber/breeze", + "{simultaneous_mobs: 1.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:breeze\"}}, weight: 1}], ticks_between_spawn: 20, total_mobs: 2.0f, total_mobs_added_per_player: 1.0f}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 2.0f, total_mobs: 4.0f}", + ), + ( + "trial_chamber/melee/husk", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:husk\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:husk\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_melee\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/melee/spider", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:spider\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],simultaneous_mobs: 4.0f, total_mobs: 12.0f}", + ), + ( + "trial_chamber/melee/zombie", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:zombie\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],spawn_potentials: [{data: {entity: {id: \"minecraft:zombie\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_melee\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/ranged/poison_skeleton", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/ranged/skeleton", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/ranged/stray", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/slow_ranged/poison_skeleton", + "{simultaneous_mobs: 4.0f, simultaneous_mobs_added_per_player: 2.0f, spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}}, weight: 1}], ticks_between_spawn: 160}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/slow_ranged/skeleton", + "{simultaneous_mobs: 4.0f, simultaneous_mobs_added_per_player: 2.0f, spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}}, weight: 1}], ticks_between_spawn: 160}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/slow_ranged/stray", + "{simultaneous_mobs: 4.0f, simultaneous_mobs_added_per_player: 2.0f, spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}}, weight: 1}], ticks_between_spawn: 160}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/small_melee/baby_zombie", + "{simultaneous_mobs: 2.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {IsBaby: 1b, id: \"minecraft:zombie\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {IsBaby: 1b, id: \"minecraft:zombie\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_melee\", slot_drop_chances: 0.0f}}, weight: 1}]}", + ), + ( + "trial_chamber/small_melee/cave_spider", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:cave_spider\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 4.0f, total_mobs: 12.0f}", + ), + ( + "trial_chamber/small_melee/silverfish", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:silverfish\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 4.0f, total_mobs: 12.0f}", + ), + ( + "trial_chamber/small_melee/slime", + "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {Size: 1, id: \"minecraft:slime\"}}, weight: 3}, {data: {entity: {Size: 2, id: \"minecraft:slime\"}}, weight: 1}], ticks_between_spawn: 20}", + "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 4.0f, total_mobs: 12.0f}", + ), +]; + +fn snbt(s: &str) -> Tag { + crate::nbt::parse_snbt(s).map(|(t, _)| t).unwrap_or_else(|_| empty_map()) +} +/// `CompoundTag.merge`. +fn merge(into: &mut Tag, other: &Tag) { + let (Tag::Compound(a), Tag::Compound(b)) = (into, other) else { + return; + }; + for (k, v) in b { + match (a.get_mut(k), v) { + (Some(existing @ Tag::Compound(_)), Tag::Compound(_)) => merge(existing, v), + _ => { + a.insert(k.clone(), v.clone()); + } + } + } +} +fn remove_trial_defaults(tag: &mut Tag) { + let int = |t: &Tag, k: &str| as_int(t.get(k), 0); + let float = |t: &Tag, k: &str| as_float(t.get(k), 0.0); + if int(tag, "spawn_range") == 4 { + remove(tag, "spawn_range"); + } + if float(tag, "total_mobs") == 6.0 { + remove(tag, "total_mobs"); + } + if float(tag, "simultaneous_mobs") == 2.0 { + remove(tag, "simultaneous_mobs"); + } + if float(tag, "total_mobs_added_per_player") == 2.0 { + remove(tag, "total_mobs_added_per_player"); + } + if float(tag, "simultaneous_mobs_added_per_player") == 1.0 { + remove(tag, "simultaneous_mobs_added_per_player"); + } + if int(tag, "ticks_between_spawn") == 40 { + remove(tag, "ticks_between_spawn"); + } +} + +fn trial_config_key(normal: &Tag, ominous: &Tag) -> Option<&'static str> { + TRIAL_CONFIGS.iter().find_map(|(name, n, o)| { + let n = snbt(n); + if *normal != n { + return None; + } + let o = snbt(o); + let mut merged = n.clone(); + merge(&mut merged, &o); + let mut trimmed = merged.clone(); + remove_trial_defaults(&mut trimmed); + (*ominous == o || *ominous == merged || *ominous == trimmed).then_some(*name) + }) +} + +fn trial_spawner_config_in_registry(cx: &mut Cx) { + cx.block_entity("minecraft:trial_spawner", |be| { + let (Some(normal), Some(ominous)) = (be.get("normal_config"), be.get("ominous_config")) else { + return; + }; + if let Some(name) = trial_config_key(normal, ominous) { + set(be, "normal_config", string(&format!("minecraft:{name}/normal"))); + set(be, "ominous_config", string(&format!("minecraft:{name}/ominous"))); + } + }); +} + +// ------------------------------------------------------------------ 4067, 4068 + +fn boat_split(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + let id = namespaced(&as_str(e.get("id")).unwrap_or_default()); + let chest = id == "minecraft:chest_boat"; + if !chest && id != "minecraft:boat" { + return; + } + let wood = as_str(e.get("Type")); + let new = match wood.as_deref() { + Some("spruce") => "spruce", + Some("birch") => "birch", + Some("jungle") => "jungle", + Some("acacia") => "acacia", + Some("cherry") => "cherry", + Some("dark_oak") => "dark_oak", + Some("mangrove") => "mangrove", + Some("bamboo") => "bamboo", + _ => "oak", + }; + let new_id = match (new, chest) { + ("bamboo", false) => "minecraft:bamboo_raft".into(), + ("bamboo", true) => "minecraft:bamboo_chest_raft".into(), + (w, false) => format!("minecraft:{w}_boat"), + (w, true) => format!("minecraft:{w}_chest_boat"), + }; + remove(e, "Type"); + set(e, "id", string(&new_id)); + }); +} + +/// `LockComponentPredicateFix.fixLock`. +fn fix_lock(v: Tag) -> Option { + let name = as_str(Some(&v))?; + if name.is_empty() { + return None; + } + let escaped = name.replace('\\', "\\\\").replace('"', "\\\""); + Some(Tag::compound([( + "components", + Tag::compound([("minecraft:custom_name", string(&format!("\"{escaped}\"")))]), + )])) +} + +/// `InvalidLockComponentFix.isBrokenLock`: `{components: {custom_name: '""'}}`. +fn broken_lock(v: &Tag) -> bool { + let one = |t: &Tag, field: &str| matches!(t, Tag::Compound(c) if c.len() == 1) && t.get(field).is_some(); + one(v, "components") + && v.get("components").is_some_and(|c| { + one(c, "minecraft:custom_name") && as_str(c.get("minecraft:custom_name")).as_deref() == Some("\"\"") + }) +} + +// ------------------------------------------------------------------ 4181, 4187 + +fn furnace_burn_time(cx: &mut Cx, id: &str) { + cx.block_entity(id, |be| { + rename(be, "CookTime", "cooking_time_spent"); + rename(be, "CookTimeTotal", "cooking_total_time"); + rename(be, "BurnTime", "lit_time_remaining"); + if let Some(v) = be.get("lit_time_remaining").cloned() { + set(be, "lit_total_time", v); + } + }); +} + +fn base_value(cx: &mut Cx, entity: &str, attribute: &str, from: f64, to: f64) { + cx.entity(entity, |e| { + update(e, "attributes", |list| { + Tag::List( + stream(Some(&list)) + .into_iter() + .map(|mut a| { + if namespaced(&as_string(a.get("id"), "")) == attribute { + let base = as_double(a.get("base"), 0.0); + let new = if base == from { to } else { base }; + set(&mut a, "base", Tag::Double(new.to_bits())); + } + a + }) + .collect(), + ) + }); + }); +} + +// ------------------------------------------------------------------ 4293, 4294, 4297 + +fn drop_chances_format(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + let parse = |v: Option<&Tag>| -> Vec { stream(v).iter().map(|t| as_float(Some(t), 0.085)).collect() }; + let armor = parse(e.get("ArmorDropChances")); + let hands = parse(e.get("HandDropChances")); + let body = e.get("body_armor_drop_chance").and_then(as_number).map_or(0.085, |n| n.float_value()); + remove(e, "ArmorDropChances"); + remove(e, "HandDropChances"); + remove(e, "body_armor_drop_chance"); + let mut chances = empty_map(); + for (slots, values) in [(&["feet", "legs", "chest", "head"][..], &armor), (&["mainhand", "offhand"][..], &hands)] { + for (slot, chance) in slots.iter().zip(values.iter()) { + if *chance != 0.085 { + set(&mut chances, slot, Tag::Float(chance.to_bits())); + } + } + } + if body != 0.085 { + set(&mut chances, "body", Tag::Float(body.to_bits())); + } + if !is_empty_map(&chances) { + set(e, "drop_chances", chances); + } + }); +} + +/// `LegacyBlockPropertyRenameAndFix`. +fn block_property_rename(cx: &mut Cx, block: &str, old: &str, new: &str, f: &dyn Fn(&str) -> String) { + cx.each(Ty::BlockState, |s| { + if as_str(s.get("Name")).map(|n| namespaced(&n)).as_deref() != Some(block) { + return; + } + if let Some(props) = s.get_mut("Properties") { + rename_and_fix(props, old, new, |v| Some(string(&f(&as_string(Some(&v), ""))))); + } + }); +} + +fn forced_chunk_to_ticket(cx: &mut Cx) { + cx.each(Ty::SavedTickets, |v| { + if let Some(data) = v.get_mut("data") { + rename_and_fix(data, "Forced", "tickets", |forced| { + let longs: Vec = match &forced { + Tag::LongArray(a) => a.clone(), + other => stream(Some(other)).iter().map(|t| as_long(Some(t), 0)).collect(), + }; + Some(Tag::List( + longs + .into_iter() + .map(|l| { + Tag::compound([ + ("type", string("minecraft:forced")), + ("level", Tag::Int(31)), + ("ticks_left", Tag::Long(0)), + ("chunk_pos", Tag::Long(l)), + ]) + }) + .collect(), + )) + }); + } + }); +} + +// ------------------------------------------------------------------ 4299 + +fn entity_spawner_item_variants(cx: &mut Cx) { + cx.each(Ty::ItemStack, |s| { + let id = namespaced(&as_str(s.get("id")).unwrap_or_default()); + let Some(c) = s.get_mut("components") else { + return; + }; + if id == "minecraft:painting" { + fix_painting(c); + return; + } + let Some(bucket) = c.get("minecraft:bucket_entity_data").cloned() else { + return; + }; + match id.as_str() { + "minecraft:salmon_bucket" => { + if let Some(ty) = bucket.get("type").cloned() { + update(c, "minecraft:bucket_entity_data", |mut b| { + remove(&mut b, "type"); + b + }); + set(c, "minecraft:salmon/size", ty); + } + } + "minecraft:axolotl_bucket" => { + if let Some(v) = bucket.get("Variant").and_then(as_number) { + let variant = match v.int_value() { + 1 => "wild", + 2 => "gold", + 3 => "cyan", + 4 => "blue", + _ => "lucy", + }; + update(c, "minecraft:bucket_entity_data", |mut b| { + remove(&mut b, "Variant"); + b + }); + set(c, "minecraft:axolotl/variant", string(variant)); + } + } + "minecraft:tropical_fish_bucket" => { + if let Some(v) = bucket.get("BucketVariantTag").and_then(as_number) { + let packed = v.int_value(); + let pattern = match packed & 0xFFFF { + 1 => "flopper", + 256 => "sunstreak", + 257 => "stripey", + 512 => "snooper", + 513 => "glitter", + 768 => "dasher", + 769 => "blockfish", + 1024 => "brinely", + 1025 => "betty", + 1280 => "spotty", + 1281 => "clayfish", + _ => "kob", + }; + update(c, "minecraft:bucket_entity_data", |mut b| { + remove(&mut b, "BucketVariantTag"); + b + }); + set(c, "minecraft:tropical_fish/pattern", string(pattern)); + set(c, "minecraft:tropical_fish/base_color", string(dye_color(packed >> 16 & 0xFF))); + set(c, "minecraft:tropical_fish/pattern_color", string(dye_color(packed >> 24 & 0xFF))); + } + } + _ => {} + } + }); +} + +fn fix_painting(c: &mut Tag) { + let Some(data) = c.get("minecraft:entity_data").cloned() else { + return; + }; + if as_str(data.get("id")).as_deref() != Some("minecraft:painting") { + return; + } + let variant = data.get("variant").cloned(); + let mut rest = data; + remove(&mut rest, "variant"); + let mut without_id = rest.clone(); + remove(&mut without_id, "id"); + if is_empty_map(&without_id) { + remove(c, "minecraft:entity_data"); + } else { + set(c, "minecraft:entity_data", rest); + } + if let Some(v) = variant { + set(c, "minecraft:painting/variant", v); + } +} + +// ------------------------------------------------------------------ 4300, 4301, 4303 + +fn saddle_drop_chance(e: &mut Tag) { + let mut chances = or_empty_map(e.get("drop_chances")); + set(&mut chances, "saddle", Tag::Float(2.0f32.to_bits())); + set(e, "drop_chances", chances); +} + +fn saddle_equipment_slot(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + let id = namespaced(&as_str(e.get("id")).unwrap_or_default()); + match id.as_str() { + "minecraft:horse" | "minecraft:skeleton_horse" | "minecraft:zombie_horse" | "minecraft:donkey" + | "minecraft:mule" | "minecraft:camel" | "minecraft:llama" | "minecraft:trader_llama" => { + if e.get("SaddleItem").is_some() { + rename(e, "SaddleItem", "saddle"); + saddle_drop_chance(e); + } + } + "minecraft:pig" | "minecraft:strider" => { + let saddled = as_bool(e.get("Saddle"), false); + remove(e, "Saddle"); + if saddled { + set(e, "saddle", Tag::compound([("id", string("minecraft:saddle")), ("count", Tag::Int(1))])); + saddle_drop_chance(e); + } + } + _ => {} + } + }); +} + +/// `EquipmentFormatFix` over an entity's (or player's) equipment fields. +pub(super) fn equipment_to_slots(e: &mut Tag) { + let armor = match remove(e, "ArmorItems") { + Some(Tag::List(l)) => l, + _ => Vec::new(), + }; + let hands = match remove(e, "HandItems") { + Some(Tag::List(l)) => l, + _ => Vec::new(), + }; + let body = remove(e, "body_armor_item"); + let saddle = remove(e, "saddle"); + // A stack without an id is a placeholder for an empty slot. + let pick = |list: &Vec, i: usize| list.get(i).filter(|s| s.get("id").is_some()).cloned(); + let slots = [ + ("mainhand", pick(&hands, 0)), + ("offhand", pick(&hands, 1)), + ("feet", pick(&armor, 0)), + ("legs", pick(&armor, 1)), + ("chest", pick(&armor, 2)), + ("head", pick(&armor, 3)), + ("body", body), + ("saddle", saddle), + ]; + if slots.iter().all(|(_, s)| s.is_none()) { + return; + } + let mut eq = Compound::new(); + for (name, stack) in slots { + if let Some(s) = stack { + eq.insert(name.into(), s); + } + } + set(e, "equipment", Tag::Compound(eq)); +} + +fn equipment_format(cx: &mut Cx) { + cx.each(Ty::Entity, equipment_to_slots); +} + +fn fall_distance_to_double(e: &mut Tag) { + rename_and_fix(e, "FallDistance", "fall_distance", |f| { + Some(Tag::Double(f64::from(as_float(Some(&f), 0.0)).to_bits())) + }); +} + +// ------------------------------------------------------------------ 4306, 4307 + +fn thrown_potion_split(cx: &mut Cx) { + cx.each(Ty::Entity, |e| { + if !id_is(e, "minecraft:potion") { + return; + } + let item = e.get("Item").and_then(|i| as_str(i.get("id"))).map(|i| namespaced(&i)).unwrap_or_default(); + let new = if item == "minecraft:lingering_potion" { "minecraft:lingering_potion" } else { "minecraft:splash_potion" }; + set(e, "id", string(new)); + }); +} + +const ADDITIONAL_TOOLTIP_TYPES: [&str; 17] = [ + "minecraft:banner_patterns", + "minecraft:bees", + "minecraft:block_entity_data", + "minecraft:block_state", + "minecraft:bundle_contents", + "minecraft:charged_projectiles", + "minecraft:container", + "minecraft:container_loot", + "minecraft:firework_explosion", + "minecraft:fireworks", + "minecraft:instrument", + "minecraft:map_id", + "minecraft:painting/variant", + "minecraft:pot_decorations", + "minecraft:potion_contents", + "minecraft:tropical_fish/pattern", + "minecraft:written_book_content", +]; + +fn tooltip_display(cx: &mut Cx) { + cx.each(Ty::DataComponents, |c| { + let mut hidden: Vec = Vec::new(); + let add = |h: &mut Vec, id: &str| { + if !h.iter().any(|x| x == id) { + h.push(id.to_owned()); + } + }; + for id in ["minecraft:can_place_on", "minecraft:can_break"] { + if let Some(p) = c.get_mut(id) + && let Some(predicates) = p.get("predicates").cloned() + { + if !as_bool(p.get("show_in_tooltip"), true) { + add(&mut hidden, id); + } + *p = predicates; + } + } + let fix_component = |c: &mut Tag, h: &mut Vec, id: &str, unwrap: Option<&str>| { + update(c, id, |mut component| { + if !as_bool(component.get("show_in_tooltip"), true) { + add(h, id); + } + remove(&mut component, "show_in_tooltip"); + match unwrap.and_then(|f| component.get(f).cloned()) { + Some(inner) => inner, + None => component, + } + }); + }; + fix_component(c, &mut hidden, "minecraft:trim", None); + fix_component(c, &mut hidden, "minecraft:unbreakable", None); + fix_component(c, &mut hidden, "minecraft:dyed_color", Some("rgb")); + fix_component(c, &mut hidden, "minecraft:attribute_modifiers", Some("modifiers")); + fix_component(c, &mut hidden, "minecraft:enchantments", Some("levels")); + fix_component(c, &mut hidden, "minecraft:stored_enchantments", Some("levels")); + fix_component(c, &mut hidden, "minecraft:jukebox_playable", Some("song")); + let hide_tooltip = remove(c, "minecraft:hide_tooltip").is_some(); + let hide_additional = remove(c, "minecraft:hide_additional_tooltip").is_some(); + if hide_additional { + for id in ADDITIONAL_TOOLTIP_TYPES { + if c.get(id).is_some() && !hidden.iter().any(|x| x == id) { + hidden.push(id.to_owned()); + } + } + } + if !hidden.is_empty() || hide_tooltip { + // A HashSet: its iteration order. + let hidden = java_hash_order(hidden, String::clone); + set( + c, + "minecraft:tooltip_display", + Tag::compound([ + ("hide_tooltip", Tag::Byte(i8::from(hide_tooltip))), + ("hidden_components", Tag::List(hidden.iter().map(|s| string(s)).collect())), + ]), + ); + } + }); +} + +// ------------------------------------------------------------------ 4309, 4311, 4312, 4314 + +fn raid_renames(cx: &mut Cx) { + cx.each(Ty::SavedRaids, |v| { + if let Some(d) = v.get_mut("data") { + rename_and_fix(d, "Raids", "raids", |raids| { + Some(Tag::List( + stream(Some(&raids)) + .into_iter() + .map(|mut r| { + fix_inline_block_pos(&mut r, "CX", "CY", "CZ", "center"); + for (a, b) in [ + ("Id", "id"), + ("Started", "started"), + ("Active", "active"), + ("TicksActive", "ticks_active"), + ("BadOmenLevel", "raid_omen_level"), + ("GroupsSpawned", "groups_spawned"), + ("PreRaidTicks", "cooldown_ticks"), + ("PostRaidTicks", "post_raid_ticks"), + ("TotalHealth", "total_health"), + ("NumGroups", "group_count"), + ("Status", "status"), + ("HeroesOfTheVillage", "heroes_of_the_village"), + ] { + rename(&mut r, a, b); + } + r + }) + .collect(), + )) + }); + rename(d, "Tick", "tick"); + rename(d, "NextAvailableID", "next_id"); + } + }); +} + +fn chunk_ticket_unpack_pos(cx: &mut Cx) { + cx.each(Ty::SavedTickets, |v| { + if let Some(d) = v.get_mut("data") { + update(d, "tickets", |tickets| { + Tag::List( + stream(Some(&tickets)) + .into_iter() + .map(|mut t| { + update(&mut t, "chunk_pos", |p| { + let key = as_long(Some(&p), 0); + Tag::IntArray(vec![key as i32, (key as u64 >> 32) as i32]) + }); + t + }) + .collect(), + ) + }); + } + }); +} + +/// `AdvancementsRenameFix`: the advancement ids of an advancements file. +pub(super) fn advancements_rename(cx: &mut Cx, rename_fn: &dyn Fn(&str) -> Option) { + cx.each(Ty::Advancements, |v| { + if let Tag::Compound(c) = v { + let old = std::mem::take(c); + for (k, value) in old { + let id = k.to_string_lossy(); + let k = rename_fn(&id).map_or(k, |n| n.as_str().into()); + c.insert(k, value); + } + } + }); +} + +fn player_equipment(cx: &mut Cx) { + cx.each(Ty::Player, |p| { + let mut equipment = Compound::new(); + update(p, "Inventory", |inv| { + Tag::List( + stream(Some(&inv)) + .into_iter() + .filter_map(|mut item| { + let slot = match as_int(item.get("Slot"), -1) { + 100 => Some("feet"), + 101 => Some("legs"), + 102 => Some("chest"), + 103 => Some("head"), + -106 => Some("offhand"), + _ => None, + }; + match slot { + Some(s) => { + remove(&mut item, "Slot"); + equipment.insert(s.into(), item); + None + } + None => Some(item), + } + }) + .collect(), + ) + }); + set(p, "equipment", Tag::Compound(equipment)); + }); +} + +fn inline_block_pos_format(cx: &mut Cx) { + cx.each(Ty::Player, |p| { + fix_inline_block_pos(p, "SleepingX", "SleepingY", "SleepingZ", "sleeping_pos"); + let spawn = ( + p.get("SpawnX").and_then(as_number), + p.get("SpawnY").and_then(as_number), + p.get("SpawnZ").and_then(as_number), + ); + if let (Some(x), Some(y), Some(z)) = spawn { + let mut respawn = Tag::compound([("pos", Tag::IntArray(vec![x.int_value(), y.int_value(), z.int_value()]))]); + let source = p.clone(); + super::dynamic::copy_field(&source, "SpawnAngle", &mut respawn, "angle"); + super::dynamic::copy_field(&source, "SpawnDimension", &mut respawn, "dimension"); + super::dynamic::copy_field(&source, "SpawnForced", &mut respawn, "forced"); + for k in ["SpawnX", "SpawnY", "SpawnZ", "SpawnAngle", "SpawnDimension", "SpawnForced"] { + remove(p, k); + } + set(p, "respawn", respawn); + } + if let Some(pos) = remove(p, "enteredNetherPosition") { + set( + p, + "entered_nether_pos", + Tag::List( + ["x", "y", "z"].iter().map(|k| Tag::Double(as_double(pos.get(k), 0.0).to_bits())).collect(), + ), + ); + } + }); + cx.each(Ty::Entity, |e| { + fix_inline_block_pos(e, "SleepingX", "SleepingY", "SleepingZ", "sleeping_pos"); + if id_is(e, "minecraft:vex") { + rename(e, "LifeTicks", "life_ticks"); + fix_inline_block_pos(e, "BoundX", "BoundY", "BoundZ", "bound_pos"); + } + if id_is(e, "minecraft:phantom") { + rename(e, "Size", "size"); + fix_inline_block_pos(e, "AX", "AY", "AZ", "anchor_pos"); + } + if id_is(e, "minecraft:turtle") { + for k in ["TravelPosX", "TravelPosY", "TravelPosZ"] { + remove(e, k); + } + fix_inline_block_pos(e, "HomePosX", "HomePosY", "HomePosZ", "home_pos"); + rename(e, "HasEgg", "has_egg"); + } + for id in ["minecraft:item_frame", "minecraft:glow_item_frame", "minecraft:painting", "minecraft:leash_knot"] { + if id_is(e, id) { + fix_inline_block_pos(e, "TileX", "TileY", "TileZ", "block_pos"); + } + } + }); +} diff --git a/crates/horizon-world/src/datafix/fixes_c.rs b/crates/horizon-world/src/datafix/fixes_c.rs new file mode 100644 index 000000000..fa8221a27 --- /dev/null +++ b/crates/horizon-world/src/datafix/fixes_c.rs @@ -0,0 +1,694 @@ +//! Fixes of 1.21.6 to 26.3 (keys 4424 to 5016). +use super::dynamic::{ + as_bool, as_double, as_float, as_int, as_long, as_str, as_string, boolean, empty_map, int_list, namespaced, remove, + rename, rename_and_fix, set, stream, string, update, +}; +use super::fixes_a::{block_rename, component_fix}; +use super::fixes_b::{advancements_rename, attributes_rename, feature_flag_remove}; +use super::schema::{Key, Ty, key}; +use super::{Cx, FixFn, id_is, renamer}; +use crate::nbt::Tag; + +pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + out.push((key(4424, 0), |cx| feature_flag_remove(cx, &["minecraft:locator_bar"]))); + out.push((key(4424, 0), |cx| cx.each(Ty::Player, add_locator_bar_style))); + out.push((key(4424, 0), |cx| cx.each(Ty::Entity, add_locator_bar_style))); + out.push((key(4535, 0), |cx| { + cx.entity("minecraft:copper_golem", |e| { + update(e, "weather_state", |v| { + string(match as_int(Some(&v), 0) { + 1 => "exposed", + 2 => "weathered", + 3 => "oxidized", + _ => "unaffected", + }) + }); + }); + })); + out.push((key(4537, 0), |cx| { + cx.each(Ty::Chunk, |c| { + remove(c, "isLightOn"); + if let Some(Tag::List(sections)) = c.get_mut("sections") { + for s in sections { + remove(s, "BlockLight"); + remove(s, "SkyLight"); + } + } + }); + })); + out.push((key(4541, 0), |cx| block_rename(cx, &[("minecraft:chain", "minecraft:iron_chain")]))); + out.push((key(4541, 0), |cx| cx.rename(Ty::ItemName, renamer(&[("minecraft:chain", "minecraft:iron_chain")])))); + out.push((key(4544, 0), legacy_world_border)); + out.push((key(4548, 0), world_spawn_data)); + out.push((key(4548, 0), |cx| { + cx.each(Ty::Player, |p| { + update(p, "respawn", |mut r| { + let dimension = as_string(r.get("dimension"), "minecraft:overworld"); + let yaw = as_float(r.get("angle"), 0.0); + set(&mut r, "dimension", string(&dimension)); + set(&mut r, "yaw", Tag::Float(yaw.to_bits())); + set(&mut r, "pitch", Tag::Float(0f32.to_bits())); + remove(&mut r, "angle"); + r + }); + }); + })); + out.push((key(4649, 0), |cx| { + component_fix(cx, "minecraft:consumable", "minecraft:consumable", |mut c| { + if as_str(c.get("animation")).as_deref() == Some("spear") { + set(&mut c, "animation", string("trident")); + } + Some(c) + }); + })); + out.push((key(4657, 0), |cx| { + cx.each_out(Ty::SavedWorldBorder, |v| { + if let Some(d) = v.get_mut("data") { + update(d, "warning_time", |t| Tag::Int(as_int(Some(&t), 15).wrapping_mul(20))); + } + }); + })); + out.push((key(4658, 0), game_rule_registry)); + out.push((key(4763, 0), |cx| { + cx.entity("minecraft:villager", |e| set(e, "VillagerDataFinalized", boolean(true))); + })); + out.push((key(4763, 0), |cx| { + cx.entity("minecraft:zombie_villager", |e| set(e, "VillagerDataFinalized", boolean(true))); + })); + out.push((key(4766, 0), day_time_to_clock)); + out.push((key(4769, 0), |cx| cx.rename(Ty::Recipe, renamer(STONECUTTER_RECIPES)))); + out.push((key(4769, 0), |cx| { + advancements_rename(cx, &|id| { + let id = namespaced(id); + STONECUTTER_ADVANCEMENTS.iter().find(|(from, _)| *from == id).map(|(_, to)| (*to).to_owned()) + }); + })); + out.push((key(4771, 0), level_dat_difficulty)); + out.push((key(4771, 0), level_dat_to_saved_data_preparation)); + out.push((key(4885, 0), remove_bed_block_entities)); + out.push((key(4888, 0), |cx| { + attributes_rename(cx, false, &|id| (id == "minecraft:nameplate_distance").then(|| "minecraft:name_tag_distance".into())); + })); + out.push((key(4996, 0), |cx| { + cx.each_out(Ty::DataComponents, |c| update(c, "minecraft:pot_decorations", unpack_pot_decorations)); + })); + out.push((key(4996, 1), |cx| { + cx.block_entity("minecraft:decorated_pot", |be| { + let sherds = be.get("sherds").cloned().unwrap_or(Tag::List(Vec::new())); + set(be, "sherds", unpack_pot_decorations(sherds)); + }); + })); + out.push((key(4997, 0), |cx| cx.each_out(Ty::Chunk, blending_data))); + out.push((key(5002, 0), |cx| { + cx.block_entity("minecraft:sign", |be| set(be, "allow_op_features", boolean(true))); + })); + out.push((key(5002, 0), |cx| { + cx.block_entity("minecraft:hanging_sign", |be| set(be, "allow_op_features", boolean(true))); + })); + out.push((key(5006, 0), |cx| { + cx.each(Ty::BlockState, |s| { + rename(s, "Name", "id"); + rename(s, "Properties", "properties"); + }); + })); + out.push((key(5007, 0), |cx| { + cx.each(Ty::DataComponents, |c| { + if let Some(swing) = remove(c, "minecraft:swing_animation") { + set(c, "minecraft:attack_animation", swing.clone()); + set(c, "minecraft:interact_animation", swing); + } + }); + })); + out.push((key(5008, 0), explorer_map_item)); + out.push((key(5008, 0), |cx| component_fix(cx, "minecraft:map_color", "minecraft:map_color", |_| None))); + out.push((key(5009, 0), |cx| { + cx.each(Ty::Chunk, |c| { + if let Some(b) = c.get_mut("blending_data") { + update(b, "heights", |h| { + Tag::List( + stream(Some(&h)) + .iter() + .map(|v| { + let d = as_double(Some(v), f64::MAX); + let f = if d == f64::MAX { f32::MAX } else { d as f32 }; + Tag::Float(f.to_bits()) + }) + .collect(), + ) + }); + } + }); + })); + out.push((key(5012, 0), |cx| { + cx.rename( + Ty::ItemName, + renamer(&[ + ("minecraft:ocean_explorer_map", "minecraft:ocean_monument_map"), + ("minecraft:swamp_explorer_map", "minecraft:swamp_hut_map"), + ("minecraft:trial_explorer_map", "minecraft:buried_trial_chambers_map"), + ("minecraft:woodland_explorer_map", "minecraft:woodland_mansion_map"), + ("minecraft:jungle_explorer_map", "minecraft:jungle_pyramid_map"), + ]), + ); + })); + out.push((key(5013, 0), merge_terrain_chunk_status)); + out.push((key(5014, 0), |cx| { + cx.each(Ty::DataComponents, |c| { + remove(c, "minecraft:block_transformer"); + }); + })); + out.push((key(5016, 0), |cx| { + cx.item(|id| id == "minecraft:map", |s| { + if let Some(c) = s.get_mut("components") + && let Some(name) = c.get_mut("minecraft:item_name") + && plain_translation_key(name).as_deref() == Some("filled_map.buried_treasure") + { + set(name, "translate", string("item.minecraft.buried_treasure_map")); + } + }); + })); +} + +// ------------------------------------------------------------------ 4424 + +/// `AddFieldFix` with the path `locator_bar_icon`: as vanilla does it, the +/// value is replaced by its `locator_bar_icon` with a style added. +fn add_locator_bar_style(v: &mut Tag) { + if let Some(mut icon) = v.get("locator_bar_icon").cloned() { + set(&mut icon, "style", string("minecraft:default")); + *v = icon; + } +} + +// ------------------------------------------------------------------ 4544, 4548, 4658, 4766 + +fn legacy_world_border(cx: &mut Cx) { + cx.each(Ty::Level, |l| { + let d = |l: &Tag, k: &str, def: f64| Tag::Double(as_double(l.get(k), def).to_bits()); + let border = Tag::compound([ + ("center_x", d(l, "BorderCenterX", 0.0)), + ("center_z", d(l, "BorderCenterZ", 0.0)), + ("size", d(l, "BorderSize", f64::from(5.999997E7f32))), + ("lerp_time", Tag::Long(as_long(l.get("BorderSizeLerpTime"), 0))), + ("lerp_target", d(l, "BorderSizeLerpTarget", 0.0)), + ("safe_zone", d(l, "BorderSafeZone", 5.0)), + ("damage_per_block", d(l, "BorderDamagePerBlock", 0.2)), + ("warning_blocks", Tag::Int(as_int(l.get("BorderWarningBlocks"), 5))), + ("warning_time", Tag::Int(as_int(l.get("BorderWarningTime"), 15))), + ]); + for k in [ + "BorderCenterX", + "BorderCenterZ", + "BorderSize", + "BorderSizeLerpTime", + "BorderSizeLerpTarget", + "BorderSafeZone", + "BorderDamagePerBlock", + "BorderWarningBlocks", + "BorderWarningTime", + ] { + remove(l, k); + } + set(l, "world_border", border); + }); +} + +fn world_spawn_data(cx: &mut Cx) { + cx.each(Ty::Level, |l| { + let pos = [as_int(l.get("SpawnX"), 0), as_int(l.get("SpawnY"), 0), as_int(l.get("SpawnZ"), 0)]; + let angle = as_float(l.get("SpawnAngle"), 0.0); + for k in ["SpawnX", "SpawnY", "SpawnZ", "SpawnAngle"] { + remove(l, k); + } + set( + l, + "spawn", + Tag::compound([ + ("dimension", string("minecraft:overworld")), + ("pos", int_list(&pos)), + ("yaw", Tag::Float(angle.to_bits())), + ("pitch", Tag::Float(0f32.to_bits())), + ]), + ); + }); +} + +const GAME_RULES: &[(&str, &str, Conv)] = &[ + ("allowEnteringNetherUsingPortals", "minecraft:allow_entering_nether_using_portals", Conv::Bool), + ("announceAdvancements", "minecraft:show_advancement_messages", Conv::Bool), + ("blockExplosionDropDecay", "minecraft:block_explosion_drop_decay", Conv::Bool), + ("commandBlockOutput", "minecraft:command_block_output", Conv::Bool), + ("enableCommandBlocks", "minecraft:command_blocks_work", Conv::Bool), + ("commandBlocksEnabled", "minecraft:command_blocks_work", Conv::Bool), + ("commandModificationBlockLimit", "minecraft:max_block_modifications", Conv::Int(1, i32::MAX)), + ("disableElytraMovementCheck", "minecraft:elytra_movement_check", Conv::NotBool), + ("disablePlayerMovementCheck", "minecraft:player_movement_check", Conv::NotBool), + ("disableRaids", "minecraft:raids", Conv::NotBool), + ("doDaylightCycle", "minecraft:advance_time", Conv::Bool), + ("doEntityDrops", "minecraft:entity_drops", Conv::Bool), + ("doImmediateRespawn", "minecraft:immediate_respawn", Conv::Bool), + ("doInsomnia", "minecraft:spawn_phantoms", Conv::Bool), + ("doLimitedCrafting", "minecraft:limited_crafting", Conv::Bool), + ("doMobLoot", "minecraft:mob_drops", Conv::Bool), + ("doMobSpawning", "minecraft:spawn_mobs", Conv::Bool), + ("doPatrolSpawning", "minecraft:spawn_patrols", Conv::Bool), + ("doTileDrops", "minecraft:block_drops", Conv::Bool), + ("doTraderSpawning", "minecraft:spawn_wandering_traders", Conv::Bool), + ("doVinesSpread", "minecraft:spread_vines", Conv::Bool), + ("doWardenSpawning", "minecraft:spawn_wardens", Conv::Bool), + ("doWeatherCycle", "minecraft:advance_weather", Conv::Bool), + ("drowningDamage", "minecraft:drowning_damage", Conv::Bool), + ("enderPearlsVanishOnDeath", "minecraft:ender_pearls_vanish_on_death", Conv::Bool), + ("fallDamage", "minecraft:fall_damage", Conv::Bool), + ("fireDamage", "minecraft:fire_damage", Conv::Bool), + ("forgiveDeadPlayers", "minecraft:forgive_dead_players", Conv::Bool), + ("freezeDamage", "minecraft:freeze_damage", Conv::Bool), + ("globalSoundEvents", "minecraft:global_sound_events", Conv::Bool), + ("keepInventory", "minecraft:keep_inventory", Conv::Bool), + ("lavaSourceConversion", "minecraft:lava_source_conversion", Conv::Bool), + ("locatorBar", "minecraft:locator_bar", Conv::Bool), + ("logAdminCommands", "minecraft:log_admin_commands", Conv::Bool), + ("maxCommandChainLength", "minecraft:max_command_sequence_length", Conv::Int(0, i32::MAX)), + ("maxCommandForkCount", "minecraft:max_command_forks", Conv::Int(0, i32::MAX)), + ("maxEntityCramming", "minecraft:max_entity_cramming", Conv::Int(0, i32::MAX)), + ("minecartMaxSpeed", "minecraft:max_minecart_speed", Conv::Int(i32::MIN, i32::MAX)), + ("mobExplosionDropDecay", "minecraft:mob_explosion_drop_decay", Conv::Bool), + ("mobGriefing", "minecraft:mob_griefing", Conv::Bool), + ("naturalRegeneration", "minecraft:natural_health_regeneration", Conv::Bool), + ("playersNetherPortalCreativeDelay", "minecraft:players_nether_portal_creative_delay", Conv::Int(0, i32::MAX)), + ("playersNetherPortalDefaultDelay", "minecraft:players_nether_portal_default_delay", Conv::Int(0, i32::MAX)), + ("playersSleepingPercentage", "minecraft:players_sleeping_percentage", Conv::Int(0, i32::MAX)), + ("projectilesCanBreakBlocks", "minecraft:projectiles_can_break_blocks", Conv::Bool), + ("pvp", "minecraft:pvp", Conv::Bool), + ("randomTickSpeed", "minecraft:random_tick_speed", Conv::Int(0, i32::MAX)), + ("reducedDebugInfo", "minecraft:reduced_debug_info", Conv::Bool), + ("sendCommandFeedback", "minecraft:send_command_feedback", Conv::Bool), + ("showDeathMessages", "minecraft:show_death_messages", Conv::Bool), + ("snowAccumulationHeight", "minecraft:max_snow_accumulation_height", Conv::Int(0, 8)), + ("spawnMonsters", "minecraft:spawn_monsters", Conv::Bool), + ("spawnRadius", "minecraft:respawn_radius", Conv::Int(i32::MIN, i32::MAX)), + ("spawnerBlocksEnabled", "minecraft:spawner_blocks_work", Conv::Bool), + ("spectatorsGenerateChunks", "minecraft:spectators_generate_chunks", Conv::Bool), + ("tntExplodes", "minecraft:tnt_explodes", Conv::Bool), + ("tntExplosionDropDecay", "minecraft:tnt_explosion_drop_decay", Conv::Bool), + ("universalAnger", "minecraft:universal_anger", Conv::Bool), + ("waterSourceConversion", "minecraft:water_source_conversion", Conv::Bool), +]; + +#[derive(Clone, Copy)] +enum Conv { + Bool, + NotBool, + Int(i32, i32), +} + +/// `Boolean.parseBoolean`. +fn parse_bool(s: &str) -> bool { + s.eq_ignore_ascii_case("true") +} + +fn convert(v: Tag, conv: Conv) -> Tag { + let s = as_string(Some(&v), ""); + match conv { + Conv::Bool => boolean(parse_bool(&s)), + Conv::NotBool => boolean(!parse_bool(&s)), + Conv::Int(min, max) => match parse_int(&s) { + Some(i) => Tag::Int(i.clamp(min, max)), + None => v, + }, + } +} + +/// `Integer.parseInt`. +fn parse_int(s: &str) -> Option { + let digits = s.strip_prefix(['+', '-']).unwrap_or(s); + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + s.parse().ok() +} + +fn game_rule_registry(cx: &mut Cx) { + cx.each(Ty::Level, |l| { + rename_and_fix(l, "GameRules", "game_rules", |mut rules| { + let fire_tick = parse_bool(&as_string(rules.get("doFireTick"), "true")); + let away = parse_bool(&as_string(rules.get("allowFireTicksAwayFromPlayer"), "false")); + let radius = if !fire_tick { + 0 + } else if !away { + 128 + } else { + -1 + }; + if radius != 128 { + set(&mut rules, "minecraft:fire_spread_radius_around_player", Tag::Int(radius)); + } + for k in [ + "spawnChunkRadius", + "entitiesWithPassengersCanUsePortals", + "gameLoopFunction", + "doFireTick", + "allowFireTicksAwayFromPlayer", + ] { + remove(&mut rules, k); + } + for (old, new, conv) in GAME_RULES { + rename_and_fix(&mut rules, old, new, |v| Some(convert(v, *conv))); + } + Some(rules) + }); + }); +} + +fn day_time_to_clock(cx: &mut Cx) { + cx.each(Ty::Level, |l| { + let game_time = as_long(l.get("Time"), 0); + let day_time = as_long(l.get("DayTime"), game_time); + remove(l, "DayTime"); + let clock = Tag::compound([("total_ticks", Tag::Long(day_time)), ("paused", boolean(false))]); + set(l, "world_clocks", Tag::compound([("minecraft:overworld", clock)])); + }); +} + +// ------------------------------------------------------------------ 4769, 4771 + +const STONECUTTER_RECIPES: &[(&str, &str)] = &[ + ("minecraft:chiseled_stone_bricks_stone_from_stonecutting", "minecraft:chiseled_stone_bricks_from_stone_stonecutting"), + ( + "minecraft:end_stone_brick_slab_from_end_stone_brick_stonecutting", + "minecraft:end_stone_brick_slab_from_end_stone_bricks_stonecutting", + ), + ( + "minecraft:end_stone_brick_stairs_from_end_stone_brick_stonecutting", + "minecraft:end_stone_brick_stairs_from_end_stone_bricks_stonecutting", + ), + ( + "minecraft:end_stone_brick_wall_from_end_stone_brick_stonecutting", + "minecraft:end_stone_brick_wall_from_end_stone_bricks_stonecutting", + ), + ( + "minecraft:mossy_stone_brick_slab_from_mossy_stone_brick_stonecutting", + "minecraft:mossy_stone_brick_slab_from_mossy_stone_bricks_stonecutting", + ), + ( + "minecraft:mossy_stone_brick_stairs_from_mossy_stone_brick_stonecutting", + "minecraft:mossy_stone_brick_stairs_from_mossy_stone_bricks_stonecutting", + ), + ( + "minecraft:mossy_stone_brick_wall_from_mossy_stone_brick_stonecutting", + "minecraft:mossy_stone_brick_wall_from_mossy_stone_bricks_stonecutting", + ), + ( + "minecraft:prismarine_brick_slab_from_prismarine_stonecutting", + "minecraft:prismarine_brick_slab_from_prismarine_bricks_stonecutting", + ), + ( + "minecraft:prismarine_brick_stairs_from_prismarine_stonecutting", + "minecraft:prismarine_brick_stairs_from_prismarine_bricks_stonecutting", + ), + ("minecraft:quartz_slab_from_stonecutting", "minecraft:quartz_slab_from_quartz_block_stonecutting"), + ("minecraft:stone_brick_walls_from_stone_stonecutting", "minecraft:stone_brick_wall_from_stone_stonecutting"), +]; +const STONECUTTER_ADVANCEMENTS: &[(&str, &str)] = &[ + ( + "minecraft:recipes/building_blocks/chiseled_stone_bricks_stone_from_stonecutting", + "minecraft:recipes/building_blocks/chiseled_stone_bricks_from_stone_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/end_stone_brick_slab_from_end_stone_brick_stonecutting", + "minecraft:recipes/building_blocks/end_stone_brick_slab_from_end_stone_bricks_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/end_stone_brick_stairs_from_end_stone_brick_stonecutting", + "minecraft:recipes/building_blocks/end_stone_brick_stairs_from_end_stone_bricks_stonecutting", + ), + ( + "minecraft:recipes/decorations/end_stone_brick_wall_from_end_stone_brick_stonecutting", + "minecraft:recipes/decorations/end_stone_brick_wall_from_end_stone_bricks_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/mossy_stone_brick_slab_from_mossy_stone_brick_stonecutting", + "minecraft:recipes/building_blocks/mossy_stone_brick_slab_from_mossy_stone_bricks_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/mossy_stone_brick_stairs_from_mossy_stone_brick_stonecutting", + "minecraft:recipes/building_blocks/mossy_stone_brick_stairs_from_mossy_stone_bricks_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/mossy_stone_brick_wall_from_mossy_stone_brick_stonecutting", + "minecraft:recipes/decorations/mossy_stone_brick_wall_from_mossy_stone_bricks_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/prismarine_brick_slab_from_prismarine_stonecutting", + "minecraft:recipes/building_blocks/prismarine_brick_slab_from_prismarine_bricks_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/prismarine_brick_stairs_from_prismarine_stonecutting", + "minecraft:recipes/building_blocks/prismarine_brick_stairs_from_prismarine_bricks_stonecutting", + ), + ( + "minecraft:recipes/building_blocks/quartz_slab_from_stonecutting", + "minecraft:recipes/building_blocks/quartz_slab_from_quartz_block_stonecutting", + ), + ( + "minecraft:recipes/decorations/stone_brick_walls_from_stone_stonecutting", + "minecraft:recipes/decorations/stone_brick_wall_from_stone_stonecutting", + ), +]; + +fn level_dat_difficulty(cx: &mut Cx) { + cx.each(Ty::LightweightLevel, |l| { + let difficulty = match as_int(l.get("Difficulty"), 2) { + 0 => "peaceful", + 1 => "easy", + 3 => "hard", + _ => "normal", + }; + let settings = Tag::compound([ + ("difficulty", string(difficulty)), + ("hardcore", boolean(as_bool(l.get("hardcore"), false))), + ("locked", boolean(as_bool(l.get("DifficultyLocked"), false))), + ]); + set(l, "difficulty_settings", settings); + remove(l, "Difficulty"); + remove(l, "hardcore"); + remove(l, "DifficultyLocked"); + }); +} + +fn level_dat_to_saved_data_preparation(cx: &mut Cx) { + cx.each(Ty::Level, |l| { + let source = l.clone(); + rename_and_fix(l, "DragonFight", "dragon_fight", |mut fight| { + let respawning = as_bool(fight.get("IsRespawning"), false); + for (a, b) in [ + ("NeedsStateScanning", "needs_state_scanning"), + ("DragonKilled", "dragon_killed"), + ("PreviouslyKilled", "previously_killed"), + ("Dragon", "dragon_uuid"), + ("ExitPortalLocation", "exit_portal_location"), + ("Gateways", "gateways"), + ] { + rename(&mut fight, a, b); + } + if respawning { + set(&mut fight, "respawn_stage", string("start")); + set(&mut fight, "respawn_time", Tag::Int(0)); + } + remove(&mut fight, "IsRespawning"); + Some(fight) + }); + let trader = Tag::compound([ + ("spawn_delay", Tag::Int(as_int(source.get("WanderingTraderSpawnDelay"), 24000))), + ("spawn_chance", Tag::Int(as_int(source.get("WanderingTraderSpawnChance"), 25))), + ]); + set(l, "wandering_trader_migration_data", trader); + for k in ["WanderingTraderSpawnDelay", "WanderingTraderSpawnChance", "WanderingTraderId"] { + remove(l, k); + } + let weather = Tag::compound([ + ("clear_weather_time", Tag::Int(as_int(source.get("clearWeatherTime"), 0))), + ("rain_time", Tag::Int(as_int(source.get("rainTime"), 0))), + ("thunder_time", Tag::Int(as_int(source.get("thunderTime"), 0))), + ("raining", boolean(as_bool(source.get("raining"), false))), + ("thundering", boolean(as_bool(source.get("thundering"), false))), + ]); + for k in ["clearWeatherTime", "rainTime", "thunderTime", "raining", "thundering"] { + remove(l, k); + } + set(l, "weather_data", weather); + // `asList` of something that is not a list is empty. + let events: Vec = stream(source.get("ScheduledEvents")) + .into_iter() + .map(|mut e| { + rename(&mut e, "Name", "id"); + rename(&mut e, "TriggerTime", "trigger_time"); + rename_and_fix(&mut e, "Callback", "callback", |mut c| { + rename(&mut c, "Type", "type"); + rename(&mut c, "Name", "id"); + Some(c) + }); + e + }) + .collect(); + remove(l, "ScheduledEvents"); + set(l, "scheduled_events", Tag::compound([("events", Tag::List(events))])); + rename_and_fix(l, "WorldGenSettings", "world_gen_settings", |mut s| { + rename(&mut s, "generate_features", "generate_structures"); + Some(s) + }); + }); +} + +// ------------------------------------------------------------------ 4885, 4996, 4997 + +/// `RemoveBlockEntityTagFix` for beds: item block entity data, falling block +/// entity data and chunk block entities of beds go. +fn remove_bed_block_entities(cx: &mut Cx) { + let is_bed = |v: &Tag| id_is(v, "minecraft:bed"); + cx.each(Ty::ItemStack, |s| { + if let Some(c) = s.get_mut("components") + && c.get("minecraft:block_entity_data").is_some_and(is_bed) + { + remove(c, "minecraft:block_entity_data"); + } + }); + cx.entity("minecraft:falling_block", |e| { + if e.get("TileEntityData").is_some_and(is_bed) { + remove(e, "TileEntityData"); + } + }); + cx.each(Ty::Chunk, |c| { + if let Some(Tag::List(bes)) = c.get_mut("block_entities") { + bes.retain(|be| !is_bed(be)); + } + }); +} + +/// `PotDecorationsComponentUnflatteningFix.unpackList`. +fn unpack_pot_decorations(original: Tag) -> Tag { + if !super::dynamic::is_list(Some(&original)) { + return original; + } + let ids: Vec> = stream(Some(&original)).iter().map(|t| as_str(Some(t))).collect(); + let mut result = empty_map(); + for (i, side) in ["back", "left", "right", "front"].iter().enumerate() { + let id = ids.get(i).cloned().flatten().unwrap_or_else(|| "minecraft:brick".into()); + if id.is_empty() { + return original; + } + set(&mut result, side, Tag::compound([("id", string(&id))])); + } + result +} + +fn blending_data(c: &mut Tag) { + remove(c, "blending_data"); + let overworld = c + .get("__context") + .and_then(|ctx| as_str(ctx.get("dimension"))) + .is_some_and(|d| d == "minecraft:overworld"); + let Some(status) = c.get("Status") else { + return; + }; + if !overworld { + return; + } + const SKIP: [&str; 4] = ["minecraft:empty", "minecraft:structure_starts", "minecraft:structure_references", "minecraft:biomes"]; + let status = namespaced(&as_string(Some(status), "empty")); + let blending = |height: i32, min_y: i32| { + Tag::compound([("min_section", Tag::Int(min_y >> 4)), ("max_section", Tag::Int((min_y + height) >> 4))]) + }; + if !SKIP.contains(&status.as_str()) { + set(c, "blending_data", blending(384, -64)); + } else if let Some(retrogen) = c.get("below_zero_retrogen") { + let target = namespaced(&as_string(retrogen.get("target_status"), "empty")); + if !SKIP.contains(&target.as_str()) { + set(c, "blending_data", blending(256, 0)); + } + } +} + +// ------------------------------------------------------------------ 5008, 5013 + +/// `ExtraDataFixUtils.getPlainTranslationKey`: `{translate: key}` alone. +fn plain_translation_key(v: &Tag) -> Option { + match v { + Tag::Compound(c) if c.len() == 1 => as_str(v.get("translate")), + _ => None, + } +} + +const EXPLORER_MAPS: &[(&str, &str, &str)] = &[ + ("minecraft:mansion", "minecraft:woodland_explorer_map", "filled_map.mansion"), + ("minecraft:monument", "minecraft:ocean_explorer_map", "filled_map.monument"), + ("minecraft:trial_chambers", "minecraft:trial_explorer_map", "filled_map.trial_chambers"), + ("minecraft:jungle_temple", "minecraft:jungle_explorer_map", "filled_map.explorer_jungle"), + ("minecraft:swamp_hut", "minecraft:swamp_explorer_map", "filled_map.explorer_swamp"), + ("minecraft:village_desert", "minecraft:desert_village_map", "filled_map.village_desert"), + ("minecraft:village_plains", "minecraft:plains_village_map", "filled_map.village_plains"), + ("minecraft:village_savanna", "minecraft:savanna_village_map", "filled_map.village_savanna"), + ("minecraft:village_snowy", "minecraft:snowy_village_map", "filled_map.village_snowy"), + ("minecraft:village_taiga", "minecraft:taiga_village_map", "filled_map.village_taiga"), + ("minecraft:red_x", "minecraft:buried_treasure_map", "filled_map.buried_treasure"), +]; + +fn explorer_map_item(cx: &mut Cx) { + cx.item(|id| id == "minecraft:filled_map", |s| { + let Some(decoration) = s + .get("components") + .and_then(|c| c.get("minecraft:map_decorations")) + .and_then(|d| d.get("+")) + else { + return; + }; + let ty = namespaced(&as_string(decoration.get("type"), "")); + let Some((_, item, legacy)) = EXPLORER_MAPS.iter().find(|(t, _, _)| *t == ty) else { + return; + }; + set(s, "id", string(item)); + if let Some(c) = s.get_mut("components") + && c.get("minecraft:item_name").and_then(plain_translation_key).as_deref() == Some(*legacy) + { + remove(c, "minecraft:item_name"); + } + }); +} + +fn merge_terrain_chunk_status(cx: &mut Cx) { + cx.each(Ty::Chunk, |c| { + const INTERMEDIATE: [&str; 2] = ["minecraft:noise", "minecraft:surface"]; + let fix = |s: &str| -> String { + if s == "minecraft:carvers" { + "minecraft:terrain".into() + } else if INTERMEDIATE.contains(&s) { + "minecraft:biomes".into() + } else { + s.to_owned() + } + }; + let old_status = namespaced(&as_string(c.get("Status"), "minecraft:empty")); + set(c, "Status", string(&fix(&old_status))); + let mut old_target = None; + if let Some(r) = c.get_mut("below_zero_retrogen") { + let target = namespaced(&as_string(r.get("target_status"), "minecraft:empty")); + set(r, "target_status", string(&fix(&target))); + old_target = Some(target); + } + let status_intermediate = INTERMEDIATE.contains(&old_status.as_str()); + let target_intermediate = old_target.as_deref().is_some_and(|t| INTERMEDIATE.contains(&t)); + if status_intermediate || target_intermediate { + let only_below_zero = old_target.is_some() && !target_intermediate; + remove(c, "Heightmaps"); + remove(c, "blending_data"); + if let Some(Tag::List(sections)) = c.get_mut("sections") { + for s in sections { + let y = super::dynamic::as_number(s.get("Y").unwrap_or(&Tag::End)).map_or(0, |n| n.byte_value()); + if !(y >= 0 && only_below_zero) { + remove(s, "block_states"); + } + } + } + } + }); +} diff --git a/crates/horizon-world/src/datafix/items.rs b/crates/horizon-world/src/datafix/items.rs new file mode 100644 index 000000000..e5a3b2400 --- /dev/null +++ b/crates/horizon-world/src/datafix/items.rs @@ -0,0 +1,1113 @@ +//! Item stacks: the V705 read hook (`ADD_NAMES`), `ItemStackComponentizationFix` +//! (1.20.5) and `ParticleUnflatteningFix`. +use super::dynamic::{ + as_bool, as_bool_opt, as_double, as_int, as_long, as_number, as_str, as_string, empty_map, int_list, is_empty_map, + namespaced, remove, rename, set, stream, string, +}; +use super::json; +use crate::nbt::{JavaString, Tag}; + +/// `V704.ITEM_TO_BLOCKENTITY`. +const ITEM_TO_BLOCK_ENTITY: &[(&str, &str)] = &[ + ("minecraft:furnace", "minecraft:furnace"), + ("minecraft:lit_furnace", "minecraft:furnace"), + ("minecraft:chest", "minecraft:chest"), + ("minecraft:trapped_chest", "minecraft:chest"), + ("minecraft:ender_chest", "minecraft:ender_chest"), + ("minecraft:jukebox", "minecraft:jukebox"), + ("minecraft:dispenser", "minecraft:dispenser"), + ("minecraft:dropper", "minecraft:dropper"), + ("minecraft:sign", "minecraft:sign"), + ("minecraft:mob_spawner", "minecraft:mob_spawner"), + ("minecraft:spawner", "minecraft:mob_spawner"), + ("minecraft:noteblock", "minecraft:noteblock"), + ("minecraft:brewing_stand", "minecraft:brewing_stand"), + ("minecraft:enhanting_table", "minecraft:enchanting_table"), + ("minecraft:command_block", "minecraft:command_block"), + ("minecraft:beacon", "minecraft:beacon"), + ("minecraft:skull", "minecraft:skull"), + ("minecraft:daylight_detector", "minecraft:daylight_detector"), + ("minecraft:hopper", "minecraft:hopper"), + ("minecraft:banner", "minecraft:banner"), + ("minecraft:flower_pot", "minecraft:flower_pot"), + ("minecraft:repeating_command_block", "minecraft:command_block"), + ("minecraft:chain_command_block", "minecraft:command_block"), + ("minecraft:shulker_box", "minecraft:shulker_box"), + ("minecraft:white_shulker_box", "minecraft:shulker_box"), + ("minecraft:orange_shulker_box", "minecraft:shulker_box"), + ("minecraft:magenta_shulker_box", "minecraft:shulker_box"), + ("minecraft:light_blue_shulker_box", "minecraft:shulker_box"), + ("minecraft:yellow_shulker_box", "minecraft:shulker_box"), + ("minecraft:lime_shulker_box", "minecraft:shulker_box"), + ("minecraft:pink_shulker_box", "minecraft:shulker_box"), + ("minecraft:gray_shulker_box", "minecraft:shulker_box"), + ("minecraft:silver_shulker_box", "minecraft:shulker_box"), + ("minecraft:cyan_shulker_box", "minecraft:shulker_box"), + ("minecraft:purple_shulker_box", "minecraft:shulker_box"), + ("minecraft:blue_shulker_box", "minecraft:shulker_box"), + ("minecraft:brown_shulker_box", "minecraft:shulker_box"), + ("minecraft:green_shulker_box", "minecraft:shulker_box"), + ("minecraft:red_shulker_box", "minecraft:shulker_box"), + ("minecraft:black_shulker_box", "minecraft:shulker_box"), + ("minecraft:bed", "minecraft:bed"), + ("minecraft:light_gray_shulker_box", "minecraft:shulker_box"), + ("minecraft:white_banner", "minecraft:banner"), + ("minecraft:orange_banner", "minecraft:banner"), + ("minecraft:magenta_banner", "minecraft:banner"), + ("minecraft:light_blue_banner", "minecraft:banner"), + ("minecraft:yellow_banner", "minecraft:banner"), + ("minecraft:lime_banner", "minecraft:banner"), + ("minecraft:pink_banner", "minecraft:banner"), + ("minecraft:gray_banner", "minecraft:banner"), + ("minecraft:silver_banner", "minecraft:banner"), + ("minecraft:light_gray_banner", "minecraft:banner"), + ("minecraft:cyan_banner", "minecraft:banner"), + ("minecraft:purple_banner", "minecraft:banner"), + ("minecraft:blue_banner", "minecraft:banner"), + ("minecraft:brown_banner", "minecraft:banner"), + ("minecraft:green_banner", "minecraft:banner"), + ("minecraft:red_banner", "minecraft:banner"), + ("minecraft:black_banner", "minecraft:banner"), + ("minecraft:standing_sign", "minecraft:sign"), + ("minecraft:wall_sign", "minecraft:sign"), + ("minecraft:piston_head", "minecraft:piston"), + ("minecraft:daylight_detector_inverted", "minecraft:daylight_detector"), + ("minecraft:unpowered_comparator", "minecraft:comparator"), + ("minecraft:powered_comparator", "minecraft:comparator"), + ("minecraft:wall_banner", "minecraft:banner"), + ("minecraft:standing_banner", "minecraft:banner"), + ("minecraft:structure_block", "minecraft:structure_block"), + ("minecraft:end_portal", "minecraft:end_portal"), + ("minecraft:end_gateway", "minecraft:end_gateway"), + ("minecraft:shield", "minecraft:banner"), + ("minecraft:white_bed", "minecraft:bed"), + ("minecraft:orange_bed", "minecraft:bed"), + ("minecraft:magenta_bed", "minecraft:bed"), + ("minecraft:light_blue_bed", "minecraft:bed"), + ("minecraft:yellow_bed", "minecraft:bed"), + ("minecraft:lime_bed", "minecraft:bed"), + ("minecraft:pink_bed", "minecraft:bed"), + ("minecraft:gray_bed", "minecraft:bed"), + ("minecraft:silver_bed", "minecraft:bed"), + ("minecraft:light_gray_bed", "minecraft:bed"), + ("minecraft:cyan_bed", "minecraft:bed"), + ("minecraft:purple_bed", "minecraft:bed"), + ("minecraft:blue_bed", "minecraft:bed"), + ("minecraft:brown_bed", "minecraft:bed"), + ("minecraft:green_bed", "minecraft:bed"), + ("minecraft:red_bed", "minecraft:bed"), + ("minecraft:black_bed", "minecraft:bed"), + ("minecraft:oak_sign", "minecraft:sign"), + ("minecraft:spruce_sign", "minecraft:sign"), + ("minecraft:birch_sign", "minecraft:sign"), + ("minecraft:jungle_sign", "minecraft:sign"), + ("minecraft:acacia_sign", "minecraft:sign"), + ("minecraft:dark_oak_sign", "minecraft:sign"), + ("minecraft:crimson_sign", "minecraft:sign"), + ("minecraft:warped_sign", "minecraft:sign"), + ("minecraft:skeleton_skull", "minecraft:skull"), + ("minecraft:wither_skeleton_skull", "minecraft:skull"), + ("minecraft:zombie_head", "minecraft:skull"), + ("minecraft:player_head", "minecraft:skull"), + ("minecraft:creeper_head", "minecraft:skull"), + ("minecraft:dragon_head", "minecraft:skull"), + ("minecraft:barrel", "minecraft:barrel"), + ("minecraft:conduit", "minecraft:conduit"), + ("minecraft:smoker", "minecraft:smoker"), + ("minecraft:blast_furnace", "minecraft:blast_furnace"), + ("minecraft:lectern", "minecraft:lectern"), + ("minecraft:bell", "minecraft:bell"), + ("minecraft:jigsaw", "minecraft:jigsaw"), + ("minecraft:campfire", "minecraft:campfire"), + ("minecraft:bee_nest", "minecraft:beehive"), + ("minecraft:beehive", "minecraft:beehive"), + ("minecraft:sculk_sensor", "minecraft:sculk_sensor"), + ("minecraft:decorated_pot", "minecraft:decorated_pot"), + ("minecraft:crafter", "minecraft:crafter"), +]; + +/// `V705.ITEM_TO_ENTITY`: spawn eggs and the items that place an entity. +fn item_to_entity(item: &str) -> Option { + if let Some(mob) = item.strip_suffix("_spawn_egg").and_then(|m| m.strip_prefix("minecraft:")) + && SPAWN_EGG_MOBS.contains(&mob) + { + return Some(format!("minecraft:{mob}")); + } + match item { + "minecraft:armor_stand" | "minecraft:painting" | "minecraft:item_frame" | "minecraft:minecart" + | "minecraft:chest_minecart" | "minecraft:furnace_minecart" | "minecraft:tnt_minecart" + | "minecraft:hopper_minecart" | "minecraft:bamboo_raft" | "minecraft:bamboo_chest_raft" => Some(item.to_owned()), + "minecraft:boat" => Some("minecraft:oak_boat".into()), + _ => { + let wood = item.strip_prefix("minecraft:")?; + let wood = wood.strip_suffix("_chest_boat").or_else(|| wood.strip_suffix("_boat"))?; + BOAT_WOODS.contains(&wood).then(|| item.to_owned()) + } + } +} +const BOAT_WOODS: [&str; 8] = ["oak", "spruce", "birch", "jungle", "acacia", "cherry", "dark_oak", "mangrove"]; +const SPAWN_EGG_MOBS: &[&str] = &[ + "armadillo", "allay", "axolotl", "bat", "bee", "blaze", "bogged", "breeze", "cat", "camel", "cave_spider", "chicken", + "cod", "cow", "creeper", "dolphin", "donkey", "drowned", "elder_guardian", "ender_dragon", "enderman", "endermite", + "evoker", "fox", "frog", "ghast", "glow_squid", "goat", "guardian", "hoglin", "horse", "husk", "iron_golem", "llama", + "magma_cube", "mooshroom", "mule", "ocelot", "panda", "parrot", "phantom", "pig", "piglin", "piglin_brute", + "pillager", "polar_bear", "pufferfish", "rabbit", "ravager", "salmon", "sheep", "shulker", "silverfish", "skeleton", + "skeleton_horse", "slime", "sniffer", "snow_golem", "spider", "squid", "stray", "strider", "tadpole", "trader_llama", + "tropical_fish", "turtle", "vex", "villager", "vindicator", "wandering_trader", "warden", "witch", "wither", + "wither_skeleton", "wolf", "zoglin", "zombie", "zombie_horse", "zombie_villager", "zombified_piglin", +]; + +/// `V99.addNames` with V704/V705's tables: the read hook of item stacks before 1.20.5. +pub(super) fn add_names(stack: &mut Tag) { + let item = namespaced(&as_str(stack.get("id")).unwrap_or_else(|| "minecraft:air".into())); + let Some(tag) = stack.get_mut("tag") else { + return; + }; + if let Some(be) = tag.get_mut("BlockEntityTag") + && item != "minecraft:air" + && let Some((_, id)) = ITEM_TO_BLOCK_ENTITY.iter().find(|(i, _)| *i == item) + { + set(be, "id", string(id)); + } + if let Some(e) = tag.get_mut("EntityTag") + && e.get("id").is_none() + && let Some(id) = item_to_entity(&item) + { + set(e, "id", string(&id)); + } +} + +/// `ItemStackComponentizationFix.ItemStackData`. +struct Stack { + item: String, + count: i32, + components: Tag, + remainder: Tag, + tag: Tag, +} + +impl Stack { + fn read(v: &Tag) -> Option { + let item = as_str(v.get("id"))?; + let count = as_number(v.get("Count")?)?.int_value(); + let mut remainder = v.clone(); + remove(&mut remainder, "id"); + remove(&mut remainder, "Count"); + let tag = super::dynamic::or_empty_map(remainder.get("tag")); + remove(&mut remainder, "tag"); + Some(Self { item: namespaced(&item), count, components: empty_map(), remainder, tag }) + } + fn remove_tag(&mut self, key: &str) -> Option { + remove(&mut self.tag, key) + } + fn set_component(&mut self, ty: &str, value: Tag) { + set(&mut self.components, ty, value); + } + fn set_component_opt(&mut self, ty: &str, value: Option) { + if let Some(v) = value { + self.set_component(ty, v); + } + } + fn move_tag_into(&mut self, from: &str, target: &mut Tag, to: &str) { + if let Some(v) = self.remove_tag(from) { + set(target, to, v); + } + } + fn move_to_component_default(&mut self, key: &str, ty: &str, default: &Tag) { + if let Some(v) = self.remove_tag(key) + && v != *default + { + self.set_component(ty, v); + } + } + fn move_to_component(&mut self, key: &str, ty: &str) { + if let Some(v) = self.remove_tag(key) { + self.set_component(ty, v); + } + } + /// `fixSubTag`. + fn fix_sub_tag(&mut self, key: &str, skip_missing: bool, f: impl FnOnce(&mut Self, Tag) -> Tag) { + let value = self.tag.get(key).cloned(); + if skip_missing && value.is_none() { + return; + } + let map = value.unwrap_or_else(empty_map); + let map = f(self, map); + if is_empty_map(&map) { + remove(&mut self.tag, key); + } else { + set(&mut self.tag, key, map); + } + } + fn is(&self, id: &str) -> bool { + self.item == id + } + fn write(mut self) -> Tag { + let mut result = Tag::compound([("id", string(&self.item)), ("count", Tag::Int(self.count))]); + if !is_empty_map(&self.tag) { + let tag = std::mem::replace(&mut self.tag, empty_map()); + self.set_component("minecraft:custom_data", tag); + } + if !is_empty_map(&self.components) { + set(&mut result, "components", self.components); + } + // mergeRemainder: the remainder's fields under the stack's own. + let mut out = self.remainder; + if let (Tag::Compound(o), Tag::Compound(r)) = (&mut out, result) { + o.extend(r); + out + } else { + out + } + } +} + +const BUCKETED_MOBS: [&str; 6] = [ + "minecraft:pufferfish_bucket", + "minecraft:salmon_bucket", + "minecraft:cod_bucket", + "minecraft:tropical_fish_bucket", + "minecraft:axolotl_bucket", + "minecraft:tadpole_bucket", +]; +const BUCKETED_MOB_TAGS: [&str; 10] = + ["NoAI", "Silent", "NoGravity", "Glowing", "Invulnerable", "Health", "Age", "Variant", "HuntingCooldown", "BucketVariantTag"]; +const BOOLEAN_BLOCK_STATE_PROPERTIES: &[&str] = &[ + "attached", "bottom", "conditional", "disarmed", "drag", "enabled", "extended", "eye", "falling", "hanging", + "has_bottle_0", "has_bottle_1", "has_bottle_2", "has_record", "has_book", "inverted", "in_wall", "lit", "locked", + "occupied", "open", "persistent", "powered", "short", "signal_fire", "snowy", "triggered", "unstable", "waterlogged", + "berries", "bloom", "shrieking", "can_summon", "up", "down", "north", "east", "south", "west", "slot_0_occupied", + "slot_1_occupied", "slot_2_occupied", "slot_3_occupied", "slot_4_occupied", "slot_5_occupied", "cracked", "crafting", +]; + +/// The fix for one item stack (`writeFixAndRead` over ITEM_STACK). +pub(super) fn componentize(v: &mut Tag) { + if let Some(mut s) = Stack::read(v) { + fix_stack(&mut s); + *v = s.write(); + } +} + +fn fix_stack(s: &mut Stack) { + let hide_flags = as_int(s.remove_tag("HideFlags").as_ref(), 0); + s.move_to_component_default("Damage", "minecraft:damage", &Tag::Int(0)); + s.move_to_component_default("RepairCost", "minecraft:repair_cost", &Tag::Int(0)); + s.move_to_component("CustomModelData", "minecraft:custom_model_data"); + if let Some(b) = s.remove_tag("BlockStateTag") { + s.set_component("minecraft:block_state", fix_block_state_tag(b)); + } + s.move_to_component("EntityTag", "minecraft:entity_data"); + s.fix_sub_tag("BlockEntityTag", false, |s, be| { + let id = namespaced(&as_string(be.get("id"), "")); + let be = fix_block_entity_tag(s, be, &id); + let mut without_id = be.clone(); + remove(&mut without_id, "id"); + if is_empty_map(&without_id) { without_id } else { be } + }); + s.move_to_component("BlockEntityTag", "minecraft:block_entity_data"); + if as_bool(s.remove_tag("Unbreakable").as_ref(), false) { + let mut c = empty_map(); + if hide_flags & 4 != 0 { + set(&mut c, "show_in_tooltip", Tag::Byte(0)); + } + s.set_component("minecraft:unbreakable", c); + } + fix_enchantments(s, "Enchantments", "minecraft:enchantments", hide_flags & 1 != 0); + if s.is("minecraft:enchanted_book") { + fix_enchantments(s, "StoredEnchantments", "minecraft:stored_enchantments", hide_flags & 32 != 0); + } + s.fix_sub_tag("display", false, |s, d| fix_display(s, d, hide_flags)); + fix_block_predicates(s, "CanDestroy", "minecraft:can_break", hide_flags & 8 != 0); + fix_block_predicates(s, "CanPlaceOn", "minecraft:can_place_on", hide_flags & 16 != 0); + fix_attribute_modifiers(s, hide_flags); + if let Some(mut trim) = s.remove_tag("Trim") { + if hide_flags & 128 != 0 { + set(&mut trim, "show_in_tooltip", Tag::Byte(0)); + } + s.set_component("minecraft:trim", trim); + } + if hide_flags & 32 != 0 { + s.set_component("minecraft:hide_additional_tooltip", empty_map()); + } + if s.is("minecraft:crossbow") { + s.remove_tag("Charged"); + s.move_to_component_default("ChargedProjectiles", "minecraft:charged_projectiles", &Tag::List(vec![])); + } + if s.is("minecraft:bundle") { + s.move_to_component_default("Items", "minecraft:bundle_contents", &Tag::List(vec![])); + } + if s.is("minecraft:filled_map") { + s.move_to_component("map", "minecraft:map_id"); + let decorations = stream(s.remove_tag("Decorations").as_ref()); + let mut map = crate::nbt::Compound::new(); + for d in decorations { + let (id, value) = fix_map_decoration(&d); + // Collectors.toMap(..., (first, second) -> first): the first stays. + map.entry(id).or_insert(value); + } + if !map.is_empty() { + s.set_component("minecraft:map_decorations", Tag::Compound(map)); + } + } + if matches!( + s.item.as_str(), + "minecraft:potion" | "minecraft:splash_potion" | "minecraft:lingering_potion" | "minecraft:tipped_arrow" + ) { + let mut c = empty_map(); + if let Some(p) = as_str(s.remove_tag("Potion").as_ref()).filter(|p| p != "minecraft:empty") { + set(&mut c, "potion", string(&p)); + } + s.move_tag_into("CustomPotionColor", &mut c, "custom_color"); + s.move_tag_into("custom_potion_effects", &mut c, "custom_effects"); + if !is_empty_map(&c) { + s.set_component("minecraft:potion_contents", c); + } + } + if s.is("minecraft:writable_book") + && let Some(pages) = fix_book_pages(s) + { + s.set_component("minecraft:writable_book_content", Tag::compound([("pages", pages)])); + } + if s.is("minecraft:written_book") { + let pages = fix_book_pages(s); + let title = as_string(s.remove_tag("title").as_ref(), ""); + let filtered_title = as_str(s.remove_tag("filtered_title").as_ref()); + let mut c = empty_map(); + set(&mut c, "title", filtered_text(&title, filtered_title.as_deref())); + s.move_tag_into("author", &mut c, "author"); + s.move_tag_into("resolved", &mut c, "resolved"); + s.move_tag_into("generation", &mut c, "generation"); + if let Some(p) = pages { + set(&mut c, "pages", p); + } + s.set_component("minecraft:written_book_content", c); + } + if s.is("minecraft:suspicious_stew") { + s.move_to_component("effects", "minecraft:suspicious_stew_effects"); + } + if s.is("minecraft:debug_stick") { + s.move_to_component("DebugProperty", "minecraft:debug_stick_state"); + } + if BUCKETED_MOBS.contains(&s.item.as_str()) { + let mut data = empty_map(); + for k in BUCKETED_MOB_TAGS { + s.move_tag_into(k, &mut data, k); + } + if !is_empty_map(&data) { + s.set_component("minecraft:bucket_entity_data", data); + } + } + if s.is("minecraft:goat_horn") { + s.move_to_component("instrument", "minecraft:instrument"); + } + if s.is("minecraft:knowledge_book") { + s.move_to_component("Recipes", "minecraft:recipes"); + } + if s.is("minecraft:compass") { + fix_lodestone_tracker(s); + } + if s.is("minecraft:firework_rocket") { + s.fix_sub_tag("Fireworks", true, |s, mut f| { + let explosions: Vec = stream(f.get("Explosions")).into_iter().map(fix_firework_explosion).collect(); + let flight = as_int(f.get("Flight"), 0); + s.set_component( + "minecraft:fireworks", + Tag::compound([("explosions", Tag::List(explosions)), ("flight_duration", Tag::Byte(flight as i8))]), + ); + remove(&mut f, "Explosions"); + remove(&mut f, "Flight"); + f + }); + } + if s.is("minecraft:firework_star") { + s.fix_sub_tag("Explosion", true, |s, mut e| { + s.set_component("minecraft:firework_explosion", fix_firework_explosion(e.clone())); + for k in ["Type", "Colors", "FadeColors", "Trail", "Flicker"] { + remove(&mut e, k); + } + e + }); + } + if s.is("minecraft:player_head") + && let Some(owner) = s.remove_tag("SkullOwner") + { + s.set_component("minecraft:profile", fix_profile(&owner)); + } +} + +fn fix_block_state_tag(v: Tag) -> Tag { + let Tag::Compound(map) = v else { + return v; + }; + let mut out = crate::nbt::Compound::new(); + for (k, value) in map { + let name = k.to_string_lossy(); + if BOOLEAN_BLOCK_STATE_PROPERTIES.contains(&name.as_str()) + && let Some(b) = as_bool_opt(Some(&value)) + { + out.insert(k, string(if b { "true" } else { "false" })); + continue; + } + let value = match as_number(&value) { + Some(n) => string(&n.java_string()), + None => value, + }; + out.insert(k, value); + } + Tag::Compound(out) +} + +fn is_strictly_valid_json(v: &Tag) -> bool { + matches!(v, Tag::String(s) if json::parse_strict(&s.0).is_some()) +} + +fn fix_display(s: &mut Stack, mut d: Tag, hide_flags: i32) -> Tag { + if let Some(name) = d.get("Name").filter(|n| is_strictly_valid_json(n)) { + s.set_component("minecraft:custom_name", name.clone()); + } + if d.get("Lore").is_some() { + let lore = stream(d.get("Lore")).into_iter().filter(is_strictly_valid_json).collect(); + s.set_component("minecraft:lore", Tag::List(lore)); + } + let color = d.get("color").and_then(as_number).map(|n| n.int_value()); + let hide_dye = hide_flags & 64 != 0; + if color.is_some() || hide_dye { + let mut c = Tag::compound([("rgb", Tag::Int(color.unwrap_or(10_511_680)))]); + if hide_dye { + set(&mut c, "show_in_tooltip", Tag::Byte(0)); + } + s.set_component("minecraft:dyed_color", c); + } + if let Some(loc) = as_str(d.get("LocName")) { + s.set_component( + "minecraft:item_name", + Tag::String(JavaString(json::translatable_component_json(&loc))), + ); + } + if s.is("minecraft:filled_map") { + let color = d.get("MapColor").cloned(); + s.set_component_opt("minecraft:map_color", color); + remove(&mut d, "MapColor"); + } + for k in ["Name", "Lore", "color", "LocName"] { + remove(&mut d, k); + } + d +} + +fn fix_block_entity_tag(s: &mut Stack, mut be: Tag, id: &str) -> Tag { + let lock = be.get("Lock").cloned(); + s.set_component_opt("minecraft:lock", lock); + remove(&mut be, "Lock"); + if let Some(loot) = be.get("LootTable").cloned() { + let mut c = Tag::compound([("loot_table", loot)]); + let seed = as_long(be.get("LootTableSeed"), 0); + if seed != 0 { + set(&mut c, "seed", Tag::Long(seed)); + } + s.set_component("minecraft:container_loot", c); + remove(&mut be, "LootTable"); + remove(&mut be, "LootTableSeed"); + } + match id { + "minecraft:skull" => { + let v = be.get("note_block_sound").cloned(); + s.set_component_opt("minecraft:note_block_sound", v); + remove(&mut be, "note_block_sound"); + } + "minecraft:decorated_pot" => { + let sherds = be.get("sherds").cloned(); + s.set_component_opt("minecraft:pot_decorations", sherds); + if let Some(item) = be.get("item").cloned() { + s.set_component( + "minecraft:container", + Tag::List(vec![Tag::compound([("slot", Tag::Int(0)), ("item", item)])]), + ); + } + remove(&mut be, "sherds"); + remove(&mut be, "item"); + } + "minecraft:banner" => { + let patterns = be.get("patterns").cloned(); + s.set_component_opt("minecraft:banner_patterns", patterns); + if let Some(base) = be.get("Base").and_then(as_number) { + s.set_component("minecraft:base_color", string(super::fixes_a::dye_color(base.int_value()))); + } + remove(&mut be, "patterns"); + remove(&mut be, "Base"); + } + "minecraft:shulker_box" | "minecraft:chest" | "minecraft:trapped_chest" | "minecraft:furnace" + | "minecraft:ender_chest" | "minecraft:dispenser" | "minecraft:dropper" | "minecraft:brewing_stand" + | "minecraft:hopper" | "minecraft:barrel" | "minecraft:smoker" | "minecraft:blast_furnace" + | "minecraft:campfire" | "minecraft:chiseled_bookshelf" | "minecraft:crafter" => { + let items: Vec = stream(be.get("Items")) + .into_iter() + .map(|mut item| { + let slot = item.get("Slot").and_then(as_number).map_or(0, |n| n.byte_value()) as u8; + remove(&mut item, "Slot"); + Tag::compound([("slot", Tag::Int(i32::from(slot))), ("item", item)]) + }) + .collect(); + if !items.is_empty() { + s.set_component("minecraft:container", Tag::List(items)); + } + remove(&mut be, "Items"); + } + "minecraft:beehive" => { + let bees = be.get("bees").cloned(); + s.set_component_opt("minecraft:bees", bees); + remove(&mut be, "bees"); + } + _ => {} + } + be +} + +fn fix_enchantments(s: &mut Stack, key: &str, ty: &str, hide: bool) { + let raw = s.remove_tag(key); + let mut enchantments: Vec<(String, i32)> = Vec::new(); + for e in stream(raw.as_ref()) { + if let (Some(id), Some(level)) = (as_str(e.get("id")), e.get("lvl").and_then(as_number)) { + let level = level.int_value().clamp(0, 255); + if level > 0 { + enchantments.push((id, level)); + } + } + } + if !enchantments.is_empty() || hide { + let mut levels = empty_map(); + for (id, level) in &enchantments { + set(&mut levels, id, Tag::Int(*level)); + } + let mut c = Tag::compound([("levels", levels)]); + if hide { + set(&mut c, "show_in_tooltip", Tag::Byte(0)); + } + s.set_component(ty, c); + } + if raw.is_some() && enchantments.is_empty() { + s.set_component("minecraft:enchantment_glint_override", Tag::Byte(1)); + } +} + +fn fix_block_predicates(s: &mut Stack, tag: &str, component: &str, hide: bool) { + let Some(old) = s.remove_tag(tag) else { + return; + }; + let predicates = stream(Some(&old)) + .into_iter() + .map(|v| match as_str(Some(&v)) { + Some(text) => fix_block_predicate(&text), + None => v, + }) + .collect(); + let mut c = Tag::compound([("predicates", Tag::List(predicates))]); + if hide { + set(&mut c, "show_in_tooltip", Tag::Byte(0)); + } + s.set_component(component, c); +} + +fn fix_block_predicate(text: &str) -> Tag { + let start_props = text.find('['); + let start_nbt = text.find('{'); + let mut end = text.len(); + if let Some(p) = start_props { + end = p; + } + if let Some(n) = start_nbt { + end = end.min(n); + } + let mut predicate = Tag::compound([("blocks", string(text[..end].trim()))]); + let end_props = text.find(']'); + if let (Some(start), Some(stop)) = (start_props, end_props) { + let mut props = empty_map(); + // Guava Splitter.on(','): String.substring throws when ] is before [. + if start < stop { + for property in text[start + 1..stop].split(',') { + if let Some(eq) = property.find('=') { + set(&mut props, property[..eq].trim(), string(property[eq + 1..].trim())); + } + } + } + set(&mut predicate, "state", props); + } + let end_nbt = text.find('}'); + if let (Some(start), Some(stop)) = (start_nbt, end_nbt) + && start <= stop + { + set(&mut predicate, "nbt", string(&text[start..=stop])); + } + predicate +} + +fn fix_attribute_modifiers(s: &mut Stack, hide_flags: i32) { + let Some(field) = s.remove_tag("AttributeModifiers") else { + return; + }; + let modifiers = stream(Some(&field)).into_iter().map(|m| fix_attribute_modifier(&m)).collect(); + let mut c = Tag::compound([("modifiers", Tag::List(modifiers))]); + if hide_flags & 2 != 0 { + set(&mut c, "show_in_tooltip", Tag::Byte(0)); + } + s.set_component("minecraft:attribute_modifiers", c); +} + +fn fix_attribute_modifier(input: &Tag) -> Tag { + let mut r = Tag::compound([ + ("name", string("")), + ("amount", Tag::Double(0f64.to_bits())), + ("operation", string("add_value")), + ]); + super::dynamic::copy_field(input, "AttributeName", &mut r, "type"); + super::dynamic::copy_field(input, "Slot", &mut r, "slot"); + super::dynamic::copy_field(input, "UUID", &mut r, "uuid"); + super::dynamic::copy_field(input, "Name", &mut r, "name"); + super::dynamic::copy_field(input, "Amount", &mut r, "amount"); + if let Some(op) = input.get("Operation") { + let name = match as_int(Some(op), 0) { + 1 => "add_multiplied_base", + 2 => "add_multiplied_total", + _ => "add_value", + }; + set(&mut r, "operation", string(name)); + } + r +} + +fn fix_map_decoration(d: &Tag) -> (JavaString, Tag) { + let id = match d.get("id") { + Some(Tag::String(s)) => s.clone(), + // A non-string key fails the map; vanilla throws on it. + // createMap throws on a key that is not a string; such a stack is left as it was. + Some(_) => JavaString::default(), + None => JavaString::default(), + }; + let ty = match as_int(d.get("type"), 0) { + 1 => "frame", + 2 => "red_marker", + 3 => "blue_marker", + 4 => "target_x", + 5 => "target_point", + 6 => "player_off_map", + 7 => "player_off_limits", + 8 => "mansion", + 9 => "monument", + 10 => "banner_white", + 11 => "banner_orange", + 12 => "banner_magenta", + 13 => "banner_light_blue", + 14 => "banner_yellow", + 15 => "banner_lime", + 16 => "banner_pink", + 17 => "banner_gray", + 18 => "banner_light_gray", + 19 => "banner_cyan", + 20 => "banner_purple", + 21 => "banner_blue", + 22 => "banner_brown", + 23 => "banner_green", + 24 => "banner_red", + 25 => "banner_black", + 26 => "red_x", + 27 => "village_desert", + 28 => "village_plains", + 29 => "village_savanna", + 30 => "village_snowy", + 31 => "village_taiga", + 32 => "jungle_temple", + 33 => "swamp_hut", + _ => "player", + }; + let value = Tag::compound([ + ("type", string(ty)), + ("x", Tag::Double(as_double(d.get("x"), 0.0).to_bits())), + ("z", Tag::Double(as_double(d.get("z"), 0.0).to_bits())), + ("rotation", Tag::Float((as_double(d.get("rot"), 0.0) as f32).to_bits())), + ]); + (id, value) +} + +fn fix_book_pages(s: &mut Stack) -> Option { + let pages: Vec = stream(s.remove_tag("pages").as_ref()).iter().map(|p| as_string(Some(p), "")).collect(); + let filtered: Vec<(String, String)> = match s.remove_tag("filtered_pages") { + Some(Tag::Compound(c)) => c + .iter() + .map(|(k, v)| (k.to_string_lossy(), as_string(Some(v), ""))) + .collect(), + _ => Vec::new(), + }; + if pages.is_empty() { + return None; + } + let fixed = pages + .iter() + .enumerate() + .map(|(i, page)| { + let f = filtered.iter().find(|(k, _)| *k == i.to_string()).map(|(_, v)| v.as_str()); + filtered_text(page, f) + }) + .collect(); + Some(Tag::List(fixed)) +} + +fn filtered_text(text: &str, filtered: Option<&str>) -> Tag { + let mut t = Tag::compound([("raw", string(text))]); + if let Some(f) = filtered { + set(&mut t, "filtered", string(f)); + } + t +} + +fn fix_lodestone_tracker(s: &mut Stack) { + let pos = s.remove_tag("LodestonePos"); + let dimension = s.remove_tag("LodestoneDimension"); + if pos.is_none() && dimension.is_none() { + return; + } + let tracked = as_bool(s.remove_tag("LodestoneTracked").as_ref(), true); + let mut c = empty_map(); + if let (Some(p), Some(d)) = (pos, dimension) { + set(&mut c, "target", Tag::compound([("pos", p), ("dimension", d)])); + } + if !tracked { + set(&mut c, "tracked", Tag::Byte(0)); + } + s.set_component("minecraft:lodestone_tracker", c); +} + +fn fix_firework_explosion(mut e: Tag) -> Tag { + let shape = match as_int(e.get("Type"), 0) { + 1 => "large_ball", + 2 => "star", + 3 => "creeper", + 4 => "burst", + _ => "small_ball", + }; + set(&mut e, "shape", string(shape)); + remove(&mut e, "Type"); + rename(&mut e, "Colors", "colors"); + rename(&mut e, "FadeColors", "fade_colors"); + rename(&mut e, "Trail", "has_trail"); + rename(&mut e, "Flicker", "has_twinkle"); + e +} + +/// `ItemStackComponentizationFix.fixProfile`. +pub(super) fn fix_profile(v: &Tag) -> Tag { + if let Some(name) = as_str(Some(v)) { + return if valid_player_name(&name) { Tag::compound([("name", string(&name))]) } else { empty_map() }; + } + let name = as_string(v.get("Name"), ""); + let id = v.get("Id").cloned(); + let properties = fix_profile_properties(v.get("Properties")); + let mut p = empty_map(); + if valid_player_name(&name) { + set(&mut p, "name", string(&name)); + } + if let Some(id) = id { + set(&mut p, "id", id); + } + if let Some(props) = properties { + set(&mut p, "properties", props); + } + p +} + +fn valid_player_name(name: &str) -> bool { + let units: Vec = name.encode_utf16().collect(); + units.len() <= 16 && units.iter().all(|&c| c > 32 && c < 127) +} + +fn fix_profile_properties(v: Option<&Tag>) -> Option { + let Some(Tag::Compound(map)) = v else { + return None; + }; + // asMap collects into a HashMap: the property names in Java hash order. + let mut entries: Vec<(String, Vec<(String, Option)>)> = map + .iter() + .map(|(k, list)| { + let props = stream(Some(list)) + .iter() + .map(|p| (as_string(p.get("Value"), ""), as_str(p.get("Signature")))) + .collect(); + (k.to_string_lossy(), props) + }) + .collect(); + if entries.is_empty() { + return None; + } + entries = java_hash_order(entries, |(k, _)| k.clone()); + let out = entries + .into_iter() + .flat_map(|(name, props)| { + props.into_iter().map(move |(value, signature)| { + let mut p = Tag::compound([("name", string(&name)), ("value", string(&value))]); + if let Some(sig) = signature { + set(&mut p, "signature", string(&sig)); + } + p + }) + }) + .collect(); + Some(Tag::List(out)) +} + +/// Java `String.hashCode`. +pub(super) fn java_string_hash(s: &str) -> i32 { + s.encode_utf16().fold(0i32, |h, c| h.wrapping_mul(31).wrapping_add(i32::from(c))) +} + +/// Iteration order of a `java.util.HashMap`/`HashSet` built by inserting the +/// items in order (default capacity, string keys, no tree bins). +pub(super) fn java_hash_order(items: Vec, key: impl Fn(&T) -> String) -> Vec { + let mut capacity = 16usize; + while items.len() as f64 > capacity as f64 * 0.75 { + capacity *= 2; + } + let spread = |h: i32| (h ^ ((h as u32) >> 16) as i32) as u32 as usize; + let mut buckets: Vec> = (0..capacity).map(|_| Vec::new()).collect(); + for item in items { + let index = spread(java_string_hash(&key(&item))) & (capacity - 1); + buckets[index].push(item); + } + buckets.into_iter().flatten().collect() +} + +// ------------------------------------------------------------------ particles + +/// `ParticleUnflatteningFix.fix` of a particle description string. +pub(super) fn unflatten_particle(description: &str) -> Tag { + let (first, rest) = match description.split_once(' ') { + Some((a, b)) => (a, Some(b)), + None => (description, None), + }; + let id = namespaced(first); + let mut result = Tag::compound([("type", string(&id))]); + let Some(contents) = rest else { + return result; + }; + match id.as_str() { + "minecraft:item" => { + let mut stack = Tag::compound([("Count", Tag::Int(1))]); + match contents.find('{') { + None => set(&mut stack, "id", string(contents)), + Some(i) => { + set(&mut stack, "id", string(&contents[..i])); + if let Ok((tag, used)) = crate::nbt::parse_snbt(&contents[i..]) + && used == contents[i..].len() + { + set(&mut stack, "tag", tag); + } + } + } + set(&mut result, "item", stack); + } + "minecraft:block" | "minecraft:block_marker" | "minecraft:falling_dust" | "minecraft:dust_pillar" => { + let mut state = empty_map(); + match contents.find('[') { + None => set(&mut state, "Name", string(&namespaced(contents))), + Some(i) => { + set(&mut state, "Name", string(&namespaced(&contents[..i]))); + let props = parse_block_properties(&contents[i..]); + if let Some(Tag::Compound(p)) = &props + && !p.is_empty() + { + set(&mut state, "Properties", props.unwrap_or_else(empty_map)); + } + } + } + set(&mut result, "block_state", state); + } + "minecraft:dust" => { + let mut r = Reader::new(contents); + if let Some(color) = r.vector() + && r.expect(' ') + && let Some(scale) = r.float() + { + set(&mut result, "color", color); + set(&mut result, "scale", Tag::Float(scale.to_bits())); + } + } + "minecraft:dust_color_transition" => { + let mut r = Reader::new(contents); + if let Some(from) = r.vector() + && r.expect(' ') + && let Some(scale) = r.float() + && r.expect(' ') + && let Some(to) = r.vector() + { + set(&mut result, "from_color", from); + set(&mut result, "to_color", to); + set(&mut result, "scale", Tag::Float(scale.to_bits())); + } + } + "minecraft:sculk_charge" => { + if let Some(roll) = Reader::new(contents).float() { + set(&mut result, "roll", Tag::Float(roll.to_bits())); + } + } + "minecraft:vibration" => { + let mut r = Reader::new(contents); + if let Some(x) = r.double() + && r.expect(' ') + && let Some(y) = r.double() + && r.expect(' ') + && let Some(z) = r.double() + && r.expect(' ') + && let Some(ticks) = r.int() + { + let pos = int_list(&[ + (x as f32).floor() as i32, + (y as f32).floor() as i32, + (z as f32).floor() as i32, + ]); + set( + &mut result, + "destination", + Tag::compound([("type", string("minecraft:block")), ("pos", pos)]), + ); + set(&mut result, "arrival_in_ticks", Tag::Int(ticks)); + } + } + "minecraft:shriek" => { + if let Some(delay) = Reader::new(contents).int() { + set(&mut result, "delay", Tag::Int(delay)); + } + } + _ => {} + } + result +} + +/// `parseBlockProperties` with Brigadier's StringReader. +fn parse_block_properties(contents: &str) -> Option { + let mut r = Reader::new(contents); + let mut out = empty_map(); + if !r.expect('[') { + return Some(empty_map()); + } + r.skip_ws(); + while r.peek().is_some_and(|c| c != ']') { + r.skip_ws(); + let Some(key) = r.string() else { + return Some(empty_map()); + }; + r.skip_ws(); + if !r.expect('=') { + return Some(empty_map()); + } + r.skip_ws(); + let Some(value) = r.string() else { + return Some(empty_map()); + }; + r.skip_ws(); + set(&mut out, &key, string(&value)); + if r.peek().is_some() { + if r.peek() != Some(',') { + break; + } + r.pos += 1; + } + } + if !r.expect(']') { + return Some(empty_map()); + } + Some(out) +} + +/// The parts of Brigadier's `StringReader` the particle fix uses. +struct Reader { + s: Vec, + pos: usize, +} +impl Reader { + fn new(s: &str) -> Self { + Self { s: s.chars().collect(), pos: 0 } + } + fn peek(&self) -> Option { + self.s.get(self.pos).copied() + } + fn expect(&mut self, c: char) -> bool { + if self.peek() == Some(c) { + self.pos += 1; + true + } else { + false + } + } + fn skip_ws(&mut self) { + while self.peek().is_some_and(char::is_whitespace) { + self.pos += 1; + } + } + fn number_text(&mut self) -> String { + let start = self.pos; + while self.peek().is_some_and(|c| c.is_ascii_digit() || c == '.' || c == '-') { + self.pos += 1; + } + self.s[start..self.pos].iter().collect() + } + fn float(&mut self) -> Option { + self.number_text().parse().ok() + } + fn double(&mut self) -> Option { + self.number_text().parse().ok() + } + fn int(&mut self) -> Option { + let start = self.pos; + while self.peek().is_some_and(|c| c.is_ascii_digit() || c == '-') { + self.pos += 1; + } + self.s[start..self.pos].iter().collect::().parse().ok() + } + fn vector(&mut self) -> Option { + let x = self.float()?; + if !self.expect(' ') { + return None; + } + let y = self.float()?; + if !self.expect(' ') { + return None; + } + let z = self.float()?; + Some(Tag::List(vec![Tag::Float(x.to_bits()), Tag::Float(y.to_bits()), Tag::Float(z.to_bits())])) + } + /// `StringReader.readString`: quoted or unquoted. + fn string(&mut self) -> Option { + match self.peek() { + Some(q @ ('"' | '\'')) => { + self.pos += 1; + let mut out = String::new(); + let mut escaped = false; + while let Some(c) = self.peek() { + self.pos += 1; + if escaped { + if c != q && c != '\\' { + return None; + } + out.push(c); + escaped = false; + } else if c == '\\' { + escaped = true; + } else if c == q { + return Some(out); + } else { + out.push(c); + } + } + None + } + _ => { + let start = self.pos; + while self + .peek() + .is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.' | '+')) + { + self.pos += 1; + } + Some(self.s[start..self.pos].iter().collect()) + } + } + } +} diff --git a/crates/horizon-world/src/datafix/json.rs b/crates/horizon-world/src/datafix/json.rs new file mode 100644 index 000000000..dda40663f --- /dev/null +++ b/crates/horizon-world/src/datafix/json.rs @@ -0,0 +1,610 @@ +//! The JSON that DataFixers read and write: Gson's `JsonReader` in strict and +//! lenient mode, `GsonHelper.toStableString`, and `JsonOps.convertTo` into NBT. +//! Strings stay UTF-16 like Java's; numbers keep their literal like Gson's +//! `LazilyParsedNumber`. +use crate::nbt::{Compound, JavaString, Tag}; + +#[derive(Clone, Debug, PartialEq)] +pub enum Json { + Null, + Bool(bool), + /// The literal as written (`LazilyParsedNumber`) or `Number.toString`. + Num(String), + Str(Vec), + Arr(Vec), + /// Insertion order, a repeated key replacing the value in place. + Obj(Vec<(Vec, Json)>), +} + +impl Json { + pub fn get(&self, key: &str) -> Option<&Json> { + let key: Vec = key.encode_utf16().collect(); + match self { + Json::Obj(entries) => entries.iter().find(|(k, _)| *k == key).map(|(_, v)| v), + _ => None, + } + } + fn insert(entries: &mut Vec<(Vec, Json)>, key: Vec, value: Json) { + if let Some(e) = entries.iter_mut().find(|(k, _)| *k == key) { + e.1 = value; + } else { + entries.push((key, value)); + } + } + pub fn object(entries: Vec<(&str, Json)>) -> Json { + let mut out = Vec::new(); + for (k, v) in entries { + Json::insert(&mut out, k.encode_utf16().collect(), v); + } + Json::Obj(out) + } + pub fn string(s: &str) -> Json { + Json::Str(s.encode_utf16().collect()) + } +} + +#[derive(Clone, Copy, PartialEq)] +enum Mode { + Strict, + Lenient, +} + +struct Reader<'a> { + s: &'a [u16], + pos: usize, + mode: Mode, +} + +type R = Result; + +const fn u(c: char) -> u16 { + c as u16 +} + +impl Reader<'_> { + fn lenient(&self) -> bool { + self.mode == Mode::Lenient + } + fn peek_char(&self) -> Option { + self.s.get(self.pos).copied() + } + /// `JsonReader.nextNonWhitespace`, with comments when lenient. + fn skip_ws(&mut self) -> R> { + loop { + let Some(c) = self.peek_char() else { + return Ok(None); + }; + match c { + 0x20 | 0x0A | 0x09 | 0x0D => self.pos += 1, + 0x2F if self.s.get(self.pos + 1) == Some(&u('*')) || self.s.get(self.pos + 1) == Some(&u('/')) => { + if !self.lenient() { + return Err(()); + } + if self.s[self.pos + 1] == u('*') { + let start = self.pos + 2; + let end = (start..self.s.len().saturating_sub(1)) + .find(|&i| self.s[i] == u('*') && self.s[i + 1] == u('/')) + .ok_or(())?; + self.pos = end + 2; + } else { + self.skip_line(); + } + } + 0x23 => { + if !self.lenient() { + return Err(()); + } + self.skip_line(); + } + _ => return Ok(Some(c)), + } + } + } + fn skip_line(&mut self) { + while let Some(c) = self.peek_char() { + self.pos += 1; + if c == 0x0A || c == 0x0D { + break; + } + } + } + fn value(&mut self, depth: usize) -> R { + if depth > 255 { + return Err(()); + } + let c = self.skip_ws()?.ok_or(())?; + match c { + 0x7B => self.object(depth), + 0x5B => self.array(depth), + 0x22 => { + self.pos += 1; + Ok(Json::Str(self.quoted(0x22)?)) + } + 0x27 => { + if !self.lenient() { + return Err(()); + } + self.pos += 1; + Ok(Json::Str(self.quoted(0x27)?)) + } + _ => self.literal(), + } + } + fn object(&mut self, depth: usize) -> R { + self.pos += 1; + let mut entries = Vec::new(); + if self.skip_ws()? == Some(u('}')) { + self.pos += 1; + return Ok(Json::Obj(entries)); + } + loop { + let c = self.skip_ws()?.ok_or(())?; + let name = match c { + 0x22 => { + self.pos += 1; + self.quoted(0x22)? + } + 0x27 if self.lenient() => { + self.pos += 1; + self.quoted(0x27)? + } + _ if self.lenient() => { + let lit = self.unquoted(); + if lit.is_empty() { + return Err(()); + } + lit + } + _ => return Err(()), + }; + match self.skip_ws()?.ok_or(())? { + 0x3A => self.pos += 1, + 0x3D if self.lenient() => { + self.pos += 1; + if self.peek_char() == Some(u('>')) { + self.pos += 1; + } + } + _ => return Err(()), + } + let value = self.value(depth + 1)?; + Json::insert(&mut entries, name, value); + match self.skip_ws()?.ok_or(())? { + 0x7D => { + self.pos += 1; + return Ok(Json::Obj(entries)); + } + 0x2C => self.pos += 1, + 0x3B if self.lenient() => self.pos += 1, + _ => return Err(()), + } + } + } + fn array(&mut self, depth: usize) -> R { + self.pos += 1; + let mut out = Vec::new(); + if self.skip_ws()? == Some(u(']')) { + self.pos += 1; + return Ok(Json::Arr(out)); + } + loop { + let c = self.skip_ws()?.ok_or(())?; + // Lenient: an empty element between separators is a null. + if self.lenient() && (c == u(',') || c == u(';')) { + out.push(Json::Null); + self.pos += 1; + continue; + } + if self.lenient() && c == u(']') { + out.push(Json::Null); + self.pos += 1; + return Ok(Json::Arr(out)); + } + out.push(self.value(depth + 1)?); + match self.skip_ws()?.ok_or(())? { + 0x5D => { + self.pos += 1; + return Ok(Json::Arr(out)); + } + 0x2C => self.pos += 1, + 0x3B if self.lenient() => self.pos += 1, + _ => return Err(()), + } + } + } + fn quoted(&mut self, quote: u16) -> R> { + let mut out = Vec::new(); + loop { + let c = self.peek_char().ok_or(())?; + self.pos += 1; + if c == quote { + return Ok(out); + } + if c == u('\\') { + let e = self.peek_char().ok_or(())?; + self.pos += 1; + match e { + 0x75 => { + let hex = self.s.get(self.pos..self.pos + 4).ok_or(())?; + let text = String::from_utf16(hex).map_err(|_| ())?; + out.push(u16::from_str_radix(&text, 16).map_err(|_| ())?); + self.pos += 4; + } + 0x74 => out.push(u('\t')), + 0x62 => out.push(0x08), + 0x6E => out.push(u('\n')), + 0x72 => out.push(u('\r')), + 0x66 => out.push(0x0C), + 0x0A | 0x22 | 0x5C | 0x2F => out.push(e), + 0x27 => out.push(e), + _ if self.lenient() => out.push(e), + _ => return Err(()), + } + } else { + if !self.lenient() && c < 0x20 { + return Err(()); + } + out.push(c); + } + } + } + fn unquoted(&mut self) -> Vec { + let start = self.pos; + while let Some(c) = self.peek_char() { + if matches!( + c, + 0x2F | 0x5C | 0x3B | 0x23 | 0x3D | 0x7B | 0x7D | 0x5B | 0x5D | 0x3A | 0x2C | 0x20 | 0x09 | 0x0C | 0x0D | 0x0A + ) { + break; + } + self.pos += 1; + } + self.s[start..self.pos].to_vec() + } + /// A keyword, a number, or (lenient) an unquoted string. + fn literal(&mut self) -> R { + let lit = self.unquoted(); + if lit.is_empty() { + return Err(()); + } + let text = String::from_utf16_lossy(&lit); + for (word, value) in [("true", Json::Bool(true)), ("false", Json::Bool(false)), ("null", Json::Null)] { + if text.len() == word.len() + && text.chars().zip(word.chars()).all(|(a, b)| { + a == b || self.lenient() && a == b.to_ascii_uppercase() + }) + { + return Ok(value); + } + } + if is_json_number(&text) { + return Ok(Json::Num(text)); + } + if self.lenient() { + return Ok(Json::Str(lit)); + } + Err(()) + } +} + +/// `JsonReader.peekNumber`. +fn is_json_number(s: &str) -> bool { + let b = s.as_bytes(); + let mut i = 0; + if b.get(i) == Some(&b'-') { + i += 1; + } + match b.get(i) { + Some(b'0') => { + i += 1; + if b.get(i).is_some_and(u8::is_ascii_digit) { + return false; + } + } + Some(c) if c.is_ascii_digit() => { + while b.get(i).is_some_and(u8::is_ascii_digit) { + i += 1; + } + } + _ => return false, + } + if b.get(i) == Some(&b'.') { + i += 1; + if !b.get(i).is_some_and(u8::is_ascii_digit) { + return false; + } + while b.get(i).is_some_and(u8::is_ascii_digit) { + i += 1; + } + } + if matches!(b.get(i), Some(b'e' | b'E')) { + i += 1; + if matches!(b.get(i), Some(b'+' | b'-')) { + i += 1; + } + if !b.get(i).is_some_and(u8::is_ascii_digit) { + return false; + } + while b.get(i).is_some_and(u8::is_ascii_digit) { + i += 1; + } + } + i == b.len() +} + +fn parse_mode(s: &[u16], mode: Mode) -> Option { + let mut r = Reader { s, pos: 0, mode }; + let value = r.value(0).ok()?; + // JsonParser.parseReader: the whole document must be consumed. + if r.skip_ws().ok()?.is_some() { + if mode == Mode::Lenient && value == Json::Null { + return Some(value); + } + return None; + } + Some(value) +} + +/// `LenientJsonParser.parse` (Gson `JsonParser.parseString`). +pub fn parse_lenient(s: &[u16]) -> Option { + parse_mode(s, Mode::Lenient) +} +/// `StrictJsonParser.parse`. +pub fn parse_strict(s: &[u16]) -> Option { + parse_mode(s, Mode::Strict) +} + +/// `GsonHelper.toStableString`: keys in natural (UTF-16) order, Gson escaping. +pub fn to_stable_string(json: &Json) -> Vec { + let mut out = Vec::new(); + write(json, &mut out); + out +} +fn write(json: &Json, out: &mut Vec) { + let push = |out: &mut Vec, s: &str| out.extend(s.encode_utf16()); + match json { + Json::Null => push(out, "null"), + Json::Bool(b) => push(out, if *b { "true" } else { "false" }), + Json::Num(n) => push(out, n), + Json::Str(s) => write_string(s, out), + Json::Arr(items) => { + out.push(u('[')); + for (i, item) in items.iter().enumerate() { + if i > 0 { + out.push(u(',')); + } + write(item, out); + } + out.push(u(']')); + } + Json::Obj(entries) => { + let mut sorted: Vec<&(Vec, Json)> = entries.iter().collect(); + sorted.sort_by(|a, b| a.0.cmp(&b.0)); + out.push(u('{')); + for (i, (k, v)) in sorted.into_iter().enumerate() { + if i > 0 { + out.push(u(',')); + } + write_string(k, out); + out.push(u(':')); + write(v, out); + } + out.push(u('}')); + } + } +} +/// `JsonWriter.string` without HTML escaping. +fn write_string(s: &[u16], out: &mut Vec) { + out.push(u('"')); + for &c in s { + match c { + 0x22 => out.extend("\\\"".encode_utf16()), + 0x5C => out.extend("\\\\".encode_utf16()), + 0x09 => out.extend("\\t".encode_utf16()), + 0x08 => out.extend("\\b".encode_utf16()), + 0x0A => out.extend("\\n".encode_utf16()), + 0x0D => out.extend("\\r".encode_utf16()), + 0x0C => out.extend("\\f".encode_utf16()), + c if c < 0x20 || c == 0x2028 || c == 0x2029 => { + out.extend(format!("\\u{c:04x}").encode_utf16()); + } + c => out.push(c), + } + } + out.push(u('"')); +} + +/// `JsonOps.convertTo(NbtOps)`. Nulls give `None` (an end tag in Java). +pub fn to_nbt(json: &Json) -> Option { + Some(match json { + Json::Null => return None, + Json::Bool(b) => Tag::Byte(i8::from(*b)), + Json::Str(s) => Tag::String(JavaString(s.clone())), + Json::Num(n) => number_to_nbt(n), + Json::Arr(items) => Tag::List(items.iter().filter_map(to_nbt).collect()), + Json::Obj(entries) => { + let mut c = Compound::new(); + for (k, v) in entries { + if let Some(v) = to_nbt(v) { + c.insert(JavaString(k.clone()), v); + } + } + Tag::Compound(c) + } + }) +} + +/// `JsonOps.convertTo` for a number: the smallest integer tag that holds an +/// exact integral value, else float when exact, else double. +fn number_to_nbt(literal: &str) -> Tag { + if let Some(l) = exact_long(literal) { + if let Ok(b) = i8::try_from(l) { + return Tag::Byte(b); + } + if let Ok(s) = i16::try_from(l) { + return Tag::Short(s); + } + if let Ok(i) = i32::try_from(l) { + return Tag::Int(i); + } + return Tag::Long(l); + } + let d: f64 = literal.parse().unwrap_or(0.0); + let f = d as f32; + if f64::from(f) == d { + Tag::Float(f.to_bits()) + } else { + Tag::Double(d.to_bits()) + } +} + +/// `BigDecimal.longValueExact` of a JSON number literal. +fn exact_long(literal: &str) -> Option { + let (mantissa, exponent) = match literal.find(['e', 'E']) { + Some(i) => (&literal[..i], literal[i + 1..].parse::().ok()?), + None => (literal, 0), + }; + let negative = mantissa.starts_with('-'); + let mantissa = mantissa.trim_start_matches('-'); + let (int_part, frac) = match mantissa.find('.') { + Some(i) => (&mantissa[..i], &mantissa[i + 1..]), + None => (mantissa, ""), + }; + let mut digits: String = format!("{int_part}{frac}"); + let mut scale = frac.len() as i64 - exponent; + // Trailing zeros of the unscaled value do not change the value. + while scale > 0 && digits.ends_with('0') { + digits.pop(); + scale -= 1; + } + if scale > 0 { + return None; + } + let digits = digits.trim_start_matches('0'); + if digits.is_empty() { + return Some(0); + } + if digits.len() as i64 - scale > 19 { + return None; + } + let mut value: i128 = digits.parse().ok()?; + for _ in 0..-scale { + value = value.checked_mul(10)?; + } + let value = if negative { -value } else { value }; + i64::try_from(value).ok() +} + +/// `NbtOps.convertTo(JsonOps)`. +pub fn from_nbt(tag: &Tag) -> Json { + match tag { + Tag::End => Json::Null, + Tag::Byte(v) => Json::Num(v.to_string()), + Tag::Short(v) => Json::Num(v.to_string()), + Tag::Int(v) => Json::Num(v.to_string()), + Tag::Long(v) => Json::Num(v.to_string()), + Tag::Float(v) => Json::Num(java_float_string(f32::from_bits(*v))), + Tag::Double(v) => Json::Num(java_double_string(f64::from_bits(*v))), + Tag::ByteArray(v) => Json::Arr(v.iter().map(|b| Json::Num((*b as i8).to_string())).collect()), + Tag::IntArray(v) => Json::Arr(v.iter().map(|i| Json::Num(i.to_string())).collect()), + Tag::LongArray(v) => Json::Arr(v.iter().map(|l| Json::Num(l.to_string())).collect()), + Tag::String(s) => Json::Str(s.0.clone()), + Tag::List(items) => Json::Arr(items.iter().map(from_nbt).collect()), + Tag::Compound(c) => Json::Obj(c.iter().map(|(k, v)| (k.0.clone(), from_nbt(v))).collect()), + } +} + +/// Java `Double.toString`. +pub fn java_double_string(d: f64) -> String { + java_fp_string(d, format!("{d:e}")) +} +/// Java `Float.toString`. +pub fn java_float_string(f: f32) -> String { + java_fp_string(f64::from(f), format!("{f:e}")) +} +fn java_fp_string(d: f64, sci: String) -> String { + if d.is_nan() { + return "NaN".into(); + } + if d.is_infinite() { + return if d > 0.0 { "Infinity" } else { "-Infinity" }.into(); + } + if d == 0.0 { + return if d.is_sign_negative() { "-0.0" } else { "0.0" }.into(); + } + // `sci` is Rust's shortest round-trip form: d.ddde. + let (mantissa, exp) = sci.split_once('e').unwrap_or((&sci, "0")); + let exp: i32 = exp.parse().unwrap_or(0); + let negative = mantissa.starts_with('-'); + let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect(); + let sign = if negative { "-" } else { "" }; + let a = d.abs(); + if (1e-3..1e7).contains(&a) { + if exp >= 0 { + let e = exp as usize; + let (int, frac) = if digits.len() > e + 1 { + (digits[..=e].to_string(), digits[e + 1..].to_string()) + } else { + (format!("{digits:0 Vec { + to_stable_string(&Json::Obj(vec![("text".encode_utf16().collect(), Json::Str(text.to_vec()))])) +} +pub fn translatable_component_json(key: &str) -> Vec { + to_stable_string(&Json::object(vec![("translate", Json::string(key))])) +} + +#[cfg(test)] +mod tests { + use super::*; + fn w(s: &str) -> Vec { + s.encode_utf16().collect() + } + #[test] + fn numbers_become_the_smallest_tag() { + let tag = |s: &str| to_nbt(&parse_strict(&w(s)).unwrap()).unwrap(); + assert_eq!(tag("1"), Tag::Byte(1)); + assert_eq!(tag("1.0"), Tag::Byte(1)); + assert_eq!(tag("1e2"), Tag::Byte(100)); + assert_eq!(tag("300"), Tag::Short(300)); + assert_eq!(tag("1.5"), Tag::Float(1.5f32.to_bits())); + assert_eq!(tag("0.1"), Tag::Double(0.1f64.to_bits())); + assert_eq!(tag("3000000000"), Tag::Long(3_000_000_000)); + } + #[test] + fn lenient_reads_what_strict_refuses() { + assert!(parse_strict(&w("hello")).is_none()); + assert_eq!(parse_lenient(&w("hello")), Some(Json::string("hello"))); + assert!(parse_lenient(&w("hello world")).is_none()); + assert_eq!(parse_lenient(&w("{a:'b'}")), Some(Json::object(vec![("a", Json::string("b"))]))); + } + #[test] + fn stable_strings_sort_keys() { + let j = parse_strict(&w(r#"{"text":"a","color":"red"}"#)).unwrap(); + assert_eq!(String::from_utf16(&to_stable_string(&j)).unwrap(), r#"{"color":"red","text":"a"}"#); + } + #[test] + fn java_floating_point_strings() { + assert_eq!(java_double_string(1.0), "1.0"); + assert_eq!(java_double_string(0.001), "0.001"); + assert_eq!(java_double_string(1e7), "1.0E7"); + assert_eq!(java_double_string(123456.789), "123456.789"); + assert_eq!(java_double_string(1e-4), "1.0E-4"); + assert_eq!(java_float_string(0.1), "0.1"); + assert_eq!(java_float_string(1.5e10), "1.5E10"); + } +} diff --git a/crates/horizon-world/src/datafix/mod.rs b/crates/horizon-world/src/datafix/mod.rs new file mode 100644 index 000000000..24a6263aa --- /dev/null +++ b/crates/horizon-world/src/datafix/mod.rs @@ -0,0 +1,233 @@ +//! Vanilla's DataFixers from the 1.20 schemas on (`net.minecraft.util.datafix`): +//! the data of worlds saved by 1.20 to 26.2 brought to the 26.3 format exactly +//! as 26.3 does when it reads them, and the file fixes it runs when it opens +//! such a world (`net.minecraft.util.filefix`). +//! +//! DataFixerUpper reads the data once with the schema of its version (running +//! the schema's read hooks), applies every fix with a key above that version +//! in order, and writes it with the current schema. Each fix finds its values +//! with the schema just below its own key ([`schema`]). Data older than the +//! 1.20 schemas (DataVersion 3463) is not supported: the fixes before them are +//! not ported, see [`OLDEST_SUPPORTED`]. +pub mod dynamic; +mod fixes_a; +mod fixes_b; +mod fixes_c; +pub mod files; +mod items; +pub mod json; +pub mod schema; +mod text; + +use crate::nbt::Tag; +use dynamic::{as_str, string}; +use schema::{Key, Ty, key, walk}; + +/// The DataVersion of the game. +pub const CURRENT: i32 = crate::DATA_VERSION; +/// Data saved before this DataVersion (1.20) needs fixes that are not ported. +pub const OLDEST_SUPPORTED: i32 = 3463; + +/// `Identifier.tryParse` validity, as `NamespacedSchema.ensureNamespaced` uses it. +pub fn identifier(id: &str) -> Option<(String, String)> { + let (ns, path) = match id.split_once(':') { + Some((ns, path)) => (if ns.is_empty() { "minecraft" } else { ns }, path), + None => ("minecraft", id), + }; + let ns_ok = ns.bytes().all(|c| matches!(c, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'-' | b'.')); + let path_ok = path + .bytes() + .all(|c| matches!(c, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'-' | b'.' | b'/')); + (ns_ok && path_ok).then(|| (ns.to_owned(), path.to_owned())) +} + +/// What a fix works on: the root value, its type and the fix's key. +pub struct Cx<'a> { + root: Ty, + data: &'a mut Tag, + k: Key, +} + +impl Cx<'_> { + /// Every value of `ty`, read with the fix's input schema. + pub fn each(&mut self, ty: Ty, mut f: impl FnMut(&mut Tag)) { + walk(self.root, self.data, self.k - 1, ty, &mut f); + } + /// Every value of `ty` read with the fix's output schema (fixes declared + /// on `getOutputSchema().getType(...)`). + pub fn each_out(&mut self, ty: Ty, mut f: impl FnMut(&mut Tag)) { + walk(self.root, self.data, self.k, ty, &mut f); + } + /// Every entity of one type (`NamedEntityFix` and friends). + pub fn entity(&mut self, id: &str, mut f: impl FnMut(&mut Tag)) { + self.each(Ty::Entity, |e| { + if id_is(e, id) { + f(e); + } + }); + } + /// Every block entity of one type. + pub fn block_entity(&mut self, id: &str, mut f: impl FnMut(&mut Tag)) { + self.each(Ty::BlockEntity, |e| { + if id_is(e, id) { + f(e); + } + }); + } + /// Every item stack whose id passes `filter`. + pub fn item(&mut self, filter: impl Fn(&str) -> bool, mut f: impl FnMut(&mut Tag)) { + self.each(Ty::ItemStack, |s| { + if let Some(id) = as_str(s.get("id")) + && filter(&dynamic::namespaced(&id)) + { + f(s); + } + }); + } + /// A name type renamed in place (`fixTypeEverywhere` over a namespaced string). + pub fn rename(&mut self, ty: Ty, rename: impl Fn(&str) -> Option<&'static str>) { + self.each(ty, |v| { + if let Some(name) = as_str(Some(v)) + && let Some(new) = rename(&name) + { + *v = string(new); + } + }); + } +} + +/// The entity or block entity `id` field, namespaced. +pub fn id_is(v: &Tag, id: &str) -> bool { + as_str(v.get("id")).is_some_and(|s| dynamic::namespaced(&s) == id) +} + +/// `createRenamer`: the id namespaced, looked up in a table. +pub fn renamer<'a>(table: &'a [(&'a str, &'static str)]) -> impl Fn(&str) -> Option<&'static str> + 'a { + move |id: &str| { + let id = dynamic::namespaced(id); + table.iter().find(|(from, _)| *from == id).map(|(_, to)| *to) + } +} + +type FixFn = fn(&mut Cx); + +/// Every fix from the 1.20 schemas on, in DataFixers order. +fn fixes() -> Vec<(Key, FixFn)> { + let mut out = Vec::new(); + fixes_a::register(&mut out); + fixes_b::register(&mut out); + fixes_c::register(&mut out); + out +} + +/// `DataFixer.update(type, data, from, to)` for a root value. +pub fn update(root: Ty, data: &mut Tag, from: i32, to: i32) { + if from >= to { + return; + } + let from_key = key(from.max(0) as u32, 0); + let to_version = to as u32; + // The data is read once with the schema of its version (its read hooks)... + read_hooks(root, data, from_key); + // ...then every fix above that key, up to the target version... + for (k, fix) in fixes() { + if k > from_key && k / 10 <= to_version { + fix(&mut Cx { root, data, k }); + } + } + // ...and written with the target schema (its write hooks). + write_hooks(root, data, key(to_version, 9)); +} + +/// The read hooks of the schema the data was saved with. +fn read_hooks(root: Ty, data: &mut Tag, k: Key) { + walk(root, data, k, Ty::ItemStack, &mut |stack| { + if k < key(3818, 5) { + items::add_names(stack); + } else if let Tag::String(_) = stack { + // V3818_5 UNPACK_PLAIN_ID: a bare item id is a stack of one. + *stack = Tag::compound([("id", stack.clone()), ("count", Tag::Int(1))]); + } + }); + walk(root, data, k, Ty::Objective, &mut fixes_a::unpack_objective); +} + +fn write_hooks(root: Ty, data: &mut Tag, k: Key) { + walk(root, data, k, Ty::Objective, &mut fixes_a::repack_objective); +} + +/// `SimpleRegionStorage.upgradeChunkTag` for a region chunk (`ChunkMap`): +/// the dimension and generator are passed to the fixes as `__context`. +pub fn update_chunk(tag: &mut Tag, dimension: &str, generator: Option<&str>) { + let version = dynamic::as_int(tag.get("DataVersion"), -1); + if version >= CURRENT { + return; + } + let mut context = Tag::compound([("dimension", string(dimension))]); + if let Some(g) = generator { + dynamic::set(&mut context, "generator", string(g)); + } + dynamic::set(tag, "__context", context); + update(Ty::Chunk, tag, version, CURRENT); + dynamic::remove(tag, "__context"); + dynamic::set(tag, "DataVersion", Tag::Int(CURRENT)); +} + +/// `SimpleRegionStorage.upgradeChunkTag` for entity chunks (default version -1) +/// and POI chunks (default 1945). +pub fn update_region_tag(root: Ty, tag: &mut Tag) { + let default = if root == Ty::PoiChunk { 1945 } else { -1 }; + let version = dynamic::as_int(tag.get("DataVersion"), default); + if version >= CURRENT { + return; + } + update(root, tag, version, CURRENT); + dynamic::set(tag, "DataVersion", Tag::Int(CURRENT)); +} + +/// `PlayerDataStorage.load`: the DataVersion field stays as loaded. +pub fn update_player(tag: &mut Tag) { + let version = dynamic::as_int(tag.get("DataVersion"), -1); + update(Ty::Player, tag, version, CURRENT); +} + +/// The saved data type of a file below a `data` folder (`namespace/path.dat`). +pub fn saved_data_type(relative: &str) -> Option { + let name = relative.strip_suffix(".dat").unwrap_or(relative); + if name.ends_with("/command_storage") { + return Some(Ty::SavedCommandStorage); + } + if let Some(n) = name.strip_prefix("minecraft/maps/") + && !n.is_empty() + && n.bytes().all(|b| b.is_ascii_digit()) + { + return Some(Ty::SavedMapData); + } + Some(match name { + "minecraft/custom_boss_events" => Ty::SavedCustomBossEvents, + "minecraft/scoreboard" => Ty::SavedScoreboard, + "minecraft/chunk_tickets" => Ty::SavedTickets, + "minecraft/world_border" => Ty::SavedWorldBorder, + "minecraft/game_rules" => Ty::SavedGameRules, + "minecraft/ender_dragon_fight" => Ty::SavedEnderDragonFight, + "minecraft/wandering_trader" => Ty::SavedWanderingTrader, + "minecraft/maps/last_id" => Ty::SavedMapIndex, + "minecraft/weather" => Ty::SavedWeather, + "minecraft/scheduled_events" => Ty::SavedScheduledEvents, + "minecraft/world_gen_settings" => Ty::SavedWorldGenSettings, + "minecraft/random_sequences" => Ty::SavedRandomSequences, + "minecraft/stopwatches" => Ty::SavedStopwatches, + "minecraft/raids" => Ty::SavedRaids, + "minecraft/world_clocks" => Ty::SavedWorldClocks, + _ => return None, + }) +} + +/// `SavedDataStorage.readTagFromDisk`: default version 1343, field kept. +pub fn update_saved_data(ty: Ty, tag: &mut Tag) { + let version = dynamic::as_int(tag.get("DataVersion"), 1343); + update(ty, tag, version, CURRENT); +} + +#[cfg(test)] +mod tests; diff --git a/crates/horizon-world/src/datafix/schema.rs b/crates/horizon-world/src/datafix/schema.rs new file mode 100644 index 000000000..33d0963db --- /dev/null +++ b/crates/horizon-world/src/datafix/schema.rs @@ -0,0 +1,787 @@ +//! Where DataFixerUpper finds each type inside the data, schema by schema +//! (`net.minecraft.util.datafix.schemas`), from the 1.20 schemas on. +//! +//! `walk` visits every value of the target type under a root of a given type, +//! children before parents (DataFixerUpper rewrites recursive types bottom-up). +//! Keys are `version * 10 + sub-version` as in `DataFixUtils.makeKey`, and a +//! fix with key K reads its input with the schema just below K. +use super::dynamic::{as_str, namespaced}; +use crate::nbt::Tag; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Ty { + Level, + /// `LIGHTWEIGHT_LEVEL`: the level data apart from its typed fields (the same value). + LightweightLevel, + Player, + Chunk, + EntityChunk, + PoiChunk, + BlockEntity, + EntityTree, + Entity, + ItemStack, + DataComponents, + TextComponent, + VillagerTrade, + Particle, + BlockState, + FlatBlockState, + BlockName, + ItemName, + EntityName, + Biome, + Recipe, + StructureFeature, + GameEventName, + Advancements, + Stats, + Hotbar, + Objective, + Team, + WorldGenSettings, + SavedCustomBossEvents, + SavedMapData, + SavedScoreboard, + SavedWorldGenSettings, + SavedStructureFeatureIndices, + /// Saved data that has no typed fields (`DSL.remainder`). + SavedRemainder, + /// Each saved data kind still needs its own identity for fixes that target it. + SavedEnderDragonFight, + SavedGameRules, + SavedTickets, + SavedRaids, + SavedRandomSequences, + SavedWorldBorder, + SavedWeather, + SavedWorldClocks, + SavedWanderingTrader, + SavedScheduledEvents, + SavedMapIndex, + SavedCommandStorage, + SavedStopwatches, + Options, + DebugProfile, +} + +/// A schema key: `version * 10 + sub`. +pub type Key = u32; +pub const fn key(version: u32, sub: u32) -> Key { + version * 10 + sub +} + +pub struct Walker<'f> { + /// The schema the data is read with. + pub k: Key, + pub target: Ty, + pub f: &'f mut dyn FnMut(&mut Tag), +} + +impl Walker<'_> { + pub fn walk(&mut self, ty: Ty, v: &mut Tag) { + self.children(ty, v); + if ty == self.target { + (self.f)(v); + } + } + fn field(&mut self, ty: Ty, v: &mut Tag, name: &str) { + if let Some(child) = v.get_mut(name) { + self.walk(ty, child); + } + } + fn list(&mut self, ty: Ty, v: &mut Tag, name: &str) { + if let Some(Tag::List(items)) = v.get_mut(name) { + for item in items { + self.walk(ty, item); + } + } + } + fn list_in(&mut self, ty: Ty, items: &mut Tag) { + if let Tag::List(items) = items { + for item in items { + self.walk(ty, item); + } + } + } + /// `DSL.compoundList(type)`: every value of a map. + fn values(&mut self, ty: Ty, v: &mut Tag, name: &str) { + if let Some(Tag::Compound(c)) = v.get_mut(name) { + for value in c.values_mut() { + self.walk(ty, value); + } + } + } + /// A list of compounds, each with a typed field. + fn list_of_field(&mut self, ty: Ty, v: &mut Tag, list: &str, field: &str) { + if let Some(Tag::List(items)) = v.get_mut(list) { + for item in items { + self.field(ty, item, field); + } + } + } + /// `DSL.compoundList(keyType, valueType)` with typed string keys: keys are + /// renamed in place by the callback (as a string tag). + fn keys(&mut self, ty: Ty, v: &mut Tag, name: &str) { + if ty != self.target { + return; + } + if let Some(Tag::Compound(c)) = v.get_mut(name) { + let entries: Vec<_> = std::mem::take(c).into_iter().collect(); + for (k, value) in entries { + let mut key_tag = Tag::String(k.clone()); + (self.f)(&mut key_tag); + let k = match key_tag { + Tag::String(s) => s, + _ => k, + }; + c.insert(k, value); + } + } + } + + fn children(&mut self, ty: Ty, v: &mut Tag) { + let k = self.k; + match ty { + Ty::Level => { + if k < key(4771, 0) { + if let Some(Tag::Compound(c)) = v.get_mut("CustomBossEvents") { + for e in c.values_mut() { + self.field(Ty::TextComponent, e, "Name"); + } + } + } + self.walk(Ty::LightweightLevel, v); + } + Ty::Player => { + if k >= key(4312, 0) { + self.equipment(v); + } + if let Some(rv) = v.get_mut("RootVehicle") { + self.field(Ty::EntityTree, rv, "Entity"); + } + self.list(Ty::EntityTree, v, "ender_pearls"); + self.list(Ty::ItemStack, v, "Inventory"); + self.list(Ty::ItemStack, v, "EnderItems"); + self.field(Ty::EntityTree, v, "ShoulderEntityLeft"); + self.field(Ty::EntityTree, v, "ShoulderEntityRight"); + if let Some(book) = v.get_mut("recipeBook") { + self.list(Ty::Recipe, book, "recipes"); + self.list(Ty::Recipe, book, "toBeDisplayed"); + } + } + Ty::Chunk => { + self.list(Ty::EntityTree, v, "entities"); + if let Some(Tag::List(items)) = v.get_mut("block_entities") { + for be in items { + // DSL.or(BLOCK_ENTITY, remainder): unknown ids stay as they are. + if block_entity_known(be, k) { + self.walk(Ty::BlockEntity, be); + } + } + } + self.list_of_field(Ty::BlockName, v, "block_ticks", "i"); + if let Some(Tag::List(sections)) = v.get_mut("sections") { + for s in sections { + if let Some(b) = s.get_mut("biomes") { + self.list(Ty::Biome, b, "palette"); + } + if let Some(b) = s.get_mut("block_states") { + self.list(Ty::BlockState, b, "palette"); + } + } + } + if let Some(s) = v.get_mut("structures") { + self.values(Ty::StructureFeature, s, "starts"); + } + } + Ty::EntityChunk => self.list(Ty::EntityTree, v, "Entities"), + Ty::EntityTree => { + self.list(Ty::EntityTree, v, "Passengers"); + self.walk(Ty::Entity, v); + } + Ty::Entity => self.entity(v), + Ty::BlockEntity => self.block_entity(v), + Ty::ItemStack => self.item_stack(v), + Ty::DataComponents => self.components(v), + Ty::TextComponent => self.text(v), + Ty::VillagerTrade => { + self.field(Ty::ItemStack, v, "buy"); + self.field(Ty::ItemStack, v, "buyB"); + self.field(Ty::ItemStack, v, "sell"); + } + Ty::Particle => { + if k >= key(3818, 4) { + self.field(Ty::ItemStack, v, "item"); + self.field(Ty::BlockState, v, "block_state"); + } + } + Ty::Hotbar => { + if let Tag::Compound(c) = v { + for row in c.values_mut() { + self.list_in(Ty::ItemStack, row); + } + } + } + Ty::Stats => { + if let Some(stats) = v.get_mut("stats") { + self.keys(Ty::BlockName, stats, "minecraft:mined"); + for s in ["crafted", "used", "broken", "picked_up", "dropped"] { + self.keys(Ty::ItemName, stats, &format!("minecraft:{s}")); + } + self.keys(Ty::EntityName, stats, "minecraft:killed"); + self.keys(Ty::EntityName, stats, "minecraft:killed_by"); + } + } + Ty::Advancements => { + for (adv, kind) in [ + ("minecraft:adventure/adventuring_time", Ty::Biome), + ("minecraft:adventure/kill_a_mob", Ty::EntityName), + ("minecraft:adventure/kill_all_mobs", Ty::EntityName), + ("minecraft:husbandry/bred_all_animals", Ty::EntityName), + ] { + if let Some(a) = v.get_mut(adv) { + self.keys(kind, a, "criteria"); + } + } + } + Ty::Objective => { + // The hook unpacked CriteriaName into CriteriaType {type, id}. + if let Some(ct) = v.get_mut("CriteriaType") { + let kind = match as_str(ct.get("type")).as_deref() { + Some("minecraft:mined") => Some(Ty::BlockName), + Some( + "minecraft:crafted" | "minecraft:used" | "minecraft:broken" | "minecraft:picked_up" + | "minecraft:dropped", + ) => Some(Ty::ItemName), + Some("minecraft:killed" | "minecraft:killed_by") => Some(Ty::EntityName), + _ => None, + }; + if let Some(kind) = kind { + self.field(kind, ct, "id"); + } + } + self.field(Ty::TextComponent, v, "DisplayName"); + } + Ty::Team => { + for f in ["MemberNamePrefix", "MemberNameSuffix", "DisplayName"] { + self.field(Ty::TextComponent, v, f); + } + } + Ty::SavedCustomBossEvents => { + if let Some(Tag::Compound(c)) = v.get_mut("data") { + for e in c.values_mut() { + self.field(Ty::TextComponent, e, "Name"); + } + } + } + Ty::SavedMapData => { + if let Some(d) = v.get_mut("data") { + let name = if k >= key(3813, 0) { "name" } else { "Name" }; + self.list_of_field(Ty::TextComponent, d, "banners", name); + } + } + Ty::SavedScoreboard => { + if let Some(d) = v.get_mut("data") { + self.list(Ty::Objective, d, "Objectives"); + self.list(Ty::Team, d, "Teams"); + self.list_of_field(Ty::TextComponent, d, "PlayerScores", "display"); + } + } + Ty::SavedWorldGenSettings => self.field(Ty::WorldGenSettings, v, "data"), + Ty::SavedStructureFeatureIndices => { + if let Some(d) = v.get_mut("data") { + self.values(Ty::StructureFeature, d, "Features"); + } + } + Ty::WorldGenSettings => self.world_gen(v), + _ => {} + } + } + + fn equipment(&mut self, v: &mut Tag) { + if self.k >= key(4301, 0) { + if let Some(eq) = v.get_mut("equipment") { + for slot in ["mainhand", "offhand", "feet", "legs", "chest", "head", "body", "saddle"] { + self.field(Ty::ItemStack, eq, slot); + } + } + } else { + self.list(Ty::ItemStack, v, "ArmorItems"); + self.list(Ty::ItemStack, v, "HandItems"); + self.field(Ty::ItemStack, v, "body_armor_item"); + self.field(Ty::ItemStack, v, "saddle"); + } + } + + fn entity(&mut self, v: &mut Tag) { + let k = self.k; + self.equipment(v); + self.field(Ty::TextComponent, v, "CustomName"); + let id = namespaced(&as_str(v.get("id")).unwrap_or_default()); + let id = id.as_str(); + let listener = |w: &mut Self, v: &mut Tag| { + if let Some(l) = v.get_mut("listener") + && let Some(e) = l.get_mut("event") + { + w.field(Ty::GameEventName, e, "game_event"); + } + }; + match id { + "minecraft:area_effect_cloud" => { + let f = if k >= key(4420, 0) { "custom_particle" } else { "Particle" }; + self.field(Ty::Particle, v, f); + } + "minecraft:arrow" | "minecraft:spectral_arrow" => { + self.field(Ty::BlockState, v, "inBlockState"); + if k >= key(3685, 0) { + self.field(Ty::ItemStack, v, "item"); + } + if k >= key(3938, 0) { + self.field(Ty::ItemStack, v, "weapon"); + } + } + "minecraft:trident" => { + self.field(Ty::BlockState, v, "inBlockState"); + let f = if k >= key(3685, 0) { "item" } else { "Trident" }; + self.field(Ty::ItemStack, v, f); + } + "minecraft:chest_minecart" | "minecraft:hopper_minecart" => { + self.field(Ty::BlockState, v, "DisplayState"); + self.list(Ty::ItemStack, v, "Items"); + } + "minecraft:minecart" | "minecraft:furnace_minecart" | "minecraft:tnt_minecart" => { + self.field(Ty::BlockState, v, "DisplayState"); + } + "minecraft:command_block_minecart" => { + self.field(Ty::BlockState, v, "DisplayState"); + self.field(Ty::TextComponent, v, "LastOutput"); + } + "minecraft:spawner_minecart" => { + self.field(Ty::BlockState, v, "DisplayState"); + self.spawner(v); + } + "minecraft:donkey" | "minecraft:mule" => { + self.list(Ty::ItemStack, v, "Items"); + if k < key(4300, 0) { + self.field(Ty::ItemStack, v, "SaddleItem"); + } + } + "minecraft:llama" | "minecraft:trader_llama" => { + self.list(Ty::ItemStack, v, "Items"); + if k < key(4300, 0) { + self.field(Ty::ItemStack, v, "SaddleItem"); + } + let split = if id == "minecraft:llama" { key(3808, 1) } else { key(3808, 2) }; + if k < split { + self.field(Ty::ItemStack, v, "DecorItem"); + } + } + "minecraft:horse" => { + if k < key(4300, 0) { + self.field(Ty::ItemStack, v, "SaddleItem"); + } + if k < key(3808, 0) { + self.field(Ty::ItemStack, v, "ArmorItem"); + } + } + "minecraft:skeleton_horse" | "minecraft:zombie_horse" => { + if k < key(4300, 0) { + self.field(Ty::ItemStack, v, "SaddleItem"); + } + } + "minecraft:enderman" => self.field(Ty::BlockState, v, "carriedBlockState"), + "minecraft:falling_block" => { + self.field(Ty::BlockState, v, "BlockState"); + self.field(Ty::BlockEntity, v, "TileEntityData"); + } + "minecraft:firework_rocket" => self.field(Ty::ItemStack, v, "FireworksItem"), + "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:splash_potion" | "minecraft:lingering_potion" if k >= key(4306, 0) => { + self.field(Ty::ItemStack, v, "Item"); + } + "minecraft:villager" | "minecraft:wandering_trader" => { + self.list(Ty::ItemStack, v, "Inventory"); + if let Some(o) = v.get_mut("Offers") { + self.list(Ty::VillagerTrade, o, "Recipes"); + } + } + "minecraft:zombie_villager" => { + if let Some(o) = v.get_mut("Offers") { + self.list(Ty::VillagerTrade, o, "Recipes"); + } + } + "minecraft:pillager" | "minecraft:piglin" => self.list(Ty::ItemStack, v, "Inventory"), + "minecraft:allay" => { + self.list(Ty::ItemStack, v, "Inventory"); + listener(self, v); + } + "minecraft:warden" => listener(self, v), + "minecraft:tnt" if k >= key(3683, 0) => self.field(Ty::BlockState, v, "block_state"), + "minecraft:chest_boat" if k < key(4067, 0) => self.list(Ty::ItemStack, v, "Items"), + "minecraft:item_display" => self.field(Ty::ItemStack, v, "item"), + "minecraft:block_display" => self.field(Ty::BlockState, v, "block_state"), + "minecraft:text_display" => self.field(Ty::TextComponent, v, "text"), + "minecraft:ominous_item_spawner" if k >= key(3825, 0) => self.field(Ty::ItemStack, v, "item"), + _ if k >= key(4067, 0) && (id.ends_with("_chest_boat") || id == "minecraft:bamboo_chest_raft") => { + self.list(Ty::ItemStack, v, "Items"); + } + _ => {} + } + } + + /// `UNTAGGED_SPAWNER` (V2831). + fn spawner(&mut self, v: &mut Tag) { + if let Some(Tag::List(items)) = v.get_mut("SpawnPotentials") { + for p in items { + if let Some(d) = p.get_mut("data") { + self.field(Ty::EntityTree, d, "entity"); + } + } + } + if let Some(d) = v.get_mut("SpawnData") { + self.field(Ty::EntityTree, d, "entity"); + } + } + + fn block_entity(&mut self, v: &mut Tag) { + let k = self.k; + self.field(Ty::DataComponents, v, "components"); + let id = namespaced(&as_str(v.get("id")).unwrap_or_default()); + let named_inventory = |w: &mut Self, v: &mut Tag| { + w.list(Ty::ItemStack, v, "Items"); + w.field(Ty::TextComponent, v, "CustomName"); + }; + match id.as_str() { + "minecraft:furnace" | "minecraft:smoker" | "minecraft:blast_furnace" => { + named_inventory(self, v); + self.keys(Ty::Recipe, v, "RecipesUsed"); + } + "minecraft:chest" | "minecraft:trapped_chest" | "minecraft:dispenser" | "minecraft:dropper" + | "minecraft:hopper" | "minecraft:shulker_box" | "minecraft:brewing_stand" | "minecraft:barrel" => { + named_inventory(self, v); + } + "minecraft:crafter" if k >= key(3682, 0) => named_inventory(self, v), + "minecraft:jukebox" => self.field(Ty::ItemStack, v, "RecordItem"), + "minecraft:sign" | "minecraft:hanging_sign" => { + for side in ["front_text", "back_text"] { + if let Some(s) = v.get_mut(side) { + self.list(Ty::TextComponent, s, "messages"); + self.list(Ty::TextComponent, s, "filtered_messages"); + } + } + } + "minecraft:mob_spawner" => self.spawner(v), + "minecraft:piston" => self.field(Ty::BlockState, v, "blockState"), + "minecraft:enchanting_table" | "minecraft:beacon" | "minecraft:banner" => { + self.field(Ty::TextComponent, v, "CustomName"); + } + "minecraft:skull" => self.field(Ty::TextComponent, v, "custom_name"), + "minecraft:command_block" => { + self.field(Ty::TextComponent, v, "CustomName"); + self.field(Ty::TextComponent, v, "LastOutput"); + } + "minecraft:jigsaw" => self.field(Ty::FlatBlockState, v, "final_state"), + "minecraft:campfire" | "minecraft:chiseled_bookshelf" => self.list(Ty::ItemStack, v, "Items"), + "minecraft:shelf" if k >= key(4533, 0) => self.list(Ty::ItemStack, v, "Items"), + "minecraft:lectern" => self.field(Ty::ItemStack, v, "Book"), + "minecraft:beehive" => { + if k >= key(3818, 0) { + self.list_of_field(Ty::EntityTree, v, "bees", "entity_data"); + } else { + self.list_of_field(Ty::EntityTree, v, "Bees", "EntityData"); + } + } + "minecraft:sculk_sensor" | "minecraft:sculk_shrieker" => { + if let Some(l) = v.get_mut("listener") + && let Some(e) = l.get_mut("event") + { + self.field(Ty::GameEventName, e, "game_event"); + } + } + "minecraft:decorated_pot" => { + if k >= key(4996, 1) { + if let Some(s) = v.get_mut("sherds") { + for side in ["back", "left", "right", "front"] { + self.field(Ty::ItemStack, s, side); + } + } + } else { + self.list(Ty::ItemName, v, "sherds"); + } + self.field(Ty::ItemStack, v, "item"); + } + "minecraft:brushable_block" => self.field(Ty::ItemStack, v, "item"), + "minecraft:trial_spawner" if k >= key(3689, 0) => { + if let Some(Tag::List(items)) = v.get_mut("spawn_potentials") { + for p in items { + if let Some(d) = p.get_mut("data") { + self.field(Ty::EntityTree, d, "entity"); + } + } + } + if let Some(d) = v.get_mut("spawn_data") { + self.field(Ty::EntityTree, d, "entity"); + } + } + "minecraft:vault" if k >= key(3807, 0) => { + if let Some(c) = v.get_mut("config") { + self.field(Ty::ItemStack, c, "key_item"); + } + if let Some(c) = v.get_mut("server_data") { + self.list(Ty::ItemStack, c, "items_to_eject"); + } + if let Some(c) = v.get_mut("shared_data") { + self.field(Ty::ItemStack, c, "display_item"); + } + } + "minecraft:test_instance_block" if k >= key(4302, 0) => { + if let Some(d) = v.get_mut("data") { + self.field(Ty::TextComponent, d, "error_message"); + } + self.list_of_field(Ty::TextComponent, v, "errors", "text"); + } + _ => {} + } + } + + fn item_stack(&mut self, v: &mut Tag) { + if self.k >= key(3818, 5) { + self.field(Ty::ItemName, v, "id"); + self.field(Ty::DataComponents, v, "components"); + return; + } + self.field(Ty::ItemName, v, "id"); + let Some(tag) = v.get_mut("tag") else { + return; + }; + self.field(Ty::EntityTree, tag, "EntityTag"); + self.field(Ty::BlockEntity, tag, "BlockEntityTag"); + self.list(Ty::BlockName, tag, "CanDestroy"); + self.list(Ty::BlockName, tag, "CanPlaceOn"); + self.list(Ty::ItemStack, tag, "Items"); + self.list(Ty::ItemStack, tag, "ChargedProjectiles"); + self.list(Ty::TextComponent, tag, "pages"); + self.values(Ty::TextComponent, tag, "filtered_pages"); + if let Some(d) = tag.get_mut("display") { + self.field(Ty::TextComponent, d, "Name"); + self.list(Ty::TextComponent, d, "Lore"); + } + } + + fn components(&mut self, v: &mut Tag) { + let k = self.k; + if k < key(3818, 3) { + return; + } + let Tag::Compound(_) = v else { + return; + }; + self.list_of_field(Ty::EntityTree, v, "minecraft:bees", "entity_data"); + self.field(Ty::BlockEntity, v, "minecraft:block_entity_data"); + self.list(Ty::ItemStack, v, "minecraft:bundle_contents"); + for c in ["minecraft:can_break", "minecraft:can_place_on"] { + let Some(p) = v.get_mut(c) else { + continue; + }; + if k >= key(4307, 0) { + // A predicate or a list of them. + if let Tag::List(items) = p { + for item in items { + self.blocks_predicate(item); + } + } else { + self.blocks_predicate(p); + } + } else if let Some(Tag::List(items)) = p.get_mut("predicates") { + for item in items { + self.blocks_predicate(item); + } + } + } + self.list(Ty::ItemStack, v, "minecraft:charged_projectiles"); + self.list_of_field(Ty::ItemStack, v, "minecraft:container", "item"); + self.field(Ty::EntityTree, v, "minecraft:entity_data"); + if k >= key(4996, 0) { + if let Some(p) = v.get_mut("minecraft:pot_decorations") { + for side in ["back", "left", "right", "front"] { + self.field(Ty::ItemStack, p, side); + } + } + } else { + self.list(Ty::ItemName, v, "minecraft:pot_decorations"); + } + if k < key(4059, 0) { + if let Some(f) = v.get_mut("minecraft:food") { + self.field(Ty::ItemStack, f, "using_converts_to"); + } + } else { + self.field(Ty::ItemStack, v, "minecraft:use_remainder"); + if let Some(e) = v.get_mut("minecraft:equippable") + && let Some(a) = e.get_mut("allowed_entities") + { + if let Tag::List(_) = a { + self.list_in(Ty::EntityName, a); + } else { + self.walk(Ty::EntityName, a); + } + } + self.field(Ty::ItemStack, v, "minecraft:sulfur_cube_content"); + } + self.field(Ty::TextComponent, v, "minecraft:custom_name"); + self.field(Ty::TextComponent, v, "minecraft:item_name"); + self.list(Ty::TextComponent, v, "minecraft:lore"); + if let Some(b) = v.get_mut("minecraft:written_book_content") + && let Some(Tag::List(pages)) = b.get_mut("pages") + { + for page in pages { + // DSL.or({raw, filtered}, TEXT_COMPONENT): any compound is the filterable form. + if let Tag::Compound(_) = page { + self.field(Ty::TextComponent, page, "raw"); + self.field(Ty::TextComponent, page, "filtered"); + } else { + self.walk(Ty::TextComponent, page); + } + } + } + } + + fn blocks_predicate(&mut self, p: &mut Tag) { + if let Some(b) = p.get_mut("blocks") { + if let Tag::List(_) = b { + self.list_in(Ty::BlockName, b); + } else { + self.walk(Ty::BlockName, b); + } + } + } + + fn text(&mut self, v: &mut Tag) { + let k = self.k; + if k < key(4290, 0) { + return; + } + match v { + Tag::List(_) => self.list_in(Ty::TextComponent, v), + Tag::Compound(_) => { + self.list(Ty::TextComponent, v, "extra"); + self.field(Ty::TextComponent, v, "separator"); + if k >= key(4292, 0) { + if let Some(h) = v.get_mut("hover_event") { + match as_str(h.get("action")).as_deref() { + Some("show_text") => self.field(Ty::TextComponent, h, "value"), + Some("show_item") => self.walk(Ty::ItemStack, h), + Some("show_entity") => { + self.field(Ty::EntityName, h, "id"); + self.field(Ty::TextComponent, h, "name"); + } + _ => {} + } + } + } else if let Some(h) = v.get_mut("hoverEvent") { + match as_str(h.get("action")).as_deref() { + Some("show_text") => self.field(Ty::TextComponent, h, "contents"), + Some("show_item") => { + if let Some(c) = h.get_mut("contents") { + // DSL.or(ITEM_STACK, ITEM_NAME) + if let Tag::Compound(_) = c { + self.walk(Ty::ItemStack, c); + } else { + self.walk(Ty::ItemName, c); + } + } + } + Some("show_entity") => { + if let Some(c) = h.get_mut("contents") { + self.field(Ty::EntityName, c, "type"); + self.field(Ty::TextComponent, c, "name"); + } + } + _ => {} + } + } + } + _ => {} + } + } + + /// `WORLD_GEN_SETTINGS` (V2832). + fn world_gen(&mut self, v: &mut Tag) { + let Some(Tag::Compound(dims)) = v.get_mut("dimensions") else { + return; + }; + for dim in dims.values_mut() { + let Some(g) = dim.get_mut("generator") else { + continue; + }; + match as_str(g.get("type")).as_deref() { + Some("minecraft:flat") => { + if let Some(s) = g.get_mut("settings") { + self.field(Ty::Biome, s, "biome"); + self.list_of_field(Ty::BlockName, s, "layers", "block"); + } + } + Some("minecraft:noise") => { + if let Some(b) = g.get_mut("biome_source") { + match as_str(b.get("type")).as_deref() { + Some("minecraft:fixed") => self.field(Ty::Biome, b, "biome"), + Some("minecraft:checkerboard") => self.list(Ty::Biome, b, "biomes"), + _ => {} + } + } + if let Some(s) = g.get_mut("settings") + && let Tag::Compound(_) = s + { + self.field(Ty::BlockName, s, "default_block"); + self.field(Ty::BlockName, s, "default_fluid"); + } + } + _ => {} + } + } + } +} + +/// Block entity ids the schema at `k` knows (others are read as remainder). +pub fn block_entity_known(be: &Tag, k: Key) -> bool { + let id = namespaced(&as_str(be.get("id")).unwrap_or_default()); + let since = match id.as_str() { + "minecraft:crafter" => key(3682, 0), + "minecraft:trial_spawner" => key(3689, 0), + "minecraft:vault" => key(3807, 0), + "minecraft:creaking_heart" => key(4071, 0), + "minecraft:test_block" | "minecraft:test_instance_block" => key(4302, 0), + "minecraft:copper_golem_statue" => key(4532, 0), + "minecraft:shelf" => key(4533, 0), + "minecraft:potent_sulfur" => key(4881, 0), + "minecraft:bed" => return k < key(4885, 0), + "minecraft:suspicious_sand" => return k < key(3438, 0), + "minecraft:brushable_block" | "minecraft:calibrated_sculk_sensor" => key(3438, 0), + _ => 0, + }; + k >= since && BLOCK_ENTITIES.contains(&id.as_str()) +} + +/// Block entity types of the schemas from 1.20 on. +const BLOCK_ENTITIES: &[&str] = &[ + "minecraft:furnace", "minecraft:chest", "minecraft:trapped_chest", "minecraft:ender_chest", + "minecraft:jukebox", "minecraft:dispenser", "minecraft:dropper", "minecraft:sign", "minecraft:mob_spawner", + "minecraft:piston", "minecraft:brewing_stand", "minecraft:enchanting_table", "minecraft:end_portal", + "minecraft:beacon", "minecraft:skull", "minecraft:daylight_detector", "minecraft:hopper", "minecraft:comparator", + "minecraft:banner", "minecraft:structure_block", "minecraft:end_gateway", "minecraft:command_block", + "minecraft:shulker_box", "minecraft:bed", "minecraft:conduit", "minecraft:barrel", "minecraft:smoker", + "minecraft:blast_furnace", "minecraft:lectern", "minecraft:bell", "minecraft:jigsaw", "minecraft:campfire", + "minecraft:beehive", "minecraft:sculk_sensor", "minecraft:sculk_catalyst", "minecraft:sculk_shrieker", + "minecraft:hanging_sign", "minecraft:chiseled_bookshelf", "minecraft:decorated_pot", "minecraft:suspicious_sand", + "minecraft:brushable_block", "minecraft:calibrated_sculk_sensor", "minecraft:crafter", + "minecraft:trial_spawner", "minecraft:vault", "minecraft:creaking_heart", "minecraft:test_block", + "minecraft:test_instance_block", "minecraft:copper_golem_statue", "minecraft:shelf", "minecraft:potent_sulfur", +]; + +/// Visit every value of `target` under a `root`-typed value read with schema `k`. +pub fn walk(root: Ty, v: &mut Tag, k: Key, target: Ty, f: &mut dyn FnMut(&mut Tag)) { + Walker { k, target, f }.walk(root, v); +} diff --git a/crates/horizon-world/src/datafix/tests.rs b/crates/horizon-world/src/datafix/tests.rs new file mode 100644 index 000000000..1a8c67ec8 --- /dev/null +++ b/crates/horizon-world/src/datafix/tests.rs @@ -0,0 +1,63 @@ +use super::*; +use crate::nbt::parse_snbt; + +fn snbt(s: &str) -> Tag { + parse_snbt(s).unwrap().0 +} + +#[test] +fn current_data_is_left_alone() { + let mut tag = snbt("{DataVersion: 5023, Status: \"minecraft:full\"}"); + let before = tag.clone(); + update_chunk(&mut tag, "minecraft:overworld", Some("minecraft:noise")); + assert_eq!(tag, before); +} + +#[test] +fn item_stacks_of_1_20_take_components() { + let mut player = snbt( + "{DataVersion: 3465, Inventory: [{Slot: 0b, id: \"minecraft:diamond_sword\", Count: 1b, \ + tag: {Damage: 5, display: {Name: '{\"text\":\"Blade\"}'}}}]}", + ); + update_player(&mut player); + let stack = &dynamic::stream(player.get("Inventory"))[0]; + assert_eq!(dynamic::as_str(stack.get("id")).as_deref(), Some("minecraft:diamond_sword")); + assert_eq!(stack.get("count"), Some(&Tag::Int(1))); + let components = stack.get("components").expect("components"); + assert_eq!(components.get("minecraft:damage"), Some(&Tag::Int(5))); + assert_eq!( + components.get("minecraft:custom_name"), + Some(&Tag::compound([("text", dynamic::string("Blade"))])) + ); +} + +#[test] +fn level_data_of_1_20_moves_to_the_new_fields() { + let mut level = snbt( + "{DataVersion: 3465, Difficulty: 3b, DifficultyLocked: 1b, hardcore: 0b, SpawnX: 1, SpawnY: 64, SpawnZ: -3, \ + SpawnAngle: 90.0f, GameRules: {doFireTick: \"false\", keepInventory: \"true\", randomTickSpeed: \"5\"}}", + ); + update(Ty::Level, &mut level, 3465, CURRENT); + let settings = level.get("difficulty_settings").expect("difficulty settings"); + assert_eq!(dynamic::as_str(settings.get("difficulty")).as_deref(), Some("hard")); + assert_eq!(settings.get("locked"), Some(&Tag::Byte(1))); + let rules = level.get("game_rules").expect("game rules"); + assert_eq!(rules.get("minecraft:keep_inventory"), Some(&Tag::Byte(1))); + assert_eq!(rules.get("minecraft:random_tick_speed"), Some(&Tag::Int(5))); + assert_eq!(rules.get("minecraft:fire_spread_radius_around_player"), Some(&Tag::Int(0))); + assert_eq!(level.get("spawn").and_then(|s| s.get("pos")), Some(&Tag::IntArray(vec![1, 64, -3]))); +} + +#[test] +fn block_states_take_their_new_field_names() { + let mut chunk = snbt( + "{DataVersion: 4903, Status: \"minecraft:full\", sections: [{Y: 0b, block_states: \ + {palette: [{Name: \"minecraft:chain\", Properties: {axis: \"y\"}}]}}]}", + ); + update_chunk(&mut chunk, "minecraft:the_nether", Some("minecraft:noise")); + let section = &dynamic::stream(chunk.get("sections"))[0]; + let state = &dynamic::stream(section.get("block_states").and_then(|b| b.get("palette")))[0]; + assert_eq!(dynamic::as_str(state.get("id")).as_deref(), Some("minecraft:chain")); + assert!(state.get("properties").is_some()); + assert_eq!(chunk.get("DataVersion"), Some(&Tag::Int(CURRENT))); +} diff --git a/crates/horizon-world/src/datafix/text.rs b/crates/horizon-world/src/datafix/text.rs new file mode 100644 index 000000000..60bf3aff3 --- /dev/null +++ b/crates/horizon-world/src/datafix/text.rs @@ -0,0 +1,225 @@ +//! The text component fixes of 1.21.5 (keys 4290 to 4292): JSON strings become +//! NBT components, legacy hover events are replaced, stringified style flags +//! become booleans and hover/click events take their new field names. +use super::dynamic::{as_number, as_str, as_string, boolean, empty_map, remove, rename, rename_and_fix, set, string}; +use super::json; +use super::schema::{Key, Ty, key}; +use super::FixFn; +use crate::nbt::Tag; + +pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + out.push((key(4290, 0), |cx| cx.each(Ty::TextComponent, unflatten))); + out.push((key(4291, 0), |cx| cx.each(Ty::TextComponent, legacy_hover_event))); + out.push((key(4291, 0), |cx| cx.each(Ty::TextComponent, stringified_flags))); + out.push((key(4292, 0), |cx| cx.each(Ty::TextComponent, hover_and_click_event))); +} + +/// `UnflattenTextComponentFix`: the JSON of a string component as NBT. +fn unflatten(v: &mut Tag) { + let Tag::String(s) = v else { + return; + }; + if let Some(parsed) = json::parse_lenient(&s.0) + && let Some(tag) = json::to_nbt(&parsed) + { + *v = tag; + } +} + +fn is_full(v: &Tag) -> bool { + matches!(v, Tag::Compound(_)) +} + +/// The hover event a schema of 4290/4291 reads as typed (a known action). +fn typed_hover(v: &Tag) -> Option { + let h = v.get("hoverEvent")?; + let action = as_str(h.get("action"))?; + matches!(action.as_str(), "show_text" | "show_item" | "show_entity").then_some(action) +} + +/// `LegacyHoverEventFix`. +fn legacy_hover_event(v: &mut Tag) { + if !is_full(v) { + return; + } + let Some(hover) = v.get("hoverEvent").cloned() else { + return; + }; + if hover.get("value").is_none() { + return; + } + let action = typed_hover(v).unwrap_or_default(); + let new = if action == "show_text" { + let mut h = hover; + rename(&mut h, "value", "contents"); + h + } else { + let mut text = "Legacy hoverEvent: ".encode_utf16().collect::>(); + text.extend(json::to_stable_string(&json::from_nbt(&hover))); + Tag::compound([ + ("action", string("show_text")), + ("contents", Tag::compound([("text", Tag::String(crate::nbt::JavaString(text)))])), + ]) + }; + set(v, "hoverEvent", new); +} + +/// `TextComponentStringifiedFlagsFix`: `Boolean.parseBoolean` of string flags. +fn stringified_flags(v: &mut Tag) { + if !is_full(v) { + return; + } + for flag in ["bold", "italic", "underlined", "strikethrough", "obfuscated"] { + if let Some(s) = as_str(v.get(flag)) { + set(v, flag, boolean(s.eq_ignore_ascii_case("true"))); + } + } +} + +/// `TextComponentHoverAndClickEventFix`. +fn hover_and_click_event(v: &mut Tag) { + if !is_full(v) { + return; + } + if typed_hover(v).is_none() && v.get("clickEvent").is_none() { + return; + } + rename_and_fix(v, "hoverEvent", "hover_event", |h| Some(fix_hover_event(h))); + rename_and_fix(v, "clickEvent", "click_event", fix_click_event); +} + +fn copy_fields(target: &mut Tag, source: &Tag, fields: &[&str]) { + for f in fields { + if let Some(value) = source.get(f) { + set(target, f, value.clone()); + } + } +} + +fn fix_hover_event(mut h: Tag) -> Tag { + match as_string(h.get("action"), "").as_str() { + "show_text" => rename(&mut h, "contents", "value"), + "show_item" => { + let contents = h.get("contents").cloned().unwrap_or_else(empty_map); + remove(&mut h, "contents"); + copy_fields(&mut h, &contents, &["id", "count", "components"]); + } + "show_entity" => { + let contents = h.get("contents").cloned().unwrap_or_else(empty_map); + remove(&mut h, "contents"); + copy_fields(&mut h, &contents, &["id", "type", "name"]); + rename(&mut h, "id", "uuid"); + rename(&mut h, "type", "id"); + } + _ => {} + } + h +} + +fn fix_click_event(mut c: Tag) -> Option { + let action = as_string(c.get("action"), ""); + let value = as_string(c.get("value"), ""); + match action.as_str() { + "open_url" => { + if !valid_uri(&value) { + return None; + } + rename(&mut c, "value", "url"); + } + "open_file" => rename(&mut c, "value", "path"), + "run_command" | "suggest_command" => { + if !valid_chat(&value) { + return None; + } + rename(&mut c, "value", "command"); + } + "change_page" => { + let old = c.get("value").and_then(|v| match as_number(v) { + Some(n) => Some(n.int_value()), + None => parse_int(&as_string(Some(v), "")), + })?; + remove(&mut c, "value"); + set(&mut c, "page", Tag::Int(old.max(1))); + } + _ => {} + } + Some(c) +} + +/// `Integer.parseInt`. +fn parse_int(s: &str) -> Option { + let digits = s.strip_prefix(['+', '-']).unwrap_or(s); + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + s.parse().ok() +} + +/// `new URI(uri)` succeeds with an http or https scheme. +fn valid_uri(uri: &str) -> bool { + let Some((scheme, rest)) = uri.split_once(':') else { + return false; + }; + let mut chars = scheme.chars(); + let scheme_ok = chars.next().is_some_and(|c| c.is_ascii_alphabetic()) + && chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')); + if !scheme_ok || rest.is_empty() { + return false; + } + // Characters java.net.URI refuses anywhere, and escapes it checks. + let bytes: Vec = rest.chars().collect(); + let mut hashes = 0; + let mut i = 0; + while i < bytes.len() { + let c = bytes[i]; + if c == '%' { + if !bytes.get(i + 1).is_some_and(char::is_ascii_hexdigit) || !bytes.get(i + 2).is_some_and(char::is_ascii_hexdigit) { + return false; + } + i += 3; + continue; + } + if c == '#' { + hashes += 1; + } + if c.is_control() || c.is_whitespace() || matches!(c, '"' | '<' | '>' | '\\' | '^' | '`' | '{' | '|' | '}') { + return false; + } + i += 1; + } + hashes <= 1 && matches!(scheme.to_ascii_lowercase().as_str(), "http" | "https") +} + +/// `StringUtil.isAllowedChatCharacter` for every character. +fn valid_chat(s: &str) -> bool { + s.encode_utf16().all(|c| c != 167 && c >= 32 && c != 127) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn j(s: &str) -> Tag { + json::to_nbt(&json::parse_lenient(&s.encode_utf16().collect::>()).unwrap()).unwrap() + } + + #[test] + fn click_events_take_their_new_fields() { + let mut c = j(r#"{"text":"a","clickEvent":{"action":"change_page","value":"0"}}"#); + hover_and_click_event(&mut c); + assert_eq!(c, j(r#"{"text":"a","click_event":{"action":"change_page","page":1}}"#)); + let mut c = j(r#"{"text":"a","clickEvent":{"action":"open_url","value":"ftp://x"}}"#); + hover_and_click_event(&mut c); + assert_eq!(c, j(r#"{"text":"a"}"#)); + } + + #[test] + fn legacy_hover_events_become_text() { + let mut c = j(r#"{"text":"a","hoverEvent":{"action":"show_item","value":"{id:stone}"}}"#); + legacy_hover_event(&mut c); + let expected = j( + r#"{"text":"a","hoverEvent":{"action":"show_text","contents":{"text":"Legacy hoverEvent: {\"action\":\"show_item\",\"value\":\"{id:stone}\"}"}}}"#, + ); + assert_eq!(c, expected); + } +} diff --git a/crates/horizon-world/src/lib.rs b/crates/horizon-world/src/lib.rs index 7830a035d..a055ec5ab 100644 --- a/crates/horizon-world/src/lib.rs +++ b/crates/horizon-world/src/lib.rs @@ -34,6 +34,7 @@ pub mod consume_effect; pub mod damage_immunity; pub mod damage_reduction; pub mod damage_source; +pub mod datafix; pub mod dynamic_support; pub mod edge_backoff; pub mod effect_attribute_modifiers; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java new file mode 100644 index 000000000..2bb7028f9 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java @@ -0,0 +1,246 @@ +package dev.horizonlab.parity; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.mojang.serialization.Dynamic; +import com.mojang.serialization.JsonOps; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; +import java.util.*; +import java.util.stream.Stream; +import net.minecraft.SharedConstants; +import net.minecraft.core.registries.Registries; +import net.minecraft.nbt.*; +import net.minecraft.resources.Identifier; +import net.minecraft.resources.ResourceKey; +import net.minecraft.server.Bootstrap; +import net.minecraft.server.level.ChunkMap; +import net.minecraft.util.datafix.DataFixTypes; +import net.minecraft.util.datafix.DataFixers; +import net.minecraft.util.worldupdate.UpgradeProgress; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.chunk.storage.RegionFile; +import net.minecraft.world.level.chunk.storage.RegionStorageInfo; +import net.minecraft.world.level.storage.LevelStorageSource; + +/** + * Opens a world saved by an older game version the way 26.3 does and writes out + * what 26.3 reads: the file fixes (FileFixerUpper) on the folder, level.dat fixed + * to the current DataVersion, then every chunk, entity chunk and POI chunk + * (SimpleRegionStorage.upgradeChunkTag with ChunkMap's context), player file + * (PlayerDataStorage.load), saved data file (SavedDataStorage.readTagFromDisk), + * advancements and statistics through the real DataFixers. + * + * VanillaDataFixOracle IN_WORLD OUT_WORLD + * + * Files the game rewrites after loading get the current DataVersion so that the + * output compares with what the game would save. Nothing is answered by hand. + */ +public final class VanillaDataFixOracle { + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + + public static void main(String[] args) throws Exception { + SharedConstants.tryDetectVersion(); + Bootstrap.bootStrap(); + Path in = Path.of(args[0]); + Path out = Path.of(args[1]); + copy(in, out); + int current = SharedConstants.getCurrentVersion().dataVersion().version(); + var fixer = DataFixers.getDataFixer(); + // FileFixerUpper.fix through a real level storage access, as the game opens a + // world: the file fixes on a copy-on-write view swapped in, then level.dat + // fixed to the current version and stamped (DataVersion, LastPlayed, Version). + LevelStorageSource source = LevelStorageSource.createDefault(out.getParent()); + try (LevelStorageSource.LevelStorageAccess access = source.createAccess(out.getFileName().toString())) { + Dynamic levelData = access.getUnfixedDataTag(false); + Dynamic fixed = DataFixers.getFileFixer().fix(access, levelData, new UpgradeProgress()); + access.saveLevelData(fixed); + } + Files.deleteIfExists(out.resolve("level.dat_old")); + Map generators = generators(out); + Path dimensions = out.resolve("dimensions"); + if (Files.isDirectory(dimensions)) { + List dims; + try (Stream s = Files.walk(dimensions, 2)) { + dims = s.filter(p -> dimensions.relativize(p).getNameCount() == 2).sorted().toList(); + } + for (Path dim : dims) { + String id = dim.getParent().getFileName() + ":" + dim.getFileName(); + ResourceKey key = ResourceKey.create(Registries.DIMENSION, Identifier.parse(id)); + String generator = generators.get(id); + CompoundTag context = ChunkMap.getChunkDataFixContextTag( + key, Optional.ofNullable(generator).map(Identifier::parse)); + upgradeRegions(dim.resolve("region"), key, "chunk", DataFixTypes.CHUNK, -1, context, current); + upgradeRegions(dim.resolve("entities"), key, "entities", DataFixTypes.ENTITY_CHUNK, -1, null, current); + upgradeRegions(dim.resolve("poi"), key, "poi", DataFixTypes.POI_CHUNK, 1945, null, current); + upgradeSavedData(dim.resolve("data"), current); + } + } + upgradeSavedData(out.resolve("data"), current); + for (Path file : list(out.resolve("players/data"), ".dat")) { + CompoundTag tag = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap()); + tag = DataFixTypes.PLAYER.updateToCurrentVersion(fixer, tag, NbtUtils.getDataVersion(tag)); + NbtIo.writeCompressed(NbtUtils.addDataVersion(tag, current), file); + } + for (Path file : list(out.resolve("players/advancements"), ".json")) { + // PlayerAdvancements: DataFixTypes.ADVANCEMENTS.wrapCodec, default version 1343. + JsonElement json = JsonParser.parseString(Files.readString(file, StandardCharsets.UTF_8)); + var dynamic = new Dynamic<>(JsonOps.INSTANCE, json); + int from = dynamic.get("DataVersion").asInt(1343); + var fixed = DataFixTypes.ADVANCEMENTS.updateToCurrentVersion(fixer, dynamic.remove("DataVersion"), from); + fixed = fixed.set("DataVersion", fixed.createInt(current)); + Files.writeString(file, GSON.toJson(fixed.getValue()), StandardCharsets.UTF_8); + } + for (Path file : list(out.resolve("players/stats"), ".json")) { + // ServerStatsCounter.parse: the version field stays in the data. + JsonElement json = JsonParser.parseString(Files.readString(file, StandardCharsets.UTF_8)); + var dynamic = new Dynamic<>(JsonOps.INSTANCE, json); + var fixed = DataFixTypes.STATS.updateToCurrentVersion(fixer, dynamic, NbtUtils.getDataVersion(dynamic, 1343)); + fixed = fixed.set("DataVersion", fixed.createInt(current)); + Files.writeString(file, GSON.toJson(fixed.getValue()), StandardCharsets.UTF_8); + } + } + + /** Chunk generator type per dimension, from the saved world generation settings. */ + private static Map generators(Path world) throws Exception { + Map out = new HashMap<>(); + Path file = world.resolve("data/minecraft/world_gen_settings.dat"); + if (!Files.exists(file)) { + return out; + } + CompoundTag tag = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap()); + CompoundTag dims = tag.getCompoundOrEmpty("data").getCompoundOrEmpty("dimensions"); + for (String dim : dims.keySet()) { + dims.getCompoundOrEmpty(dim).getCompoundOrEmpty("generator").getString("type").ifPresent(t -> out.put(dim, t)); + } + return out; + } + + private static void upgradeRegions(Path folder, ResourceKey dimension, String type, DataFixTypes fixType, + int defaultVersion, CompoundTag context, int current) throws Exception { + if (!Files.isDirectory(folder)) { + return; + } + var fixer = DataFixers.getDataFixer(); + for (Path file : list(folder, ".mca")) { + String[] parts = file.getFileName().toString().split("\\."); + int rx = Integer.parseInt(parts[1]); + int rz = Integer.parseInt(parts[2]); + RegionStorageInfo info = new RegionStorageInfo("oracle", dimension, type); + Map chunks = new LinkedHashMap<>(); + try (RegionFile region = new RegionFile(info, file, folder, false)) { + for (int z = 0; z < 32; z++) { + for (int x = 0; x < 32; x++) { + ChunkPos pos = new ChunkPos(rx * 32 + x, rz * 32 + z); + if (!region.hasChunk(pos)) { + continue; + } + try (DataInputStream in = region.getChunkDataInputStream(pos)) { + if (in != null) { + chunks.put(pos, NbtIo.read(in)); + } + } + } + } + } + Files.delete(file); + try (RegionFile region = new RegionFile(info, file, folder, false)) { + for (var entry : chunks.entrySet()) { + CompoundTag tag = entry.getValue(); + int version = NbtUtils.getDataVersion(tag, defaultVersion); + if (version < current) { + // SimpleRegionStorage.upgradeChunkTag + if (context != null) { + tag.put("__context", context.copy()); + } + tag = fixType.update(fixer, tag, version, current); + tag.remove("__context"); + NbtUtils.addDataVersion(tag, current); + } + try (DataOutputStream o = region.getChunkDataOutputStream(entry.getKey())) { + NbtIo.write(tag, o); + } + } + } + } + } + + /** SavedDataStorage.readTagFromDisk for every known saved data file under a data folder. */ + private static void upgradeSavedData(Path data, int current) throws Exception { + if (!Files.isDirectory(data)) { + return; + } + List files; + try (Stream s = Files.walk(data)) { + files = s.filter(p -> p.toString().endsWith(".dat")).sorted().toList(); + } + var fixer = DataFixers.getDataFixer(); + for (Path file : files) { + String rel = data.relativize(file).toString().replace('\\', '/'); + DataFixTypes type = savedDataType(rel); + if (type == null) { + continue; + } + CompoundTag tag = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap()); + int version = NbtUtils.getDataVersion(tag, 1343); + tag = type.update(fixer, tag, version, current); + NbtIo.writeCompressed(NbtUtils.addDataVersion(tag, current), file); + } + } + + /** The SavedDataType registered for a file below data/ (namespace/path.dat). */ + static DataFixTypes savedDataType(String rel) { + String name = rel.endsWith(".dat") ? rel.substring(0, rel.length() - 4) : rel; + if (name.endsWith("/command_storage")) { + return DataFixTypes.SAVED_DATA_COMMAND_STORAGE; + } + if (name.matches("minecraft/maps/\\d+")) { + return DataFixTypes.SAVED_DATA_MAP_DATA; + } + return switch (name) { + case "minecraft/custom_boss_events" -> DataFixTypes.SAVED_DATA_CUSTOM_BOSS_EVENTS; + case "minecraft/scoreboard" -> DataFixTypes.SAVED_DATA_SCOREBOARD; + case "minecraft/chunk_tickets" -> DataFixTypes.SAVED_DATA_FORCED_CHUNKS; + case "minecraft/world_border" -> DataFixTypes.SAVED_DATA_WORLD_BORDER; + case "minecraft/game_rules" -> DataFixTypes.SAVED_DATA_GAME_RULES; + case "minecraft/ender_dragon_fight" -> DataFixTypes.SAVED_DATA_ENDER_DRAGON_FIGHT; + case "minecraft/wandering_trader" -> DataFixTypes.SAVED_DATA_WANDERING_TRADER; + case "minecraft/maps/last_id" -> DataFixTypes.SAVED_DATA_MAP_INDEX; + case "minecraft/weather" -> DataFixTypes.SAVED_DATA_WEATHER; + case "minecraft/scheduled_events" -> DataFixTypes.SAVED_DATA_SCHEDULED_EVENTS; + case "minecraft/world_gen_settings" -> DataFixTypes.SAVED_DATA_WORLD_GEN_SETTINGS; + case "minecraft/random_sequences" -> DataFixTypes.SAVED_DATA_RANDOM_SEQUENCES; + case "minecraft/stopwatches" -> DataFixTypes.SAVED_DATA_STOPWATCHES; + case "minecraft/raids" -> DataFixTypes.SAVED_DATA_RAIDS; + case "minecraft/world_clocks" -> DataFixTypes.SAVED_DATA_WORLD_CLOCKS; + default -> null; + }; + } + + private static List list(Path folder, String suffix) throws Exception { + if (!Files.isDirectory(folder)) { + return List.of(); + } + try (Stream s = Files.list(folder)) { + return s.filter(p -> p.getFileName().toString().endsWith(suffix)).sorted().toList(); + } + } + + private static void copy(Path from, Path to) throws Exception { + try (Stream s = Files.walk(from)) { + for (Path p : s.sorted().toList()) { + Path target = to.resolve(from.relativize(p).toString()); + if (Files.isDirectory(p)) { + Files.createDirectories(target); + } else if (!p.getFileName().toString().equals("session.lock")) { + Files.copy(p, target, StandardCopyOption.REPLACE_EXISTING); + } + } + } + } +} diff --git a/tools/datafix_scene.py b/tools/datafix_scene.py new file mode 100644 index 000000000..270078d4e --- /dev/null +++ b/tools/datafix_scene.py @@ -0,0 +1,343 @@ +"""Scenes for tools/datafix_worlds.py: block entities, entities and items in the save +formats of older game versions, placed by console commands of that version. + +Three syntax eras matter for the commands: + A before 1.20.5 (DataVersion < 3818): item `tag` NBT, `Count`, JSON text strings + B 1.20.5 to 1.21.4 (< 4325): item components, JSON text strings + C 1.21.5 and later: item components, SNBT text components, `equipment` + +Every entry is (minimum DataVersion, command template). `{x} {y} {z}` are filled +with a free position; entities get a fixed UUID so outputs can be matched. +""" + +A, B, C = 'A', 'B', 'C' + + +def era(version): + return A if version < 3818 else B if version < 4325 else C + + +def text(e, s): + """A text component value for NBT: JSON string (A/B) or SNBT compound (C).""" + if e == C: + return '{text:"%s"}' % s + return "'{\"text\":\"%s\"}'" % s + + +# ------------------------------------------------------------------ items, era A +ITEMS_A = [ + '{id:"minecraft:diamond_sword",Count:1b,tag:{Damage:5,RepairCost:3,Unbreakable:1b,HideFlags:255,' + 'Enchantments:[{id:"minecraft:sharpness",lvl:3s},{id:"minecraft:sweeping",lvl:2s}],' + 'display:{Name:\'{"text":"Blade","color":"red","italic":false}\',Lore:[\'{"text":"line one"}\',\'"plain"\']},' + 'AttributeModifiers:[{AttributeName:"generic.attack_damage",Name:"x",Amount:2.5d,Operation:0,' + 'UUID:[I;1,2,3,4],Slot:"mainhand"}],CustomModelData:7}}', + '{id:"minecraft:enchanted_book",Count:1b,tag:{StoredEnchantments:[{id:"minecraft:mending",lvl:1s}]}}', + '{id:"minecraft:written_book",Count:1b,tag:{title:"Title",author:"Me",generation:1,resolved:1b,' + 'pages:[\'{"text":"page one","bold":true}\',\'{"text":"two","clickEvent":{"action":"change_page","value":"2"},' + '"hoverEvent":{"action":"show_text","value":"hi"}}\'],filtered_pages:{"1":\'"f"\'}}}', + '{id:"minecraft:writable_book",Count:1b,tag:{pages:["draft one","draft two"]}}', + '{id:"minecraft:potion",Count:1b,tag:{Potion:"minecraft:strong_healing",CustomPotionColor:255}}', + '{id:"minecraft:splash_potion",Count:1b,tag:{CustomPotionEffects:[{Id:1b,Amplifier:1b,Duration:200}]}}', + '{id:"minecraft:tipped_arrow",Count:5b,tag:{Potion:"minecraft:poison"}}', + '{id:"minecraft:suspicious_stew",Count:1b,tag:{Effects:[{EffectId:3,EffectDuration:160}]}}', + '{id:"minecraft:firework_rocket",Count:3b,tag:{Fireworks:{Flight:2b,Explosions:[{Type:1b,Colors:[I;255],' + 'FadeColors:[I;16711680],Trail:1b,Flicker:0b}]}}}', + '{id:"minecraft:firework_star",Count:1b,tag:{Explosion:{Type:3b,Colors:[I;65280]}}}', + '{id:"minecraft:player_head",Count:1b,tag:{SkullOwner:{Name:"Notch",Id:[I;1,2,3,4],' + 'Properties:{textures:[{Value:"abc",Signature:"sig"}]}}}}', + '{id:"minecraft:player_head",Count:1b,tag:{SkullOwner:"jeb_"}}', + '{id:"minecraft:filled_map",Count:1b,tag:{map:3,display:{MapColor:255},Decorations:[{id:"x",type:9b,x:1.0d,z:2.0d,rot:180.0d}]}}', + '{id:"minecraft:compass",Count:1b,tag:{LodestonePos:{X:1,Y:2,Z:3},LodestoneDimension:"minecraft:overworld",LodestoneTracked:0b}}', + '{id:"minecraft:crossbow",Count:1b,tag:{Charged:1b,ChargedProjectiles:[{id:"minecraft:arrow",Count:1b}]}}', + '{id:"minecraft:shield",Count:1b,tag:{BlockEntityTag:{Base:4,Patterns:[{Pattern:"cre",Color:15},{Pattern:"bs",Color:1}]}}}', + '{id:"minecraft:white_banner",Count:1b,tag:{BlockEntityTag:{Patterns:[{Pattern:"mr",Color:9}]},' + 'display:{Name:\'{"translate":"block.minecraft.ominous_banner","color":"gold"}\'}}}', + '{id:"minecraft:red_shulker_box",Count:1b,tag:{BlockEntityTag:{Items:[{Slot:3b,id:"minecraft:stone",Count:9b},' + '{Slot:0b,id:"minecraft:golden_apple",Count:2b,tag:{display:{Name:\'"Gold"\'}}}],Lock:"key",' + 'CustomName:\'{"text":"Box"}\'}}}', + '{id:"minecraft:chest",Count:1b,tag:{BlockEntityTag:{LootTable:"minecraft:chests/simple_dungeon",LootTableSeed:5L}}}', + '{id:"minecraft:pig_spawn_egg",Count:1b,tag:{EntityTag:{CustomName:\'"Babe"\',Age:-24000}}}', + '{id:"minecraft:armor_stand",Count:1b,tag:{EntityTag:{Small:1b}}}', + '{id:"minecraft:leather_chestplate",Count:1b,tag:{display:{color:16711680},' + 'Trim:{material:"minecraft:gold",pattern:"minecraft:coast"}}}', + '{id:"minecraft:goat_horn",Count:1b,tag:{instrument:"minecraft:sing_goat_horn"}}', + '{id:"minecraft:axolotl_bucket",Count:1b,tag:{Variant:2,Age:-100,Health:7.0f,NoAI:1b}}', + '{id:"minecraft:tropical_fish_bucket",Count:1b,tag:{BucketVariantTag:65536}}', + '{id:"minecraft:knowledge_book",Count:1b,tag:{Recipes:["minecraft:stone"]}}', + '{id:"minecraft:debug_stick",Count:1b,tag:{DebugProperty:{"minecraft:oak_stairs":"facing"}}}', + '{id:"minecraft:diamond_pickaxe",Count:1b,tag:{CanDestroy:["minecraft:stone","#minecraft:logs",' + '"minecraft:oak_stairs[facing=east]"],HideFlags:8}}', + '{id:"minecraft:stone",Count:4b,tag:{CanPlaceOn:["minecraft:dirt"],BlockStateTag:{}}}', + '{id:"minecraft:oak_slab",Count:1b,tag:{BlockStateTag:{type:"top",waterlogged:1b}}}', + '{id:"minecraft:grass",Count:2b}', + '{id:"minecraft:scute",Count:2b}', + '{id:"minecraft:chain",Count:1b}', + '{id:"minecraft:decorated_pot",Count:1b,tag:{BlockEntityTag:{sherds:["minecraft:brick","minecraft:angler_pottery_sherd",' + '"minecraft:brick","minecraft:arms_up_pottery_sherd"]}}}', + '{id:"minecraft:beehive",Count:1b,tag:{BlockEntityTag:{Bees:[{EntityData:{id:"minecraft:bee"},TicksInHive:3,' + 'MinOccupationTicks:600}]}}}', + '{id:"minecraft:cooked_beef",Count:64b,tag:{custom:"data",nested:{a:1}}}', + '{id:"minecraft:iron_sword",Count:1b,tag:{Enchantments:[]}}', + '{id:"minecraft:bow",Count:1b,tag:{Enchantments:[{id:"minecraft:power",lvl:0s}],HideFlags:1}}', +] + +# ------------------------------------------------------------------ items, era B/C +ITEMS_B = [ + '{id:"minecraft:diamond_sword",count:1,components:{"minecraft:damage":5,"minecraft:repair_cost":3,' + '"minecraft:unbreakable":{show_in_tooltip:false},"minecraft:enchantments":{levels:{"minecraft:sharpness":3}},' + '"minecraft:custom_name":\'{"text":"Blade","color":"red"}\',"minecraft:lore":[\'"plain"\'],' + '"minecraft:attribute_modifiers":{modifiers:[{type:"minecraft:generic.attack_damage",id:"minecraft:x",' + 'amount:2.5d,operation:"add_value",slot:"mainhand"}]}}}', + '{id:"minecraft:written_book",count:1,components:{"minecraft:written_book_content":{title:{raw:"T"},author:"Me",' + 'pages:[{raw:\'{"text":"p1","clickEvent":{"action":"run_command","value":"/say hi"}}\'}]}}}', + '{id:"minecraft:potion",count:1,components:{"minecraft:potion_contents":{potion:"minecraft:healing"}}}', + '{id:"minecraft:white_banner",count:1,components:{"minecraft:banner_patterns":[{pattern:"minecraft:creeper",color:"black"}],' + '"minecraft:item_name":\'{"translate":"block.minecraft.ominous_banner"}\',"minecraft:rarity":"uncommon"}}', + '{id:"minecraft:red_shulker_box",count:1,components:{"minecraft:container":[{slot:0,item:{id:"minecraft:stone",count:3}}],' + '"minecraft:lock":"key"}}', + '{id:"minecraft:apple",count:2,components:{"minecraft:food":{nutrition:4,saturation:2.4f,can_always_eat:true,' + 'eat_seconds:1.0f}}}', + '{id:"minecraft:netherite_sword",count:1,components:{"minecraft:fire_resistant":{}}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:custom_model_data":5}}', + '{id:"minecraft:pig_spawn_egg",count:1,components:{"minecraft:entity_data":{id:"minecraft:pig",Age:-200}}}', + '{id:"minecraft:diamond_helmet",count:1,components:{"minecraft:hide_additional_tooltip":{},' + '"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:coast",show_in_tooltip:false}}}', + '{id:"minecraft:decorated_pot",count:1,components:{"minecraft:pot_decorations":["minecraft:brick",' + '"minecraft:angler_pottery_sherd"]}}', + '{id:"minecraft:chain",count:1}', +] +ITEMS_C = [ + '{id:"minecraft:diamond_sword",count:1,components:{"minecraft:damage":5,' + '"minecraft:custom_name":{text:"Blade",color:"red"},"minecraft:tooltip_display":{hidden_components:["minecraft:damage"]}}}', + '{id:"minecraft:written_book",count:1,components:{"minecraft:written_book_content":{title:{raw:"T"},author:"Me",' + 'pages:[{raw:{text:"p1",click_event:{action:"change_page",page:2}}}]}}}', + '{id:"minecraft:decorated_pot",count:1,components:{"minecraft:pot_decorations":["minecraft:brick",' + '"minecraft:angler_pottery_sherd"]}}', + '{id:"minecraft:filled_map",count:1,components:{"minecraft:map_id":2,"minecraft:map_color":255}}', + '{id:"minecraft:chain",count:1}', + '{id:"minecraft:name_tag",count:1,components:{"minecraft:custom_name":"Bob"}}', +] + + +def items(e): + return ITEMS_A if e == A else ITEMS_B if e == B else ITEMS_C + + +def slotted(stacks): + out = [] + for i, s in enumerate(stacks[:27]): + out.append(s[:1] + f'Slot:{i}b,' + s[1:]) + return '[' + ','.join(out) + ']' + + +def chests(e): + """Chests holding every sample item (27 per chest).""" + st = items(e) + return [st[i:i + 27] for i in range(0, len(st), 27)] + + +# ------------------------------------------------------------------ block entities +def block_entities(e, version): + name = lambda s: text(e, s) + out = [] + for group in chests(e): + out.append(('chest', f'{{Items:{slotted(group)},CustomName:{name("Stash")}}}')) + sign_a = ('{front_text:{messages:[\'{"text":"one"}\',\'"two"\',\'""\',\'""\'],color:"red",has_glowing_text:1b},' + 'back_text:{messages:[\'"b"\',\'""\',\'""\',\'""\'],filtered_messages:[\'"x"\',\'""\',\'""\',\'""\']},' + 'is_waxed:1b}') + sign_c = ('{front_text:{messages:[{text:"one"},"two","",""],color:"red",has_glowing_text:1b},' + 'back_text:{messages:["b","","",""]},is_waxed:1b}') + sign = sign_c if e == C else sign_a + out += [('oak_sign', sign), ('oak_hanging_sign', sign)] + if e == A: + out += [ + ('white_banner', '{Patterns:[{Pattern:"cre",Color:15}],CustomName:\'{"translate":"block.minecraft.ominous_banner"}\'}'), + ('player_head', '{SkullOwner:{Name:"Notch",Id:[I;1,2,3,4]}}'), + ('decorated_pot', '{sherds:["minecraft:brick","minecraft:skull_pottery_sherd","minecraft:brick","minecraft:brick"]}'), + ('beehive', '{Bees:[{EntityData:{id:"minecraft:bee",CustomName:\'"Bz"\'},TicksInHive:2,MinOccupationTicks:400}],' + 'FlowerPos:{X:1,Y:2,Z:3}}'), + ('furnace', '{BurnTime:100s,CookTime:20s,CookTimeTotal:200s,Items:[{Slot:0b,id:"minecraft:iron_ore",Count:3b}],' + 'RecipesUsed:{"minecraft:iron_ingot":2}}'), + ('jukebox', '{RecordItem:{id:"minecraft:music_disc_cat",Count:1b},RecordStartTick:100L,TickCount:250L,IsPlaying:1b}'), + ('suspicious_sand', '{item:{id:"minecraft:emerald",Count:1b}}'), + ('lectern', '{Book:{id:"minecraft:written_book",Count:1b,tag:{title:"L",author:"A",pages:[\'"x"\']}},Page:0}'), + ('spawner', '{SpawnData:{entity:{id:"minecraft:zombie",ArmorItems:[{},{},{},{id:"minecraft:iron_helmet",Count:1b}]}},' + 'SpawnPotentials:[{weight:1,data:{entity:{id:"minecraft:skeleton"}}}]}'), + ('command_block', '{Command:"say hi",CustomName:\'"Cmd"\',LastOutput:\'{"text":"out"}\'}'), + ('end_gateway', '{ExitPortal:{X:1,Y:70,Z:2},ExactTeleport:1b,Age:5L}'), + ('chiseled_bookshelf', '{Items:[{Slot:1b,id:"minecraft:book",Count:1b}],last_interacted_slot:1}'), + ('campfire', '{Items:[{Slot:0b,id:"minecraft:beef",Count:1b}],CookingTimes:[I;5,0,0,0],CookingTotalTimes:[I;600,0,0,0]}'), + ('hopper', '{Items:[{Slot:0b,id:"minecraft:stone",Count:1b}],Lock:"key"}'), + ('structure_block', '{name:"x:y",posX:1,posY:2,posZ:3,sizeX:4,sizeY:5,sizeZ:6,mode:"SAVE"}'), + ('skeleton_skull', '{}'), + ] + if version >= 3682: + out.append(('crafter', '{Items:[{Slot:0b,id:"minecraft:stone",Count:1b}],triggered:1,crafting_ticks_remaining:2}')) + else: + out += [ + ('white_banner', '{patterns:[{pattern:"minecraft:creeper",color:"black"}],CustomName:%s}' % name("Flag")), + ('player_head', '{profile:{name:"Notch"}}'), + ('decorated_pot', '{sherds:["minecraft:brick","minecraft:skull_pottery_sherd","minecraft:brick","minecraft:brick"]}'), + ('furnace', '{BurnTime:100s,CookTime:20s,CookTimeTotal:200s,Items:[{Slot:0b,id:"minecraft:iron_ore",count:3}]}' + if version < 4181 else '{lit_time_remaining:100s,cooking_time_spent:20s,cooking_total_time:200s}'), + ('jukebox', '{RecordItem:{id:"minecraft:music_disc_cat",count:1},ticks_since_song_started:5L}'), + ('trial_spawner', '{required_player_range:10}'), + ('vault', '{config:{key_item:{id:"minecraft:trial_key",count:1}}}'), + ('crafter', '{Items:[{Slot:0b,id:"minecraft:stone",count:1}],triggered:1}'), + ('spawner', '{SpawnData:{entity:{id:"minecraft:zombie"}}}'), + ('hopper', '{Items:[{Slot:0b,id:"minecraft:stone",count:1}],lock:{items:"minecraft:stick"}}' + if version >= 4068 else '{Items:[{Slot:0b,id:"minecraft:stone",count:1}],Lock:"key"}'), + ] + if version >= 4071: + out.append(('creaking_heart', '')) + if version >= 4533: + out.append(('oak_shelf', '{Items:[{Slot:0b,id:"minecraft:apple",count:1}]}')) + if version >= 4532: + out.append(('copper_golem_statue', '')) + return out + + +# ------------------------------------------------------------------ entities +def entities(e, version): + """(id, SNBT fields) for summon; item fields in the era's form.""" + if e == A: + sword = '{id:"minecraft:iron_sword",Count:1b,tag:{display:{Name:\'"S"\'}}}' + helmet = '{id:"minecraft:iron_helmet",Count:1b}' + out = [ + ('zombie', f'ArmorItems:[{{}},{{}},{{}},{helmet}],HandItems:[{sword},{{}}],ArmorDropChances:[0.1f,0.1f,0.1f,2.0f],' + 'HandDropChances:[1.0f,0.085f],CustomName:\'"Zed"\''), + ('villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"},Xp:3,' + 'Offers:{Recipes:[{buy:{id:"minecraft:wheat",Count:20b},buyB:{id:"minecraft:air",Count:1b},' + 'sell:{id:"minecraft:emerald",Count:1b},maxUses:16,uses:1}]},Inventory:[{id:"minecraft:bread",Count:3b}]'), + ('horse', 'Tame:1b,SaddleItem:{id:"minecraft:saddle",Count:1b},ArmorItem:{id:"minecraft:iron_horse_armor",Count:1b},' + 'Attributes:[{Name:"minecraft:horse.jump_strength",Base:0.7d},{Name:"minecraft:generic.max_health",Base:20.0d,' + 'Modifiers:[{Name:"m",Amount:1.0d,Operation:0,UUID:[I;5,6,7,8]}]}]'), + ('llama', 'ChestedHorse:1b,Items:[{Slot:2b,id:"minecraft:apple",Count:1b}],DecorItem:{id:"minecraft:red_carpet",Count:1b}'), + ('donkey', 'ChestedHorse:1b,Items:[{Slot:2b,id:"minecraft:apple",Count:1b}],SaddleItem:{id:"minecraft:saddle",Count:1b}'), + ('wolf', 'Health:15.0f,Owner:[I;1,2,3,4],CollarColor:3b'), + ('salmon', ''), + ('armor_stand', f'ArmorItems:[{{}},{{}},{{}},{helmet}],HandItems:[{sword},{{}}],Small:1b'), + ('item_frame', f'Item:{sword},Facing:3b'), + ('item', 'Item:{id:"minecraft:written_book",Count:1b,tag:{pages:[\'"p"\'],title:"t",author:"a"}},PickupDelay:10s'), + ('arrow', 'inGround:1b,Potion:"minecraft:poison",CustomPotionEffects:[{Id:19b,Duration:100}]'), + ('trident', 'Trident:{id:"minecraft:trident",Count:1b},inGround:1b'), + ('area_effect_cloud', 'Potion:"minecraft:healing",Particle:"entity_effect",Radius:2.0f,Duration:600'), + ('potion', 'Item:{id:"minecraft:splash_potion",Count:1b,tag:{Potion:"minecraft:swiftness"}}'), + ('falling_block', 'BlockState:{Name:"minecraft:sand"},Time:-1000,NoGravity:1b'), + ('tnt', 'Fuse:80s'), + ('tnt_minecart', 'TNTFuse:40'), + ('chest_minecart', 'Items:[{Slot:0b,id:"minecraft:apple",Count:1b}]'), + ('boat', 'Type:"spruce"'), + ('chest_boat', 'Type:"birch",Items:[{Slot:0b,id:"minecraft:apple",Count:1b}]'), + ('text_display', 'text:\'{"text":"Hi","bold":true}\''), + ('item_display', 'item:{id:"minecraft:stone",Count:1b}'), + ('pig', 'Saddle:1b'), + ('cat', 'variant:"minecraft:black"'), + ('fox', 'Type:"red"'), + ('zombie_villager', 'VillagerData:{profession:"minecraft:none",level:1,type:"minecraft:plains"}'), + ('wandering_trader', ''), + ('piglin', 'Inventory:[{id:"minecraft:gold_ingot",Count:2b}]'), + ('firework_rocket', 'FireworksItem:{id:"minecraft:firework_rocket",Count:1b,tag:{Fireworks:{Flight:1b}}}'), + ('allay', 'Inventory:[{id:"minecraft:diamond",Count:1b}]'), + ] + return out + sword = '{id:"minecraft:iron_sword",count:1}' + helmet = '{id:"minecraft:iron_helmet",count:1}' + name = lambda s: text(e, s) + if e == B: + out = [ + ('zombie', f'ArmorItems:[{{}},{{}},{{}},{helmet}],HandItems:[{sword},{{}}],ArmorDropChances:[0.1f,0.1f,0.1f,2.0f],' + f'CustomName:{name("Zed")}'), + ('horse', 'Tame:1b,SaddleItem:{id:"minecraft:saddle",count:1},body_armor_item:{id:"minecraft:iron_horse_armor",count:1}'), + ('llama', 'ChestedHorse:1b,Items:[{Slot:2b,id:"minecraft:apple",count:1}],body_armor_item:{id:"minecraft:red_carpet",count:1}'), + ('area_effect_cloud', 'potion_contents:{potion:"minecraft:healing"},Particle:{type:"minecraft:entity_effect",color:255},Duration:600'), + ('potion', 'Item:{id:"minecraft:lingering_potion",count:1}'), + ('tnt_minecart', 'TNTFuse:40' if version < 4173 else 'fuse:40'), + ('salmon', ''), + ('item_frame', f'Item:{sword},Facing:3b'), + ('arrow', 'inGround:1b,item:{id:"minecraft:arrow",count:1}'), + ('text_display', f'text:{name("Hi")}'), + ('villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"}'), + ('pig', 'Saddle:1b'), + ('falling_block', 'BlockState:{Name:"minecraft:sand"},Time:-1000,NoGravity:1b'), + ] + if version >= 4067: + out.append(('pale_oak_chest_boat' if version >= 4070 else 'oak_chest_boat', 'Items:[{Slot:0b,id:"minecraft:apple",count:1}]')) + else: + out.append(('chest_boat', 'Type:"birch",Items:[{Slot:0b,id:"minecraft:apple",count:1}]')) + return out + out = [ + ('zombie', f'equipment:{{head:{helmet},mainhand:{sword}}},drop_chances:{{head:2.0f}},CustomName:{name("Zed")}'), + ('horse', 'Tame:1b,equipment:{saddle:{id:"minecraft:saddle",count:1},body:{id:"minecraft:iron_horse_armor",count:1}}'), + ('area_effect_cloud', 'potion_contents:{potion:"minecraft:healing"},Duration:600'), + ('splash_potion' if version >= 4306 else 'potion', 'Item:{id:"minecraft:splash_potion",count:1}'), + ('tnt_minecart', 'fuse:40'), + ('text_display', f'text:{name("Hi")}'), + ('villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"}'), + ('falling_block', 'BlockState:{Name:"minecraft:sand"},Time:-1000,NoGravity:1b'), + ('trident', 'item:{id:"minecraft:trident",count:1},inGround:1b'), + ] + if version >= 4531: + out.append(('copper_golem', '')) + if version >= 4543: + out.append(('mannequin', '')) + return out + + +def player_commands(e, name): + """Give the joined bot a varied inventory and ender chest.""" + out = [] + for i, stack in enumerate(items(e)[:30]): + out.append(f'item replace entity {name} container.{i} with {give_form(e, stack)}') + out.append(f'item replace entity {name} enderchest.0 with {give_form(e, items(e)[0])}') + out.append(f'item replace entity {name} armor.head with minecraft:iron_helmet') + out.append(f'xp set {name} 5 levels') + return out + + +def give_form(e, stack): + """`item replace ... with` argument from a stack SNBT: id plus tag/components.""" + import re + m = re.match(r'\{id:"([^"]+)",(?:Count:(\d+)b|count:(\d+))(?:,(tag|components):(.*))?\}$', stack, re.S) + item, count = m.group(1), m.group(2) or m.group(3) + if m.group(4) == 'tag': + return f'{item}{m.group(5)} {count}' + if m.group(4) == 'components': + body = m.group(5)[1:-1] + parts = split_top(body) + comps = [] + for p in parts: + if p.startswith('"'): + end = p.index('"', 1) + key, value = p[1:end], p[end + 1:].lstrip()[1:] + else: + key, value = p.split(':', 1) + comps.append(f'{key}={value.strip()}') + comps = ','.join(comps) + return f'{item}[{comps}] {count}' + return f'{item} {count}' + + +def split_top(s): + """Split SNBT compound contents at top-level commas.""" + out, depth, cur, q = [], 0, '', None + for ch in s: + if q: + cur += ch + if ch == q: + q = None + continue + if ch in '"\'': + q = ch + elif ch in '{[': + depth += 1 + elif ch in '}]': + depth -= 1 + elif ch == ',' and depth == 0: + out.append(cur) + cur = '' + continue + cur += ch + if cur: + out.append(cur) + return out diff --git a/tools/datafix_worlds.py b/tools/datafix_worlds.py new file mode 100644 index 000000000..e0f904241 --- /dev/null +++ b/tools/datafix_worlds.py @@ -0,0 +1,325 @@ +#!/usr/bin/env python3 +"""Make sample worlds with older vanilla servers, for the DataFixer harness. + +For each listed version, the official server jar (cached under +.local/datafix/servers/VERSION/server.jar) generates a default world around spawn, +gets a scene of block entities, entities and items in that version's own save +format (tools/datafix_scene.py), and a bot player joins (with its inventory filled) +so that player data exists. The saved world is kept in .local/datafix/worlds/VERSION. + + datafix_worlds.py [--force] [VERSION...] (default: every version below) +""" +import argparse +import hashlib +import json +import os +import queue +import re +import shutil +import socket +import struct +import subprocess +import sys +import threading +import time +import urllib.request +import uuid +import zipfile +import zlib +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import datafix_scene as scene # noqa: E402 + +ROOT = Path(__file__).resolve().parent.parent +LOCAL = ROOT / '.local' / 'datafix' +VERSIONS = ['1.20.1', '1.20.4', '1.20.6', '1.21.1', '1.21.4', '1.21.5', '1.21.8', '1.21.10', '1.21.11', '26.1.2', '26.2'] +MANIFEST = 'https://piston-meta.mojang.com/mc/game/version_manifest_v2.json' +BOT = 'Upgrader' +UUID_BASE = 0x6000 + + +def server_jar(version): + path = LOCAL / 'servers' / version / 'server.jar' + if not path.exists(): + path.parent.mkdir(parents=True, exist_ok=True) + manifest = json.load(urllib.request.urlopen(MANIFEST)) + entry = next(v for v in manifest['versions'] if v['id'] == version) + meta = json.load(urllib.request.urlopen(entry['url'])) + download = meta['downloads']['server'] + data = urllib.request.urlopen(download['url']).read() + if hashlib.sha1(data).hexdigest() != download['sha1']: + sys.exit(f'{version}: server.jar checksum mismatch') + path.write_bytes(data) + info = json.loads(zipfile.ZipFile(path).read('version.json')) + return path, info['world_version'], info['protocol_version'] + + +class Server: + def __init__(self, directory, jar, port): + 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', + 'view-distance': '4', 'simulation-distance': '4', 'spawn-protection': '0', 'sync-chunk-writes': 'true', + 'enable-rcon': 'false', 'enable-query': 'false', 'max-tick-time': '-1'} + (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) + self.lines = [] + self.queue = queue.Queue() + self.counter = 0 + threading.Thread(target=self._pump, daemon=True).start() + self.wait('Done (', 600) + + def _pump(self): + for line in self.process.stdout: + line = line.rstrip('\n') + self.lines.append(line) + self.queue.put(line) + + def wait(self, pattern, timeout=60): + end = time.time() + timeout + seen = [] + while time.time() < end: + try: + line = self.queue.get(timeout=1) + except queue.Empty: + if self.process.poll() is not None: + break + continue + seen.append(line) + if pattern in line: + return seen + raise TimeoutError(f'no "{pattern}" in {timeout}s; last lines: {seen[-5:]}') + + def run(self, command, timeout=60): + self.counter += 1 + marker = f'@@{self.counter}@@' + self.process.stdin.write(command + '\nsay ' + marker + '\n') + self.process.stdin.flush() + out = self.wait(marker, timeout) + return [re.sub(r'^\[[0-9:]+\] \[[^\]]*\]: ', '', l) for l in out[:-1]] + + def stop(self): + try: + self.process.stdin.write('stop\n') + self.process.stdin.flush() + self.process.wait(120) + except Exception: + self.process.kill() + + +def failed(lines): + return [l for l in lines if re.search(r'(Unknown|Expected|Incorrect|Invalid|Could not|Unable|Failed|That block|' + r'error|Malformed|Can\'t)', l, re.I)] + + +# ------------------------------------------------------------------ minimal bot + +def varint(v): + out = b'' + v &= 0xFFFFFFFF + while True: + b = v & 0x7F + v >>= 7 + out += bytes([b | (0x80 if v else 0)]) + if not v: + return out + + +def mcstring(s): + data = s.encode() + return varint(len(data)) + data + + +def parse_varint(data, pos): + value = shift = 0 + while True: + b = data[pos] + pos += 1 + value |= (b & 0x7F) << shift + shift += 7 + if not b & 0x80: + return pos, value + + +def offline_uuid(name): + digest = bytearray(hashlib.md5(('OfflinePlayer:' + name).encode()).digest()) + digest[6] = (digest[6] & 0x0F) | 0x30 + digest[8] = (digest[8] & 0x3F) | 0x80 + return uuid.UUID(bytes=bytes(digest)) + + +class Bot: + """Logs in (offline mode) with the given protocol and stays until told to leave.""" + + def __init__(self, port, protocol, name): + self.sock = socket.create_connection(('127.0.0.1', port), timeout=30) + self.threshold = -1 + self.buffer = b'' + self.protocol = protocol + self.stop = threading.Event() + self.send(0, varint(protocol) + mcstring('127.0.0.1') + struct.pack('>H', port) + varint(2)) + if protocol <= 763: # 1.20.1: optional UUID + self.send(0, mcstring(name) + b'\x01' + offline_uuid(name).bytes) + else: + self.send(0, mcstring(name) + offline_uuid(name).bytes) + while True: + pid, data = self.receive() + if pid == 3: + self.threshold = parse_varint(data, 0)[1] + elif pid == 2: + break + elif pid == 0: + raise RuntimeError(f'login refused: {data!r}') + if protocol >= 764: + self.send(3) # login acknowledged -> configuration + self.configure() + threading.Thread(target=self.idle, daemon=True).start() + + def configure(self): + old = self.protocol < 766 # 1.20.2 - 1.20.4 packet ids + finish, keep_alive, ping, disconnect = (2, 3, 4, 1) if old else (3, 4, 5, 2) + while True: + pid, data = self.receive() + if pid == finish: + self.send(2 if old else 3) + return + if pid == keep_alive: + self.send(3 if old else 4, data) + elif pid == ping: + self.send(4 if old else 5, data) + elif pid == disconnect: + raise RuntimeError(f'disconnected during configuration: {data!r}') + elif not old and self.known_packs(data): + self.send(7, varint(0)) # claim no packs: the server sends full registries + + @staticmethod + def known_packs(data): + try: + pos, count = parse_varint(data, 0) + if count < 1: + return False + pos, n = parse_varint(data, pos) + return data[pos:pos + n] == b'minecraft' + except IndexError: + return False + + def idle(self): + self.sock.settimeout(1.0) + while not self.stop.is_set(): + try: + self.receive() + except socket.timeout: + continue + except (EOFError, OSError): + break + + def leave(self): + self.stop.set() + time.sleep(1.5) + self.sock.close() + + def read_exact(self, n): + while len(self.buffer) < n: + chunk = self.sock.recv(65536) + if not chunk: + raise EOFError('server closed the connection') + self.buffer += chunk + out, self.buffer = self.buffer[:n], self.buffer[n:] + return out + + def read_varint(self): + value = shift = 0 + while True: + b = self.read_exact(1)[0] + value |= (b & 0x7F) << shift + shift += 7 + if not b & 0x80: + return value + + def send(self, packet_id, payload=b''): + body = varint(packet_id) + payload + if self.threshold >= 0: + body = varint(len(body)) + zlib.compress(body) if len(body) >= self.threshold else varint(0) + body + self.sock.sendall(varint(len(body)) + body) + + def receive(self): + length = self.read_varint() + body = self.read_exact(length) + if self.threshold >= 0: + pos, size = parse_varint(body, 0) + body = zlib.decompress(body[pos:]) if size else body[pos:] + pos, packet_id = parse_varint(body, 0) + return packet_id, body[pos:] + + +# ------------------------------------------------------------------ world + +def uuid_nbt(i): + return f'[I;{UUID_BASE},0,0,{i}]' + + +def build(version, force=False): + jar, data_version, protocol = server_jar(version) + target = LOCAL / 'worlds' / version + if target.exists() and not force: + print(f'{version}: kept {target}') + return [] + era = scene.era(data_version) + work = LOCAL / 'gen' / version + shutil.rmtree(work, ignore_errors=True) + port = 30000 + (os.getpid() + data_version) % 20000 + server = Server(work, jar, port) + problems = [] + + def run(command): + out = server.run(command) + bad = failed(out) + if bad: + problems.append((command[:160], bad[:2])) + return out + + 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): + run(command) + time.sleep(3) + run('save-all flush') + time.sleep(2) + bot.leave() + time.sleep(2) + server.stop() + shutil.rmtree(target, ignore_errors=True) + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copytree(work / 'world', target) + (target / 'datafix-source.json').write_text(json.dumps( + {'version': version, 'data_version': data_version, 'problems': problems}, indent=1)) + for command, out in problems: + print(f'{version}: command failed: {command}\n {out}') + print(f'{version}: DataVersion {data_version}, world in {target}') + return problems + + +def main(): + p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + p.add_argument('versions', nargs='*') + p.add_argument('--force', action='store_true') + args = p.parse_args() + for version in args.versions or VERSIONS: + build(version, args.force) + + +if __name__ == '__main__': + main() diff --git a/tools/test_datafix_parity.py b/tools/test_datafix_parity.py new file mode 100644 index 000000000..da3e6250f --- /dev/null +++ b/tools/test_datafix_parity.py @@ -0,0 +1,214 @@ +#!/usr/bin/env python3 +"""DataFixer differential test: worlds saved by older game versions, upgraded by +vanilla 26.3 and by Horizon, must give the same data. + +For every sample world in .local/datafix/worlds (made by tools/datafix_worlds.py +with the official server jars of 1.20.1 to 26.2), two upgrades run on a copy: + + vanilla VanillaDataFixOracle: FileFixerUpper, then DataFixers on level.dat, every + chunk, entity chunk and POI chunk, player, saved data, advancements and + statistics file, exactly as 26.3 does when it opens the world + horizon the `datafix_world` example of horizon-world, which does the same with + horizon_world::datafix + +Then every file is compared: region files chunk by chunk, NBT files and JSON +files value by value (compound and object key order does not matter). The only +tolerated differences are listed in IGNORED (wall-clock time). + + test_datafix_parity.py [--keep] [--skip-build] [VERSION...] +""" +import argparse +import json +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import anvil_dump # noqa: E402 + +ROOT = Path(__file__).resolve().parent.parent +LOCAL = ROOT / '.local' / 'datafix' +WORLDS = LOCAL / 'worlds' +RUN = LOCAL / 'run' + +IGNORED = { + r'^level\.dat\.Data\.LastPlayed$': 'wall-clock time of the upgrade', +} + + +def java_classpath(): + import oracle_harness as harness + import test_rust_parity as base + import test_model_baking_parity as baking + lock, jars, _ = base.verify_artifacts() + libraries = baking.client_libraries() + build = LOCAL / 'build' + build.mkdir(parents=True, exist_ok=True) + classes, dependencies, _ = harness.compile_oracle(build, 'VanillaDataFixOracle', jars, libraries, + lock['minecraft']['java_major']) + return str(classes) + os.pathsep + dependencies + + +def build_rust(): + subprocess.run(['cargo', 'build', '--locked', '--release', '-p', 'horizon-world', '--example', 'datafix_world'], + cwd=ROOT, check=True) + meta = json.loads(subprocess.check_output(['cargo', 'metadata', '--locked', '--no-deps', '--format-version', '1'], + cwd=ROOT)) + return Path(meta['target_directory']) / 'release' / 'examples' / 'datafix_world' + + +def run_vanilla(classpath, world, out): + shutil.rmtree(out, ignore_errors=True) + log = out.parent / (out.name + '.log') + with log.open('w') as f: + subprocess.run(['java', '-Xmx3g', '-Djava.awt.headless=true', '-cp', classpath, + 'dev.horizonlab.parity.VanillaDataFixOracle', str(world), str(out)], + check=True, stdout=f, stderr=subprocess.STDOUT) + + +def run_horizon(binary, world, out): + shutil.rmtree(out, ignore_errors=True) + log = out.parent / (out.name + '.log') + with log.open('w') as f: + subprocess.run([str(binary), str(world), str(out)], check=True, stdout=f, stderr=subprocess.STDOUT) + + +# ---------------------------------------------------------------- comparison + +def norm(v): + """Python values from anvil_dump, with 26.3 mixed-list wrappers removed.""" + if isinstance(v, dict): + return {k: norm(x) for k, x in v.items()} + if isinstance(v, list): + return [norm(x[''] if isinstance(x, dict) and list(x) == [''] else x) for x in v] + return v + + +def diff(a, b, path, out): + if type(a) is not type(b): + out.append((path, a, b)) + elif isinstance(a, dict): + for k in sorted(set(a) | set(b), key=str): + if k not in a: + out.append((f'{path}.{k}', None, b[k])) + elif k not in b: + out.append((f'{path}.{k}', a[k], None)) + else: + diff(a[k], b[k], f'{path}.{k}', out) + elif isinstance(a, list): + if len(a) != len(b): + out.append((f'{path}[len]', len(a), len(b))) + for i, (x, y) in enumerate(zip(a, b)): + diff(x, y, f'{path}[{i}]', out) + elif a != b: + out.append((path, a, b)) + + +def read_json(path): + return json.loads(path.read_text(encoding='utf-8')) + + +def files(world): + out = set() + for p in world.rglob('*'): + if p.is_file() and p.name not in ('session.lock', 'datafix-source.json') and not p.name.endswith('_old'): + out.add(p.relative_to(world)) + return out + + +def compare(vanilla, horizon): + report = [] + a, b = files(vanilla), files(horizon) + for rel in sorted(a | b, key=str): + name = str(rel) + if rel not in b: + report.append((name, 'present', 'missing from Horizon')) + continue + if rel not in a: + report.append((name, 'missing from vanilla', 'present')) + continue + va, ha = vanilla / rel, horizon / rel + if name.endswith('.mca'): + vc, hc = anvil_dump.region_chunks(str(va)), anvil_dump.region_chunks(str(ha)) + for key in sorted(set(vc) | set(hc)): + label = f'{name}{list(key)}' + if key not in hc: + report.append((label, 'present', 'missing from Horizon')) + elif key not in vc: + report.append((label, 'missing from vanilla', 'present')) + else: + out = [] + diff(norm(vc[key]), norm(hc[key]), 'chunk', out) + report.extend((f'{p} @{label}', x, y) for p, x, y in out) + elif name.endswith('.dat'): + out = [] + diff(norm(anvil_dump.read_dat(str(va))), norm(anvil_dump.read_dat(str(ha))), name, out) + report.extend(out) + elif name.endswith('.json'): + out = [] + diff(read_json(va), read_json(ha), name, out) + report.extend(out) + elif va.read_bytes() != ha.read_bytes(): + report.append((name, 'bytes differ', '')) + return report + + +def pattern_of(path): + return re.sub(r'\[\d+\]', '[]', path.split(' @')[0]) + + +def summarize(report): + groups = {} + for path, a, b in report: + groups.setdefault(pattern_of(path), []).append((path, a, b)) + failures = 0 + for pattern, items in groups.items(): + why = next((w for p, w in IGNORED.items() if re.search(p, pattern)), None) + if why: + print(f' ignored {pattern} x{len(items)} ({why})') + continue + failures += len(items) + path, a, b = items[0] + short = lambda v: (s := json.dumps(v, default=str))[:200] + ('...' if len(s) > 200 else '') + print(f' DIFF {pattern} x{len(items)}\n e.g. {path}\n vanilla {short(a)}\n horizon {short(b)}') + return failures + + +def main(): + p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + p.add_argument('versions', nargs='*') + p.add_argument('--keep', action='store_true') + p.add_argument('--vanilla-only', action='store_true', help='only run the oracle') + p.add_argument('--binary', help='a prebuilt datafix_world example') + args = p.parse_args() + worlds = sorted(p.name for p in WORLDS.iterdir()) if WORLDS.is_dir() else [] + versions = args.versions or worlds + if not versions: + sys.exit('no sample worlds: run tools/datafix_worlds.py first') + classpath = java_classpath() + binary = None if args.vanilla_only else Path(args.binary) if args.binary else build_rust() + total = 0 + for version in versions: + world = WORLDS / version + source = json.loads((world / 'datafix-source.json').read_text()) + print(f'{version} (DataVersion {source["data_version"]})') + RUN.mkdir(parents=True, exist_ok=True) + run_vanilla(classpath, world, RUN / f'{version}-vanilla') + if binary is None: + continue + run_horizon(binary, world, RUN / f'{version}-horizon') + failures = summarize(compare(RUN / f'{version}-vanilla', RUN / f'{version}-horizon')) + print(f' {version}: {"ok" if not failures else f"{failures} differences"}') + total += failures + if binary is not None: + print('datafix parity', 'FAILED' if total else 'ok', f'({total} differences over {len(versions)} worlds)') + if not total and not args.keep: + shutil.rmtree(RUN, ignore_errors=True) + sys.exit(1 if total else 0) + + +if __name__ == '__main__': + main()