From 68092c4c7f1e487aa0cf7b4a048f28cd4feae1d4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 10 Oct 2026 22:24:03 +0200 Subject: [PATCH 01/24] WIP: unfinished saves parity work, stopped for the night Uncommitted changes the agent had in progress when it was stopped; may not build or pass its harnesses yet. Co-Authored-By: Claude Opus 5.5 --- .../horizon-world/examples/datafix_world.rs | 45 + crates/horizon-world/src/datafix/dynamic.rs | 238 ++++ crates/horizon-world/src/datafix/files.rs | 570 +++++++++ crates/horizon-world/src/datafix/fixes_a.rs | 937 ++++++++++++++ crates/horizon-world/src/datafix/fixes_b.rs | 1140 +++++++++++++++++ crates/horizon-world/src/datafix/fixes_c.rs | 694 ++++++++++ crates/horizon-world/src/datafix/items.rs | 1113 ++++++++++++++++ crates/horizon-world/src/datafix/json.rs | 610 +++++++++ crates/horizon-world/src/datafix/mod.rs | 233 ++++ crates/horizon-world/src/datafix/schema.rs | 787 ++++++++++++ crates/horizon-world/src/datafix/tests.rs | 63 + crates/horizon-world/src/datafix/text.rs | 225 ++++ crates/horizon-world/src/lib.rs | 1 + .../parity/VanillaDataFixOracle.java | 246 ++++ tools/datafix_scene.py | 343 +++++ tools/datafix_worlds.py | 325 +++++ tools/test_datafix_parity.py | 214 ++++ 17 files changed, 7784 insertions(+) create mode 100644 crates/horizon-world/examples/datafix_world.rs create mode 100644 crates/horizon-world/src/datafix/dynamic.rs create mode 100644 crates/horizon-world/src/datafix/files.rs create mode 100644 crates/horizon-world/src/datafix/fixes_a.rs create mode 100644 crates/horizon-world/src/datafix/fixes_b.rs create mode 100644 crates/horizon-world/src/datafix/fixes_c.rs create mode 100644 crates/horizon-world/src/datafix/items.rs create mode 100644 crates/horizon-world/src/datafix/json.rs create mode 100644 crates/horizon-world/src/datafix/mod.rs create mode 100644 crates/horizon-world/src/datafix/schema.rs create mode 100644 crates/horizon-world/src/datafix/tests.rs create mode 100644 crates/horizon-world/src/datafix/text.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDataFixOracle.java create mode 100644 tools/datafix_scene.py create mode 100644 tools/datafix_worlds.py create mode 100644 tools/test_datafix_parity.py 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() From f78de6d9aefcfab8163c0b629a863b10aced77bc Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 10 Oct 2026 22:24:03 +0200 Subject: [PATCH 02/24] WIP: unfinished blocks parity work, stopped for the night Uncommitted changes the agent had in progress when it was stopped; may not build or pass its harnesses yet. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 53 +- crates/horizon-client/src/game/boats.rs | 10 + .../src/game/dispense_fixture.rs | 449 +++++++++ crates/horizon-client/src/game/dispenser.rs | 937 +++++++++++++----- crates/horizon-client/src/game/fireworks.rs | 13 +- crates/horizon-client/src/game/potions.rs | 21 + crates/horizon-client/src/game/sculk.rs | 3 + crates/horizon-client/src/items.rs | 59 +- crates/horizon-client/src/mobs.rs | 3 +- crates/horizon-client/src/mobs/ai.rs | 4 + crates/horizon-world/src/level/buckets.rs | 26 +- crates/horizon-world/src/level/fluids.rs | 2 + .../dev/horizonlab/parity/ParityData.java | 3 + .../parity/VanillaBlockUpdateOracle.java | 67 +- .../parity/VanillaDispenserOracle.java | 271 +++++ tools/test_dispenser_parity.py | 303 ++++++ 16 files changed, 1931 insertions(+), 293 deletions(-) create mode 100644 crates/horizon-client/src/game/dispense_fixture.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java create mode 100644 tools/test_dispenser_parity.py diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b81c403fd..c29b221d0 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -19,6 +19,7 @@ mod control; mod creaking; mod creatures; mod crossbow; +mod dispense_fixture; mod dispenser; mod dragon; mod enchant_effects; @@ -1050,8 +1051,9 @@ impl Game<'_> { f64::from(pos[2]) + d(z), ]; let v = [d(x) - 0.5, d(y) - 0.5, d(z) - 0.5]; + // LevelEventHandler (2001) runs on the client: its random, not the level's. let particle = - Particle::terrain(&mut self.world.random, p, v, sprite.clone(), tint); + Particle::terrain(self.particles.random(), p, v, sprite.clone(), tint); self.particles.push(particle); } } @@ -1062,7 +1064,8 @@ impl Game<'_> { let Some((sprite, tint)) = self.particle_sprite(state) else { return; }; - let r = &mut self.world.random; + // Client-side particles draw from the client random, not the level's. + let r = self.particles.random(); let mut p = [0.0; 3]; for i in 0..3 { let base = f64::from(pos[i]); @@ -1089,7 +1092,8 @@ impl Game<'_> { ) { self.next_entity += 1; let p = Vec3::new(position[0], position[1], position[2]); - let bob = self.world.random.next_float() * std::f32::consts::PI * 2.0; + // ItemEntity.bobOffs comes from the entity's own random, not the level's. + let bob = self.particles.random().next_float() * std::f32::consts::PI * 2.0; self.dropped.push(ItemEntity { id: self.next_entity, stack, @@ -1102,23 +1106,36 @@ impl Game<'_> { bob_offset: bob, }); } + /// `Block.getDrops`: the block's loot table, drawing from its server random + /// sequence (`LootContext.Builder.create`), else the level random. + fn block_loot(&mut self, state: BlockStateId, held: Option<&ItemStack>) -> Vec { + let registry = self.res.assets.registry; + match self.res.items.block_loot_sequence(registry, state) { + Some(sequence) => { + let random = self.rand_sequences.get(&sequence, self.level.seed); + self.res.items.block_drops(registry, state, held, random) + } + None => self + .res + .items + .block_drops(registry, state, held, &mut self.world.random), + } + } /// Block.popResource fn pop_resource(&mut self, pos: [i32; 3], mut stack: ItemStack) { self.attach_shulker_contents(pos, &mut stack); self.attach_container_name(pos, &mut stack); self.attach_banner_layers(pos, &mut stack); + // Block.popResource: Mth.nextDouble offsets from the level random; the + // motion is `new ItemEntity(..)`'s own, from the item's random. let r = &mut self.world.random; - let mut offset = || f64::from(r.next_float()) * 0.5 - 0.25; + let mut offset = || -0.25 + r.next_double() * 0.5; let p = [ f64::from(pos[0]) + 0.5 + offset(), f64::from(pos[1]) + 0.5 + offset() - 0.125, f64::from(pos[2]) + 0.5 + offset(), ]; - let v = [ - r.next_double() * 0.2 - 0.1, - 0.2, - r.next_double() * 0.2 - 0.1, - ]; + let v = dispenser::new_item_motion(); self.spawn_item(stack, p, v, 10); } /// Player.drop: thrown from the eyes along the view direction. @@ -1390,6 +1407,7 @@ impl Game<'_> { fn process_level_events(&mut self) -> Result<()> { let events = std::mem::take(&mut self.level_events); self.lan_record_events(&events); + dispense_fixture::record_events(&events); for event in events { match event { LevelEvent::LevelEvent { @@ -1463,12 +1481,7 @@ impl Game<'_> { let name = self.res.facts.get(state).name.clone(); self.release_contents([pos.x, pos.y, pos.z], entity, &name); } - let drops = self.res.items.block_drops( - self.res.assets.registry, - state, - None, - &mut self.world.random, - ); + let drops = self.block_loot(state, None); for d in drops { if &*d.item != "minecraft:air" { self.pop_resource([pos.x, pos.y, pos.z], d); @@ -1777,12 +1790,7 @@ impl Game<'_> { let harvest = !facts.requires_tool || self.res.items.correct_for_drops(held.as_ref(), &facts.name); if harvest { - let drops = self.res.items.block_drops( - self.res.assets.registry, - state, - held.as_ref(), - &mut self.world.random, - ); + let drops = self.block_loot(state, held.as_ref()); let silk = held .as_ref() .is_some_and(|h| h.enchantment_level("minecraft:silk_touch") > 0); @@ -8641,6 +8649,9 @@ pub fn run() -> Result<()> { eprintln!("Horizon: control socket {}", server.path().display()); game.control = Some(server); } + if let Ok(spec) = std::env::var("HORIZON_DISPENSE_FIXTURE") { + return game.dispense_fixture(&spec); + } match game.options.world.clone() { _ if game.options.server.is_some() => { let (address, name) = ( diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index 2f6086e17..1bac6b220 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -95,6 +95,16 @@ impl Boat { } } /// Item id -> (boat entity id, wood). +impl Boat { + /// The vanilla entity type id (`oak_boat`, `bamboo_raft`). + pub(super) fn entity_type(&self) -> String { + if &*self.wood == "bamboo" { + "minecraft:bamboo_raft".to_owned() + } else { + format!("minecraft:{}_boat", self.wood) + } + } +} pub(super) fn boat_wood(item: &str) -> Option<&str> { let name = item.strip_prefix("minecraft:")?; name.strip_suffix("_boat") diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs new file mode 100644 index 000000000..011523f52 --- /dev/null +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -0,0 +1,449 @@ +//! The Rust side of tools/test_dispenser_parity.py: with +//! `HORIZON_DISPENSE_FIXTURE=CASES,OUTPUT` the game loads its resources, runs +//! every case (a dispenser or dropper with items, blocks and target entities +//! around it) through `dispense_from` on a fresh headless world and prints what +//! changed in the format of `VanillaDispenserOracle`. +use super::{ + Game, + dispenser::{Slot, body_in_head, mob_set_slot, mob_slot_item}, +}; +use horizon_client::{ + Result, + chunk::Chunk, + containers::BlockEntity, + gamerules::Value as RuleValue, + item_data::{canon_stack, parse_stack}, + mobs::{Mob, Species}, + projectiles::ProjectileKind, + world::World, +}; +use horizon_core::{ + position::BlockPos, + random::{LegacyRandom, RandomSource}, +}; +use horizon_world::{level::Ticks, registry::BlockStateId}; +use serde_json::Value; +use std::{cell::RefCell, collections::HashMap, fmt::Write as _, io::Write as _, sync::Arc}; + +thread_local! { + /// Level and game events recorded while a fixture case runs. + static LOG: RefCell>> = const { RefCell::new(None) }; +} + +/// Record a line if a fixture case is running. +pub(super) fn record(line: impl FnOnce() -> String) { + LOG.with(|log| { + if let Some(lines) = log.borrow_mut().as_mut() { + lines.push(line()); + } + }); +} +/// `Level.levelEvent` lines for the level events the behaviour library reported. +pub(super) fn record_events(events: &[horizon_world::level::LevelEvent]) { + for event in events { + if let horizon_world::level::LevelEvent::LevelEvent { id, pos, data } = event { + record(|| format!("L {id} {},{},{} {data}", pos.x, pos.y, pos.z)); + } + } +} + +fn num(v: f64) -> String { + format!("{v:.6}") +} +fn floor_pos(p: [f64; 3]) -> String { + format!( + "{},{},{}", + p[0].floor() as i32, + p[1].floor() as i32, + p[2].floor() as i32 + ) +} + +/// The targets a case put in front of the dispenser. +enum Target { + Mob(u32), + Stand(u32), +} + + +impl Game<'_> { + /// Run every case of `spec` ("CASES,OUTPUT") and write the results. + pub(super) fn dispense_fixture(&mut self, spec: &str) -> Result<()> { + let (input, output) = spec.split_once(',').ok_or("HORIZON_DISPENSE_FIXTURE=CASES,OUTPUT")?; + // Saplings and feature placers grow from the live feature registry. + self.install_live_features(); + let text = std::fs::read_to_string(input)?; + let mut out = std::io::BufWriter::new(std::fs::File::create(output)?); + for (index, line) in text.lines().enumerate() { + let case: Value = serde_json::from_str(line)?; + writeln!(out, "case {index}")?; + match self.dispense_case(&case) { + Ok(lines) => { + for l in lines { + writeln!(out, "{l}")?; + } + } + Err(e) => writeln!(out, "failed {e}")?, + } + } + LOG.with(|log| *log.borrow_mut() = None); + Ok(()) + } + + fn fixture_reset(&mut self, seed: i64) { + self.world = World::new(self.res.facts.air, seed); + for cx in -2..=2 { + for cz in -2..=2 { + self.world + .chunks + .insert((cx, cz), Arc::new(Chunk::empty(cx, cz))); + } + } + self.block_ticks = Ticks::new(0); + self.tick_chunks.clear(); + self.sync_tick_chunks(); + self.mobs.clear(); + self.dropped.clear(); + self.creatures.projectiles.clear(); + self.tnts.clear(); + self.boats.clear(); + self.carts.clear(); + self.armor_stands.clear(); + self.rockets.clear(); + self.potions.clear_thrown(); + self.level_events.clear(); + self.orbs.clear(); + self.falling.clear(); + self.ui.rules = Default::default(); + // The oracle level has world seed 0 and fresh random sequences. + self.level.seed = 0; + self.rand_sequences = Default::default(); + } + + fn dispense_case(&mut self, case: &Value) -> Result> { + let seed = case["seed"].as_i64().unwrap_or(0); + self.fixture_reset(seed); + self.world.game_time = case["time"].as_i64().unwrap_or(0); + // The oracle level's clock stands at day time 6000. + self.world.day_time = 6000; + if let Some(rules) = case["rules"].as_object() { + for (name, value) in rules { + if let Some(b) = value.as_bool() { + self.ui.rules.set(name, RuleValue::Bool(b)); + } + } + } + let mut before: HashMap<(i32, i32, i32), u32> = HashMap::new(); + for cell in case["cells"].as_array().into_iter().flatten() { + let p = ( + cell[0].as_i64().unwrap_or(0) as i32, + cell[1].as_i64().unwrap_or(0) as i32, + cell[2].as_i64().unwrap_or(0) as i32, + ); + let state = BlockStateId(cell[3].as_u64().unwrap_or(0) as u32); + self.world.set(p.0, p.1, p.2, state)?; + before.insert(p, state.0); + if let Some(entity) = BlockEntity::for_block(&self.res.facts.get(state).name) { + self.world.block_entities.insert(p, entity); + } + } + for item in case["items"].as_array().into_iter().flatten() { + let p = ( + item[0].as_i64().unwrap_or(0) as i32, + item[1].as_i64().unwrap_or(0) as i32, + item[2].as_i64().unwrap_or(0) as i32, + ); + let slot = item[3].as_u64().unwrap_or(0) as usize; + let stack = parse_stack(item[4].as_str().unwrap_or("-"))?; + if let Some(cell) = self + .world + .block_entities + .get_mut(&p) + .and_then(|e| e.slots_mut().get_mut(slot)) + { + *cell = stack; + } + } + let mut targets = Vec::new(); + for e in case["entities"].as_array().into_iter().flatten() { + targets.push(self.fixture_target(e)?); + } + // Creating the targets must not move the stream the dispenser draws from. + self.world.random = LegacyRandom::new(seed); + self.world.block_log = Some(Vec::new()); + self.audio.capture = Some(Vec::new()); + LOG.with(|log| *log.borrow_mut() = Some(Vec::new())); + let old_mobs: Vec = self.mobs.iter().map(|m| m.id).collect(); + let old_stands: Vec = self.armor_stands.iter().map(|s| s.id).collect(); + + let pos = &case["pos"]; + let origin = BlockPos::new( + pos[0].as_i64().unwrap_or(0) as i32, + pos[1].as_i64().unwrap_or(0) as i32, + pos[2].as_i64().unwrap_or(0) as i32, + ); + let state = self.world.block(origin.x, origin.y, origin.z); + self.dispense_from(origin, state)?; + + let mut out = Vec::new(); + let mut changed: Vec<(i32, i32, i32)> = self.world.block_log.take().unwrap_or_default(); + changed.sort_unstable(); + changed.dedup(); + for p in changed { + let now = self.world.block(p.0, p.1, p.2).0; + if before.get(&p).copied().unwrap_or(self.res.facts.air.0) != now { + out.push(format!("set {},{},{} {now}", p.0, p.1, p.2)); + } + } + let mut containers: Vec<_> = self.world.block_entities.keys().copied().collect(); + containers.sort_unstable(); + for p in containers { + let entity = &self.world.block_entities[&p]; + for (i, slot) in entity.slots().iter().enumerate() { + if let Some(stack) = slot { + out.push(format!( + "inv {},{},{} {i} {}", + p.0, + p.1, + p.2, + canon_stack(Some(stack)) + )); + } + } + } + self.fixture_entities(&old_mobs, &old_stands, &mut out); + for (i, t) in targets.iter().enumerate() { + out.push(self.fixture_describe(i, t)); + } + let log = LOG.with(|log| log.borrow_mut().take()).unwrap_or_default(); + out.extend(log); + for (sound, at, _, _) in self.audio.capture.take().unwrap_or_default() { + out.push(format!("S {} {sound}", floor_pos(at))); + } + out.push(format!("rng {}", self.world.random.next_long())); + Ok(out) + } + + /// `EntityType.create` and `snapTo` for one target: no spawn finalization. + fn fixture_target(&mut self, e: &Value) -> Result { + let kind = e["type"].as_str().unwrap_or("minecraft:pig"); + let p = &e["pos"]; + let at = horizon_core::geometry::Vec3::new( + p[0].as_f64().unwrap_or(0.0), + p[1].as_f64().unwrap_or(0.0), + p[2].as_f64().unwrap_or(0.0), + ); + let yaw = e["yaw"].as_f64().unwrap_or(0.0) as f32; + self.next_entity += 1; + let id = self.next_entity; + if kind == "minecraft:armor_stand" { + self.armor_stands + .push(horizon_client::entities::ArmorStand::new(id, at, yaw)); + return Ok(Target::Stand(id)); + } + let t = self + .res + .mob_types + .get(kind) + .cloned() + .ok_or_else(|| format!("no mob type {kind}"))?; + let mut mob = Mob::new(id, t, at, yaw); + // EntityType.create equips nothing (that is finalizeSpawn's job). + for slot in [ + &mut mob.extra.main_hand, + &mut mob.extra.off_hand, + &mut mob.extra.head, + &mut mob.extra.chest, + &mut mob.extra.legs, + &mut mob.extra.feet, + ] { + *slot = None; + } + let s = mob.species(); + let flag = |name: &str| e[name].as_bool().unwrap_or(false); + let ageable = !matches!( + s, + Species::Zombie + | Species::Skeleton + | Species::Bogged + | Species::SnowGolem + ); + if flag("baby") && ageable { + mob.extra.age = -24000; + } + let tamable = body_in_head(s) + || matches!(s, Species::Cat | Species::Parrot | Species::Ocelot); + if flag("tame") && tamable { + mob.extra.tame = true; + } + if flag("sheared") && matches!(s, Species::Sheep | Species::Bogged | Species::SnowGolem) { + mob.extra.sheared = true; + } + if flag("chest") + && matches!( + s, + Species::Donkey | Species::Mule | Species::Llama | Species::TraderLlama + ) + { + mob.extra.chested = true; + } + if flag("loot") { + mob.extra.pick_up_loot = true; + } + if flag("brown") && s == Species::Mooshroom { + mob.extra.variant = 1; + } + for (slot, snbt) in e["equip"].as_object().into_iter().flatten() { + if let (Some(slot), Some(stack)) = ( + Slot::from_name(slot), + parse_stack(snbt.as_str().unwrap_or("-"))?, + ) { + mob_set_slot(&mut mob, slot, stack); + } + } + self.mobs.push(mob); + Ok(Target::Mob(id)) + } + + /// `T` lines: removal, equipment by `EquipmentSlot`, shearing readiness, chests. + fn fixture_describe(&self, index: usize, target: &Target) -> String { + let mut line = format!("T {index} "); + match *target { + Target::Stand(id) => { + line.push_str("minecraft:armor_stand"); + let Some(stand) = self.armor_stands.iter().find(|s| s.id == id) else { + line.push_str(" removed"); + return line; + }; + for (slot, name) in [ + (0, "mainhand"), + (1, "offhand"), + (2, "feet"), + (3, "legs"), + (4, "chest"), + (5, "head"), + ] { + if let Some(s) = &stand.equipment[slot] { + let _ = write!(line, " {name}={}x{}", s.item, s.count); + } + } + } + Target::Mob(id) => { + let Some(m) = self.mobs.iter().find(|m| m.id == id) else { + return line + "removed"; + }; + line.push_str(&m.kind.id); + if m.removed || !m.alive() { + line.push_str(" removed"); + } + let s = m.species(); + let x = &m.extra; + for (slot, name) in [ + (Slot::MainHand, "mainhand"), + (Slot::OffHand, "offhand"), + (Slot::Feet, "feet"), + (Slot::Legs, "legs"), + (Slot::Chest, "chest"), + (Slot::Head, "head"), + (Slot::Body, "body"), + (Slot::Saddle, "saddle"), + ] { + if let Some(item) = mob_slot_item(m, slot) { + let _ = write!(line, " {name}={item}x1"); + } + } + let ready = match s { + Species::Sheep => Some(!x.sheared && !m.is_baby()), + Species::Mooshroom => Some(!m.is_baby()), + Species::Bogged => Some(!x.sheared), + Species::SnowGolem => Some(!x.sheared), + _ => None, + }; + if let Some(r) = ready { + line.push_str(if r { " ready" } else { " unready" }); + } + if x.chested && matches!(s, Species::Donkey | Species::Mule | Species::Llama | Species::TraderLlama) { + line.push_str(" chest"); + } + } + } + line + } + + /// `N` lines for every entity the dispenser added. + fn fixture_entities(&self, old_mobs: &[u32], old_stands: &[u32], out: &mut Vec) { + let line = |kind: &str, p: [f64; 3], v: [f64; 3], extra: &str| { + let mut l = format!( + "N {kind} {} {} {} {} {} {}", + num(p[0]), + num(p[1]), + num(p[2]), + num(v[0]), + num(v[1]), + num(v[2]) + ); + if !extra.is_empty() { + l.push(' '); + l.push_str(extra); + } + l + }; + for m in self.mobs.iter().filter(|m| !old_mobs.contains(&m.id)) { + let (p, v) = (m.position, m.velocity); + out.push(line(&m.kind.id, [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + } + for s in self.armor_stands.iter().filter(|s| !old_stands.contains(&s.id)) { + let (p, v) = (s.position, s.velocity); + out.push(line("minecraft:armor_stand", [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + } + for d in &self.dropped { + let (p, v) = (d.position, d.velocity); + out.push(line( + "minecraft:item", + [p.x, p.y, p.z], + [v.x, v.y, v.z], + &canon_stack(Some(&d.stack)), + )); + } + for shot in &self.creatures.projectiles { + let kind = match &shot.kind { + ProjectileKind::Arrow { + effect: Some(("minecraft:glowing", 200)), + .. + } => "minecraft:spectral_arrow", + ProjectileKind::Arrow { .. } => "minecraft:arrow", + ProjectileKind::Snowball => "minecraft:snowball", + ProjectileKind::Egg => "minecraft:egg", + ProjectileKind::SmallFireball => "minecraft:small_fireball", + ProjectileKind::WindCharge => "minecraft:wind_charge", + _ => "minecraft:unknown_projectile", + }; + let (p, v) = (shot.position, shot.velocity); + out.push(line(kind, [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + } + for (kind, p, v) in self.potions.thrown_entities() { + out.push(line(kind, [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + } + for (p, v) in self.rockets.iter().map(|r| r.motion()) { + out.push(line("minecraft:firework_rocket", [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + } + for t in &self.tnts { + let (p, v) = (t.position, t.velocity); + out.push(line( + "minecraft:tnt", + [p.x, p.y, p.z], + [v.x, v.y, v.z], + &format!("fuse={}", t.fuse), + )); + } + for b in &self.boats { + let kind = b.entity_type(); + let (p, v) = (b.position, b.velocity); + out.push(line(&kind, [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + } + for c in &self.carts { + let (p, v) = (c.position, c.velocity); + out.push(line(c.kind.id(), [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + } + } +} diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index f8754bbb1..6389ec791 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -9,20 +9,21 @@ use horizon_client::{ collisions::SceneCollisions, containers::BlockEntity, items::ItemStack, - mobs::{MobEvent, SpawnSetup, Species}, + mobs::{Mob, Species, sulfur_cube}, particles::{ParticleOptions, ParticleType}, projectiles::{Projectile, ProjectileKind}, }; use horizon_core::{ geometry::{Aabb, Direction, Vec3}, position::BlockPos, - random::RandomSource, + random::{LegacyRandom, RandomSource}, }; use horizon_world::{ fluid::FluidKind, level::{BucketContent, PlaceContext, PlaceOutcome}, registry::BlockStateId, }; +use std::sync::Arc; type Pos = (i32, i32, i32); @@ -89,13 +90,221 @@ fn shrink(mut stack: ItemStack) -> Option { /// A dispenser's answer: the slot's new content and the level event played. type Dispensed = (Option, i32); +thread_local! { + /// `OptionalDispenseItemBehavior.success` of the registered behaviours + /// that leave it unset on some paths (brush, TNT, honeycomb, chest). + /// Vanilla keeps one behaviour instance per item for the whole run, so + /// there the flag carries over from that item's previous dispense. + static SUCCESS: std::cell::RefCell> = + std::cell::RefCell::new(std::collections::HashMap::new()); +} +fn set_success(behaviour: &'static str, success: bool) { + SUCCESS.with(|s| s.borrow_mut().insert(behaviour, success)); +} +/// The level event `OptionalDispenseItemBehavior.playSound` plays for the flag. +fn optional_event(behaviour: &'static str) -> i32 { + if SUCCESS.with(|s| s.borrow().get(behaviour).copied().unwrap_or(true)) { + CLICK + } else { + FAIL + } +} + /// Entities `getEntitiesOfClass(.., AABB(pos))` can find. +#[derive(Clone, Copy)] enum Target { Player, Mob(usize), Stand(usize), } +/// `RandomSource.create()`: the unseeded random an entity owns +/// (`RandomSupport.generateUniqueSeed`). Draws of an entity's own random never +/// touch the level's stream. +pub(super) fn entity_random() -> LegacyRandom { + use std::sync::atomic::{AtomicI64, Ordering}; + static UNIQUIFIER: AtomicI64 = AtomicI64::new(8_682_522_807_148_012); + const STEP: i64 = 1_181_783_497_276_652_981; + // seedUniquifier.updateAndGet(s -> s * STEP) + let mut current = UNIQUIFIER.load(Ordering::Relaxed); + let seed = loop { + let next = current.wrapping_mul(STEP); + match UNIQUIFIER.compare_exchange_weak(current, next, Ordering::Relaxed, Ordering::Relaxed) { + Ok(_) => break next, + Err(actual) => current = actual, + } + }; + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_nanos() as i64); + LegacyRandom::new(seed ^ nanos) +} +/// `new ItemEntity(level, x, y, z, stack)`'s motion, from the item's own random. +pub(super) fn new_item_motion() -> [f64; 3] { + let mut r = entity_random(); + let x = r.next_double() * 0.2 - 0.1; + let z = r.next_double() * 0.2 - 0.1; + [x, 0.2, z] +} + +/// `EquipmentSlot`. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(super) enum Slot { + MainHand, + OffHand, + Feet, + Legs, + Chest, + Head, + Body, + Saddle, +} +impl Slot { + pub(super) fn from_name(name: &str) -> Option { + Some(match name { + "mainhand" => Self::MainHand, + "offhand" => Self::OffHand, + "feet" => Self::Feet, + "legs" => Self::Legs, + "chest" => Self::Chest, + "head" => Self::Head, + "body" => Self::Body, + "saddle" => Self::Saddle, + _ => return None, + }) + } + /// The player's armour array index (head, chest, legs, feet). + fn armor_index(self) -> Option { + match self { + Self::Head => Some(0), + Self::Chest => Some(1), + Self::Legs => Some(2), + Self::Feet => Some(3), + _ => None, + } + } + /// `ArmorStand` equipment index (mainhand, offhand, feet, legs, chest, head). + fn stand_index(self) -> Option { + match self { + Self::MainHand => Some(0), + Self::OffHand => Some(1), + Self::Feet => Some(2), + Self::Legs => Some(3), + Self::Chest => Some(4), + Self::Head => Some(5), + _ => None, + } + } +} + +fn equine(s: Species) -> bool { + matches!( + s, + Species::Horse + | Species::Donkey + | Species::Mule + | Species::SkeletonHorse + | Species::ZombieHorse + | Species::Camel + | Species::CamelHusk + | Species::Llama + | Species::TraderLlama + ) +} +/// Mobs whose body slot Horizon keeps in `extra.head` (no helmet slot is drawn). +pub(super) fn body_in_head(s: Species) -> bool { + equine(s) + || matches!(s, Species::Wolf | Species::HappyGhast | Species::SulfurCube) + || horizon_client::mobs::nautilus::is_nautilus(s) +} +/// What a mob holds in a slot. +pub(super) fn mob_slot_item(m: &Mob, slot: Slot) -> Option> { + let x = &m.extra; + let body = body_in_head(m.species()); + match slot { + Slot::MainHand => x.main_hand.clone(), + Slot::OffHand => x.off_hand.clone(), + Slot::Feet => x.feet.clone(), + Slot::Legs => x.legs.clone(), + Slot::Chest => x.chest.clone(), + Slot::Head => x.head.clone().filter(|_| !body), + Slot::Body => x.head.clone().filter(|_| body), + Slot::Saddle => m.saddle_item().map(|s| s.item), + } +} +/// `LivingEntity.canUseSlot` and its overrides. +fn mob_can_use_slot(m: &Mob, slot: Slot) -> bool { + let s = m.species(); + let grown = m.alive() && !m.is_baby(); + match s { + Species::Pig | Species::Strider if slot == Slot::Saddle => grown, + Species::Horse + | Species::SkeletonHorse + | Species::ZombieHorse + | Species::Llama + | Species::TraderLlama => true, + _ if equine(s) && slot == Slot::Saddle => grown && m.extra.tame, + _ if horizon_client::mobs::nautilus::is_nautilus(s) + && matches!(slot, Slot::Saddle | Slot::Body) => + { + grown && m.extra.tame + } + Species::SulfurCube | Species::HappyGhast if slot == Slot::Body => grown, + _ => true, + } +} +/// `Mob.canDispenserEquipIntoSlot` and its overrides. +fn mob_can_dispenser_equip(m: &Mob, slot: Slot) -> bool { + let s = m.species(); + let loot = m.extra.pick_up_loot; + match s { + Species::Pig | Species::Strider => slot == Slot::Saddle || loot, + _ if equine(s) => matches!(slot, Slot::Body | Slot::Saddle) && m.extra.tame || loot, + _ if horizon_client::mobs::nautilus::is_nautilus(s) => { + matches!(slot, Slot::Body | Slot::Saddle) || loot + } + Species::SulfurCube | Species::HappyGhast => slot == Slot::Body, + Species::Dolphin | Species::Panda | Species::Fox => slot == Slot::MainHand && loot, + Species::Allay => false, + _ => loot, + } +} +/// `getEquipSound`: the saddle overrides of pigs, striders, horses, camels and nautili. +fn equip_sound(m: &Mob, slot: Slot, default: Option) -> Option { + if slot != Slot::Saddle { + return default; + } + let s = m.species(); + Some(match s { + Species::Pig => "minecraft:entity.pig.saddle".into(), + Species::Strider => "minecraft:entity.strider.saddle".into(), + Species::Camel => "minecraft:entity.camel.saddle".into(), + Species::CamelHusk => "minecraft:entity.camel_husk.saddle".into(), + _ if equine(s) => "minecraft:entity.horse.saddle".into(), + _ if horizon_client::mobs::nautilus::is_nautilus(s) => { + if m.fluids.eye_in_water { + "minecraft:entity.nautilus.saddle_underwater_equip".into() + } else { + "minecraft:entity.nautilus.saddle_equip".into() + } + } + _ => return default, + }) +} +pub(super) fn mob_set_slot(m: &mut Mob, slot: Slot, stack: ItemStack) { + let item: Arc = stack.item.clone(); + let x = &mut m.extra; + match slot { + Slot::MainHand => x.main_hand = Some(item), + Slot::OffHand => x.off_hand = Some(item), + Slot::Feet => x.feet = Some(item), + Slot::Legs => x.legs = Some(item), + Slot::Chest => x.chest = Some(item), + Slot::Head | Slot::Body => x.head = Some(item), + Slot::Saddle => m.set_saddle(Some(stack)), + } +} + impl Game<'_> { fn prop(&self, state: BlockStateId, key: &str) -> Option { self.res @@ -149,6 +358,7 @@ impl Game<'_> { /// The dispenser level events: 1000 click, 1001 fail, 1002 launch, 1004 /// firework shoot, 1018 blaze shoot and 2000 smoke shot out of the front. pub(super) fn dispenser_event(&mut self, id: i32, pos: Pos, data: i32) { + super::dispense_fixture::record(|| format!("L {id} {},{},{} {data}", pos.0, pos.1, pos.2)); let c = Self::center(pos); match id { 1000 => self @@ -163,12 +373,19 @@ impl Game<'_> { 1004 => self .audio .play("minecraft:entity.firework_rocket.shoot", Some(c), 1.0, 1.2), + // LevelEventHandler runs on the client: its pitch draws come from + // the client level's random, never the server stream. 1018 => { - let r = &mut self.world.random; + let r = self.particles.random(); let pitch = (r.next_float() - r.next_float()) * 0.2 + 1.0; self.audio .play("minecraft:entity.blaze.shoot", Some(c), 2.0, pitch); } + 1051 => { + let pitch = 0.4 / (self.particles.random().next_float() * 0.4 + 0.8); + self.audio + .play("minecraft:entity.wind_charge.throw", Some(c), 0.5, pitch); + } 2000 => { let s = match data { 0 => [0, -1, 0], @@ -237,8 +454,6 @@ impl Game<'_> { let mut at = [p.x, p.y, p.z]; at[1] -= if s[1] != 0 { 0.125 } else { 0.156_25 }; let r = &mut self.world.random; - // new ItemEntity(level, x, y, z, stack): its default motion draws first. - let _ = (r.next_double(), r.next_double()); let pow = r.next_double() * 0.1 + 0.2; let spread = 0.017_227_5 * f64::from(accuracy); let v = [ @@ -361,7 +576,8 @@ impl Game<'_> { | "minecraft:splash_potion" | "minecraft:lingering_potion" | "minecraft:firework_rocket" - | "minecraft:fire_charge" => self.dispense_projectile(pos, facing, stack), + | "minecraft:fire_charge" + | "minecraft:wind_charge" => self.dispense_projectile(pos, facing, stack), "minecraft:armor_stand" => self.dispense_armor_stand(pos, facing, stack), "minecraft:chest" => self.dispense_chest(pos, facing, stack), "minecraft:lava_bucket" @@ -372,7 +588,8 @@ impl Game<'_> { | "minecraft:pufferfish_bucket" | "minecraft:tropical_fish_bucket" | "minecraft:axolotl_bucket" - | "minecraft:tadpole_bucket" => self.dispense_filled_bucket(pos, facing, stack)?, + | "minecraft:tadpole_bucket" + | "minecraft:sulfur_cube_bucket" => self.dispense_filled_bucket(pos, facing, stack)?, "minecraft:bucket" => self.dispense_empty_bucket(pos, facing, stack)?, "minecraft:flint_and_steel" => self.dispense_flint_and_steel(pos, facing, stack)?, "minecraft:bone_meal" => self.dispense_bone_meal(pos, facing, stack)?, @@ -387,10 +604,15 @@ impl Game<'_> { "minecraft:potion" => self.dispense_potion(pos, facing, stack)?, _ if item.ends_with("shulker_box") => self.dispense_shulker_box(pos, facing, stack)?, _ if super::boats::boat_wood(&item).is_some() => self.dispense_boat(pos, facing, stack), + // BoatDispenseItemBehavior for chest boats: Horizon has no chest boat + // entity, so they always take its fallback (an inner default dispense). + _ if item.ends_with("_chest_boat") || item.ends_with("_chest_raft") => { + self.dispense_boat(pos, facing, stack) + } _ if super::minecarts::CartKind::from_id(&item).is_some() => { self.dispense_minecart(pos, facing, stack) } - _ => self.dispense_default_method(pos, facing, stack), + _ => self.dispense_default_method(pos, facing, stack)?, }; self.dispenser_event(event, pos, 0); self.dispenser_event(2000, pos, data_value(facing)); @@ -402,7 +624,7 @@ impl Game<'_> { pos: Pos, facing: Direction, stack: ItemStack, - ) -> Dispensed { + ) -> Result { let equippable = self .res .items @@ -411,35 +633,51 @@ impl Game<'_> { if equippable { let mut stack = stack; if self.dispense_equipment(pos, facing, &mut stack) { - return (Some(stack).filter(|s| s.count > 0), CLICK); + return Ok((Some(stack).filter(|s| s.count > 0), CLICK)); } - return (self.default_execute(pos, facing, stack), CLICK); + return Ok((self.default_execute(pos, facing, stack), CLICK)); + } + let swallowable = self + .res + .items + .item_tags + .get("minecraft:sulfur_cube_swallowable") + .is_some_and(|t| t.contains(&*stack.item)); + if swallowable { + let front = offset(pos, facing); + if self.sulfur_cube_swallow(front, &stack) { + return Ok((shrink(stack), CLICK)); + } + return Ok((self.default_execute(pos, facing, stack), CLICK)); } if stack.item.ends_with("_spawn_egg") { return self.dispense_spawn_egg(pos, facing, stack); } - (self.default_execute(pos, facing, stack), CLICK) + Ok((self.default_execute(pos, facing, stack), CLICK)) } // ---- entities ---- - /// Entities whose box overlaps the block at `p`. + /// Entities whose box overlaps the block at `p`, in the order the level + /// added them (an entity section keeps insertion order; ids grow with it). fn targets_at(&self, p: Pos) -> Vec { let block = block_box(p); - let mut out: Vec = self + let mut found: Vec<(u32, Target)> = self .mobs .iter() .enumerate() - .filter(|(_, m)| m.alive() && overlaps(m.bounds(), block)) - .map(|(i, _)| Target::Mob(i)) + .filter(|(_, m)| !m.removed && overlaps(m.bounds(), block)) + .map(|(i, m)| (m.id, Target::Mob(i))) .collect(); - out.extend( + found.extend( self.armor_stands .iter() .enumerate() .filter(|(_, s)| overlaps(s.bounds(), block)) - .map(|(i, _)| Target::Stand(i)), + .map(|(i, s)| (s.id, Target::Stand(i))), ); + found.sort_by_key(|(id, _)| *id); + let mut out: Vec = found.into_iter().map(|(_, t)| t).collect(); if let Some(v) = self.player_view() && v.alive && !self.spectator() @@ -487,135 +725,127 @@ impl Game<'_> { ]; self.spawn_mob(&name, at, true) } - /// `EquipmentDispenseItemBehavior.dispenseEquipment`: the first entity in - /// front that can wear the item gets one of it. - fn dispense_equipment(&mut self, pos: Pos, _facing: Direction, stack: &mut ItemStack) -> bool { + /// `EquipmentDispenseItemBehavior.dispenseEquipment`: the first living + /// entity in front that `canEquipWithDispenser` gets one of the item. + fn dispense_equipment(&mut self, pos: Pos, facing: Direction, stack: &mut ItemStack) -> bool { let Some(info) = self.res.items.get(&stack.item) else { return false; }; - let (Some(slot), true) = (info.equip_slot.clone(), info.equip_dispensable) else { + let (Some(slot), true) = ( + info.equip_slot.as_deref().and_then(Slot::from_name), + info.equip_dispensable, + ) else { return false; }; let allowed = info.equip_allowed.clone(); - let sound = info.equip_sound.clone(); - let front = offset(pos, _facing); + // Equippable.equipSound defaults to the generic armour sound. + let sound = info + .equip_sound + .clone() + .or_else(|| Some("minecraft:item.armor.equip_generic".into())); + let front = offset(pos, facing); let one = ItemStack { count: 1, ..stack.clone() }; - let armor_index = match slot.as_str() { - "head" => Some(0), - "chest" => Some(1), - "legs" => Some(2), - "feet" => Some(3), - _ => None, + let allows = |id: &str| allowed.as_ref().is_none_or(|a| a.contains(id)); + // LivingEntity.canEquipWithDispenser: alive, the slot usable, the + // item's allowed entities, the slot empty and the dispenser rule. + let target = self.targets_at(front).into_iter().find(|t| match *t { + Target::Player => slot.armor_index().is_some_and(|i| { + allows("minecraft:player") && self.armor[i].is_none() + }), + Target::Stand(i) => slot.stand_index().is_some_and(|s| { + allows("minecraft:armor_stand") && self.armor_stands[i].equipment[s].is_none() + }), + Target::Mob(i) => { + let m = &self.mobs[i]; + m.alive() + && mob_can_use_slot(m, slot) + && allows(&m.kind.id) + && mob_slot_item(m, slot).is_none() + && mob_can_dispenser_equip(m, slot) + } + }); + let Some(target) = target else { + return false; }; - let id_allowed = |id: &str| allowed.as_ref().is_none_or(|a| a.contains(id)); - let item: std::sync::Arc = stack.item.clone(); - for target in self.targets_at(front) { - let at = match target { - Target::Player => { - // The player wears armour only. - let Some(i) = armor_index else { continue }; - if !id_allowed("minecraft:player") || self.armor[i].is_some() { - continue; - } - self.armor[i] = Some(one.clone()); - self.apply_equipment(); - self.player_view().map(|v| v.position) + stack.count -= 1; + // setItemSlot: onEquipItem plays the equip sound and the equip event. + let (at, sound) = match target { + Target::Player => { + if let Some(i) = slot.armor_index() { + self.armor[i] = Some(one); } - Target::Stand(i) => { - let Some(ai) = armor_index else { continue }; - if !id_allowed("minecraft:armor_stand") { - continue; - } - // ArmorStand equipment: mainhand, offhand, feet, legs, chest, head. - let index = 5 - ai; - let stand = &mut self.armor_stands[i]; - if stand.equipment[index].is_some() { - continue; - } - stand.equipment[index] = Some(one.clone()); - Some(stand.position) + self.apply_equipment(); + (self.player_view().map(|v| v.position), sound) + } + Target::Stand(i) => { + let stand = &mut self.armor_stands[i]; + if let Some(s) = slot.stand_index() { + stand.equipment[s] = Some(one); } - Target::Mob(i) => { - let m = &mut self.mobs[i]; - if !id_allowed(&m.kind.id) { - continue; - } - let species = m.species(); - let tamed = m.extra.tame; - let equine = matches!( - species, - Species::Horse - | Species::Donkey - | Species::Mule - | Species::SkeletonHorse - | Species::ZombieHorse - | Species::Camel - | Species::CamelHusk - ); - // AbstractNautilus.canDispenserEquipIntoSlot: saddle and - // body on a tame adult. - let nautilus = horizon_client::mobs::nautilus::is_nautilus(species) - && tamed - && !m.is_baby(); - let slot_free = match (slot.as_str(), armor_index) { - (_, Some(0)) => m.extra.head.is_none(), - (_, Some(1)) => m.extra.chest.is_none(), - (_, Some(2)) => m.extra.legs.is_none(), - (_, Some(3)) => m.extra.feet.is_none(), - ("saddle", _) => !m.extra.saddled, - ("body", _) => { - (species == Species::Horse - || nautilus && item.ends_with("_nautilus_armor")) - && m.extra.head.is_none() - } - _ => false, - }; - // Mob.canDispenserEquipIntoSlot and the overrides. - let saddle_ok = slot == "saddle" - && (matches!(species, Species::Pig | Species::Strider) - || equine && tamed - || nautilus); - let body_ok = slot == "body" && (equine && tamed || nautilus); - if !slot_free || !(m.extra.pick_up_loot || saddle_ok || body_ok) { - continue; - } - if species == Species::Strider && slot == "saddle" && m.is_baby() { - continue; - } - match (slot.as_str(), armor_index) { - (_, Some(0)) => m.extra.head = Some(item.clone()), - (_, Some(1)) => m.extra.chest = Some(item.clone()), - (_, Some(2)) => m.extra.legs = Some(item.clone()), - (_, Some(3)) => m.extra.feet = Some(item.clone()), - ("saddle", _) => m.set_saddle(Some(one.clone())), - _ => m.extra.head = Some(item.clone()), - } - // setGuaranteedDrop / setPersistenceRequired. - m.persistent = true; - Some(m.position) - } - }; - if let (Some(sound), Some(at)) = (sound, at) { + (Some(stand.position), sound) + } + Target::Mob(i) => { + let m = &mut self.mobs[i]; + mob_set_slot(m, slot, one); + // setGuaranteedDrop / setPersistenceRequired. + m.persistent = true; + let sound = if m.silent { None } else { equip_sound(m, slot, sound) }; + (Some(m.position), sound) + } + }; + if let Some(at) = at { + if let Some(sound) = sound { self.audio.play(&sound, Some([at.x, at.y, at.z]), 1.0, 1.0); } - stack.count -= 1; - return true; + let who = match target { + Target::Player => Some(horizon_client::mobs::Who::Player), + Target::Mob(i) => Some(horizon_client::mobs::Who::Mob(self.mobs[i].id)), + Target::Stand(_) => None, + }; + let ctx = self.who_context(who, None); + self.post_game_event("minecraft:equip", [at.x, at.y, at.z], ctx); } - false + true } - fn dispense_spawn_egg(&mut self, pos: Pos, facing: Direction, stack: ItemStack) -> Dispensed { + /// After `EntityType.spawn` / `MobBucketItem.spawn`: `mob.playAmbientSound()`. + fn mob_ambient(&mut self, id: u32) -> Result<()> { + let Some(i) = self.mobs.iter().position(|m| m.id == id) else { + return Ok(()); + }; + let mut m = self.mobs.remove(i); + let mut events = Vec::new(); + { + let w = self.mob_world_base(&[]); + horizon_client::mobs::ai::ambient_sound(&mut m, &w, &mut events); + } + self.mobs.insert(i, m); + self.handle_mob_events(events) + } + /// A mob a bucket emptied: `setFromBucket` keeps it, then it calls out. + fn mob_placed(&mut self, id: u32) -> Result<()> { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { + m.persistent = true; + } + self.mob_ambient(id) + } + fn dispense_spawn_egg( + &mut self, + pos: Pos, + facing: Direction, + stack: ItemStack, + ) -> Result { let kind = stack.item.trim_end_matches("_spawn_egg").to_owned(); let front = offset(pos, facing); - if self - .spawn_mob_at(&kind, front, facing != Direction::Up) - .is_none() - { - return (Some(stack), CLICK); - } - (shrink(stack), CLICK) + let Some(id) = self.spawn_mob_at(&kind, front, facing != Direction::Up) else { + return Ok((Some(stack), CLICK)); + }; + self.mob_ambient(id)?; + // SpawnEggItemBehavior: the entity was placed by the dispenser. + self.post_block_game_event("minecraft:entity_place", [pos.0, pos.1, pos.2], None, None); + Ok((shrink(stack), CLICK)) } // ---- ProjectileDispenseBehavior ---- @@ -638,6 +868,12 @@ impl Game<'_> { 1.0, 1018, ), + "minecraft:wind_charge" => ( + Self::dispense_position(pos, facing, 1.0, [0.0; 3]), + 6.666_666_5, + 1.0, + 1051, + ), "minecraft:experience_bottle" | "minecraft:splash_potion" | "minecraft:lingering_potion" => ( @@ -657,17 +893,19 @@ impl Game<'_> { count: 1, ..stack.clone() }; + // Projectile.shoot spreads the shot with the projectile's own random. + let mut own = entity_random(); match item.as_str() { "minecraft:experience_bottle" | "minecraft:splash_potion" | "minecraft:lingering_potion" => { let mut shot = Projectile::new(ProjectileKind::Snowball, at, None); - shot.shoot(&mut self.world.random, dir, power, uncertainty); + shot.shoot(&mut own, dir, power, uncertainty); self.launch_thrown(one, at, shot.velocity); } "minecraft:firework_rocket" => { let mut shot = Projectile::new(ProjectileKind::Snowball, at, None); - shot.shoot(&mut self.world.random, dir, power, uncertainty); + shot.shoot(&mut own, dir, power, uncertainty); self.launch_dispensed_rocket(at, shot.velocity, &one); } _ => { @@ -698,15 +936,20 @@ impl Game<'_> { .map(|(id, duration, _)| (*id, (duration / 8).max(1))) }), }, - "minecraft:fire_charge" => { - // The fireball's own aim is drawn first, then shot along the facing. + "minecraft:fire_charge" | "minecraft:wind_charge" => { + // asProjectile aims with the level's random first; the + // shot along the facing then replaces that motion. let r = &mut self.world.random; let _ = ( r.triangle(dir[0], 0.114_85), r.triangle(dir[1], 0.114_85), r.triangle(dir[2], 0.114_85), ); - ProjectileKind::SmallFireball + if item == "minecraft:wind_charge" { + ProjectileKind::WindCharge + } else { + ProjectileKind::SmallFireball + } } "minecraft:egg" | "minecraft:blue_egg" | "minecraft:brown_egg" => { ProjectileKind::Egg @@ -714,7 +957,7 @@ impl Game<'_> { _ => ProjectileKind::Snowball, }; let mut shot = Projectile::new(kind, at, None); - shot.shoot(&mut self.world.random, dir, power, uncertainty); + shot.shoot(&mut own, dir, power, uncertainty); self.creatures.projectiles.push(shot); } } @@ -730,6 +973,8 @@ impl Game<'_> { f64::from(front.1), f64::from(front.2) + 0.5, ); + // EntityType.create rolls a yaw from the level random before the stack config turns it. + let _ = self.world.random.next_float(); self.next_entity += 1; let stand = horizon_client::entities::ArmorStand::new(self.next_entity, foot, y_rot(facing)); @@ -746,14 +991,16 @@ impl Game<'_> { if matches!( m.species(), Species::Donkey | Species::Mule | Species::Llama | Species::TraderLlama - ) && m.extra.tame + ) && m.alive() + && m.extra.tame && !m.extra.chested { m.extra.chested = true; + set_success("chest", true); return (shrink(stack), CLICK); } } - (self.default_execute(pos, facing, stack), CLICK) + (self.default_execute(pos, facing, stack), optional_event("chest")) } /// `BoatDispenseItemBehavior`. fn dispense_boat(&mut self, pos: Pos, facing: Direction, stack: ItemStack) -> Dispensed { @@ -860,17 +1107,22 @@ impl Game<'_> { None, ), "minecraft:axolotl_bucket" => ( - BucketContent::Fluid(FluidKind::Water), + BucketContent::MobWater, "minecraft:item.bucket.empty_axolotl", Some("axolotl"), ), "minecraft:tadpole_bucket" => ( - BucketContent::Fluid(FluidKind::Water), + BucketContent::MobWater, "minecraft:item.bucket.empty_tadpole", Some("tadpole"), ), + "minecraft:sulfur_cube_bucket" => ( + BucketContent::Nothing, + "minecraft:item.bucket.empty_sulfur_cube", + Some("sulfur_cube"), + ), other => ( - BucketContent::Fluid(FluidKind::Water), + BucketContent::MobWater, "minecraft:item.bucket.empty_fish", Some(match other { "minecraft:salmon_bucket" => "salmon", @@ -888,7 +1140,10 @@ impl Game<'_> { return Ok((self.dispense_default(pos, facing, stack), CLICK)); } if self.dim == super::Dim::Nether - && matches!(content, BucketContent::Fluid(FluidKind::Water)) + && matches!( + content, + BucketContent::Fluid(FluidKind::Water) | BucketContent::MobWater + ) { // The water evaporates in a cloud of smoke. let c = Self::center(target); @@ -906,9 +1161,14 @@ impl Game<'_> { [0.0; 3], ); } - } else if let Some(kind) = mob { - // MobBucketItem.checkExtraContent. - self.spawn_mob_at(kind, target, true); + } + if let Some(kind) = mob { + // MobBucketItem.checkExtraContent: the mob (from the bucket, so + // persistent) calls out, then ENTITY_PLACE. + if let Some(id) = self.spawn_mob_at(kind, target, true) { + self.mob_placed(id)?; + } + self.post_block_game_event("minecraft:entity_place", [target.0, target.1, target.2], None, None); } let bucket = ItemStack::new("minecraft:bucket", 1); Ok(( @@ -928,6 +1188,7 @@ impl Game<'_> { self.process_level_events()?; match picked { Some(filled) => { + self.post_block_game_event("minecraft:fluid_pickup", [target.x, target.y, target.z], None, None); let filled = ItemStack::new(filled, 1); Ok(( self.consume_with_remainder(pos, facing, stack, filled), @@ -951,6 +1212,32 @@ impl Game<'_> { let state = self.block_at(target); let name = self.dispenser_block_name(target); let mut success = true; + // tryIgniteExplosiveEntities: the first sulfur cube that can explode is primed. + let cube = self.targets_at(target).into_iter().find_map(|t| match t { + Target::Mob(i) => { + let m = &self.mobs[i]; + (m.species() == Species::SulfurCube + && m.alive() + && !sulfur_cube::primed(m) + && sulfur_cube::archetype(m).is_some_and(|a| a.explosion.is_some())) + .then_some(i) + } + _ => None, + }); + if let Some(i) = cube { + // primeTime(false): the fuse is lit only while TNT explodes; the + // ignition counts either way. + if self.ui.rules.bool("tnt_explodes") && sulfur_cube::prime(&mut self.mobs[i], false) { + let m = &mut self.mobs[i]; + let (at, id) = (m.position, m.id); + let mut events = Vec::new(); + m.voice("entity.tnt.primed", &mut events); + self.handle_mob_events(events)?; + let ctx = self.who_context(Some(horizon_client::mobs::Who::Mob(id)), None); + self.post_game_event("minecraft:prime_fuse", [at.x, at.y, at.z], ctx); + } + return Ok((self.hurt_and_break(stack, 1), CLICK)); + } if self.res.facts.is_air(state) { // BaseFireBlock.canBePlacedAt: a fire that survives, or a portal. let below = self.dispenser_block_name((target.0, target.1 - 1, target.2)); @@ -1018,10 +1305,29 @@ impl Game<'_> { } fn dispense_tnt(&mut self, pos: Pos, facing: Direction, stack: ItemStack) -> Dispensed { if !self.ui.rules.bool("tnt_explodes") { + set_success("tnt", false); return (Some(stack), FAIL); } - let target = Self::pos_of(offset(pos, facing)); - self.spawn_primed_tnt(target, false); + let front = offset(pos, facing); + if self.sulfur_cube_swallow(front, &stack) { + return (shrink(stack), optional_event("tnt")); + } + set_success("tnt", true); + // new PrimedTnt(level, x + 0.5, y, z + 0.5, null), its sound and ENTITY_PLACE. + let target = Self::pos_of(front); + let rot = self.world.random.next_double() * f64::from(std::f32::consts::TAU); + self.add_primed_tnt(target, rot, 80); + self.audio.play( + "minecraft:entity.tnt.primed", + Some([ + f64::from(target.x) + 0.5, + f64::from(target.y), + f64::from(target.z) + 0.5, + ]), + 1.0, + 1.0, + ); + self.post_block_game_event("minecraft:entity_place", [front.0, front.1, front.2], None, None); (shrink(stack), CLICK) } @@ -1169,13 +1475,6 @@ impl Game<'_> { self.fill_placed_box([target.0, target.1, target.2], &stack); self.name_placed_container([target.0, target.1, target.2], &stack); } - let sounds = self.res.facts.get(state).sounds.clone(); - self.audio.play( - &sounds.place, - Some(Self::center(target)), - (sounds.volume + 1.0) / 2.0, - sounds.pitch * 0.8, - ); self.process_level_events()?; Ok((shrink(stack), CLICK)) } @@ -1203,6 +1502,7 @@ impl Game<'_> { self.level_set([target.0, target.1, target.2], reset)?; } let bottle = ItemStack::new("minecraft:honey_bottle", 1); + self.post_block_game_event("minecraft:fluid_pickup", [pos.0, pos.1, pos.2], None, None); return Ok(( self.consume_with_remainder(pos, facing, stack, bottle), CLICK, @@ -1210,6 +1510,7 @@ impl Game<'_> { } if self.water_at(target) { let water = ItemStack::new("minecraft:potion", 1).with_potion("minecraft:water"); + self.post_block_game_event("minecraft:fluid_pickup", [pos.0, pos.1, pos.2], None, None); return Ok(( self.consume_with_remainder(pos, facing, stack, water), CLICK, @@ -1276,128 +1577,260 @@ impl Game<'_> { 1.0, 1.0, ); - // BeehiveBlock.dropHoneycomb, then the honey level resets. - self.pop_resource( - [target.0, target.1, target.2], - ItemStack::new("minecraft:honeycomb", 3), - ); - if let Some(reset) = self.with_props(state, &[("honey_level", "0")]) { - self.level_set([target.0, target.1, target.2], reset)?; + // BeehiveBlock.dropHoneycomb (harvest/beehive), then + // releaseBeesAndResetHoneyLevel and the shear event. + let p = [target.0, target.1, target.2]; + for stack in self.sequence_loot("minecraft:harvest/beehive", "minecraft:harvest/beehive", None) { + self.pop_resource(p, stack); } + if let Some(reset) = self.with_props(state, &[("honey_level", "0")]) { + self.level_set(p, reset)?; + } + self.post_block_game_event("minecraft:shear", p, None, None); Ok(true) } - /// `Shearable.shear` on the first ready entity in the block. + /// `LootTable.getRandomItems` of `table` in the context of `parent`: the + /// draws come from `parent`'s server random sequence when it names one (a + /// nested `loot_table` entry shares its parent's context), else the level's. + pub(super) fn sequence_loot( + &mut self, + parent: &str, + table: &str, + held: Option<&ItemStack>, + ) -> Vec { + match self.res.items.loot_sequence(parent) { + Some(sequence) => { + let random = self.rand_sequences.get(&sequence, self.level.seed); + self.res.items.loot_table_from(table, false, held, random) + } + None => self + .res + .items + .loot_table(table, false, held, &mut self.world.random), + } + } + /// `Entity.spawnAtLocation(level, stack, offset)`: an item at the entity's + /// feet plus the offset, moving as `new ItemEntity` makes it. + fn spawn_at_location(&mut self, at: Vec3, y_offset: f64, stack: ItemStack) -> [f64; 3] { + let v = new_item_motion(); + self.spawn_item(stack, [at.x, at.y + y_offset, at.z], v, 10); + v + } + /// `Mob.convertTo(type, ConversionParams.single(this, false, false))`: a + /// new mob of `kind` in the same place (no spawn finalization, so no + /// random draws) keeps the name, flags, persistence and age; the old one + /// is discarded. + pub(super) fn convert_mob(&mut self, index: usize, kind: &str) -> Option { + let t = self.res.mob_types.get(kind).cloned()?; + let old = &self.mobs[index]; + self.next_entity += 1; + let mut mob = Mob::new(self.next_entity, t, old.position, old.yaw); + mob.previous = old.previous; + mob.head_yaw = old.head_yaw; + mob.body_yaw = old.body_yaw; + mob.head_pitch = old.head_pitch; + mob.persistent = old.persistent; + mob.custom_name = old.custom_name.clone(); + mob.name_visible = old.name_visible; + mob.no_ai = old.no_ai; + mob.silent = old.silent; + mob.no_gravity = old.no_gravity; + mob.invulnerable_flag = old.invulnerable_flag; + mob.extra.age = if old.is_baby() { old.extra.age } else { 0 }; + for slot in [ + &mut mob.extra.main_hand, + &mut mob.extra.off_hand, + &mut mob.extra.head, + &mut mob.extra.chest, + &mut mob.extra.legs, + &mut mob.extra.feet, + ] { + *slot = None; + } + let id = mob.id; + self.mobs[index].removed = true; + self.mobs.push(mob); + Some(id) + } + /// `ShearsDispenseItemBehavior.tryShearEntity`: the first live, ready + /// `Shearable` in the block is sheared. fn shear_entity(&mut self, target: Pos) -> Result { + let shears = ItemStack::new("minecraft:shears", 1); for t in self.targets_at(target) { let Target::Mob(i) = t else { continue }; let m = &self.mobs[i]; - let at = [m.position.x, m.position.y, m.position.z]; + if !m.alive() { + continue; + } + let at = m.position; + let sound_at = Some([at.x, at.y, at.z]); match m.species() { Species::Sheep if !m.extra.sheared && !m.is_baby() => { - let color = - horizon_client::mobs::render::DYE_NAMES[m.extra.color as usize & 15]; - self.mobs[i].extra.sheared = true; + let color = horizon_client::mobs::render::DYE_NAMES[m.extra.color as usize & 15]; self.audio - .play("minecraft:entity.sheep.shear", Some(at), 1.0, 1.0); - let stacks = self.res.items.loot_table( + .play("minecraft:entity.sheep.shear", sound_at, 1.0, 1.0); + let drops = self.sequence_loot( + "minecraft:shearing/sheep", &format!("minecraft:shearing/sheep/{color}"), - false, - None, - &mut self.world.random, + Some(&shears), ); - self.drop_sheared(at, stacks); - return Ok(true); + for stack in drops { + for _ in 0..stack.count { + let one = ItemStack { + count: 1, + ..stack.clone() + }; + self.spawn_at_location(at, 1.0, one); + // Sheep.shear nudges each item with the sheep's own random. + let r = &mut self.mobs[i].random; + let push = [ + f64::from((r.next_float() - r.next_float()) * 0.1), + f64::from(r.next_float() * 0.05), + f64::from((r.next_float() - r.next_float()) * 0.1), + ]; + if let Some(item) = self.dropped.last_mut() { + item.velocity = Vec3::new( + item.velocity.x + push[0], + item.velocity.y + push[1], + item.velocity.z + push[2], + ); + } + } + } + self.mobs[i].extra.sheared = true; } Species::Bogged if !m.extra.sheared => { - self.mobs[i].extra.sheared = true; + let height = f64::from(m.height()); self.audio - .play("minecraft:entity.bogged.shear", Some(at), 1.0, 1.0); - let stacks = self.res.items.loot_table( + .play("minecraft:entity.bogged.shear", sound_at, 1.0, 1.0); + let drops = self.sequence_loot( "minecraft:shearing/bogged", - false, - None, - &mut self.world.random, + "minecraft:shearing/bogged", + Some(&shears), ); - self.drop_sheared(at, stacks); - return Ok(true); + for stack in drops { + self.spawn_at_location(at, height, stack); + } + self.mobs[i].extra.sheared = true; + } + Species::SnowGolem if !m.extra.sheared => { + let eye = m.eye_height(); + self.audio + .play("minecraft:entity.snow_golem.shear", sound_at, 1.0, 1.0); + self.mobs[i].extra.sheared = true; + let drops = self.sequence_loot( + "minecraft:shearing/snow_golem", + "minecraft:shearing/snow_golem", + Some(&shears), + ); + for stack in drops { + self.spawn_at_location(at, eye, stack); + } } Species::Mooshroom if !m.is_baby() => { - let mushroom = if m.extra.variant == 1 { - "minecraft:brown_mushroom" - } else { - "minecraft:red_mushroom" - }; - let setup = SpawnSetup { - yaw: m.yaw, - persistent: true, - ..Default::default() - }; - self.mobs[i].removed = true; + let variant = if m.extra.variant == 1 { "brown" } else { "red" }; + let height = f64::from(m.height()); self.audio - .play("minecraft:entity.mooshroom.shear", Some(at), 1.0, 1.0); - self.handle_mob_events(vec![MobEvent::SpawnKind { - kind: "minecraft:cow".into(), - at, - setup, - }])?; - for _ in 0..5 { - let v = [0.0, 0.2, 0.0]; - self.spawn_item( - ItemStack::new(mushroom, 1), - [at[0], at[1] + 1.0, at[2]], - v, - 10, - ); + .play("minecraft:entity.mooshroom.shear", sound_at, 1.0, 1.0); + // convertTo(COW): the cow takes its place, then the mushrooms drop. + self.convert_mob(i, "minecraft:cow"); + let drops = self.sequence_loot( + "minecraft:shearing/mooshroom", + &format!("minecraft:shearing/mooshroom/{variant}"), + Some(&shears), + ); + for stack in drops { + for _ in 0..stack.count { + let one = ItemStack { + count: 1, + ..stack.clone() + }; + self.spawn_at_location(at, height, one); + } } - return Ok(true); } - _ => {} + _ => continue, } + self.post_block_game_event("minecraft:shear", [target.0, target.1, target.2], None, None); + return Ok(true); } Ok(false) } - /// The drops of a sheared mob, one entity per item, a block above its feet. - fn drop_sheared(&mut self, at: [f64; 3], stacks: Vec) { - for stack in stacks { - for _ in 0..stack.count { - let r = &mut self.world.random; - let v = [ - f64::from((r.next_float() - r.next_float()) * 0.1), - f64::from(r.next_float() * 0.05), - f64::from((r.next_float() - r.next_float()) * 0.1), - ]; - let one = ItemStack { - count: 1, - ..stack.clone() - }; - self.spawn_item(one, [at[0], at[1] + 1.0, at[2]], v, 10); - } - } - } - /// A brush used on an adult armadillo in front. + /// The brush behaviour: the first armadillo in front that `brushOffScute`s. fn dispense_brush(&mut self, pos: Pos, facing: Direction, stack: ItemStack) -> Dispensed { let target = offset(pos, facing); - let armadillo = self.targets_at(target).into_iter().find_map(|t| match t { - Target::Mob(i) if self.mobs[i].species() == Species::Armadillo => Some(i), - _ => None, - }); - let Some(i) = armadillo.filter(|&i| !self.mobs[i].is_baby()) else { - return (Some(stack), FAIL); - }; - let m = &self.mobs[i]; - let at = [m.position.x, m.position.y, m.position.z]; - let drops = self.res.items.loot_table( - "minecraft:brush/armadillo", - false, - None, - &mut self.world.random, - ); - for d in drops { - self.spawn_item(d, [at[0], at[1] + 0.5, at[2]], [0.0, 0.2, 0.0], 10); + let armadillos: Vec = self + .targets_at(target) + .into_iter() + .filter_map(|t| match t { + Target::Mob(i) if self.mobs[i].species() == Species::Armadillo => Some(i), + _ => None, + }) + .collect(); + for i in armadillos { + if self.mobs[i].is_baby() { + continue; + } + let at = self.mobs[i].position; + let drops = self.sequence_loot( + "minecraft:brush/armadillo", + "minecraft:brush/armadillo", + Some(&stack), + ); + for d in drops { + self.spawn_at_location(at, 0.0, d); + } + self.audio.play( + "minecraft:entity.armadillo.brush", + Some([at.x, at.y, at.z]), + 1.0, + 1.0, + ); + let who = horizon_client::mobs::Who::Mob(self.mobs[i].id); + let ctx = self.who_context(Some(who), None); + self.post_game_event("minecraft:entity_interact", [at.x, at.y, at.z], ctx); + return (self.hurt_and_break(stack, 16), optional_event("brush")); } - self.audio - .play("minecraft:entity.armadillo.brush", Some(at), 1.0, 1.0); - (self.hurt_and_break(stack, 16), CLICK) + set_success("brush", false); + (Some(stack), FAIL) + } + /// `SulfurCubeBlockDispenseItemBehavior.dispenseBlock`: the first sulfur + /// cube in the block that `equipItem`s the stack swallows one. + fn sulfur_cube_swallow(&mut self, target: Pos, stack: &ItemStack) -> bool { + let cubes: Vec = self + .targets_at(target) + .into_iter() + .filter_map(|t| match t { + Target::Mob(i) if self.mobs[i].species() == Species::SulfurCube => Some(i), + _ => None, + }) + .collect(); + for i in cubes { + let m = &self.mobs[i]; + if m.is_baby() || m.extra.head.as_deref() == Some(&*stack.item) { + continue; + } + let (at, height) = (m.position, f64::from(m.height())); + // The swallowed block comes out on top (the passenger attachment). + if let Some(old) = self.mobs[i].extra.head.take() { + self.spawn_at_location(at, height, ItemStack::new(&old, 1)); + } + let m = &mut self.mobs[i]; + m.extra.head = Some(stack.item.clone()); + m.persistent = true; + let (at, id) = (m.position, m.id); + // setItemSlot: a body item without `equippable` posts UNEQUIP. + let ctx = self.who_context(Some(horizon_client::mobs::Who::Mob(id)), None); + self.post_game_event("minecraft:unequip", [at.x, at.y, at.z], ctx); + self.audio.play( + "minecraft:entity.sulfur_cube.absorb", + Some([at.x, at.y, at.z]), + 1.0, + 1.0, + ); + return true; + } + false } fn dispense_honeycomb( &mut self, @@ -1422,9 +1855,17 @@ impl Game<'_> { let Some(waxed) = waxed.filter(|_| name.contains("copper") || name.contains("lightning_rod")) else { - return Ok((self.default_execute(pos, facing, stack), CLICK)); + return Ok((self.default_execute(pos, facing, stack), optional_event("honeycomb"))); }; + set_success("honeycomb", true); self.level_set([target.0, target.1, target.2], waxed)?; + self.level_events + .push(horizon_world::level::LevelEvent::LevelEvent { + id: 3003, + pos: Self::pos_of(target), + data: 0, + }); + self.process_level_events()?; self.audio.play( "minecraft:item.honeycomb.wax_on", Some(Self::center(target)), @@ -1447,8 +1888,9 @@ impl Game<'_> { // events play on top. return Ok((self.dispense_default(pos, facing, stack), CLICK)); } + // The server picks the splash positions with the level random. for _ in 0..5 { - let r = self.particles.random(); + let r = &mut self.world.random; let p = [ f64::from(pos.0) + r.next_double(), f64::from(pos.1 + 1), @@ -1463,6 +1905,7 @@ impl Game<'_> { 1.0, 1.0, ); + self.post_block_game_event("minecraft:fluid_place", [pos.0, pos.1, pos.2], None, None); if let Some(mud) = self.res.assets.registry.default_state("minecraft:mud") { self.level_set([target.0, target.1, target.2], mud)?; } diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 4f0c3fa9c..80b72ad26 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -29,6 +29,12 @@ pub(super) struct Rocket { angled: bool, icon: Option, } +impl Rocket { + /// Position and motion (the dispenser parity fixture). + pub(super) fn motion(&self) -> (Vec3, Vec3) { + (self.position, self.velocity) + } +} fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { // The item's default component: flight 1, no explosions. stack @@ -39,10 +45,9 @@ fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { impl Game<'_> { pub(super) fn new_rocket(&mut self, at: Vec3, stack: &ItemStack, attached: bool) -> Rocket { let (flight, explosions) = fireworks_of(stack); - let r = &mut self.world.random; - let tri = |r: &mut horizon_core::random::LegacyRandom| { - 0.002297 * (f64::from(r.next_float()) - f64::from(r.next_float())) - }; + // FireworkRocketEntity: motion and lifetime come from the rocket's own random. + let r = &mut super::dispenser::entity_random(); + let tri = |r: &mut horizon_core::random::LegacyRandom| r.triangle(0.0, 0.002297); let velocity = Vec3::new(tri(r), 0.05, tri(r)); let lifetime = 10 * (1 + i32::from(flight)) + r.next_int_bound(6).unwrap_or(0) diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index fd5e30b34..f0c5f57b1 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -65,6 +65,27 @@ pub(super) struct PotionState { std::collections::HashMap, } +impl PotionState { + /// Thrown potions in flight as (entity type, position, motion), for the + /// dispenser parity fixture. + pub(super) fn thrown_entities(&self) -> Vec<(&'static str, Vec3, Vec3)> { + self.thrown + .iter() + .map(|t| { + let kind = match &*t.stack.item { + "minecraft:lingering_potion" => "minecraft:lingering_potion", + "minecraft:experience_bottle" => "minecraft:experience_bottle", + _ => "minecraft:splash_potion", + }; + (kind, t.position, t.velocity) + }) + .collect() + } + pub(super) fn clear_thrown(&mut self) { + self.thrown.clear(); + } +} + impl Game<'_> { /// Right-click with a potion, splash/lingering potion or glass bottle. pub(super) fn use_potion_item(&mut self, stack: &ItemStack) -> horizon_client::Result { diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 8602802c4..65245eaab 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -437,6 +437,9 @@ impl Game<'_> { /// `Level.gameEvent`: tell the listeners within reach. pub(super) fn post_game_event(&mut self, event: &'static str, pos: [f64; 3], ctx: Context) { + super::dispense_fixture::record(|| { + format!("G {},{},{} {event}", pos[0].floor() as i32, pos[1].floor() as i32, pos[2].floor() as i32) + }); if self.remote.is_some() || !self.sculk.tables.built { return; } diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index e0c5a0366..4912b5408 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -526,13 +526,23 @@ impl Items { pub fn has_loot_table(&self, id: &str) -> bool { self.loot.contains_key(id) } + /// The server random sequence the block's loot table draws from. + pub fn block_loot_sequence(&self, registry: &BlockRegistry, state: BlockStateId) -> Option { + let block = registry.state(state)?; + let name = block.name.trim_start_matches("minecraft:"); + let standing = name.replacen("_wall_", "_", 1); + let standing = standing.strip_prefix("wall_").unwrap_or(&standing); + let standing = standing.replacen("wall_", "", 1); + self.loot_sequence(&format!("minecraft:blocks/{name}")) + .or_else(|| self.loot_sequence(&format!("minecraft:blocks/{standing}"))) + } /// Evaluate the block's loot table for a player break without explosions. pub fn block_drops( &self, registry: &BlockRegistry, state: BlockStateId, held: Option<&ItemStack>, - random: &mut LegacyRandom, + random: &mut impl RandomSource, ) -> Vec { let Some(block) = registry.state(state) else { return vec![]; @@ -581,6 +591,34 @@ impl Items { }; ctx.evaluate(table, random) } + /// The server random sequence a loot table draws from (`random_sequence`): + /// vanilla's `LootContext.Builder.create` uses it instead of the level random. + pub fn loot_sequence(&self, id: &str) -> Option { + self.loot.get(id)?["random_sequence"] + .as_str() + .map(str::to_owned) + } + /// `loot_table` drawing from any random (a random sequence). + pub fn loot_table_from( + &self, + id: &str, + killed_by_player: bool, + held: Option<&ItemStack>, + random: &mut impl RandomSource, + ) -> Vec { + let Some(table) = self.loot.get(id) else { + return vec![]; + }; + let empty = BTreeMap::new(); + let ctx = LootContext { + items: self, + block: "", + properties: &empty, + held, + killed_by_player, + }; + ctx.evaluate(table, random) + } pub fn entity_drops( &self, entity: &str, @@ -604,14 +642,15 @@ impl Items { } } impl LootContext<'_> { - fn evaluate(&self, table: &Value, random: &mut LegacyRandom) -> Vec { + fn evaluate(&self, table: &Value, random: &mut impl RandomSource) -> Vec { let ctx = self; let mut out = Vec::new(); for pool in table["pools"].as_array().into_iter().flatten() { if !ctx.conditions(pool, random) { continue; } - let rolls = ctx.number(&pool["rolls"], random).max(0.0) as u32; + // LootPool: rolls.getInt (Mth.randomBetweenInclusive for uniform rolls). + let rolls = ctx.int_number(&pool["rolls"], random).max(0) as u32; for _ in 0..rolls { let mut produced = Vec::new(); ctx.choose( @@ -643,7 +682,7 @@ impl LootContext<'_> { fn enchantment_level(&self, id: &str) -> u32 { self.held.map_or(0, |h| h.enchantment_level(id)) } - fn number(&self, v: &Value, random: &mut LegacyRandom) -> f32 { + fn number(&self, v: &Value, random: &mut impl RandomSource) -> f32 { match v { Value::Number(n) => n.as_f64().unwrap_or(0.0) as f32, Value::Object(o) => match o @@ -674,7 +713,7 @@ impl LootContext<'_> { _ => 1.0, } } - fn int_number(&self, v: &Value, random: &mut LegacyRandom) -> i32 { + fn int_number(&self, v: &Value, random: &mut impl RandomSource) -> i32 { if let Value::Object(o) = v && o.get("type") .and_then(Value::as_str) @@ -691,7 +730,7 @@ impl LootContext<'_> { } self.number(v, random).round() as i32 } - fn condition(&self, c: &Value, random: &mut LegacyRandom) -> bool { + fn condition(&self, c: &Value, random: &mut impl RandomSource) -> bool { let (kind, c) = match c { Value::String(s) => (s.as_str(), &Value::Null), Value::Object(o) => (o.get("type").and_then(Value::as_str).unwrap_or(""), c), @@ -800,7 +839,7 @@ impl LootContext<'_> { _ => true, } } - fn conditions(&self, o: &Value, random: &mut LegacyRandom) -> bool { + fn conditions(&self, o: &Value, random: &mut impl RandomSource) -> bool { if let Some(c) = o.get("condition") && !self.condition(c, random) { @@ -816,7 +855,7 @@ impl LootContext<'_> { fn expand( &self, e: &Value, - random: &mut LegacyRandom, + random: &mut impl RandomSource, out: &mut Vec<(i32, ItemStack, Value)>, ) -> bool { if !self.conditions(e, random) { @@ -874,7 +913,7 @@ impl LootContext<'_> { _ => false, } } - fn choose(&self, entries: &[Value], random: &mut LegacyRandom, produced: &mut Vec) { + fn choose(&self, entries: &[Value], random: &mut impl RandomSource, produced: &mut Vec) { let mut candidates = Vec::new(); for e in entries { self.expand(e, random, &mut candidates); @@ -901,7 +940,7 @@ impl LootContext<'_> { self.modifiers(&modifiers, &mut stack, random); produced.push(stack); } - fn modifiers(&self, m: &Value, stack: &mut ItemStack, random: &mut LegacyRandom) { + fn modifiers(&self, m: &Value, stack: &mut ItemStack, random: &mut impl RandomSource) { let list: Vec<&Value> = match m { Value::Array(a) => a.iter().collect(), Value::Object(_) => vec![m], diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index f46420cad..7e7d2843e 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1162,7 +1162,8 @@ impl Mob { pub fn voice(&mut self, event: &'static str, events: &mut Vec) { let pitch = (self.random.next_float() - self.random.next_float()) * 0.2 + if self.is_baby() { 1.5 } else { 1.0 }; - let at = [self.position.x, self.eye_y(), self.position.z]; + // Entity.playSound: at the entity's feet. + let at = [self.position.x, self.position.y, self.position.z]; events.push(MobEvent::Sound(event, at, ai::sound_volume(self), pitch)); } } diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index d27024f31..41033acfc 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -414,6 +414,10 @@ fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) { // Entity.baseTick ends with Leashable.tickLeash. super::leash::tick(m, w, events); } +/// `Mob.playAmbientSound` on demand: a spawn egg or a mob bucket just placed `m`. +pub fn ambient_sound(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + play_ambient(m, w, events); +} fn play_ambient(m: &mut Mob, w: &MobWorld, events: &mut Vec) { let sound = match m.species() { // Warden.getAmbientSound: by anger, silent while roaring or digging. diff --git a/crates/horizon-world/src/level/buckets.rs b/crates/horizon-world/src/level/buckets.rs index 8fe4907a9..5bffd2e57 100644 --- a/crates/horizon-world/src/level/buckets.rs +++ b/crates/horizon-world/src/level/buckets.rs @@ -19,6 +19,10 @@ pub enum BucketContent { Fluid(FluidKind), /// `SolidBucketItem`: the block placed into an empty space (powder snow). Powder, + /// `MobBucketItem` holding water: its empty sound posts no `FLUID_PLACE`. + MobWater, + /// `MobBucketItem` without a fluid (the sulfur cube bucket): only the sound. + Nothing, } impl Sim<'_, L> { @@ -58,14 +62,23 @@ impl Sim<'_, L> { if self.is_in_valid_bounds(pos) && b.is_air(self.state(pos)) { let snow = b.table.default_state(b.named.powder_snow); self.set_block_and_update(pos, snow); + // SolidBucketItem.emptyContents: one FLUID_PLACE, then the place sound. self.game_event(pos, "minecraft:fluid_place"); - self.empty_sound(pos, sound); + let sound: Arc = sound.into(); + self.sound(pos, &sound, 1.0, 1.0); return true; } return false; } + BucketContent::Nothing => { + let sound: Arc = sound.into(); + self.sound(pos, &sound, 1.0, 1.0); + return true; + } + BucketContent::MobWater => FluidKind::Water, BucketContent::Fluid(fluid) => fluid, }; + let mob = content == BucketContent::MobWater; let state = self.state(pos); // canBeReplaced(fluid): replaceable or not solid (portals refuse). let may_replace = !b.instance_of(state, Class::EndPortal) @@ -91,7 +104,7 @@ impl Sim<'_, L> { } if liquid_container && water { self.place_liquid(pos, state, FluidState::new(FluidKind::Water, 8, false)); - self.empty_sound(pos, sound); + self.empty_sound(pos, sound, mob); return true; } if may_replace && !b.instance_of(state, Class::Liquid) { @@ -112,13 +125,16 @@ impl Sim<'_, L> { { return false; } - self.empty_sound(pos, sound); + self.empty_sound(pos, sound, mob); true } - fn empty_sound(&mut self, pos: BlockPos, sound: &str) { + /// `playEmptySound`: `MobBucketItem` overrides it without the game event. + fn empty_sound(&mut self, pos: BlockPos, sound: &str, mob: bool) { let sound: Arc = sound.into(); self.sound(pos, &sound, 1.0, 1.0); - self.game_event(pos, "minecraft:fluid_place"); + if !mob { + self.game_event(pos, "minecraft:fluid_place"); + } } } diff --git a/crates/horizon-world/src/level/fluids.rs b/crates/horizon-world/src/level/fluids.rs index b778f6511..47558e52d 100644 --- a/crates/horizon-world/src/level/fluids.rs +++ b/crates/horizon-world/src/level/fluids.rs @@ -265,7 +265,9 @@ impl Sim<'_, L> { return false; } if b.is_block(state, b.named.powder_snow) { + // PowderSnowBlock.pickupBlock: the block breaks with its particles and sound. self.set_block(pos, b.air(), 11); + self.level_event(2001, pos, state.0 as i32); return true; } false diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/ParityData.java b/mods/horizon/src/test/java/dev/horizonlab/parity/ParityData.java index 09d75a7d8..34cb8d05e 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/ParityData.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/ParityData.java @@ -30,6 +30,8 @@ public final class ParityData { public static RegistryAccess access; public static RegistryOps ops; public static ServerLevel level; + /** The loot tables of the vanilla data pack. */ + public static ReloadableServerRegistries.Holder loot; public static void init() throws Exception { SharedConstants.tryDetectVersion(); @@ -44,6 +46,7 @@ public final class ParityData { RegistryAccess.Frozen world = RegistryDataLoader.load(resources, context, RegistryDataLoader.WORLD_REGISTRIES, Runnable::run).join(); var worldLayers = layers.replaceFrom(RegistryLayer.WORLD, world); var loaded = ReloadableServerRegistries.reload(worldLayers, staticTags, resources, Runnable::run).join(); + loot = new ReloadableServerRegistries.Holder(loaded.lookupWithUpdatedTags()); lookup = loaded.lookupWithUpdatedTags(); var components = BuiltInRegistries.DATA_COMPONENT_INITIALIZERS.build(lookup); staticTags.forEach(Registry.PendingTags::apply); diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java index 046b2d399..21908b3e2 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java @@ -91,6 +91,12 @@ public final class VanillaBlockUpdateOracle { return old; } @Override + public void markUnsaved() { } + @Override + public BlockState getBlockState(BlockPos pos) { return world.getBlockState(pos); } + @Override + public FluidState getFluidState(BlockPos pos) { return world.getFluidState(pos); } + @Override public net.minecraft.world.level.chunk.status.ChunkStatus getPersistedStatus() { return net.minecraft.world.level.chunk.status.ChunkStatus.FULL; } @@ -145,6 +151,23 @@ public final class VanillaBlockUpdateOracle { } @Override public LevelChunk getChunkAt(BlockPos p) { return chunk; } @Override public LevelChunk getChunk(int x, int z) { return chunk; } + @Override public net.minecraft.world.level.chunk.ChunkAccess getChunk(int x, int z, net.minecraft.world.level.chunk.status.ChunkStatus status, boolean load) { return chunk; } + net.minecraft.server.waypoints.ServerWaypointManager waypoints; + @Override public net.minecraft.server.waypoints.ServerWaypointManager getWaypointManager() { + if (waypoints == null) waypoints = new net.minecraft.server.waypoints.ServerWaypointManager(); + return waypoints; + } + net.minecraft.server.ServerScoreboard scoreboard; + @Override public net.minecraft.server.ServerScoreboard getScoreboard() { + if (scoreboard == null) scoreboard = new net.minecraft.server.ServerScoreboard(server); + return scoreboard; + } + /** The biome every position reads (plains), for the oracles that load the world registries. */ + net.minecraft.core.Holder biome; + @Override public net.minecraft.core.Holder getBiome(BlockPos p) { + if (biome == null) biome = registryAccess().lookupOrThrow(net.minecraft.core.registries.Registries.BIOME).getOrThrow(net.minecraft.world.level.biome.Biomes.PLAINS); + return biome; + } @Override public boolean isLoaded(BlockPos p) { return true; } @Override public boolean hasChunkAt(BlockPos p) { return true; } @Override public boolean hasChunksAt(int x0, int z0, int x1, int z1) { return true; } @@ -160,7 +183,7 @@ public final class VanillaBlockUpdateOracle { @Override public LevelTicks getFluidTicks() { return fluidTicks; } @Override public void sendBlockUpdated(BlockPos p, BlockState a, BlockState b, int f) { } @Override public void setBlocksDirty(BlockPos p, BlockState a, BlockState b) { } - @Override public net.minecraft.core.RegistryAccess registryAccess() { return RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY); } + @Override public net.minecraft.core.RegistryAccess registryAccess() { return registries != null ? registries : RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY); } @Override public net.minecraft.world.flag.FeatureFlagSet enabledFeatures() { return FeatureFlags.DEFAULT_FLAGS; } @Override public GameRules getGameRules() { return rules; } @Override public net.minecraft.server.MinecraftServer getServer() { return server; } @@ -174,12 +197,34 @@ public final class VanillaBlockUpdateOracle { @Override public boolean isRainingAt(BlockPos p) { return raining; } @Override public boolean isUnobstructed(BlockState state, BlockPos pos, net.minecraft.world.phys.shapes.CollisionContext c) { return true; } @Override public boolean isUnobstructed(Entity entity) { return true; } - @Override public List getEntities(Entity except, AABB box, java.util.function.Predicate filter) { return List.of(); } - @Override public List getEntitiesOfClass(Class type, AABB box, java.util.function.Predicate filter) { return List.of(); } + // Entities exist only when a harness opts in (the dispenser oracle); + // they are searched in the order they were added, as within one entity section. + List entities; + net.minecraft.core.RegistryAccess registries; + @Override public List getEntities(Entity except, AABB box, java.util.function.Predicate filter) { + if (entities == null) return List.of(); + List out = new ArrayList<>(); + for (Entity e : List.copyOf(entities)) if (e != except && !e.isRemoved() && e.getBoundingBox().intersects(box) && filter.test(e)) out.add(e); + return out; + } + @Override public void getEntities(net.minecraft.world.level.entity.EntityTypeTest type, AABB box, java.util.function.Predicate filter, List out, int max) { + if (entities == null) return; + for (Entity e : List.copyOf(entities)) { + T t = type.tryCast(e); + if (t != null && !e.isRemoved() && e.getBoundingBox().intersects(box) && filter.test(t)) { + out.add(t); + if (out.size() >= max) return; + } + } + } + @Override public List getEntitiesOfClass(Class type, AABB box, java.util.function.Predicate filter) { + return getEntities(net.minecraft.world.level.entity.EntityTypeTest.forClass(type), box, filter); + } @Override public List players() { return List.of(); } @Override public net.minecraft.world.entity.player.Player getNearestPlayer(double x, double y, double z, double d, boolean b) { return null; } @Override public boolean addFreshEntity(Entity entity) { emit("E " + BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType())); + if (entities != null) entities.add(entity); return true; } @Override public void levelEvent(Entity source, int type, BlockPos p, int data) { emit("L " + type + " " + pos(p) + " " + data); } @@ -187,7 +232,7 @@ public final class VanillaBlockUpdateOracle { emit("S " + pos(BlockPos.containing(x, y, z)) + " " + sound.value().location()); } @Override public void playSeededSound(Entity except, Entity src, net.minecraft.core.Holder sound, SoundSource source, float volume, float pitch, long seed) { - emit("S entity " + sound.value().location()); + emit("S " + (entities != null ? pos(BlockPos.containing(src.position())) : "entity") + " " + sound.value().location()); } @Override public void gameEvent(net.minecraft.core.Holder e, Vec3 p, net.minecraft.world.level.gameevent.GameEvent.Context c) { emit("G " + pos(BlockPos.containing(p)) + " " + e.getRegisteredName()); @@ -252,6 +297,10 @@ public final class VanillaBlockUpdateOracle { return attributes; } @Override public net.minecraft.world.level.dimension.DimensionType dimensionType() { return OVERWORLD_TYPE; } + @Override public long getOverworldClockTime() { return DAY.totalTicks(); } + @Override public float getMoonBrightness(BlockPos p) { + return net.minecraft.world.level.dimension.DimensionType.MOON_BRIGHTNESS_PER_PHASE[environmentAttributes().getValue(net.minecraft.world.attribute.EnvironmentAttributes.MOON_PHASE, p).index()]; + } @Override public net.minecraft.world.level.storage.ServerLevelData getLevelData() { return LEVEL_DATA; } @Override public boolean anyPlayerCloseEnoughForSpawning(BlockPos p) { return false; } @Override public void playSound(Entity except, double x, double y, double z, SoundEvent sound, SoundSource source, float volume, float pitch) { @@ -339,13 +388,15 @@ public final class VanillaBlockUpdateOracle { static final class Loot extends net.minecraft.server.ReloadableServerRegistries.Holder { World world; + /** The real loot tables, for the oracles that compare drops (the dispenser's). */ + net.minecraft.server.ReloadableServerRegistries.Holder delegate; Loot() { super(null); } @Override public net.minecraft.world.level.storage.loot.LootTable getLootTable(net.minecraft.resources.ResourceKey id) { world.emit("D " + id.identifier()); - return net.minecraft.world.level.storage.loot.LootTable.EMPTY; + return delegate != null ? delegate.getLootTable(id) : net.minecraft.world.level.storage.loot.LootTable.EMPTY; } } @@ -371,6 +422,12 @@ public final class VanillaBlockUpdateOracle { @Override public boolean isPublished() { return false; } @Override public boolean shouldInformAdmins() { return false; } @Override public boolean isSingleplayerOwner(net.minecraft.server.players.NameAndId id) { return false; } + net.minecraft.world.RandomSequences sequences; + /** Loot tables with a random_sequence draw from the server's sequences (world seed 0). */ + @Override public RandomSource getRandomSequence(net.minecraft.resources.Identifier key) { + if (sequences == null) sequences = new net.minecraft.world.RandomSequences(); + return sequences.get(key, 0L); + } } static List summary(World w, Map before) { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java new file mode 100644 index 000000000..ab6f97b8f --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -0,0 +1,271 @@ +package dev.horizonlab.parity; + +import com.google.gson.*; +import java.io.PrintWriter; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.file.*; +import java.util.*; +import net.minecraft.core.BlockPos; +import net.minecraft.core.NonNullList; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.Identifier; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.Container; +import net.minecraft.world.entity.*; +import net.minecraft.world.entity.item.ItemEntity; +import net.minecraft.world.entity.item.PrimedTnt; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.gamerules.GameRules; +import net.minecraft.world.level.levelgen.LegacyRandomSource; + +/** + * Drives vanilla `DispenserBlock.dispenseFrom` (and the dropper's) over the + * headless level of {@link VanillaBlockUpdateOracle}, with entities: the + * dispenser holds the case's items, target entities stand in front, and the + * result lists block changes, container contents, spawned entities with their + * position and motion, the targets' state afterwards, level events, sounds + * (those the client plays for level events included), game events and the + * level random. Every case runs twice: numbers that differ between the runs + * come from an entity's own unseeded random and are marked with "~". + * Used by tools/test_dispenser_parity.py. + */ +public final class VanillaDispenserOracle { + /** The sound LevelEventHandler plays for a level event a dispenser can cause, or null. */ + static String eventSound(int id, int data) { + return switch (id) { + case 1000 -> "minecraft:block.dispenser.dispense"; + case 1001 -> "minecraft:block.dispenser.fail"; + case 1002 -> "minecraft:block.dispenser.launch"; + case 1004 -> "minecraft:entity.firework_rocket.shoot"; + case 1018 -> "minecraft:entity.blaze.shoot"; + case 1051 -> "minecraft:entity.wind_charge.throw"; + case 1505 -> "minecraft:item.bone_meal.use"; + case 1009 -> data == 0 ? "minecraft:block.fire.extinguish" : data == 1 ? "minecraft:block.generic.extinguish_fire" : null; + case 2001 -> { + BlockState state = Block.stateById(data); + yield state.isAir() ? null : state.getSoundType().getBreakSound().location().toString(); + } + default -> null; + }; + } + + public static void main(String[] args) throws Exception { + ParityData.init(); + VanillaBlockUpdateOracle.AIR = Blocks.AIR.defaultBlockState(); + Path resources = Path.of(args[0]); + VanillaLevelShapeOracle.bindFluidTags(resources.resolve("data/minecraft/tags/fluid")); + VanillaBlockUpdateOracle.OVERWORLD_TYPE = VanillaBlockUpdateOracle.loadOverworld(); + var allocator = Class.forName("sun.misc.Unsafe"); + var field = allocator.getDeclaredField("theUnsafe"); + field.setAccessible(true); + VanillaBlockUpdateOracle.unsafe = field.get(null); + VanillaBlockUpdateOracle.allocate = allocator.getMethod("allocateInstance", Class.class); + try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[2]))); var lines = Files.lines(Path.of(args[1]))) { + int index = 0; + for (var line : (Iterable) lines::iterator) { + var f = JsonParser.parseString(line).getAsJsonObject(); + List a = attempt(f), b = attempt(f); + out.print("case " + index++ + "\n"); + for (String s : merge(a, b)) out.print(s + "\n"); + } + } + } + + static List attempt(JsonObject f) { + List result = new ArrayList<>(); + try { + run(f, result); + } catch (Throwable e) { + Throwable c = e; + while (c.getCause() != null) c = c.getCause(); + StackTraceElement[] st = c.getStackTrace(); + result.clear(); + result.add("error " + c.getClass().getSimpleName() + " " + (st.length > 0 ? st[0].toString() : "") + " " + c.getMessage()); + } + return result; + } + + /** Lines equal in both runs stay; numbers that differ are marked "~" (from an unseeded entity random). */ + static List merge(List a, List b) { + if (a.size() != b.size()) return a; + List out = new ArrayList<>(); + for (int i = 0; i < a.size(); i++) { + String x = a.get(i), y = b.get(i); + if (x.equals(y)) { + out.add(x); + continue; + } + String[] p = x.split(" "), q = y.split(" "); + if (p.length != q.length) { + out.add(x); + continue; + } + for (int j = 0; j < p.length; j++) if (!p[j].equals(q[j])) p[j] = "~" + p[j]; + out.add(String.join(" ", p)); + } + return out; + } + + static String num(double v) { + return String.format(Locale.ROOT, "%.6f", v); + } + + static ItemStack stack(String snbt) throws Exception { + return ParityData.stack(snbt); + } + + /** The item list of a container block entity (dispensers, droppers, chests, hoppers, barrels...). */ + @SuppressWarnings("unchecked") + static NonNullList slots(BlockEntity be) throws Exception { + for (Class c = be.getClass(); c != null; c = c.getSuperclass()) { + for (Field fd : c.getDeclaredFields()) { + if (fd.getType() == NonNullList.class && !java.lang.reflect.Modifier.isStatic(fd.getModifiers())) { + fd.setAccessible(true); + return (NonNullList) fd.get(be); + } + } + } + throw new IllegalStateException("no slots in " + be); + } + + static Method dispenseFrom(Class type) throws Exception { + for (Class c = type; c != null; c = c.getSuperclass()) { + try { + Method m = c.getDeclaredMethod("dispenseFrom", ServerLevel.class, BlockState.class, BlockPos.class); + m.setAccessible(true); + return m; + } catch (NoSuchMethodException ignored) { + } + } + throw new IllegalStateException("no dispenseFrom on " + type); + } + + static Entity target(VanillaBlockUpdateOracle.World w, JsonObject e) throws Exception { + EntityType type = BuiltInRegistries.ENTITY_TYPE.getValue(Identifier.parse(e.get("type").getAsString())); + Entity ent = type.create(w, EntitySpawnReason.COMMAND); + var p = e.getAsJsonArray("pos"); + ent.snapTo(p.get(0).getAsDouble(), p.get(1).getAsDouble(), p.get(2).getAsDouble(), e.has("yaw") ? e.get("yaw").getAsFloat() : 0f, 0f); + if (e.has("baby") && ent instanceof AgeableMob ageable) ageable.setAge(-24000); + if (e.has("tame")) { + if (ent instanceof net.minecraft.world.entity.animal.equine.AbstractHorse horse) horse.setTamed(true); + if (ent instanceof TamableAnimal tamable) tamable.setTame(true, false); + } + if (e.has("sheared")) { + if (ent instanceof net.minecraft.world.entity.animal.sheep.Sheep sheep) sheep.setSheared(true); + if (ent instanceof net.minecraft.world.entity.monster.skeleton.Bogged bogged) bogged.setSheared(true); + if (ent instanceof net.minecraft.world.entity.animal.golem.SnowGolem golem) golem.setPumpkin(false); + } + if (e.has("chest") && ent instanceof net.minecraft.world.entity.animal.equine.AbstractChestedHorse chested) chested.setChest(true); + if (e.has("loot") && ent instanceof Mob mob) mob.setCanPickUpLoot(true); + if (e.has("brown") && ent instanceof net.minecraft.world.entity.animal.cow.MushroomCow cow) { + Method variant = net.minecraft.world.entity.animal.cow.MushroomCow.class.getDeclaredMethod("setVariant", net.minecraft.world.entity.animal.cow.MushroomCow.Variant.class); + variant.setAccessible(true); + variant.invoke(cow, net.minecraft.world.entity.animal.cow.MushroomCow.Variant.BROWN); + } + // A target has lived in the level for a while: past its first tick, so + // equipping it plays the equip sound. + VanillaBlockUpdateOracle.set(Entity.class, ent, "firstTick", false); + if (e.has("equip") && ent instanceof LivingEntity living) { + for (var slot : e.getAsJsonObject("equip").entrySet()) { + living.setItemSlot(EquipmentSlot.byName(slot.getKey()), stack(slot.getValue().getAsString())); + } + // A target that has ticked since it was equipped has run + // collectEquipmentChanges (a sulfur cube takes its archetype then). + Map last = new EnumMap<>(EquipmentSlot.class); + for (EquipmentSlot slot : EquipmentSlot.values()) last.put(slot, ItemStack.EMPTY); + Method collect = LivingEntity.class.getDeclaredMethod("collectEquipmentChanges", Map.class); + collect.setAccessible(true); + collect.invoke(ent, last); + } + return ent; + } + + static void run(JsonObject f, List result) throws Exception { + VanillaBlockUpdateOracle.World w = VanillaBlockUpdateOracle.build(f); + w.entities = new ArrayList<>(); + w.registries = ParityData.access; + w.server.loot.delegate = ParityData.loot; + VanillaBlockUpdateOracle.set(Level.class, w, "damageSources", new net.minecraft.world.damagesource.DamageSources(ParityData.access)); + if (f.has("rules")) { + for (var rule : f.getAsJsonObject("rules").entrySet()) { + if (rule.getKey().equals("tnt_explodes")) w.rules.set(GameRules.TNT_EXPLODES, rule.getValue().getAsBoolean(), null); + } + } + if (f.has("items")) { + for (var item : f.getAsJsonArray("items")) { + var it = item.getAsJsonArray(); + BlockPos at = new BlockPos(it.get(0).getAsInt(), it.get(1).getAsInt(), it.get(2).getAsInt()); + slots(w.getBlockEntity(at)).set(it.get(3).getAsInt(), stack(it.get(4).getAsString())); + } + } + if (f.has("entities")) { + for (var e : f.getAsJsonArray("entities")) w.entities.add(target(w, e.getAsJsonObject())); + } + // Creating the targets must not move the stream the dispenser draws from. + w.rng = new LegacyRandomSource(f.get("seed").getAsLong()); + VanillaBlockUpdateOracle.set(Level.class, w, "random", w.rng); + w.log = new StringBuilder(); + w.blockEvents.clear(); + Map before = new HashMap<>(w.cells); + List targets = List.copyOf(w.entities); + BlockPos origin = VanillaBlockUpdateOracle.readPos(f.getAsJsonArray("pos")); + BlockState state = w.getBlockState(origin); + dispenseFrom(state.getBlock().getClass()).invoke(state.getBlock(), w, state, origin); + + result.addAll(VanillaBlockUpdateOracle.summary(w, before)); + List containers = new ArrayList<>(w.blockEntities == null ? List.of() : w.blockEntities.keySet()); + containers.sort(Comparator.comparingInt(q -> q.getX()).thenComparingInt(q -> q.getY()).thenComparingInt(q -> q.getZ())); + for (BlockPos at : containers) { + if (!(w.blockEntities.get(at) instanceof Container) || w.getBlockEntity(at) != w.blockEntities.get(at)) continue; + var items = slots(w.blockEntities.get(at)); + for (int i = 0; i < items.size(); i++) { + if (!items.get(i).isEmpty()) result.add("inv " + VanillaBlockUpdateOracle.pos(at) + " " + i + " " + ParityData.canon(items.get(i))); + } + } + for (Entity e : w.entities) { + if (targets.contains(e)) continue; + StringBuilder line = new StringBuilder("N ").append(BuiltInRegistries.ENTITY_TYPE.getKey(e.getType())); + line.append(' ').append(num(e.getX())).append(' ').append(num(e.getY())).append(' ').append(num(e.getZ())); + var v = e.getDeltaMovement(); + line.append(' ').append(num(v.x)).append(' ').append(num(v.y)).append(' ').append(num(v.z)); + if (e instanceof ItemEntity item) line.append(' ').append(ParityData.canon(item.getItem())); + if (e instanceof PrimedTnt tnt) line.append(" fuse=").append(tnt.getFuse()); + result.add(line.toString()); + } + for (int i = 0; i < targets.size(); i++) result.add(describe(i, targets.get(i))); + for (String s : w.log.toString().split("\n")) { + if (s.isEmpty() || s.startsWith("E ")) continue; + result.add(s); + if (s.startsWith("L ")) { + String[] p = s.split(" "); + String sound = eventSound(Integer.parseInt(p[1]), Integer.parseInt(p[3])); + if (sound != null) result.add("S " + p[2] + " " + sound); + } + } + result.addAll(w.blockEvents); + result.add("rng " + w.rng.nextLong()); + } + + /** A target's state after dispensing: removal, equipment and readiness for shearing. */ + static String describe(int index, Entity e) { + StringBuilder line = new StringBuilder("T " + index + " " + BuiltInRegistries.ENTITY_TYPE.getKey(e.getType())); + if (e.isRemoved()) line.append(" removed"); + if (e instanceof LivingEntity living) { + for (EquipmentSlot slot : EquipmentSlot.values()) { + ItemStack s = living.getItemBySlot(slot); + if (!s.isEmpty()) line.append(' ').append(slot.getName()).append('=').append(BuiltInRegistries.ITEM.getKey(s.getItem())).append('x').append(s.getCount()); + } + } + boolean listed = e instanceof net.minecraft.world.entity.animal.sheep.Sheep || e instanceof net.minecraft.world.entity.animal.cow.MushroomCow + || e instanceof net.minecraft.world.entity.monster.skeleton.Bogged || e instanceof net.minecraft.world.entity.animal.golem.SnowGolem; + if (listed && e instanceof Shearable shearable) line.append(shearable.readyForShearing() ? " ready" : " unready"); + if (e instanceof net.minecraft.world.entity.animal.equine.AbstractChestedHorse chested && chested.hasChest()) line.append(" chest"); + return line.toString(); + } +} diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py new file mode 100644 index 000000000..c6e130697 --- /dev/null +++ b/tools/test_dispenser_parity.py @@ -0,0 +1,303 @@ +#!/usr/bin/env python3 +"""Compare vanilla dispensers and droppers with Horizon's, one activation per case. + +A dispenser or dropper at the origin faces a random direction and holds the +case's items; the block in front (and below it) comes from a palette of blocks +the behaviours care about, and some cases put a target entity in front. Vanilla +`DispenserBlock.dispenseFrom` runs over the headless level of the block update +oracle (`VanillaDispenserOracle`); Horizon runs its game glue in a headless game +(`HORIZON_DISPENSE_FIXTURE`). Both print block changes, container contents, +spawned entities (type, position, motion), the targets afterwards, level events, +sounds, game events and the level random. Motion drawn from an entity's own +unseeded random (vanilla marks it "~") is compared within its spread. +""" +import json +import os +import random +import subprocess +try: + from . import block_update_harness as h, lab, test_rust_parity as base +except ImportError: + import block_update_harness as h + import lab + import test_rust_parity as base + +DIRECTIONS = ['down', 'up', 'north', 'south', 'west', 'east'] +STEP = {'down': (0, -1, 0), 'up': (0, 1, 0), 'north': (0, 0, -1), 'south': (0, 0, 1), 'west': (-1, 0, 0), 'east': (1, 0, 0)} + + +def potion(item, name): + return '{id:"minecraft:%s",count:1,components:{"minecraft:potion_contents":{potion:"minecraft:%s"}}}' % (item, name) + + +def plain(item, count=1): + return '{id:"minecraft:%s",count:%d}' % (item, count) + + +# Items with the blocks and entities in front that exercise their behaviour. +ITEMS = { + 'drop': [plain('stone', 5), plain('diamond'), plain('stick', 64), plain('oak_sapling', 3), plain('redstone', 2)], + 'projectile': [plain('arrow', 3), potion('tipped_arrow', 'poison'), plain('spectral_arrow'), plain('egg', 2), plain('blue_egg'), + plain('brown_egg'), plain('snowball', 4), plain('experience_bottle'), potion('splash_potion', 'healing'), + potion('lingering_potion', 'swiftness'), plain('firework_rocket', 2), plain('fire_charge', 2), plain('wind_charge', 3)], + 'bucket': [plain('water_bucket'), plain('lava_bucket'), plain('powder_snow_bucket'), plain('salmon_bucket'), plain('cod_bucket'), + plain('pufferfish_bucket'), plain('tropical_fish_bucket'), plain('axolotl_bucket'), plain('tadpole_bucket'), + plain('sulfur_cube_bucket'), plain('bucket'), plain('bucket', 3)], + 'block': [plain('bone_meal', 4), plain('tnt', 2), plain('flint_and_steel'), plain('glowstone', 3), plain('honeycomb', 2), + plain('glass_bottle', 2), potion('potion', 'water'), potion('potion', 'healing'), plain('shulker_box'), + plain('red_shulker_box'), plain('carved_pumpkin'), plain('wither_skeleton_skull'), plain('shears'), plain('brush')], + 'vehicle': [plain('oak_boat'), plain('bamboo_raft'), plain('minecart'), plain('chest_minecart'), + plain('furnace_minecart'), plain('tnt_minecart'), plain('hopper_minecart'), plain('armor_stand', 2)], + 'spawn': [plain('pig_spawn_egg'), plain('sheep_spawn_egg', 2), plain('zombie_spawn_egg'), plain('cow_spawn_egg'), plain('chicken_spawn_egg')], + 'equipment': [plain('iron_helmet'), plain('diamond_chestplate'), plain('leather_leggings'), plain('golden_boots'), + plain('turtle_helmet'), plain('saddle'), plain('iron_horse_armor'), plain('white_carpet'), plain('chest'), + plain('carved_pumpkin'), plain('elytra'), plain('wolf_armor'), plain('shears'), plain('brush'), plain('player_head')], +} + +FRONT = { + 'drop': ['air', 'stone', 'water', 'chest', 'hopper'], + 'projectile': ['air', 'air', 'water', 'stone', 'glass'], + 'bucket': ['air', 'air', 'water', 'water', 'lava', 'powder_snow', 'oak_slab', 'seagrass', 'stone', 'cauldron', 'kelp', + 'grass_block', 'fire'], + 'block': ['air', 'grass_block', 'wheat', 'oak_sapling', 'tnt', 'campfire', 'candle', 'respawn_anchor', 'beehive', 'bee_nest', + 'copper_block', 'exposed_cut_copper_stairs', 'dirt', 'coarse_dirt', 'rooted_dirt', 'water', 'seagrass', 'stone', + 'sweet_berry_bush', 'cocoa', 'obsidian', 'soul_campfire', 'candle_cake', 'netherrack', 'moss_block'], + 'vehicle': ['air', 'air', 'water', 'rail', 'powered_rail', 'activator_rail', 'stone', 'detector_rail'], + 'spawn': ['air', 'air', 'water', 'stone', 'grass_block'], + 'equipment': ['air'], +} +BELOW = ['air', 'stone', 'water', 'rail', 'grass_block', 'soul_sand', 'dirt', 'farmland', 'netherrack'] + +TARGETS = { + 'iron_helmet': ['zombie', 'skeleton', 'armor_stand', 'pig'], + 'diamond_chestplate': ['zombie', 'armor_stand'], + 'leather_leggings': ['skeleton', 'armor_stand'], + 'golden_boots': ['zombie', 'armor_stand'], + 'turtle_helmet': ['armor_stand', 'zombie'], + 'saddle': ['pig', 'horse', 'strider', 'donkey', 'camel', 'llama'], + 'iron_horse_armor': ['horse', 'donkey'], + 'white_carpet': ['llama', 'horse', 'trader_llama'], + 'chest': ['donkey', 'mule', 'llama', 'horse'], + 'carved_pumpkin': ['zombie', 'armor_stand', 'snow_golem'], + 'elytra': ['armor_stand', 'zombie'], + 'wolf_armor': ['wolf'], + 'shears': ['sheep', 'sheep', 'mooshroom', 'bogged', 'snow_golem'], + 'brush': ['armadillo'], + 'tnt': ['sulfur_cube'], + 'flint_and_steel': ['sulfur_cube'], + 'stone': ['sulfur_cube'], + 'player_head': ['armor_stand', 'zombie'], +} + + +class States: + def __init__(self, facts): + self.facts = facts + + def get(self, name, **props): + name = name if ':' in name else 'minecraft:' + name + best = self.facts.defaults[name] + if not props: + return best + for index in self.facts.by_block[name]: + have = self.facts.states[index][4].get('properties', {}) + if all(have.get(k) == v for k, v in props.items()): + return index + raise KeyError((name, props)) + + def any(self, rng, name): + name = name if ':' in name else 'minecraft:' + name + return rng.choice(self.facts.by_block[name]) + + +def front_state(states, rng, name): + """Mostly the default state, sometimes a random one (charges, honey, ages, lit...).""" + if name in ('respawn_anchor', 'beehive', 'bee_nest', 'campfire', 'candle', 'wheat', 'sweet_berry_bush', 'rail', 'powered_rail', + 'candle_cake', 'soul_campfire') or rng.random() < 0.3: + return states.any(rng, name) + return states.get(name) + + +def cases(facts, count, seed, only): + rng = random.Random(seed) + states = States(facts) + kinds = list(ITEMS) + for n in range(count): + kind = kinds[n % len(kinds)] + item = rng.choice(ITEMS[kind]) + if only and only not in item: + continue + facing = rng.choice(DIRECTIONS) + block = 'dropper' if rng.random() < 0.12 else 'dispenser' + cells = [[0, 0, 0, states.get(block, facing=facing, triggered='false')]] + dx, dy, dz = STEP[facing] + front = (dx, dy, dz) + below = (dx, dy - 1, dz) + front_name = rng.choice(FRONT[kind]) + if front_name != 'air': + h.put(cells, front, front_state(states, rng, front_name)) + if below != (0, 0, 0) and rng.random() < 0.8: + name = rng.choice(BELOW) + if name != 'air': + h.put(cells, below, front_state(states, rng, name)) + # A few random blocks around (portal frames, fire supports, neighbours). + for _ in range(rng.randrange(0, 6)): + at = (rng.randrange(-2, 3), rng.randrange(-2, 3), rng.randrange(-2, 3)) + if at in ((0, 0, 0), front): + continue + h.put(cells, at, states.get(rng.choice(['stone', 'obsidian', 'dirt', 'water', 'soul_sand', 'grass_block', 'oak_planks']))) + items = [] + slots = rng.sample(range(9), rng.choice([1, 1, 1, 2, 3, 9])) + for i, slot in enumerate(slots): + items.append([0, 0, 0, slot, item if i == 0 or rng.random() < 0.6 else plain('cobblestone', rng.randrange(1, 65))]) + case = {**h.environment(rng), 'mode': 'dispense', 'pos': [0, 0, 0], 'cells': cells, 'items': items} + case.pop('raining', None) + case.pop('thundering', None) + if rng.random() < 0.08: + case['rules'] = {'tnt_explodes': False} + bare = item.split('"')[1].split(':')[1] + if kind == 'equipment' or (bare in TARGETS and rng.random() < 0.5): + entities = [] + for _ in range(rng.choice([1, 1, 1, 2])): + target = {'type': 'minecraft:' + rng.choice(TARGETS.get(bare, ['zombie'])), + 'pos': [dx + 0.5, dy + rng.choice([0.0, 0.0, 0.25]), dz + 0.5], 'yaw': rng.choice([0.0, 90.0, 180.0])} + for flag, odds in (('baby', 0.2), ('tame', 0.6), ('sheared', 0.25), ('chest', 0.15), ('loot', 0.5), ('brown', 0.3)): + if rng.random() < odds: + target[flag] = True + if target['type'] == 'minecraft:sulfur_cube' and rng.random() < 0.5: + target['equip'] = {'body': plain(rng.choice(['tnt', 'stone', 'slime_block']))} + entities.append(target) + case['entities'] = entities + if block == 'dropper' and rng.random() < 0.5: + h.put(cells, front, states.get(rng.choice(['chest', 'hopper', 'barrel', 'dispenser']))) + if rng.random() < 0.5: + items.append([dx, dy, dz, 0, plain(rng.choice(['stone', 'diamond', 'stick']), rng.randrange(1, 65))]) + yield case + + +# Items whose behaviour runs Mob.finalizeSpawn (spawn eggs, mob buckets). The +# finalization draws belong to the entity spawning port; for these cases the +# level random's end state is not compared, everything else is. +FINALIZED = ('_spawn_egg', 'salmon_bucket', 'cod_bucket', 'pufferfish_bucket', 'tropical_fish_bucket', 'axolotl_bucket', + 'tadpole_bucket', 'sulfur_cube_bucket') + + +def finalizes(case): + return any(any(tag in i[4] for tag in FINALIZED) for i in case['items'] if i[:3] == [0, 0, 0]) + +def run(command, **kwargs): + done = subprocess.run(command, capture_output=True, text=True, **kwargs) + if done.returncode: + print((done.stdout + done.stderr)[-4000:]) + raise SystemExit(f'{command[0]} failed') + return done + + +def numbers_close(java, rust): + """Token-wise equality; a "~" token is an unseeded entity draw and only needs to fall in its spread.""" + a, b = java.split(' '), rust.split(' ') + if len(a) != len(b): + return False + for x, y in zip(a, b): + if x.startswith('~'): + try: + if abs(float(x[1:]) - float(y)) > 0.25: + return False + except ValueError: + return False + elif x != y: + return False + return True + + +def same(java, rust): + java = sorted(java) + rust = sorted(rust) + if len(java) != len(rust): + return False + pending = list(rust) + for line in java: + if line in pending: + pending.remove(line) + continue + if '~' not in line: + return False + match = next((r for r in pending if numbers_close(line, r)), None) + if match is None: + return False + pending.remove(match) + return True + + +def main(): + parser = h.argument_parser(__doc__) + args = parser.parse_args() + if args.count == 40000: + args.count = 3000 + lock, jars, libraries = base.verify_artifacts() + facts = h.load_facts() + resources = lab.LOCAL / 'horizon-client' / lock['minecraft']['version'] + work = lab.LOCAL / 'dispenser-parity' + work.mkdir(parents=True, exist_ok=True) + all_cases = list(cases(facts, args.count, args.seed, args.only)) + (work / 'cases.jsonl').write_text(''.join(json.dumps(case) + '\n' for case in all_cases)) + classes = work / 'classes' + classes.mkdir(exist_ok=True) + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + java_root = lab.ROOT / 'mods/horizon/src/test/java' + run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-sourcepath', str(java_root), '-nowarn', + '-cp', dependencies, '-d', str(classes), str(java_root / 'dev/horizonlab/parity/VanillaDispenserOracle.java')]) + run(['java', '-Xmx3G', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.VanillaDispenserOracle', + str(resources), str(work / 'cases.jsonl'), str(work / 'native.txt')]) + run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) + run([str(lab.ROOT / 'target/release/horizon-client'), '--resources', str(resources)], cwd=work, + env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) + # Loot table names (D) are vanilla's bookkeeping; the drops themselves are compared. + ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) + native = h.split_cases((work / 'native.txt').read_text(), ignore) + rust = h.split_cases((work / 'rust.txt').read_text(), ignore) + assert len(native) == len(rust) == len(all_cases), (len(native), len(rust), len(all_cases)) + groups, errors = {}, {} + unseeded = 0 + for case, a, b in zip(all_cases, native, rust): + if a and a[0].startswith('error'): + errors.setdefault(a[0], []).append(case) + continue + if finalizes(case): + unseeded += 1 + # The spawned mob's variant (and so its voice) is chosen in finalizeSpawn too. + a = [line for line in a if not line.startswith('rng ') and not line.endswith('.ambient')] + b = [line for line in b if not line.startswith('rng ') and not line.endswith('.ambient')] + if not same(a, b): + item = next(i[4] for i in case['items'] if i[:3] == [0, 0, 0]) + block = facts.states[case['cells'][0][3]][0].split(':')[1] + groups.setdefault(f'{block} {item.split(chr(34))[1]}', []).append((case, a, b)) + for kind, entries in sorted(errors.items(), key=lambda e: -len(e[1])): + print(f'oracle error x{len(entries)}: {kind}') + for name, entries in sorted(groups.items(), key=lambda e: -len(e[1])): + print(f'{name}: {len(entries)} differing') + for case, a, b in entries[:args.show]: + print(' case ' + describe(case, facts)) + print(' native ' + ' | '.join(sorted(a))) + print(' rust ' + ' | '.join(sorted(b))) + total = sum(len(v) for v in groups.values()) + skipped = sum(len(v) for v in errors.values()) + print(f'{len(all_cases) - total - skipped}/{len(all_cases) - skipped} cases match ({skipped} oracle errors; ' + f'{unseeded} spawning cases compared without the final level random)') + (work / 'differences.json').write_text(json.dumps({k: [[c, a, b] for c, a, b in v[:5]] for k, v in groups.items()})) + h.finish(total) + + +def describe(case, facts): + cells = ' '.join(f'{c[0]},{c[1]},{c[2]}={facts.describe(c[3])}' for c in case['cells']) + extra = '' + if case.get('entities'): + extra += ' entities=' + json.dumps(case['entities']) + if case.get('rules'): + extra += ' rules=' + json.dumps(case['rules']) + return f'{cells} items={json.dumps(case["items"])}{extra} seed={case["seed"]}' + + +if __name__ == '__main__': + main() From 45cb5850a954aeb33a45f373fc046b2ff457a9bb Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:26:46 +0200 Subject: [PATCH 03/24] WIP: blocks parity work, paused while the harness tooling is sped up Uncommitted changes the agent had in progress when it was stopped; may not build or pass its harnesses yet. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/copper_golems.rs | 2 +- crates/horizon-client/src/game/dispenser.rs | 138 +++++++++++++++--- crates/horizon-client/src/game/leads.rs | 9 +- crates/horizon-world/src/level/hooks.rs | 15 +- crates/horizon-world/src/level/mod.rs | 20 +++ crates/horizon-world/src/level/portal.rs | 54 ++++++- .../parity/VanillaDispenserOracle.java | 20 +++ 7 files changed, 228 insertions(+), 30 deletions(-) diff --git a/crates/horizon-client/src/game/copper_golems.rs b/crates/horizon-client/src/game/copper_golems.rs index e1acaebaf..3cdc3b745 100644 --- a/crates/horizon-client/src/game/copper_golems.rs +++ b/crates/horizon-client/src/game/copper_golems.rs @@ -705,7 +705,7 @@ impl Game<'_> { } false } - fn golem_shearable(&self, index: usize) -> bool { + pub(super) fn golem_shearable(&self, index: usize) -> bool { self.mobs[index].extra.legs.as_deref().is_some_and(|a| { self.res .items diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index 6389ec791..a7f829912 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -217,11 +217,19 @@ pub(super) fn body_in_head(s: Species) -> bool { || matches!(s, Species::Wolf | Species::HappyGhast | Species::SulfurCube) || horizon_client::mobs::nautilus::is_nautilus(s) } +/// Mobs whose saddle slot Horizon keeps in `extra.legs` (the copper +/// golem's antenna, `EQUIPMENT_SLOT_ANTENNA`). +fn saddle_in_legs(s: Species) -> bool { + s == Species::CopperGolem +} /// What a mob holds in a slot. pub(super) fn mob_slot_item(m: &Mob, slot: Slot) -> Option> { let x = &m.extra; let body = body_in_head(m.species()); + let antenna = saddle_in_legs(m.species()); match slot { + Slot::Saddle if antenna => x.legs.clone(), + Slot::Legs if antenna => None, Slot::MainHand => x.main_hand.clone(), Slot::OffHand => x.off_hand.clone(), Slot::Feet => x.feet.clone(), @@ -293,8 +301,10 @@ fn equip_sound(m: &Mob, slot: Slot, default: Option) -> Option { } pub(super) fn mob_set_slot(m: &mut Mob, slot: Slot, stack: ItemStack) { let item: Arc = stack.item.clone(); + let antenna = saddle_in_legs(m.species()); let x = &mut m.extra; match slot { + Slot::Saddle if antenna => x.legs = Some(item), Slot::MainHand => x.main_hand = Some(item), Slot::OffHand => x.off_hand = Some(item), Slot::Feet => x.feet = Some(item), @@ -1238,29 +1248,15 @@ impl Game<'_> { } return Ok((self.hurt_and_break(stack, 1), CLICK)); } - if self.res.facts.is_air(state) { - // BaseFireBlock.canBePlacedAt: a fire that survives, or a portal. - let below = self.dispenser_block_name((target.0, target.1 - 1, target.2)); - let fire_name = if matches!(&*below, "minecraft:soul_sand" | "minecraft:soul_soil") { - "minecraft:soul_fire" - } else { - "minecraft:fire" - }; - let fire = self.res.assets.registry.default_state(fire_name); - if let Some(fire) = fire - && self.with_level(|b, level, ticks| b.can_survive(level, ticks, target_pos, fire)) - { - self.level_set([target.0, target.1, target.2], fire)?; - } else if !self.try_light_portal([target.0, target.1, target.2])? { - // Nothing to light; vanilla still counts it as a success. - } - } else if self.prop(state, "lit").as_deref() == Some("false") - && (name.contains("campfire") || name.contains("candle")) - && self.prop(state, "waterlogged").as_deref() != Some("true") - { + if self.with_level(|b, level, ticks| b.fire_can_be_placed_at(level, ticks, target_pos, facing)) { + let fire = self.with_level(|b, level, ticks| b.fire_state_for(level, ticks, target_pos)); + self.level_set([target.0, target.1, target.2], fire)?; + self.post_block_game_event("minecraft:block_place", [target.0, target.1, target.2], None, None); + } else if self.can_light(state, &name) { if let Some(lit) = self.with_props(state, &[("lit", "true")]) { self.level_set([target.0, target.1, target.2], lit)?; } + self.post_block_game_event("minecraft:block_change", [target.0, target.1, target.2], None, None); } else if &*name == "minecraft:tnt" { if self.ui.rules.bool("tnt_explodes") { self.spawn_primed_tnt(target_pos, false); @@ -1276,6 +1272,14 @@ impl Game<'_> { } Ok((Some(stack), event)) } + /// `CampfireBlock.canLight || CandleBlock.canLight || CandleCakeBlock.canLight`. + fn can_light(&self, state: BlockStateId, name: &str) -> bool { + let lit = self.prop(state, "lit"); + let waterlogged = self.prop(state, "waterlogged"); + let dry_unlit = lit.as_deref() == Some("false") && waterlogged.as_deref() == Some("false"); + ((self.in_block_tag("minecraft:campfires", name) || self.in_block_tag("minecraft:candles", name)) && dry_unlit) + || (self.in_block_tag("minecraft:candle_cakes", name) && lit.as_deref() == Some("false")) + } fn dispense_bone_meal( &mut self, pos: Pos, @@ -1536,6 +1540,13 @@ impl Game<'_> { self.with_props(state, &[("charges", &(charge + 1).to_string())]) { self.level_set([target.0, target.1, target.2], next)?; + // RespawnAnchorBlock.charge: Context.of(null, newState). + self.post_block_game_event( + "minecraft:block_change", + [target.0, target.1, target.2], + None, + Some(next), + ); } self.audio.play( "minecraft:block.respawn_anchor.charge", @@ -1657,6 +1668,10 @@ impl Game<'_> { fn shear_entity(&mut self, target: Pos) -> Result { let shears = ItemStack::new("minecraft:shears", 1); for t in self.targets_at(target) { + // Entity.shearOffAllLeashConnections comes first, for any entity. + if self.shear_off_leashes(t) { + return Ok(true); + } let Target::Mob(i) = t else { continue }; let m = &self.mobs[i]; if !m.alive() { @@ -1665,6 +1680,29 @@ impl Game<'_> { let at = m.position; let sound_at = Some([at.x, at.y, at.z]); match m.species() { + Species::CopperGolem if self.golem_shearable(i) => { + // CopperGolem.shear: the antenna (the saddle slot) drops 1.5 up. + self.audio + .play("minecraft:entity.copper_golem.shear", sound_at, 1.0, 1.0); + if let Some(antenna) = self.mobs[i].extra.legs.take() { + self.spawn_at_location(at, 1.5, ItemStack::new(&antenna, 1)); + } + } + Species::SulfurCube if sulfur_cube::has_body(m) => { + // SulfurCube.shear: the swallowed block comes out at the + // passenger attachment, and nothing is swallowed for 5 s. + let height = f64::from(m.height()); + let id = m.id; + if let Some(body) = self.mobs[i].extra.head.take() { + self.spawn_at_location(at, height, ItemStack::new(&body, 1)); + } + // setItemSlot(BODY, EMPTY): an unequip. + let ctx = self.who_context(Some(horizon_client::mobs::Who::Mob(id)), None); + self.post_game_event("minecraft:unequip", [at.x, at.y, at.z], ctx); + self.mobs[i].extra.ints[3] = sulfur_cube::PICKUP_TIMER; + self.audio + .play("minecraft:entity.sulfur_cube.eject", sound_at, 1.0, 1.0); + } Species::Sheep if !m.extra.sheared && !m.is_baby() => { let color = horizon_client::mobs::render::DYE_NAMES[m.extra.color as usize & 15]; self.audio @@ -1756,6 +1794,66 @@ impl Game<'_> { } Ok(false) } + /// `Entity.shearOffAllLeashConnections(null)`: the entity's own lead and + /// every lead held by it (within a 32 block box around its centre) drop; + /// a shear game event at the entity and a snip at its block follow. + fn shear_off_leashes(&mut self, t: Target) -> bool { + use horizon_client::mobs::{Who, leash::Holder}; + let (holder, centre, own) = match t { + Target::Mob(i) => { + let m = &self.mobs[i]; + let b = m.bounds(); + (Holder::Mob(m.id), (b.min + b.max) * 0.5, Some(i)) + } + Target::Player => { + let Some(v) = self.player_view() else { return false }; + (Holder::Player, (v.bounds.min + v.bounds.max) * 0.5, None) + } + Target::Stand(_) => return false, + }; + let area = Aabb::new(centre - Vec3::new(16.0, 16.0, 16.0), centre + Vec3::new(16.0, 16.0, 16.0)); + let mut leashed: Vec<(u32, usize)> = self + .mobs + .iter() + .enumerate() + .filter(|(_, m)| !m.removed && m.extra.leash == Some(holder) && overlaps(m.bounds(), area)) + .map(|(j, m)| (m.id, j)) + .collect(); + leashed.sort_unstable(); + let own_leashed = own.is_some_and(|i| self.mobs[i].extra.leash.is_some()); + if leashed.is_empty() && !own_leashed { + return false; + } + // Leashable.dropLeash: a lead item at the leashed entity's feet. + let mut drop = |game: &mut Self, j: usize| { + if game.mobs[j].extra.leash.take().is_some() { + let at = game.mobs[j].position; + game.spawn_at_location(at, 0.0, ItemStack::new("minecraft:lead", 1)); + } + }; + if let Some(i) = own.filter(|_| own_leashed) { + drop(self, i); + } + for (_, j) in leashed { + drop(self, j); + } + let (at, who) = match t { + Target::Mob(i) => { + let m = &self.mobs[i]; + (m.position, Who::Mob(m.id)) + } + _ => ( + self.player.as_ref().map_or(Vec3::ZERO, |p| p.position), + Who::Player, + ), + }; + let ctx = self.who_context(Some(who), None); + self.post_game_event("minecraft:shear", [at.x, at.y, at.z], ctx); + let block = [at.x.floor() + 0.5, at.y.floor() + 0.5, at.z.floor() + 0.5]; + self.audio + .play("minecraft:item.shears.snip", Some(block), 1.0, 1.0); + true + } /// The brush behaviour: the first armadillo in front that `brushOffScute`s. fn dispense_brush(&mut self, pos: Pos, facing: Direction, stack: ItemStack) -> Dispensed { let target = offset(pos, facing); diff --git a/crates/horizon-client/src/game/leads.rs b/crates/horizon-client/src/game/leads.rs index dfcb34746..69bebac5b 100644 --- a/crates/horizon-client/src/game/leads.rs +++ b/crates/horizon-client/src/game/leads.rs @@ -66,13 +66,10 @@ impl Game<'_> { }) .collect() } + /// `Entity.spawnAtLocation(level, Items.LEAD)`: the item entity draws its + /// motion from its own random, not the level's. fn drop_lead_item(&mut self, at: [f64; 3]) { - let r = &mut self.world.random; - let v = [ - f64::from(r.next_float()) * 0.2 - 0.1, - 0.2, - f64::from(r.next_float()) * 0.2 - 0.1, - ]; + let v = super::dispenser::new_item_motion(); self.spawn_item(ItemStack::new("minecraft:lead", 1), at, v, 10); } /// `Leashable.dropLeash` / `removeLeash`: the lead falls as an item. diff --git a/crates/horizon-world/src/level/hooks.rs b/crates/horizon-world/src/level/hooks.rs index 677b12a4b..66114eee4 100644 --- a/crates/horizon-world/src/level/hooks.rs +++ b/crates/horizon-world/src/level/hooks.rs @@ -452,9 +452,20 @@ impl Sim<'_, L> { // ---- misc helpers ---- - /// `BaseFireBlock.onPlace` (portal ignition is not modelled). + /// `BaseFireBlock.onPlace`: a fire inside an empty obsidian frame (in the + /// overworld or the nether) lights it, otherwise one that cannot survive + /// goes out. fn base_fire_on_place(&mut self, pos: BlockPos, state: BlockStateId, same: bool) { - if !same && !self.can_survive(state, pos) { + if same { + return; + } + if matches!(self.level.dimension(), super::Dimension::Overworld | super::Dimension::Nether) + && let Some(shape) = self.find_empty_portal_shape(pos, true) + { + self.create_portal_blocks(&shape); + return; + } + if !self.can_survive(state, pos) { self.remove_block(pos, false); } } diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index 166483709..5192ea0aa 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -549,6 +549,26 @@ impl Behaviours { ) -> bool { self.sim(level, ticks).player_destroy(pos, creative) } + /// `BaseFireBlock.getState(level, pos)`: soul fire on its base blocks, + /// otherwise fire shaped by its neighbours. + pub fn fire_state_for( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + ) -> BlockStateId { + self.sim(level, ticks).fire_state_for(pos) + } + /// `BaseFireBlock.canBePlacedAt(level, pos, forwardDirection)`. + pub fn fire_can_be_placed_at( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + forward: horizon_core::geometry::Direction, + ) -> bool { + self.sim(level, ticks).fire_can_be_placed_at(pos, forward) + } /// `BlockState.canSurvive(level, pos)`. pub fn can_survive( &self, diff --git a/crates/horizon-world/src/level/portal.rs b/crates/horizon-world/src/level/portal.rs index 047f474d9..403cd49c6 100644 --- a/crates/horizon-world/src/level/portal.rs +++ b/crates/horizon-world/src/level/portal.rs @@ -1,7 +1,7 @@ //! `PortalShape`: the obsidian frame search used by nether portal blocks. use super::{LevelAccess, Sim, update::rel}; use crate::registry::BlockStateId; -use horizon_core::{geometry::Direction, position::BlockPos}; +use horizon_core::{geometry::Direction, position::BlockPos, random::RandomSource}; const MIN_WIDTH: i32 = 2; const MAX_WIDTH: i32 = 21; @@ -126,4 +126,56 @@ impl Sim<'_, L> { }; shape } + /// `PortalShape.findEmptyPortalShape`: a valid frame holding no portal + /// block yet, on the preferred axis first. + pub fn find_empty_portal_shape(&self, pos: BlockPos, x_axis: bool) -> Option { + [x_axis, !x_axis] + .into_iter() + .map(|axis| self.find_portal_shape(pos, axis)) + .find(|shape| shape.is_valid() && shape.portal_blocks == 0) + } + /// `PortalShape.createPortalBlocks`: fill the frame (flags 18). + pub fn create_portal_blocks(&mut self, shape: &PortalShape) { + let b = self.b; + let base = b.default_of(b.named.nether_portal); + let state = b + .table + .with_text(base, b.props.axis, if shape.x_axis { "x" } else { "z" }) + .unwrap_or(base); + for h in 0..shape.height { + for w in 0..shape.width { + let at = step(step(shape.bottom_left, Direction::Up, h), shape.right, w); + self.set_block(at, state, 18); + } + } + } + /// `BaseFireBlock.canBePlacedAt(level, pos, forwardDirection)`: air where + /// fire would survive, or the inside of an empty portal frame (vertical + /// directions pick the frame axis with the level random). + pub fn fire_can_be_placed_at(&mut self, pos: BlockPos, forward: Direction) -> bool { + if !self.b.is_air(self.state(pos)) { + return false; + } + if self.can_survive(self.fire_state_for(pos), pos) { + return true; + } + // isPortal + if !matches!(self.level.dimension(), super::Dimension::Overworld | super::Dimension::Nether) { + return false; + } + for face in Direction::ALL { + if self.portal_frame(self.state(rel(pos, face))) { + // getCounterClockWise().getAxis(): north/south turn to X. + let x_axis = match forward { + Direction::North | Direction::South => true, + Direction::West | Direction::East => false, + Direction::Up | Direction::Down => { + self.random().next_int_bound(2).unwrap_or(0) == 0 + } + }; + return self.find_empty_portal_shape(pos, x_axis).is_some(); + } + } + false + } } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index ab6f97b8f..69ea13997 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -134,6 +134,24 @@ public final class VanillaDispenserOracle { throw new IllegalStateException("no slots in " + be); } + /** + * The chunk source bone meal reaches for: saplings, grass and moss place + * their features with `getChunkSource().getGenerator()`, which none of + * them reads beyond passing it along. + */ + static final class Chunks extends net.minecraft.server.level.ServerChunkCache { + static net.minecraft.world.level.chunk.ChunkGenerator generator; + + private Chunks() { + super(null, null, null, null, null, null, 0, 0, false, null); + } + + @Override + public net.minecraft.world.level.chunk.ChunkGenerator getGenerator() { + return generator; + } + } + static Method dispenseFrom(Class type) throws Exception { for (Class c = type; c != null; c = c.getSuperclass()) { try { @@ -191,6 +209,8 @@ public final class VanillaDispenserOracle { w.entities = new ArrayList<>(); w.registries = ParityData.access; w.server.loot.delegate = ParityData.loot; + if (Chunks.generator == null) Chunks.generator = VanillaBlockUpdateOracle.make(net.minecraft.world.level.levelgen.FlatLevelSource.class); + VanillaBlockUpdateOracle.set(ServerLevel.class, w, "chunkSource", VanillaBlockUpdateOracle.make(Chunks.class)); VanillaBlockUpdateOracle.set(Level.class, w, "damageSources", new net.minecraft.world.damagesource.DamageSources(ParityData.access)); if (f.has("rules")) { for (var rule : f.getAsJsonObject("rules").entrySet()) { From bf3de4ef0564df5c33d68d8585990ae7a325fa6c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:26:49 +0200 Subject: [PATCH 04/24] WIP: saves parity work, paused while the harness tooling is sped up Uncommitted changes the agent had in progress when it was stopped; may not build or pass its harnesses yet. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/datafix/fixes_a.rs | 30 +++++++++++-- crates/horizon-world/src/datafix/fixes_b.rs | 21 ++++++++- crates/horizon-world/src/datafix/fixes_c.rs | 10 +++++ crates/horizon-world/src/datafix/mod.rs | 49 +++++++++++++++++++++ crates/horizon-world/src/region.rs | 27 ++++++------ 5 files changed, 117 insertions(+), 20 deletions(-) diff --git a/crates/horizon-world/src/datafix/fixes_a.rs b/crates/horizon-world/src/datafix/fixes_a.rs index 7781d6820..89a5bfee5 100644 --- a/crates/horizon-world/src/datafix/fixes_a.rs +++ b/crates/horizon-world/src/datafix/fixes_a.rs @@ -5,7 +5,7 @@ use super::dynamic::{ }; use super::json; use super::schema::{Key, Ty, key}; -use super::{Cx, FixFn, id_is, items, renamer}; +use super::{Cx, FixFn, id_is, items, renamer, reread}; use crate::nbt::Tag; pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { @@ -266,15 +266,18 @@ fn mob_effect_ids(cx: &mut Cx) { // ------------------------------------------------------------------ 3683, 3685 fn primed_tnt_block_state(cx: &mut Cx) { + let k = cx.k; 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); } + reread(k, e); }); } fn projectile_stored_item(cx: &mut Cx) { + let k = cx.k; cx.each(Ty::Entity, |e| { let item = if id_is(e, "minecraft:arrow") { let potion = as_string(e.get("Potion"), "minecraft:empty"); @@ -282,9 +285,11 @@ fn projectile_stored_item(cx: &mut Cx) { } 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`. + // The trident's value is cast to the new type: its typed Trident field + // is written as `item`, and the raw Trident comes back from the remainder. if id_is(e, "minecraft:trident") { rename(e, "Trident", "item"); + super::drop_typed(e, "Trident"); } return; }; @@ -294,6 +299,7 @@ fn projectile_stored_item(cx: &mut Cx) { if let Some(s) = e.get_mut("item") { items::add_names(s); } + reread(k, e); }); } @@ -390,7 +396,15 @@ fn map_banner_block_pos(cx: &mut Cx) { // ------------------------------------------------------------------ 3808, 3809, 3812 fn horse_body_armor(cx: &mut Cx, id: &str, field: &str, clear_armor_items: bool) { + let k = cx.k; cx.entity(id, |e| { + horse_body_armor_entity(e, field, clear_armor_items); + reread(k, e); + }); +} + +fn horse_body_armor_entity(e: &mut Tag, field: &str, clear_armor_items: bool) { + { let Some(armor) = e.get(field).cloned() else { return; }; @@ -417,7 +431,7 @@ fn horse_body_armor(cx: &mut Cx, id: &str, field: &str, clear_armor_items: bool) } 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) { @@ -682,7 +696,15 @@ fn banner_patterns(cx: &mut Cx) { } fn tipped_arrow_potion(cx: &mut Cx) { + let k = cx.k; cx.entity("minecraft:arrow", |e| { + tipped_arrow_potion_entity(e); + reread(k, e); + }); +} + +fn tipped_arrow_potion_entity(e: &mut Tag) { + { let potion = e.get("Potion").cloned(); let effects = e.get("custom_potion_effects").cloned(); let color = e.get("Color").cloned(); @@ -706,7 +728,7 @@ fn tipped_arrow_potion(cx: &mut Cx) { set(&mut stack, "tag", tag); stack }); - }); + } } fn particle_unflattening(cx: &mut Cx) { diff --git a/crates/horizon-world/src/datafix/fixes_b.rs b/crates/horizon-world/src/datafix/fixes_b.rs index 656bc5dde..32cb1646f 100644 --- a/crates/horizon-world/src/datafix/fixes_b.rs +++ b/crates/horizon-world/src/datafix/fixes_b.rs @@ -6,10 +6,19 @@ use super::dynamic::{ 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 super::{Cx, FixFn, id_is, reread}; use crate::nbt::{Compound, Tag}; pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + // ProjectileStoredWeaponFix only writes and reads arrows with the new schema. + out.push((key(3938, 0), |cx| { + let k = cx.k; + cx.each(Ty::Entity, |e| { + if id_is(e, "minecraft:arrow") || id_is(e, "minecraft:spectral_arrow") { + reread(k, e); + } + }); + })); 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)); @@ -588,7 +597,8 @@ fn boat_split(cx: &mut Cx) { (w, false) => format!("minecraft:{w}_boat"), (w, true) => format!("minecraft:{w}_chest_boat"), }; - remove(e, "Type"); + // The remainder update runs on the value cast to the new type, whose choice + // no longer has the old boat id: it finds nothing, so Type stays. set(e, "id", string(&new_id)); }); } @@ -817,6 +827,7 @@ fn saddle_drop_chance(e: &mut Tag) { } fn saddle_equipment_slot(cx: &mut Cx) { + let k = cx.k; cx.each(Ty::Entity, |e| { let id = namespaced(&as_str(e.get("id")).unwrap_or_default()); match id.as_str() { @@ -826,6 +837,7 @@ fn saddle_equipment_slot(cx: &mut Cx) { rename(e, "SaddleItem", "saddle"); saddle_drop_chance(e); } + reread(k, e); } "minecraft:pig" | "minecraft:strider" => { let saddled = as_bool(e.get("Saddle"), false); @@ -834,6 +846,7 @@ fn saddle_equipment_slot(cx: &mut Cx) { set(e, "saddle", Tag::compound([("id", string("minecraft:saddle")), ("count", Tag::Int(1))])); saddle_drop_chance(e); } + reread(k, e); } _ => {} } @@ -864,6 +877,10 @@ pub(super) fn equipment_to_slots(e: &mut Tag) { ("body", body), ("saddle", saddle), ]; + // The old fields are typed: they come back from the entity's remainder. + for field in ["ArmorItems", "HandItems", "body_armor_item", "saddle"] { + super::drop_typed(e, field); + } if slots.iter().all(|(_, s)| s.is_none()) { return; } diff --git a/crates/horizon-world/src/datafix/fixes_c.rs b/crates/horizon-world/src/datafix/fixes_c.rs index fa8221a27..496ddee65 100644 --- a/crates/horizon-world/src/datafix/fixes_c.rs +++ b/crates/horizon-world/src/datafix/fixes_c.rs @@ -10,6 +10,16 @@ use super::{Cx, FixFn, id_is, renamer}; use crate::nbt::Tag; pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + // NamedEntityConvertUncheckedFix: the typed Particle is written as + // custom_particle, the raw Particle comes back from the remainder. + out.push((key(4420, 0), |cx| { + cx.entity("minecraft:area_effect_cloud", |e| { + if let Some(p) = e.get("Particle").cloned() { + set(e, "custom_particle", p); + } + super::drop_typed(e, "Particle"); + }); + })); 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))); diff --git a/crates/horizon-world/src/datafix/mod.rs b/crates/horizon-world/src/datafix/mod.rs index 24a6263aa..a29fc0f21 100644 --- a/crates/horizon-world/src/datafix/mod.rs +++ b/crates/horizon-world/src/datafix/mod.rs @@ -96,6 +96,53 @@ impl Cx<'_> { } } +/// DataFixerUpper reads a record's typed fields from its map without taking +/// them out: the record's remainder keeps the whole map it was read from, and +/// writing lays the typed fields over it. A typed field that a fix drops (by +/// casting to a type without it) therefore comes back on write with its value +/// from that read. Each entity keeps that map under this key while fixes run. +const LAST_READ: &str = "\u{0}last_read"; + +/// Remember an entity's current fields as the map it was read from. +pub fn mark_read(e: &mut Tag) { + if let Tag::Compound(_) = e { + let mut raw = e.clone(); + strip_last_read(&mut raw); + dynamic::set(e, LAST_READ, raw); + } +} + +/// `Util.writeAndReadTypedOrThrow` over an entity by the fix with key `k`: it is +/// written and read again with the output schema, so it (and every entity inside +/// it) now remembers its current fields as the map it was last read from. +pub fn reread(k: Key, e: &mut Tag) { + walk(Ty::Entity, e, k, Ty::Entity, &mut mark_read); +} + +/// A typed field dropped from an entity: what writing the entity gives back for +/// it (its value in the map the entity was last read from, if any). +pub fn drop_typed(e: &mut Tag, field: &str) { + match e.get(LAST_READ).and_then(|raw| raw.get(field)).cloned() { + Some(raw) => dynamic::set(e, field, raw), + None => { + dynamic::remove(e, field); + } + } +} + +fn strip_last_read(v: &mut Tag) { + match v { + Tag::Compound(c) => { + c.remove(&dynamic::key(LAST_READ)); + for value in c.values_mut() { + strip_last_read(value); + } + } + Tag::List(items) => items.iter_mut().for_each(strip_last_read), + _ => {} + } +} + /// 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) @@ -128,6 +175,7 @@ pub fn update(root: Ty, data: &mut Tag, from: i32, to: i32) { 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)... + walk(root, data, from_key, Ty::Entity, &mut mark_read); read_hooks(root, data, from_key); // ...then every fix above that key, up to the target version... for (k, fix) in fixes() { @@ -137,6 +185,7 @@ pub fn update(root: Ty, data: &mut Tag, from: i32, to: i32) { } // ...and written with the target schema (its write hooks). write_hooks(root, data, key(to_version, 9)); + strip_last_read(data); } /// The read hooks of the schema the data was saved with. diff --git a/crates/horizon-world/src/region.rs b/crates/horizon-world/src/region.rs index 48b6e132e..05bdc1a54 100644 --- a/crates/horizon-world/src/region.rs +++ b/crates/horizon-world/src/region.rs @@ -104,14 +104,18 @@ impl RegionFile { let position = position_from_path(&path)?; let mut file = File::open(&path)?; let len = file.metadata()?.len(); - if len < HEADER as u64 { - return Err(invalid("truncated region header")); - } + // RegionFile's constructor: a short header (an empty or truncated file) + // reads as far as it goes, the rest stays zero (no chunks). let mut header = [0; HEADER]; - file.read_exact(&mut header)?; + let mut read = 0; + while read < HEADER { + match file.read(&mut header[read..])? { + 0 => break, + n => read += n, + } + } let mut offsets = [0; 1024]; let mut timestamps = [0; 1024]; - let mut ranges = Vec::new(); for i in 0..1024 { offsets[i] = u32::from_be_bytes(header[i * 4..i * 4 + 4].try_into().unwrap()); timestamps[i] = u32::from_be_bytes( @@ -124,16 +128,11 @@ impl RegionFile { } let start = offsets[i] >> 8; let count = offsets[i] & 255; - if start < 2 || count == 0 || u64::from(start + count) > len.div_ceil(SECTOR as u64) { - return Err(invalid(format!( - "invalid sector allocation at region slot {i}" - ))); + // Vanilla drops entries that overlap the header, are empty or start past + // the end of the file (logged as invalid sectors). + if start < 2 || count == 0 || u64::from(start) * SECTOR as u64 > len { + offsets[i] = 0; } - ranges.push((start, start + count)); - } - ranges.sort_unstable(); - if ranges.windows(2).any(|r| r[0].1 > r[1].0) { - return Err(invalid("overlapping region sector allocations")); } Ok(Self { file, From a5aaa53ab7060b34c153e47bc71ec3aaef221e90 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:31:35 +0200 Subject: [PATCH 05/24] Light fires, portals, candles and anchors from dispensers as vanilla does Flint and steel uses BaseFireBlock.canBePlacedAt and getState (a portal frame draws the level random for vertical dispensers), posts block_place and block_change, and lights only what CampfireBlock, CandleBlock and CandleCakeBlock.canLight accept. Fire placed inside an empty obsidian frame now lights the portal (BaseFireBlock.onPlace). Glowstone posts the anchor's block_change. Shears cut leads (shearOffAllLeashConnections) and empty a sulfur cube. Dropped leads draw motion from the item's own random, not the level's. The oracle gives bone meal a chunk generator so sapling, grass and moss cases are compared instead of erroring. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/copper_golems.rs | 2 +- crates/horizon-client/src/game/dispenser.rs | 26 +++---------------- crates/horizon-client/src/game/leads.rs | 1 - tools/test_dispenser_parity.py | 2 +- 4 files changed, 6 insertions(+), 25 deletions(-) diff --git a/crates/horizon-client/src/game/copper_golems.rs b/crates/horizon-client/src/game/copper_golems.rs index 3cdc3b745..e1acaebaf 100644 --- a/crates/horizon-client/src/game/copper_golems.rs +++ b/crates/horizon-client/src/game/copper_golems.rs @@ -705,7 +705,7 @@ impl Game<'_> { } false } - pub(super) fn golem_shearable(&self, index: usize) -> bool { + fn golem_shearable(&self, index: usize) -> bool { self.mobs[index].extra.legs.as_deref().is_some_and(|a| { self.res .items diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index a7f829912..5b4819ef0 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -217,19 +217,11 @@ pub(super) fn body_in_head(s: Species) -> bool { || matches!(s, Species::Wolf | Species::HappyGhast | Species::SulfurCube) || horizon_client::mobs::nautilus::is_nautilus(s) } -/// Mobs whose saddle slot Horizon keeps in `extra.legs` (the copper -/// golem's antenna, `EQUIPMENT_SLOT_ANTENNA`). -fn saddle_in_legs(s: Species) -> bool { - s == Species::CopperGolem -} /// What a mob holds in a slot. pub(super) fn mob_slot_item(m: &Mob, slot: Slot) -> Option> { let x = &m.extra; let body = body_in_head(m.species()); - let antenna = saddle_in_legs(m.species()); match slot { - Slot::Saddle if antenna => x.legs.clone(), - Slot::Legs if antenna => None, Slot::MainHand => x.main_hand.clone(), Slot::OffHand => x.off_hand.clone(), Slot::Feet => x.feet.clone(), @@ -301,10 +293,8 @@ fn equip_sound(m: &Mob, slot: Slot, default: Option) -> Option { } pub(super) fn mob_set_slot(m: &mut Mob, slot: Slot, stack: ItemStack) { let item: Arc = stack.item.clone(); - let antenna = saddle_in_legs(m.species()); let x = &mut m.extra; match slot { - Slot::Saddle if antenna => x.legs = Some(item), Slot::MainHand => x.main_hand = Some(item), Slot::OffHand => x.off_hand = Some(item), Slot::Feet => x.feet = Some(item), @@ -1680,14 +1670,6 @@ impl Game<'_> { let at = m.position; let sound_at = Some([at.x, at.y, at.z]); match m.species() { - Species::CopperGolem if self.golem_shearable(i) => { - // CopperGolem.shear: the antenna (the saddle slot) drops 1.5 up. - self.audio - .play("minecraft:entity.copper_golem.shear", sound_at, 1.0, 1.0); - if let Some(antenna) = self.mobs[i].extra.legs.take() { - self.spawn_at_location(at, 1.5, ItemStack::new(&antenna, 1)); - } - } Species::SulfurCube if sulfur_cube::has_body(m) => { // SulfurCube.shear: the swallowed block comes out at the // passenger attachment, and nothing is swallowed for 5 s. @@ -1803,15 +1785,15 @@ impl Game<'_> { Target::Mob(i) => { let m = &self.mobs[i]; let b = m.bounds(); - (Holder::Mob(m.id), (b.min + b.max) * 0.5, Some(i)) + (Holder::Mob(m.id), b.center(), Some(i)) } Target::Player => { let Some(v) = self.player_view() else { return false }; - (Holder::Player, (v.bounds.min + v.bounds.max) * 0.5, None) + (Holder::Player, v.bounds.center(), None) } Target::Stand(_) => return false, }; - let area = Aabb::new(centre - Vec3::new(16.0, 16.0, 16.0), centre + Vec3::new(16.0, 16.0, 16.0)); + let area = Aabb::new(centre, centre).inflate(Vec3::new(16.0, 16.0, 16.0)); let mut leashed: Vec<(u32, usize)> = self .mobs .iter() @@ -1825,7 +1807,7 @@ impl Game<'_> { return false; } // Leashable.dropLeash: a lead item at the leashed entity's feet. - let mut drop = |game: &mut Self, j: usize| { + let drop = |game: &mut Self, j: usize| { if game.mobs[j].extra.leash.take().is_some() { let at = game.mobs[j].position; game.spawn_at_location(at, 0.0, ItemStack::new("minecraft:lead", 1)); diff --git a/crates/horizon-client/src/game/leads.rs b/crates/horizon-client/src/game/leads.rs index 69bebac5b..77ccc9a97 100644 --- a/crates/horizon-client/src/game/leads.rs +++ b/crates/horizon-client/src/game/leads.rs @@ -8,7 +8,6 @@ use horizon_client::{ mesher::Vertex, mobs::leash::{self, Holder, Pending}, }; -use horizon_core::random::RandomSource; use horizon_world::nbt::Tag; /// `Vec3.yRot(angle)`. diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index c6e130697..59c434eaf 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -251,7 +251,7 @@ def main(): str(resources), str(work / 'cases.jsonl'), str(work / 'native.txt')]) run(['cargo', 'build', '-q', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, env={**os.environ, 'CARGO_BUILD_JOBS': os.environ.get('CARGO_BUILD_JOBS', '3')}) - run([str(lab.ROOT / 'target/release/horizon-client'), '--resources', str(resources)], cwd=work, + run([str(lab.cargo_target() / 'release/horizon-client'), '--resources', str(resources)], cwd=work, env={**os.environ, 'HORIZON_DISPENSE_FIXTURE': f'{work / "cases.jsonl"},{work / "rust.txt"}', 'HORIZON_NO_AUDIO': '1'}) # Loot table names (D) are vanilla's bookkeeping; the drops themselves are compared. ignore = tuple(prefix + ' ' for prefix in args.ignore.split(',') if prefix) + ('D ',) From f9fcb004a5cc0a56ff6f9c2ae0d3e8981e310a86 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:38:13 +0200 Subject: [PATCH 06/24] Dispense chest boats and command block minecarts as vanilla does BoatDispenseItemBehavior spawns the chest boat or chest raft itself: boats now carry ChestBoat's 27-slot chest, drawn with its model, saved with its Items, opened by sneaking, spilt when broken. MinecartDispenseItemBehavior spawns command block minecarts (an empty command). The dispenser harness now draws fronts per item (fire, portal frames, anchors, crops, copper, hives, golem and wither builds), every boat wood, more equipment, spawn eggs, sulfur cube swallowables and leashed targets, over 6000 cases. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entity_nbt.rs | 21 +-- crates/horizon-client/src/game/boats.rs | 105 ++++++++++--- crates/horizon-client/src/game/commands.rs | 12 +- .../horizon-client/src/game/commands/data.rs | 3 +- .../src/game/dispense_fixture.rs | 16 ++ crates/horizon-client/src/game/dispenser.rs | 18 +-- crates/horizon-client/src/game/minecarts.rs | 32 +++- crates/horizon-client/src/game/persist.rs | 23 +-- .../parity/VanillaDispenserOracle.java | 20 ++- tools/test_dispenser_parity.py | 148 +++++++++++++++--- 10 files changed, 320 insertions(+), 78 deletions(-) diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index b38fb8469..f5f9306db 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -1136,22 +1136,19 @@ pub fn armor_stand_from_nbt(tag: &Tag, id: u32) -> Option { a.on_ground = flag(tag, "OnGround"); Some(a) } -/// A boat entity (`minecraft:_boat`, `minecraft:bamboo_raft`). +/// A boat entity (`minecraft:_boat`, `minecraft:bamboo_chest_raft`...): +/// chest boats keep their slots in `Items` (`ContainerHelper.saveAllItems`). pub fn boat_to_nbt( - wood: &str, + id: &str, position: Vec3, motion: Vec3, yaw: f32, uuid: [i32; 4], raw: Option<&Tag>, + items: Option<&[Option]>, ) -> Tag { - let id = if wood == "bamboo" { - "minecraft:bamboo_raft".to_owned() - } else { - format!("minecraft:{wood}_boat") - }; - Base { - id: &id, + let mut tag = Base { + id, uuid, position, motion, @@ -1159,7 +1156,11 @@ pub fn boat_to_nbt( on_ground: false, fall_distance: 0.0, } - .tag(raw) + .tag(raw); + if let (Some(items), Tag::Compound(map)) = (items, &mut tag) { + map.insert("Items".into(), crate::save::encode_items(items)); + } + tag } /// A vehicle's base entity tag (boats and minecarts). pub fn vehicle_to_nbt( diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index 1bac6b220..a1d0458bf 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -48,7 +48,12 @@ pub(super) struct Boat { /// left, right, up, down pub input: [bool; 4], pub on_ground: bool, + /// `ChestBoat`: a chest with 27 slots in the back seat. + pub chest: bool, + pub items: Vec>, } +/// `ChestBoat.CONTAINER_SIZE`. +pub(super) const CHEST_SLOTS: usize = 27; const WIDTH: f64 = 1.375; const HEIGHT: f64 = 0.5625; /// Player.DEFAULT_VEHICLE_ATTACHMENT minus the boat's passenger attachment. @@ -76,8 +81,16 @@ impl Boat { paddling: [false; 2], input: [false; 4], on_ground: false, + chest: false, + items: Vec::new(), } } + /// A chest boat or chest raft. + pub fn with_chest(mut self) -> Self { + self.chest = true; + self.items = vec![None; CHEST_SLOTS]; + self + } pub fn bounds(&self) -> Aabb { let (w, p) = (WIDTH / 2.0, self.position); Aabb::new( @@ -96,15 +109,29 @@ impl Boat { } /// Item id -> (boat entity id, wood). impl Boat { - /// The vanilla entity type id (`oak_boat`, `bamboo_raft`). + /// The vanilla entity type id (`oak_boat`, `bamboo_chest_raft`), also + /// the item it drops. pub(super) fn entity_type(&self) -> String { + let chest = if self.chest { "chest_" } else { "" }; if &*self.wood == "bamboo" { - "minecraft:bamboo_raft".to_owned() + format!("minecraft:bamboo_{chest}raft") } else { - format!("minecraft:{}_boat", self.wood) + format!("minecraft:{}_{chest}boat", self.wood) } } } +/// The wood of a boat or chest boat item or entity type, and whether it +/// carries a chest. +pub(super) fn any_boat_wood(item: &str) -> Option<(&str, bool)> { + let name = item.strip_prefix("minecraft:")?; + if let Some(wood) = name + .strip_suffix("_chest_boat") + .or_else(|| name.strip_suffix("_chest_raft")) + { + return Some((wood, true)); + } + boat_wood(item).map(|w| (w, false)) +} pub(super) fn boat_wood(item: &str) -> Option<&str> { let name = item.strip_prefix("minecraft:")?; name.strip_suffix("_boat") @@ -113,10 +140,20 @@ pub(super) fn boat_wood(item: &str) -> Option<&str> { } pub(super) struct BoatModels { pub models: HashMap, + pub chest_models: HashMap, } impl BoatModels { + /// Whether a boat of this wood (with or without a chest) can be drawn. + pub fn has(&self, wood: &str, chest: bool) -> bool { + if chest { + self.chest_models.contains_key(wood) + } else { + self.models.contains_key(wood) + } + } pub fn load(root: &std::path::Path) -> Self { let mut models = HashMap::new(); + let mut chest_models = HashMap::new(); if let Ok(bytes) = std::fs::read(root.join("entity-models.json")) && let Ok(serde_json::Value::Object(all)) = serde_json::from_slice::(&bytes) @@ -129,9 +166,16 @@ impl BoatModels { { models.insert(wood.to_owned(), part); } + if let Some(wood) = key + .strip_prefix("minecraft:chest_boat/") + .and_then(|k| k.strip_suffix("#main")) + && let Some(part) = horizon_client::mobs::load_model(root, key) + { + chest_models.insert(wood.to_owned(), part); + } } } - Self { models } + Self { models, chest_models } } } impl Game<'_> { @@ -467,10 +511,10 @@ impl Game<'_> { } /// `BoatItem.use`: aim at a block or the water surface. pub(super) fn place_boat(&mut self, stack: &ItemStack) -> bool { - let Some(wood) = boat_wood(&stack.item) else { + let Some((wood, chest)) = any_boat_wood(&stack.item) else { return false; }; - if !self.boat_models.models.contains_key(wood) { + if !self.boat_models.has(wood, chest) { return false; } let Some(eye) = self.eye_position() else { @@ -517,12 +561,15 @@ impl Game<'_> { (None, Some(w)) => w.0, (None, None) => return false, }; - let boat = Boat::new( + let mut boat = Boat::new( self.next_entity + 1, Arc::from(wood), Vec3::new(hit[0], hit[1], hit[2]), self.yaw, ); + if chest { + boat = boat.with_chest(); + } let bb = boat.bounds(); let (lo, hi) = (bb.min_position(), bb.max_position()); let e = 1.0e-7; @@ -585,11 +632,23 @@ impl Game<'_> { return false; }; let sneaking = self.keys.contains(&winit::keyboard::KeyCode::ShiftLeft); - if !self.before_block(d) - || sneaking - || self.boats[i].out_of_control >= 60.0 - || self.riding.is_some() - { + if !self.before_block(d) { + return false; + } + // ChestBoat.interact: sneaking (or a full seat) opens the chest. + if self.boats[i].chest && (sneaking || self.riding.is_some()) { + let b = &self.boats[i]; + let key = (i32::MAX - 15, super::minecarts::CART_KEY_Y, b.id as i32); + self.world + .block_entities + .insert(key, horizon_client::containers::BlockEntity::Chest(b.items.clone())); + self.container = Some(key); + self.screen = super::Screen::Inventory; + self.capture(false); + self.right = false; + return true; + } + if sneaking || self.boats[i].out_of_control >= 60.0 || self.riding.is_some() { return false; } self.riding = Some(self.boats[i].id); @@ -611,13 +670,13 @@ impl Game<'_> { let destroyed = self.creative || b.damage > 40.0; if destroyed { let b = self.boats.remove(i); + let at = [b.position.x, b.position.y, b.position.z]; if !self.creative && self.ui.rules.bool("entity_drops") { - let item = if &*b.wood == "bamboo" { - "minecraft:bamboo_raft".to_owned() - } else { - format!("minecraft:{}_boat", b.wood) - }; - let at = [b.position.x, b.position.y, b.position.z]; + // ChestBoat.destroy: chestVehicleDestroyed spills the contents. + for stack in b.items.iter().flatten() { + self.spawn_item(stack.clone(), at, super::dispenser::new_item_motion(), 10); + } + let item = b.entity_type(); self.spawn_item( ItemStack { item: Arc::from(item.as_str()), @@ -638,14 +697,20 @@ impl Game<'_> { pub(super) fn boat_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let a = alpha as f32; for b in &self.boats { - let Some(model) = self.boat_models.models.get(&*b.wood) else { + let models = if b.chest { + &self.boat_models.chest_models + } else { + &self.boat_models.models + }; + let Some(model) = models.get(&*b.wood) else { continue; }; + let folder = if b.chest { "chest_boat" } else { "boat" }; let Some(uv) = self .res .atlas .sprites - .get(&format!("entity/boat/{}", b.wood)) + .get(&format!("entity/{folder}/{}", b.wood)) .map(|r| [r.u0, r.v0, r.u1, r.v1]) else { continue; diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 9dfed52ea..2f0a7acc9 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -266,13 +266,11 @@ impl Game<'_> { } } Target::Painting(_) => "minecraft:painting".into(), - Target::Boat(id) => self.boats.iter().find(|b| b.id == id).map_or_else( - || "minecraft:oak_boat".into(), - |b| { - let kind = if &*b.wood == "bamboo" { "raft" } else { "boat" }; - format!("minecraft:{}_{kind}", b.wood) - }, - ), + Target::Boat(id) => self + .boats + .iter() + .find(|b| b.id == id) + .map_or_else(|| "minecraft:oak_boat".into(), |b| b.entity_type()), Target::Cart(id) => self .carts .iter() diff --git a/crates/horizon-client/src/game/commands/data.rs b/crates/horizon-client/src/game/commands/data.rs index d7436a628..85d298f8f 100644 --- a/crates/horizon-client/src/game/commands/data.rs +++ b/crates/horizon-client/src/game/commands/data.rs @@ -73,12 +73,13 @@ impl Game<'_> { let uuid = self.uuid_for(id); let b = self.boats.iter().find(|b| b.id == id)?; entity_nbt::boat_to_nbt( - &b.wood, + &b.entity_type(), b.position, b.velocity, b.yaw, uuid, self.persist.raw.get(&id), + b.chest.then_some(&b.items[..]), ) } Target::Cart(id) => { diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index 011523f52..33bbdb279 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -168,6 +168,19 @@ impl Game<'_> { for e in case["entities"].as_array().into_iter().flatten() { targets.push(self.fixture_target(e)?); } + // Leashable.setLeashedTo: a lead from a target to an earlier one. + for (e, t) in case["entities"].as_array().into_iter().flatten().zip(&targets) { + let (Some(holder), Target::Mob(id)) = (e["leash"].as_u64(), t) else { + continue; + }; + let holder = match targets.get(holder as usize) { + Some(Target::Mob(h)) => horizon_client::mobs::leash::Holder::Mob(*h), + _ => continue, + }; + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == *id) { + m.extra.leash = Some(holder); + } + } // Creating the targets must not move the stream the dispenser draws from. self.world.random = LegacyRandom::new(seed); self.world.block_log = Some(Vec::new()); @@ -365,6 +378,9 @@ impl Game<'_> { if x.chested && matches!(s, Species::Donkey | Species::Mule | Species::Llama | Species::TraderLlama) { line.push_str(" chest"); } + if x.leash.is_some() { + line.push_str(" leashed"); + } } } line diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index 5b4819ef0..26f5e0b50 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -603,12 +603,7 @@ impl Game<'_> { "minecraft:honeycomb" => self.dispense_honeycomb(pos, facing, stack)?, "minecraft:potion" => self.dispense_potion(pos, facing, stack)?, _ if item.ends_with("shulker_box") => self.dispense_shulker_box(pos, facing, stack)?, - _ if super::boats::boat_wood(&item).is_some() => self.dispense_boat(pos, facing, stack), - // BoatDispenseItemBehavior for chest boats: Horizon has no chest boat - // entity, so they always take its fallback (an inner default dispense). - _ if item.ends_with("_chest_boat") || item.ends_with("_chest_raft") => { - self.dispense_boat(pos, facing, stack) - } + _ if super::boats::any_boat_wood(&item).is_some() => self.dispense_boat(pos, facing, stack), _ if super::minecarts::CartKind::from_id(&item).is_some() => { self.dispense_minecart(pos, facing, stack) } @@ -1004,7 +999,9 @@ impl Game<'_> { } /// `BoatDispenseItemBehavior`. fn dispense_boat(&mut self, pos: Pos, facing: Direction, stack: ItemStack) -> Dispensed { - let Some(wood) = super::boats::boat_wood(&stack.item).map(str::to_owned) else { + let Some((wood, chest)) = + super::boats::any_boat_wood(&stack.item).map(|(w, c)| (w.to_owned(), c)) + else { return (self.dispense_default(pos, facing, stack), CLICK); }; let front = offset(pos, facing); @@ -1025,16 +1022,19 @@ impl Game<'_> { } else { return (self.dispense_default(pos, facing, stack), CLICK); }; - if !self.boat_models.models.contains_key(&wood) { + if !self.boat_models.has(&wood, chest) { return (self.dispense_default(pos, facing, stack), CLICK); } self.next_entity += 1; - let boat = super::boats::Boat::new( + let mut boat = super::boats::Boat::new( self.next_entity, wood.as_str().into(), Vec3::new(x, y + y_offset, z), y_rot(facing), ); + if chest { + boat = boat.with_chest(); + } self.world.modified.insert((front.0 >> 4, front.2 >> 4)); self.boats.push(boat); (shrink(stack), CLICK) diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 967bef7e4..c9baac7d9 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -28,6 +28,9 @@ pub(super) enum CartKind { Hopper, Furnace, Tnt, + /// `MinecartCommandBlock`; its command block holds no command, so an + /// activator rail runs nothing. + CommandBlock, } impl CartKind { pub fn from_id(id: &str) -> Option { @@ -37,6 +40,7 @@ impl CartKind { "hopper_minecart" => Self::Hopper, "furnace_minecart" => Self::Furnace, "tnt_minecart" => Self::Tnt, + "command_block_minecart" => Self::CommandBlock, _ => return None, }) } @@ -47,6 +51,7 @@ impl CartKind { Self::Hopper => "minecraft:hopper_minecart", Self::Furnace => "minecraft:furnace_minecart", Self::Tnt => "minecraft:tnt_minecart", + Self::CommandBlock => "minecraft:command_block_minecart", } } fn slots(self) -> usize { @@ -192,6 +197,7 @@ impl CartModels { CartKind::Hopper, CartKind::Furnace, CartKind::Tnt, + CartKind::CommandBlock, ]; let models = kinds .into_iter() @@ -981,6 +987,8 @@ impl Game<'_> { self.prime_cart(&mut c); self.carts[i] = c; } + // BaseCommandBlock.usedBy: only an operator in creative opens it. + CartKind::CommandBlock => return false, } self.swing_arm(); true @@ -990,9 +998,10 @@ impl Game<'_> { let Some(key) = self.container.filter(|k| k.1 == CART_KEY_Y) else { return; }; - if let Some(BlockEntity::Chest(items)) = self.world.block_entities.remove(&key) - && let Some(c) = self.carts.iter_mut().find(|c| c.id as i32 == key.2) - { + let Some(BlockEntity::Chest(items)) = self.world.block_entities.remove(&key) else { + return; + }; + if let Some(c) = self.carts.iter_mut().find(|c| c.id as i32 == key.2) { c.items = items; let at = [c.position.x, c.position.y, c.position.z]; if c.kind == CartKind::Chest { @@ -1000,15 +1009,24 @@ impl Game<'_> { self.audio .play("minecraft:block.chest.close", Some(at), 0.5, pitch); } + } else if let Some(b) = self.boats.iter_mut().find(|b| b.id as i32 == key.2) { + b.items = items; } } /// The open cart's kind when a cart container is shown. pub(super) fn open_cart_kind(&self) -> Option { let key = self.container.filter(|k| k.1 == CART_KEY_Y)?; + // A chest boat's chest shows like a chest minecart's. self.carts .iter() .find(|c| c.id as i32 == key.2) .map(|c| c.kind) + .or_else(|| { + self.boats + .iter() + .any(|b| b.chest && b.id as i32 == key.2) + .then_some(CartKind::Chest) + }) } /// `VehicleEntity.hurtServer` from the player's attack. pub(super) fn attack_cart(&mut self, damage: f32) -> bool { @@ -1121,6 +1139,7 @@ impl Game<'_> { ], ), CartKind::Tnt => registry.default_state("minecraft:tnt"), + CartKind::CommandBlock => registry.default_state("minecraft:command_block"), } } /// `AbstractMinecartRenderer` (old render): on the rail between the @@ -1283,6 +1302,13 @@ impl Minecart { put("Fuel", Tag::Short(self.fuel as i16)); } CartKind::Tnt => put("fuse", Tag::Int(self.fuse)), + // BaseCommandBlock.save of an empty command block. + CartKind::CommandBlock => { + put("Command", Tag::String(JavaString::from(""))); + put("SuccessCount", Tag::Int(0)); + put("TrackOutput", Tag::Byte(1)); + put("UpdateLastExecution", Tag::Byte(1)); + } CartKind::Rideable => {} } } diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 2aa368383..957ede43a 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -198,18 +198,22 @@ impl Game<'_> { }); } } - k if super::boats::boat_wood(k).is_some() - && self - .boat_models - .models - .contains_key(super::boats::boat_wood(k).unwrap_or("")) => + k if super::boats::any_boat_wood(k) + .is_some_and(|(wood, chest)| self.boat_models.has(wood, chest)) => { if let Some((position, velocity, yaw)) = entity_nbt::boat_from_nbt(&tag) { self.next_entity += 1; let id = self.next_entity; - let wood = - std::sync::Arc::from(super::boats::boat_wood(k).unwrap_or("oak")); - let mut b = super::boats::Boat::new(id, wood, position, yaw); + let (wood, chest) = super::boats::any_boat_wood(k).unwrap_or(("oak", false)); + let mut b = + super::boats::Boat::new(id, std::sync::Arc::from(wood), position, yaw); + if chest { + b = b.with_chest(); + b.items = horizon_client::save::decode_items( + tag.get("Items"), + super::boats::CHEST_SLOTS, + ); + } b.velocity = velocity; self.remember(id, uuid, tag); self.boats.push(b); @@ -536,12 +540,13 @@ impl Game<'_> { .copied() .unwrap_or_else(random_uuid); let tag = entity_nbt::boat_to_nbt( - &b.wood, + &b.entity_type(), b.position, b.velocity, b.yaw, uuid, self.persist.raw.get(&b.id), + b.chest.then_some(&b.items[..]), ); out.entry(chunk_of(b.position)).or_default().push(tag); } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index 69ea13997..0aba1dce4 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -150,6 +150,11 @@ public final class VanillaDispenserOracle { public net.minecraft.world.level.chunk.ChunkGenerator getGenerator() { return generator; } + + /** Leads send entity link packets; nobody is tracking. */ + @Override + public void sendToTrackingPlayers(final Entity entity, final net.minecraft.network.protocol.Packet packet) { + } } static Method dispenseFrom(Class type) throws Exception { @@ -225,7 +230,19 @@ public final class VanillaDispenserOracle { } } if (f.has("entities")) { - for (var e : f.getAsJsonArray("entities")) w.entities.add(target(w, e.getAsJsonObject())); + List created = new ArrayList<>(); + for (var e : f.getAsJsonArray("entities")) { + Entity ent = target(w, e.getAsJsonObject()); + created.add(ent); + w.entities.add(ent); + } + var list = f.getAsJsonArray("entities"); + for (int i = 0; i < list.size(); i++) { + var e = list.get(i).getAsJsonObject(); + if (e.has("leash") && created.get(i) instanceof Leashable leashable) { + leashable.setLeashedTo(created.get(e.get("leash").getAsInt()), true); + } + } } // Creating the targets must not move the stream the dispenser draws from. w.rng = new LegacyRandomSource(f.get("seed").getAsLong()); @@ -286,6 +303,7 @@ public final class VanillaDispenserOracle { || e instanceof net.minecraft.world.entity.monster.skeleton.Bogged || e instanceof net.minecraft.world.entity.animal.golem.SnowGolem; if (listed && e instanceof Shearable shearable) line.append(shearable.readyForShearing() ? " ready" : " unready"); if (e instanceof net.minecraft.world.entity.animal.equine.AbstractChestedHorse chested && chested.hasChest()) line.append(" chest"); + if (e instanceof Leashable leashable && leashable.isLeashed()) line.append(" leashed"); return line.toString(); } } diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index 59c434eaf..33792488d 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -34,9 +34,15 @@ def plain(item, count=1): return '{id:"minecraft:%s",count:%d}' % (item, count) -# Items with the blocks and entities in front that exercise their behaviour. +WOODS = ['oak', 'spruce', 'birch', 'jungle', 'acacia', 'cherry', 'dark_oak', 'mangrove', 'pale_oak', 'poplar'] + +# Items with the blocks and entities in front that exercise their behaviour: +# one entry per DispenseItemBehavior.bootStrap registration family, plus the +# defaults of DispenserBlock.getDefaultDispenseMethod (equippables, sulfur +# cube swallowables, spawn eggs, a plain drop). ITEMS = { - 'drop': [plain('stone', 5), plain('diamond'), plain('stick', 64), plain('oak_sapling', 3), plain('redstone', 2)], + 'drop': [plain('stone', 5), plain('diamond'), plain('stick', 64), plain('oak_sapling', 3), plain('redstone', 2), plain('lead'), + plain('wheat_seeds', 7)], 'projectile': [plain('arrow', 3), potion('tipped_arrow', 'poison'), plain('spectral_arrow'), plain('egg', 2), plain('blue_egg'), plain('brown_egg'), plain('snowball', 4), plain('experience_bottle'), potion('splash_potion', 'healing'), potion('lingering_potion', 'swiftness'), plain('firework_rocket', 2), plain('fire_charge', 2), plain('wind_charge', 3)], @@ -45,13 +51,22 @@ ITEMS = { plain('sulfur_cube_bucket'), plain('bucket'), plain('bucket', 3)], 'block': [plain('bone_meal', 4), plain('tnt', 2), plain('flint_and_steel'), plain('glowstone', 3), plain('honeycomb', 2), plain('glass_bottle', 2), potion('potion', 'water'), potion('potion', 'healing'), plain('shulker_box'), - plain('red_shulker_box'), plain('carved_pumpkin'), plain('wither_skeleton_skull'), plain('shears'), plain('brush')], - 'vehicle': [plain('oak_boat'), plain('bamboo_raft'), plain('minecart'), plain('chest_minecart'), - plain('furnace_minecart'), plain('tnt_minecart'), plain('hopper_minecart'), plain('armor_stand', 2)], - 'spawn': [plain('pig_spawn_egg'), plain('sheep_spawn_egg', 2), plain('zombie_spawn_egg'), plain('cow_spawn_egg'), plain('chicken_spawn_egg')], + plain('red_shulker_box'), plain('lime_shulker_box'), plain('carved_pumpkin'), plain('wither_skeleton_skull'), + plain('shears'), plain('brush')], + 'vehicle': [plain('oak_boat'), plain('bamboo_raft'), plain('bamboo_chest_raft'), plain('minecart'), plain('chest_minecart'), + plain('furnace_minecart'), plain('tnt_minecart'), plain('hopper_minecart'), plain('command_block_minecart'), + plain('armor_stand', 2), 'BOAT', 'CHEST_BOAT'], + 'spawn': [plain('pig_spawn_egg'), plain('sheep_spawn_egg', 2), plain('zombie_spawn_egg'), plain('cow_spawn_egg'), + plain('chicken_spawn_egg'), plain('skeleton_spawn_egg'), plain('creeper_spawn_egg'), plain('villager_spawn_egg'), + plain('wolf_spawn_egg'), plain('horse_spawn_egg'), plain('squid_spawn_egg'), plain('slime_spawn_egg')], 'equipment': [plain('iron_helmet'), plain('diamond_chestplate'), plain('leather_leggings'), plain('golden_boots'), - plain('turtle_helmet'), plain('saddle'), plain('iron_horse_armor'), plain('white_carpet'), plain('chest'), - plain('carved_pumpkin'), plain('elytra'), plain('wolf_armor'), plain('shears'), plain('brush'), plain('player_head')], + plain('chainmail_helmet'), plain('netherite_boots'), plain('turtle_helmet'), plain('saddle'), + plain('iron_horse_armor'), plain('diamond_horse_armor'), plain('leather_horse_armor'), plain('white_carpet'), + plain('red_carpet'), plain('chest'), plain('carved_pumpkin'), plain('elytra'), plain('wolf_armor'), plain('shears'), + plain('brush'), plain('player_head'), plain('zombie_head'), plain('white_harness')], + 'swallow': [plain('slime_block'), plain('oak_planks', 3), plain('packed_ice'), plain('soul_sand'), plain('magma_block'), + plain('white_wool'), plain('mud', 2), plain('andesite'), plain('iron_block'), plain('mushroom_stem'), + plain('honeycomb_block'), plain('tube_coral_block'), plain('tnt'), plain('stone')], } FRONT = { @@ -65,29 +80,74 @@ FRONT = { 'vehicle': ['air', 'air', 'water', 'rail', 'powered_rail', 'activator_rail', 'stone', 'detector_rail'], 'spawn': ['air', 'air', 'water', 'stone', 'grass_block'], 'equipment': ['air'], + 'swallow': ['air'], } +# The blocks a behaviour reacts to, drawn for that item most of the time. +FRONT_FOR = { + 'flint_and_steel': ['air', 'air', 'air', 'campfire', 'soul_campfire', 'candle', 'blue_candle', 'candle_cake', + 'red_candle_cake', 'tnt', 'stone', 'obsidian', 'netherrack', 'water'], + 'glowstone': ['respawn_anchor', 'respawn_anchor', 'respawn_anchor', 'air', 'stone', 'glowstone'], + 'bone_meal': ['grass_block', 'wheat', 'carrots', 'potatoes', 'beetroots', 'oak_sapling', 'birch_sapling', 'spruce_sapling', + 'cherry_sapling', 'mangrove_propagule', 'moss_block', 'sweet_berry_bush', 'cocoa', 'bamboo_sapling', 'bamboo', + 'pumpkin_stem', 'melon_stem', 'torchflower_crop', 'pitcher_crop', 'rooted_dirt', 'glow_lichen', 'small_dripleaf', + 'big_dripleaf', 'crimson_nylium', 'warped_fungus', 'brown_mushroom', 'kelp', 'sea_pickle', 'seagrass', 'water', + 'short_grass', 'fern', 'dandelion', 'pink_petals', 'cave_vines', 'twisting_vines', 'weeping_vines', 'azalea', + 'pale_moss_block', 'stone', 'air'], + 'honeycomb': ['copper_block', 'exposed_copper', 'weathered_cut_copper', 'oxidized_cut_copper_slab', 'copper_door', + 'exposed_copper_trapdoor', 'copper_bulb', 'lightning_rod', 'waxed_copper_block', 'copper_grate', 'stone', 'air'], + 'glass_bottle': ['beehive', 'beehive', 'bee_nest', 'bee_nest', 'water', 'water', 'air', 'stone', 'seagrass'], + 'potion': ['dirt', 'coarse_dirt', 'rooted_dirt', 'dirt', 'stone', 'air', 'mud', 'water'], + 'shears': ['beehive', 'bee_nest', 'beehive', 'air', 'air', 'stone', 'pumpkin'], + 'brush': ['air', 'air', 'suspicious_sand', 'stone'], + 'tnt': ['air', 'air', 'stone', 'water', 'tnt'], + 'shulker_box': ['air', 'air', 'stone', 'water', 'short_grass', 'snow'], + 'carved_pumpkin': ['air', 'air', 'stone', 'snow'], + 'wither_skeleton_skull': ['air', 'air', 'stone'], + 'armor_stand': ['air', 'air', 'stone', 'water', 'short_grass'], +} +# Structures a behaviour completes (golems, the wither, a nether portal). +SCENES = {'carved_pumpkin': ['snow_golem', 'iron_golem'], 'wither_skeleton_skull': ['wither'], 'flint_and_steel': ['portal']} BELOW = ['air', 'stone', 'water', 'rail', 'grass_block', 'soul_sand', 'dirt', 'farmland', 'netherrack'] TARGETS = { 'iron_helmet': ['zombie', 'skeleton', 'armor_stand', 'pig'], + 'chainmail_helmet': ['zombie', 'armor_stand', 'villager'], 'diamond_chestplate': ['zombie', 'armor_stand'], 'leather_leggings': ['skeleton', 'armor_stand'], 'golden_boots': ['zombie', 'armor_stand'], + 'netherite_boots': ['zombie', 'piglin', 'armor_stand'], 'turtle_helmet': ['armor_stand', 'zombie'], - 'saddle': ['pig', 'horse', 'strider', 'donkey', 'camel', 'llama'], + 'saddle': ['pig', 'horse', 'strider', 'donkey', 'camel', 'llama', 'mule', 'skeleton_horse', 'nautilus'], 'iron_horse_armor': ['horse', 'donkey'], + 'diamond_horse_armor': ['horse', 'zombie_horse'], + 'leather_horse_armor': ['horse'], 'white_carpet': ['llama', 'horse', 'trader_llama'], - 'chest': ['donkey', 'mule', 'llama', 'horse'], + 'red_carpet': ['llama', 'trader_llama'], + 'chest': ['donkey', 'mule', 'llama', 'horse', 'trader_llama'], 'carved_pumpkin': ['zombie', 'armor_stand', 'snow_golem'], 'elytra': ['armor_stand', 'zombie'], 'wolf_armor': ['wolf'], - 'shears': ['sheep', 'sheep', 'mooshroom', 'bogged', 'snow_golem'], + 'white_harness': ['happy_ghast'], + 'shears': ['sheep', 'sheep', 'mooshroom', 'bogged', 'snow_golem', 'sulfur_cube', 'pig', 'cow'], 'brush': ['armadillo'], 'tnt': ['sulfur_cube'], 'flint_and_steel': ['sulfur_cube'], - 'stone': ['sulfur_cube'], 'player_head': ['armor_stand', 'zombie'], + 'zombie_head': ['armor_stand', 'skeleton'], } +for _block in ('stone', 'slime_block', 'oak_planks', 'packed_ice', 'soul_sand', 'magma_block', 'white_wool', 'mud', 'andesite', + 'iron_block', 'mushroom_stem', 'honeycomb_block', 'tube_coral_block'): + TARGETS[_block] = ['sulfur_cube'] +SWALLOWED = ['tnt', 'stone', 'slime_block', 'magma_block', 'white_wool', 'iron_block'] + + +def item_choice(rng, kind): + item = rng.choice(ITEMS[kind]) + if item == 'BOAT': + return plain(rng.choice(WOODS) + '_boat') + if item == 'CHEST_BOAT': + return plain(rng.choice(WOODS) + '_chest_boat') + return item class States: @@ -110,6 +170,47 @@ class States: return rng.choice(self.facts.by_block[name]) +def build_scene(cells, states, rng, scene, front, facing): + """Put the rest of a structure the behaviour's block completes around the front.""" + x, y, z = front + across = rng.choice([(1, 0, 0), (0, 0, 1)]) + + def put(at, name, **props): + if at != (0, 0, 0) and at != front: + h.put(cells, at, states.get(name, **props)) + + def side(at, k): + return (at[0] + across[0] * k, at[1], at[2] + across[2] * k) + if scene == 'snow_golem': + put((x, y - 1, z), 'snow_block') + put((x, y - 2, z), 'snow_block') + elif scene == 'iron_golem': + put((x, y - 1, z), 'iron_block') + put((x, y - 2, z), 'iron_block') + put(side((x, y - 1, z), -1), 'iron_block') + put(side((x, y - 1, z), 1), 'iron_block') + elif scene == 'wither': + put((x, y - 1, z), 'soul_sand') + put((x, y - 2, z), 'soul_sand') + put(side((x, y - 1, z), -1), rng.choice(['soul_sand', 'soul_soil'])) + put(side((x, y - 1, z), 1), 'soul_sand') + put(side((x, y, z), -1), 'wither_skeleton_skull') + put(side((x, y, z), 1), rng.choice(['wither_skeleton_skull', 'wither_skeleton_skull', 'skeleton_skull'])) + elif scene == 'portal': + # An obsidian frame around a 2x3 inside that holds the front block. + width, height = rng.choice([(2, 3), (2, 3), (3, 4)]) + left, low = rng.randrange(width), rng.randrange(height) + for i in range(-1, width + 1): + for j in range(-1, height + 1): + at = side((x, y + j - low, z), i - left) + edge = i in (-1, width) or j in (-1, height) + if edge and not (i in (-1, width) and j in (-1, height)): + put(at, 'obsidian') + elif not edge: + if at not in ((0, 0, 0), front): + h.put(cells, at, states.get('air')) + + def front_state(states, rng, name): """Mostly the default state, sometimes a random one (charges, honey, ages, lit...).""" if name in ('respawn_anchor', 'beehive', 'bee_nest', 'campfire', 'candle', 'wheat', 'sweet_berry_bush', 'rail', 'powered_rail', @@ -124,28 +225,33 @@ def cases(facts, count, seed, only): kinds = list(ITEMS) for n in range(count): kind = kinds[n % len(kinds)] - item = rng.choice(ITEMS[kind]) + item = item_choice(rng, kind) if only and only not in item: continue + bare = item.split('"')[1].split(':')[1] facing = rng.choice(DIRECTIONS) block = 'dropper' if rng.random() < 0.12 else 'dispenser' cells = [[0, 0, 0, states.get(block, facing=facing, triggered='false')]] dx, dy, dz = STEP[facing] front = (dx, dy, dz) below = (dx, dy - 1, dz) - front_name = rng.choice(FRONT[kind]) + palette = FRONT_FOR[bare] if bare in FRONT_FOR and rng.random() < 0.8 else FRONT[kind] + front_name = rng.choice(palette) if front_name != 'air': h.put(cells, front, front_state(states, rng, front_name)) if below != (0, 0, 0) and rng.random() < 0.8: name = rng.choice(BELOW) if name != 'air': h.put(cells, below, front_state(states, rng, name)) + scene = rng.choice(SCENES[bare]) if bare in SCENES and rng.random() < 0.45 else None # A few random blocks around (portal frames, fire supports, neighbours). for _ in range(rng.randrange(0, 6)): at = (rng.randrange(-2, 3), rng.randrange(-2, 3), rng.randrange(-2, 3)) if at in ((0, 0, 0), front): continue h.put(cells, at, states.get(rng.choice(['stone', 'obsidian', 'dirt', 'water', 'soul_sand', 'grass_block', 'oak_planks']))) + if scene: + build_scene(cells, states, rng, scene, front, facing) items = [] slots = rng.sample(range(9), rng.choice([1, 1, 1, 2, 3, 9])) for i, slot in enumerate(slots): @@ -155,8 +261,7 @@ def cases(facts, count, seed, only): case.pop('thundering', None) if rng.random() < 0.08: case['rules'] = {'tnt_explodes': False} - bare = item.split('"')[1].split(':')[1] - if kind == 'equipment' or (bare in TARGETS and rng.random() < 0.5): + if kind in ('equipment', 'swallow') or (bare in TARGETS and rng.random() < 0.5): entities = [] for _ in range(rng.choice([1, 1, 1, 2])): target = {'type': 'minecraft:' + rng.choice(TARGETS.get(bare, ['zombie'])), @@ -165,8 +270,15 @@ def cases(facts, count, seed, only): if rng.random() < odds: target[flag] = True if target['type'] == 'minecraft:sulfur_cube' and rng.random() < 0.5: - target['equip'] = {'body': plain(rng.choice(['tnt', 'stone', 'slime_block']))} + target['equip'] = {'body': plain(rng.choice(SWALLOWED))} + # Leads: tied to an earlier target, or that target holds this one's. + if entities and rng.random() < 0.3: + target['leash'] = rng.randrange(len(entities)) entities.append(target) + if bare == 'shears' and len(entities) == 1 and rng.random() < 0.3: + # A holder out of the dispenser's reach whose lead runs to the target. + entities.append({'type': 'minecraft:' + rng.choice(['cow', 'wolf']), 'pos': [dx + 3.5, dy, dz + 3.5], 'yaw': 0.0}) + entities[0]['leash'] = 1 case['entities'] = entities if block == 'dropper' and rng.random() < 0.5: h.put(cells, front, states.get(rng.choice(['chest', 'hopper', 'barrel', 'dispenser']))) @@ -233,7 +345,7 @@ def main(): parser = h.argument_parser(__doc__) args = parser.parse_args() if args.count == 40000: - args.count = 3000 + args.count = 6000 lock, jars, libraries = base.verify_artifacts() facts = h.load_facts() resources = lab.LOCAL / 'horizon-client' / lock['minecraft']['version'] From 49ecc8a7c3692a51cada614068a86eac829cb5b9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:40:30 +0200 Subject: [PATCH 07/24] Keep the fields DataFixerUpper's remainders bring back when upgrading saves DataFixerUpper reads typed fields without taking them out of a record's remainder, so a typed field a fix drops comes back on write with its value from the record's last read: entities keep their old ArmorItems, HandItems, body_armor_item, saddle, Trident and Particle next to the new fields, as vanilla 26.3 writes them. Fixes that write and read entities refresh that map. The area effect cloud particle moves to custom_particle (4420), boats keep Type (BoatSplitFix's remainder update misses), and region files with a short header read as empty, as vanilla's RegionFile does. The sample worlds now use the item formats each version accepts and enable its experiments. Co-Authored-By: Claude Opus 5.5 --- tools/datafix_scene.py | 54 +++++++++++++++++++++++++++++++++++------ tools/datafix_worlds.py | 11 ++++++--- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/tools/datafix_scene.py b/tools/datafix_scene.py index 270078d4e..5a5fd8fda 100644 --- a/tools/datafix_scene.py +++ b/tools/datafix_scene.py @@ -119,8 +119,45 @@ ITEMS_C = [ ] -def items(e): - return ITEMS_A if e == A else ITEMS_B if e == B else ITEMS_C +def experiments(version): + """Experimental data packs a world of `version` turns on, so that the blocks behind + them exist and the feature-flag fixes have flags to remove.""" + packs = [] + if 3698 <= version < 3939: + packs.append('update_1_21') + if 3700 <= version < 4067: + packs.append('bundle') + if 4080 <= version < 4180: + packs.append('winter_drop') + return packs + + +def items(e, version=None): + """The sample stacks of an era, in the exact component formats `version` accepts.""" + stacks = ITEMS_A if e == A else ITEMS_B if e == B else ITEMS_C + if version is None: + return stacks + return [versioned(s, version) for s in stacks] + + +def versioned(stack, version): + """Rewrite the components whose format changed inside an era.""" + if version >= 4541: # chain renamed to iron_chain + stack = stack.replace('"minecraft:chain"', '"minecraft:iron_chain"') + if '"minecraft:attribute_modifiers"' in stack: + if version < 3945: # 1.20.5: modifiers are named and carry a UUID + stack = stack.replace('id:"minecraft:x",', 'uuid:[I;1,2,3,4],name:"x",') + if version >= 4055: # 1.21.2: attribute ids lose their generic. prefix + stack = stack.replace('minecraft:generic.attack_damage', 'minecraft:attack_damage') + if version >= 4068: # 1.21.2: a lock is an item predicate + stack = stack.replace('"minecraft:lock":"key"', '"minecraft:lock":{items:"minecraft:stick"}') + if version >= 4064: # 1.21.2: fire_resistant became damage_resistant + stack = stack.replace('"minecraft:fire_resistant":{}', '"minecraft:damage_resistant":{types:"#minecraft:is_fire"}') + if version >= 4059: # 1.21.2: food lost eat_seconds (now in consumable) + stack = stack.replace(',eat_seconds:1.0f', '') + if version >= 4175: # 1.21.4: custom model data holds lists + stack = stack.replace('"minecraft:custom_model_data":5', '"minecraft:custom_model_data":{floats:[5.0f]}') + return stack def slotted(stacks): @@ -130,9 +167,9 @@ def slotted(stacks): return '[' + ','.join(out) + ']' -def chests(e): +def chests(e, version=None): """Chests holding every sample item (27 per chest).""" - st = items(e) + st = items(e, version) return [st[i:i + 27] for i in range(0, len(st), 27)] @@ -140,7 +177,7 @@ def chests(e): def block_entities(e, version): name = lambda s: text(e, s) out = [] - for group in chests(e): + for group in chests(e, version): 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"\',\'""\',\'""\',\'""\']},' @@ -284,12 +321,13 @@ def entities(e, version): return out -def player_commands(e, name): +def player_commands(e, name, version=None): """Give the joined bot a varied inventory and ender chest.""" out = [] - for i, stack in enumerate(items(e)[:30]): + stacks = items(e, version) + for i, stack in enumerate(stacks[: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} enderchest.0 with {give_form(e, stacks[0])}') out.append(f'item replace entity {name} armor.head with minecraft:iron_helmet') out.append(f'xp set {name} 5 levels') return out diff --git a/tools/datafix_worlds.py b/tools/datafix_worlds.py index e0f904241..1b9553977 100644 --- a/tools/datafix_worlds.py +++ b/tools/datafix_worlds.py @@ -33,7 +33,8 @@ 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'] +VERSIONS = ['1.20.1', '1.20.2', '1.20.4', '1.20.5', '1.20.6', '1.21.1', '1.21.3', '1.21.4', '1.21.5', '1.21.6', '1.21.8', + '1.21.9', '1.21.10', '1.21.11', '26.1', '26.1.2', '26.2'] MANIFEST = 'https://piston-meta.mojang.com/mc/game/version_manifest_v2.json' BOT = 'Upgrader' UUID_BASE = 0x6000 @@ -56,12 +57,14 @@ def server_jar(version): class Server: - def __init__(self, directory, jar, port): + def __init__(self, directory, jar, port, packs=()): 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'} + if packs: + props['initial-enabled-packs'] = ','.join(['vanilla', *packs]) (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) @@ -270,7 +273,7 @@ def build(version, force=False): work = LOCAL / 'gen' / version shutil.rmtree(work, ignore_errors=True) port = 30000 + (os.getpid() + data_version) % 20000 - server = Server(work, jar, port) + server = Server(work, jar, port, scene.experiments(data_version)) problems = [] def run(command): @@ -293,7 +296,7 @@ def build(version, force=False): run('setworldspawn 20 101 20') bot = Bot(port, protocol, BOT) time.sleep(3) - for command in scene.player_commands(era, BOT): + for command in scene.player_commands(era, BOT, data_version): run(command) time.sleep(3) run('save-all flush') From c626f696f55593bdfa4dda320278a52a1c24a55b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:41:00 +0200 Subject: [PATCH 08/24] Set dispensed mobs down as EntityType.getYOffset does Only the collision shapes overlapping the spawn block stop the fall, so a wide mob beside the dispenser no longer lands a block high. Spawn egg cases compare that the new mob speaks and where, not which of its voices its own unseeded random picked. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/dispenser.rs | 21 ++++++++++++++++----- tools/test_dispenser_parity.py | 15 ++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index 26f5e0b50..bb987a930 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -682,23 +682,34 @@ impl Game<'_> { } out } - /// The Y offset `EntityType.getYOffset` finds for a box set down into a block. + /// `EntityType.getYOffset(level, pos, false, box)`: the entity box set on + /// top of the block (`setPos(x + 0.5, y + 1, z + 0.5)`) falls one block + /// against the collision shapes overlapping that block alone. fn drop_offset(&self, width: f64, height: f64, p: Pos) -> f64 { + use horizon_world::{block_collision_scan::BlockCollisionScan, collision_context::CollisionContext}; let h = width / 2.0; let (x, z) = (f64::from(p.0) + 0.5, f64::from(p.2) + 0.5); let y = f64::from(p.1) + 1.0; - let bounds = Aabb::new( + let entity = Aabb::new( Vec3::new(x - h, y, z - h), Vec3::new(x + h, y + height, z + h), ); + let cell = Aabb::new( + Vec3::new(f64::from(p.0), f64::from(p.1), f64::from(p.2)), + Vec3::new(f64::from(p.0) + 1.0, f64::from(p.1) + 1.0, f64::from(p.2) + 1.0), + ); let mut scene = SceneCollisions { world: &self.world, binding: self.collision, }; - match scene.resolve(bounds, Vec3::new(0.0, -1.0, 0.0), 0.0, false) { - Ok(moved) => 1.0 + moved.y, - Err(_) => 0.0, + let mut amount = -1.0; + // Level.getCollisions(null, aabb): CollisionContext.empty(). + if let Ok(scan) = BlockCollisionScan::new(&mut scene, CollisionContext::empty(false), cell, false, 1_000_000) { + for hit in scan.flatten() { + amount = hit.shape.collide(horizon_core::geometry::Axis::Y, entity, amount); + } } + 1.0 + amount } /// `EntityType.spawn(.., pos, reason, tryMoveDown, false)` for a mob. fn spawn_mob_at(&mut self, kind: &str, p: Pos, try_move_down: bool) -> Option { diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index 33792488d..4dbb83b13 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -297,6 +297,13 @@ FINALIZED = ('_spawn_egg', 'salmon_bucket', 'cod_bucket', 'pufferfish_bucket', ' def finalizes(case): return any(any(tag in i[4] for tag in FINALIZED) for i in case['items'] if i[:3] == [0, 0, 0]) +def voice(line): + parts = line.split(' ') + if parts[0] == 'S' and len(parts) == 3 and parts[2].lstrip('~').startswith('minecraft:entity.'): + parts[2] = 'entity-voice' + return ' '.join(parts) + + def run(command, **kwargs): done = subprocess.run(command, capture_output=True, text=True, **kwargs) if done.returncode: @@ -378,9 +385,11 @@ def main(): continue if finalizes(case): unseeded += 1 - # The spawned mob's variant (and so its voice) is chosen in finalizeSpawn too. - a = [line for line in a if not line.startswith('rng ') and not line.endswith('.ambient')] - b = [line for line in b if not line.startswith('rng ') and not line.endswith('.ambient')] + # EntityType.spawn plays the new mob's ambient sound: which one (a + # wolf pants or barks, in its sound variant) comes from the mob's + # own unseeded random, so only that a voice plays, and where, is compared. + a = [voice(line) for line in a if not line.startswith('rng ')] + b = [voice(line) for line in b if not line.startswith('rng ')] if not same(a, b): item = next(i[4] for i in case['items'] if i[:3] == [0, 0, 0]) block = facts.states[case['cells'][0][3]][0].split(':')[1] From 3ab461cffcd529c600e30da5a9d1834f055475bd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:44:02 +0200 Subject: [PATCH 09/24] Upgrade older worlds when Horizon opens them, as 26.3 does Opening a world saved by an older version runs the file fixes, level.dat and the DataFixers upgrades of every file instead of refusing it. The save round-trip harness treats a lit campfire's cooking progress as a timer in the vanilla views: Horizon saves it as loaded (stage 3 checks it exactly), and it moves on by however many ticks each vanilla server ran before data get. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 2 +- crates/horizon-client/src/save.rs | 43 +++++++++++++++++++++-------- docs/world-data.md | 5 +--- tools/test_save_roundtrip_parity.py | 5 +++- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e83908171..0c13caf7f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4856,8 +4856,8 @@ impl Game<'_> { creative: bool, ) -> Result<()> { let save_root = std::env::current_dir()?.join("saves").join(folder); + horizon_client::save::upgrade_older_world(&save_root)?; horizon_client::save::migrate_legacy(&save_root)?; - horizon_client::save::check_data_version(&save_root)?; self.session_lock = None; self.session_lock = Some(horizon_client::save::SessionLock::acquire(&save_root)?); self.persist = Default::default(); diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 0211673ab..32f904b5b 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -111,10 +111,13 @@ impl SessionLock { Ok(Self(file)) } } -/// Refuse a world last saved by an older game version: chunks are only read -/// at the current DataVersion, and the DataFixerUpper chain is not ported. -/// `tools/upgrade_world.py` runs vanilla's own upgrader (`--forceUpgrade`). -pub fn check_data_version(world: &Path) -> Result<()> { +/// Bring a world last saved by an older game version to the current format, as +/// 26.3 does when it opens one: the file fixes and level.dat first +/// (`FileFixerUpper`), then the DataFixers upgrades of every chunk, entity chunk, +/// POI chunk, player, saved data, advancements and statistics file. Vanilla runs +/// the latter as it reads each file; Horizon runs them all on opening, which reads +/// the same data. Worlds older than the ported fixes (1.20) are refused. +pub fn upgrade_older_world(world: &Path) -> Result<()> { let Some(root) = read_dat(&world.join("level.dat")) else { return Ok(()); }; @@ -122,14 +125,20 @@ pub fn check_data_version(world: &Path) -> Result<()> { .get("Data") .and_then(|d| number(d.get("DataVersion"))) .unwrap_or(i64::from(DATA_VERSION)); - if version < i64::from(DATA_VERSION) { + if version >= i64::from(DATA_VERSION) { + return Ok(()); + } + if version < i64::from(horizon_world::datafix::OLDEST_SUPPORTED) { return Err(format!( - "{} was saved with DataVersion {version} (this game is {DATA_VERSION}); \ - upgrade it first with tools/upgrade_world.py", - world.display() + "{} was saved with DataVersion {version}; worlds older than DataVersion {} (1.20) \ + cannot be upgraded yet (tools/upgrade_world.py runs vanilla's own upgrader)", + world.display(), + horizon_world::datafix::OLDEST_SUPPORTED ) .into()); } + horizon_world::datafix::files::upgrade_world(world) + .map_err(|e| format!("upgrading {}: {e}", world.display()))?; Ok(()) } /// Move a world saved by an older Horizon build (pre-26.3 layout with a @@ -2181,14 +2190,24 @@ impl ChunkGenerator for Persistent { #[cfg(test)] mod tests { #[test] - fn old_data_version_is_refused() { + fn older_worlds_are_upgraded_on_opening() { let dir = std::env::temp_dir().join(format!("horizon-dv-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); let level = |v: i32| Tag::compound([("Data", Tag::compound([("DataVersion", Tag::Int(v))]))]); + let version = |dir: &std::path::Path| { + super::read_dat(&dir.join("level.dat")) + .and_then(|r| super::number(r.get("Data")?.get("DataVersion"))) + }; + // Older than the ported fixes: refused, left as it is. + super::write_dat(&dir.join("level.dat"), level(3000)).unwrap(); + assert!(super::upgrade_older_world(&dir).is_err()); + assert_eq!(version(&dir), Some(3000)); + // 1.21.1: upgraded to the current version. super::write_dat(&dir.join("level.dat"), level(3955)).unwrap(); - assert!(super::check_data_version(&dir).is_err()); - super::write_dat(&dir.join("level.dat"), level(super::DATA_VERSION)).unwrap(); - assert!(super::check_data_version(&dir).is_ok()); + super::upgrade_older_world(&dir).unwrap(); + assert_eq!(version(&dir), Some(i64::from(super::DATA_VERSION))); + super::upgrade_older_world(&dir).unwrap(); let _ = std::fs::remove_dir_all(&dir); } #[test] diff --git a/docs/world-data.md b/docs/world-data.md index cbd068131..57543de8f 100644 --- a/docs/world-data.md +++ b/docs/world-data.md @@ -68,10 +68,7 @@ The registry loader checks data version, duplicate states, default IDs and block-name coverage. The local oracle exports all 35,723 state identities and defaults from vanilla. This imports identities, not block implementations, shapes or behavior. Registry exports and game-derived fixtures stay in `.local/`. -Chunk access requires data version 5023. The DataFixerUpper chain is not ported: -opening a world whose `level.dat` is older fails with a message pointing at -`tools/upgrade_world.py`, which runs vanilla's own `--forceUpgrade` on a copy -(exercised only against a current-version world; no older server jar is local). `BlockRegistry::resolve` rejects invalid property descriptors. The separate +Chunk access requires data version 5023. A world saved by an older version is upgraded when it is opened, as 26.3 does: the file fixes (`FileFixerUpper`) and level.dat, then vanilla's DataFixers over every chunk, entity chunk, POI chunk, player, saved data, advancements and statistics file (`horizon_world::datafix`; vanilla upgrades each file as it reads it, Horizon all of them on opening). `tools/test_datafix_parity.py` compares the result with vanilla's own upgrade over worlds written by older server jars. Worlds older than the ported fixes are refused with a message pointing at `tools/upgrade_world.py`, which runs vanilla's `--forceUpgrade` on a copy. `BlockRegistry::resolve` rejects invalid property descriptors. The separate [block-state JSON codec](block-state-codecs.md) reproduces vanilla property fallback for compact and full JSON forms. diff --git a/tools/test_save_roundtrip_parity.py b/tools/test_save_roundtrip_parity.py index 126d39b01..a3798b28c 100644 --- a/tools/test_save_roundtrip_parity.py +++ b/tools/test_save_roundtrip_parity.py @@ -547,7 +547,10 @@ def main(): failures += 1 volatile = re.compile(r'(cooking_time_spent|lit_time_remaining|ticks_since_song_started|Age|BrewTime|' r'ticks_in_hive|triggered|state_updating_resumes_at|LastExecution|last_vibration_frequency|warning_level): -?[0-9]+[a-zA-Z]?') - volatile2 = re.compile(r'(RecipesUsed: \{[^}]*\}|listener: \{.*\}(?=, x))') + # A lit campfire keeps cooking while each server runs before `data get`: its + # progress counters advance by however many ticks that took (stage 3 checks + # that Horizon saves them exactly as loaded). + volatile2 = re.compile(r'(RecipesUsed: \{[^}]*\}|listener: \{.*\}(?=, x)|CookingTimes: \[I;[^\]]*\])') # The lit furnace keeps smelting while each game runs: its slots move on. volatile_items = re.compile(r'Items: \[[^\]]*\]') for label in views0: From 139c5abd1cce97b2e2fc3b8c7ce47a918ea3e258 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:50:56 +0200 Subject: [PATCH 10/24] Port the DataFixers of 1.19 to 1.20 for worlds from 1.18.2 on The fixes from 3076 to 3459 (game event, cat, frog, painting, goat, goat horn, filtered text, POI, blending, spawn egg, effect duration, archaeology, double-sided signs, the 1.20 experiment and chunk statuses), the block entities their schemas add, and block entities' remainders (a decorated pot's shards stay next to its sherds). The sample world generator logs in to 1.18.2 to 1.19.4 servers and gates the scene by version. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/datafix/fixes_a.rs | 2 +- crates/horizon-world/src/datafix/fixes_old.rs | 555 ++++++++++++++++++ crates/horizon-world/src/datafix/mod.rs | 20 +- crates/horizon-world/src/datafix/schema.rs | 13 +- tools/datafix_scene.py | 43 +- tools/datafix_worlds.py | 12 +- 6 files changed, 630 insertions(+), 15 deletions(-) create mode 100644 crates/horizon-world/src/datafix/fixes_old.rs diff --git a/crates/horizon-world/src/datafix/fixes_a.rs b/crates/horizon-world/src/datafix/fixes_a.rs index 89a5bfee5..433a2796f 100644 --- a/crates/horizon-world/src/datafix/fixes_a.rs +++ b/crates/horizon-world/src/datafix/fixes_a.rs @@ -48,7 +48,7 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { // ------------------------------------------------------------------ 3564 /// `BlockEntitySignDoubleSidedEditableTextFix.FIELDS_TO_DROP`. -const SIGN_FIELDS_TO_DROP: [&str; 10] = [ +pub(super) const SIGN_FIELDS_TO_DROP: [&str; 10] = [ "Text1", "Text2", "Text3", "Text4", "FilteredText1", "FilteredText2", "FilteredText3", "FilteredText4", "Color", "GlowingText", ]; diff --git a/crates/horizon-world/src/datafix/fixes_old.rs b/crates/horizon-world/src/datafix/fixes_old.rs new file mode 100644 index 000000000..b3835bc30 --- /dev/null +++ b/crates/horizon-world/src/datafix/fixes_old.rs @@ -0,0 +1,555 @@ +//! Fixes of 1.19 to 1.20 (keys 3076 to 3459): what worlds saved by 1.18.2 to +//! 1.19.4 need before the fixes of [`super::fixes_a`]. The options fixes of +//! this range (3201, 3214, 3319) are client settings, not world data. +use super::dynamic::{as_number, as_str, is_list, namespaced, remove, rename, set, stream, string, update}; +use super::fixes_a::fix_block_pos; +use super::fixes_b::feature_flag_remove; +use super::schema::{Key, Ty, key}; +use super::{Cx, FixFn, id_is, renamer, reread_as}; +use crate::nbt::{Compound, Tag}; + +pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { + out.push((key(3077, 0), chunk_delete_ignored_light)); + out.push((key(3084, 0), |cx| cx.rename(Ty::GameEventName, renamer(GAME_EVENTS)))); + out.push((key(3086, 0), |cx| entity_variant(cx, "minecraft:cat", "CatType", cat_type))); + out.push((key(3086, 0), |cx| { + criteria_rename(cx, "minecraft:husbandry/complete_catalogue", &|s| { + CAT_TEXTURES.iter().find(|(from, _)| *from == s).map(|(_, to)| (*to).to_owned()) + }); + })); + out.push((key(3087, 0), |cx| { + entity_variant(cx, "minecraft:frog", "Variant", |n| match n { + 0 => Some("minecraft:temperate"), + 1 => Some("minecraft:warm"), + 2 => Some("minecraft:cold"), + _ => None, + }) + })); + out.push((key(3090, 0), |cx| { + cx.entity("minecraft:painting", |e| { + rename(e, "Motive", "variant"); + rename(e, "Facing", "facing"); + }) + })); + out.push((key(3093, 0), |cx| { + cx.entity("minecraft:goat", |e| { + set(e, "HasLeftHorn", Tag::Byte(1)); + set(e, "HasRightHorn", Tag::Byte(1)); + }) + })); + out.push((key(3094, 0), goat_horn_id)); + out.push((key(3097, 0), filtered_books)); + out.push((key(3097, 0), filtered_signs)); + out.push((key(3097, 0), |cx| { + cx.entity("minecraft:cat", |e| { + if as_str(e.get("variant")).as_deref() == Some("minecraft:british") { + set(e, "variant", string("minecraft:british_shorthair")); + } + }) + })); + out.push((key(3097, 0), |cx| { + criteria_rename(cx, "minecraft:husbandry/complete_catalogue", &|s| { + (s == "minecraft:british").then(|| "minecraft:british_shorthair".to_owned()) + }); + })); + out.push((key(3097, 0), |cx| { + poi_records(cx, |r| { + !matches!(as_str(r.get("type")).as_deref(), Some("minecraft:unemployed" | "minecraft:nitwit") | None) + }) + })); + out.push((key(3108, 0), |cx| { + cx.each_out(Ty::Chunk, |c| { + let dimension = c.get("__context").and_then(|x| as_str(x.get("dimension"))); + if dimension.as_deref() != Some("minecraft:overworld") { + remove(c, "blending_data"); + } + }) + })); + out.push((key(3209, 0), pig_spawn_egg)); + out.push((key(3322, 0), effect_duration)); + out.push((key(3438, 0), |cx| { + cx.each(Ty::BlockEntity, |be| { + if id_is(be, "minecraft:suspicious_sand") { + set(be, "id", string("minecraft:brushable_block")); + } + }) + })); + out.push((key(3438, 0), |cx| { + cx.each_out(Ty::BlockEntity, |be| { + if id_is(be, "minecraft:brushable_block") { + rename(be, "loot_table", "LootTable"); + rename(be, "loot_table_seed", "LootTableSeed"); + } + }) + })); + out.push((key(3438, 0), |cx| cx.rename(Ty::ItemName, renamer(POTTERY_SHARDS)))); + out.push((key(3439, 0), |cx| sign_double_sided(cx, "minecraft:sign"))); + out.push((key(3439, 1), |cx| sign_double_sided(cx, "minecraft:hanging_sign"))); + out.push((key(3440, 0), multi_noise_preset)); + out.push((key(3440, 0), |cx| feature_flag_remove(cx, &["minecraft:update_1_20"]))); + out.push((key(3447, 0), |cx| { + cx.rename(Ty::ItemName, |id| { + let id = namespaced(id); + POTTERY_SHERDS.iter().find(|(from, _)| *from == id).map(|(_, to)| *to) + }) + })); + // DecoratedPotFieldRenameFix converts unchecked: the typed shards are written + // as sherds, and the raw shards come back from the block entity's remainder. + out.push((key(3448, 0), |cx| { + cx.each(Ty::BlockEntity, |be| { + if id_is(be, "minecraft:decorated_pot") + && let Some(shards) = be.get("shards").cloned() + { + set(be, "sherds", shards); + super::drop_typed(be, "shards"); + } + }) + })); + out.push((key(3450, 0), remap_chunk_status)); + out.push((key(3451, 0), chunk_delete_light)); + out.push((key(3459, 0), legacy_dragon_fight)); +} + +// ------------------------------------------------------------------ 3077, 3451 + +/// `ChunkDeleteIgnoredLightDataFix`. +fn chunk_delete_ignored_light(cx: &mut Cx) { + cx.each(Ty::Chunk, |c| { + let light_on = c.get("isLightOn").and_then(as_number).is_some_and(|n| n.double_value() != 0.0); + if !light_on && let Some(Tag::List(sections)) = c.get_mut("sections") { + for s in sections { + remove(s, "BlockLight"); + remove(s, "SkyLight"); + } + } + }); +} + +/// `ChunkDeleteLightFix`. +fn chunk_delete_light(cx: &mut 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"); + } + } + }); +} + +// ------------------------------------------------------------------ 3084 + +const GAME_EVENTS: &[(&str, &str)] = &[ + ("minecraft:block_press", "minecraft:block_activate"), + ("minecraft:block_switch", "minecraft:block_activate"), + ("minecraft:block_unpress", "minecraft:block_deactivate"), + ("minecraft:block_unswitch", "minecraft:block_deactivate"), + ("minecraft:drinking_finish", "minecraft:drink"), + ("minecraft:elytra_free_fall", "minecraft:elytra_glide"), + ("minecraft:entity_damaged", "minecraft:entity_damage"), + ("minecraft:entity_dying", "minecraft:entity_die"), + ("minecraft:entity_killed", "minecraft:entity_die"), + ("minecraft:mob_interact", "minecraft:entity_interact"), + ("minecraft:ravager_roar", "minecraft:entity_roar"), + ("minecraft:ring_bell", "minecraft:block_change"), + ("minecraft:shulker_close", "minecraft:container_close"), + ("minecraft:shulker_open", "minecraft:container_open"), + ("minecraft:wolf_shaking", "minecraft:entity_shake"), +]; + +// ------------------------------------------------------------------ 3086, 3087, 3097 + +fn cat_type(n: i32) -> Option<&'static str> { + Some(match n { + 1 => "minecraft:black", + 2 => "minecraft:red", + 3 => "minecraft:siamese", + 4 => "minecraft:british", + 5 => "minecraft:calico", + 6 => "minecraft:persian", + 7 => "minecraft:ragdoll", + 8 => "minecraft:white", + 9 => "minecraft:jellie", + 10 => "minecraft:all_black", + // The map's default return value. + _ => "minecraft:tabby", + }) +} + +/// `EntityVariantFix`: the numeric variant written to `variant`; the old field +/// stays (`updateAndRename` only sets the new key). An id the map has no entry +/// for gives Java's `null`, which `createString` cannot take: such a variant is +/// left as the number. +fn entity_variant(cx: &mut Cx, id: &str, field: &str, ids: fn(i32) -> Option<&'static str>) { + cx.entity(id, |e| { + let Some(value) = e.get(field).cloned() else { + return; + }; + let new = match as_number(&value).map(|n| ids(n.int_value())) { + Some(Some(name)) => string(name), + _ => value, + }; + set(e, "variant", new); + }); +} + +const CAT_TEXTURES: &[(&str, &str)] = &[ + ("textures/entity/cat/tabby.png", "minecraft:tabby"), + ("textures/entity/cat/black.png", "minecraft:black"), + ("textures/entity/cat/red.png", "minecraft:red"), + ("textures/entity/cat/siamese.png", "minecraft:siamese"), + ("textures/entity/cat/british_shorthair.png", "minecraft:british"), + ("textures/entity/cat/calico.png", "minecraft:calico"), + ("textures/entity/cat/persian.png", "minecraft:persian"), + ("textures/entity/cat/ragdoll.png", "minecraft:ragdoll"), + ("textures/entity/cat/white.png", "minecraft:white"), + ("textures/entity/cat/jellie.png", "minecraft:jellie"), + ("textures/entity/cat/all_black.png", "minecraft:all_black"), +]; + +/// `CriteriaRenameFix`: the criteria keys of one advancement. +fn criteria_rename(cx: &mut Cx, advancement: &str, conversions: &dyn Fn(&str) -> Option) { + cx.each(Ty::Advancements, |v| { + let Some(criteria) = v.get_mut(advancement).and_then(|a| a.get_mut("criteria")) else { + return; + }; + if let Tag::Compound(c) = criteria { + let old = std::mem::take(c); + for (k, value) in old { + let name = k.to_string_lossy(); + let k = conversions(&name).map_or(k, |n| n.as_str().into()); + c.insert(k, value); + } + } + }); +} + +// ------------------------------------------------------------------ 3094, 3097 + +/// The `tag` of every stack whose id passes `filter` (`ItemStackTagFix`). +fn item_tag(cx: &mut Cx, filter: impl Fn(&str) -> bool, mut f: impl FnMut(&mut Tag)) { + cx.item(filter, |s| { + if let Some(tag) = s.get_mut("tag") { + f(tag); + } + }); +} + +const INSTRUMENTS: [&str; 8] = [ + "minecraft:ponder_goat_horn", + "minecraft:sing_goat_horn", + "minecraft:seek_goat_horn", + "minecraft:feel_goat_horn", + "minecraft:admire_goat_horn", + "minecraft:call_goat_horn", + "minecraft:yearn_goat_horn", + "minecraft:dream_goat_horn", +]; + +/// `GoatHornIdFix`. +fn goat_horn_id(cx: &mut Cx) { + item_tag( + cx, + |id| id == "minecraft:goat_horn", + |tag| { + let variant = super::dynamic::as_int(tag.get("SoundVariant"), 0); + let sound = INSTRUMENTS[if (0..8).contains(&variant) { variant as usize } else { 0 }]; + remove(tag, "SoundVariant"); + set(tag, "instrument", string(sound)); + }, + ); +} + +/// `FilteredBooksFix`. +fn filtered_books(cx: &mut Cx) { + item_tag( + cx, + |id| id == "minecraft:writable_book" || id == "minecraft:written_book", + |tag| { + remove(tag, "filtered_title"); + remove(tag, "filtered_pages"); + }, + ); +} + +/// `FilteredSignsFix` (a `NamedEntityWriteReadFix` over signs only). +fn filtered_signs(cx: &mut Cx) { + let k = cx.k; + cx.block_entity("minecraft:sign", |be| { + for i in 1..=4 { + remove(be, &format!("FilteredText{i}")); + } + reread_as(Ty::BlockEntity, k, be); + }); +} + +/// `AbstractPoiSectionFix`: the records of every POI section kept by `keep`. +fn poi_records(cx: &mut Cx, keep: impl Fn(&Tag) -> bool) { + cx.each(Ty::PoiChunk, |poi| { + let Some(Tag::Compound(sections)) = poi.get_mut("Sections") else { + return; + }; + for section in sections.values_mut() { + if let Some(records) = section.get("Records") + && is_list(Some(records)) + { + let kept: Vec = stream(Some(records)).into_iter().filter(|r| keep(r)).collect(); + set(section, "Records", Tag::List(kept)); + } + } + }); +} + +// ------------------------------------------------------------------ 3209 + +/// `ItemStackSpawnEggFix` for `minecraft:pig_spawn_egg`: an egg whose entity has +/// an id becomes that entity's egg. +fn pig_spawn_egg(cx: &mut Cx) { + cx.item( + |id| id == "minecraft:pig_spawn_egg", + |s| { + let entity = s.get("tag").and_then(|t| t.get("EntityTag")).and_then(|e| as_str(e.get("id"))); + if let Some(entity) = entity { + let egg = SPAWN_EGGS + .iter() + .find(|(e, _)| *e == namespaced(&entity)) + .map_or("minecraft:pig_spawn_egg", |(_, egg)| egg); + set(s, "id", string(egg)); + } + }, + ); +} + +const SPAWN_EGGS: &[(&str, &str)] = &[ + ("minecraft:bat", "minecraft:bat_spawn_egg"), + ("minecraft:blaze", "minecraft:blaze_spawn_egg"), + ("minecraft:cave_spider", "minecraft:cave_spider_spawn_egg"), + ("minecraft:chicken", "minecraft:chicken_spawn_egg"), + ("minecraft:cow", "minecraft:cow_spawn_egg"), + ("minecraft:creeper", "minecraft:creeper_spawn_egg"), + ("minecraft:donkey", "minecraft:donkey_spawn_egg"), + ("minecraft:elder_guardian", "minecraft:elder_guardian_spawn_egg"), + ("minecraft:ender_dragon", "minecraft:ender_dragon_spawn_egg"), + ("minecraft:enderman", "minecraft:enderman_spawn_egg"), + ("minecraft:endermite", "minecraft:endermite_spawn_egg"), + ("minecraft:evocation_illager", "minecraft:evocation_illager_spawn_egg"), + ("minecraft:ghast", "minecraft:ghast_spawn_egg"), + ("minecraft:guardian", "minecraft:guardian_spawn_egg"), + ("minecraft:horse", "minecraft:horse_spawn_egg"), + ("minecraft:husk", "minecraft:husk_spawn_egg"), + ("minecraft:iron_golem", "minecraft:iron_golem_spawn_egg"), + ("minecraft:llama", "minecraft:llama_spawn_egg"), + ("minecraft:magma_cube", "minecraft:magma_cube_spawn_egg"), + ("minecraft:mooshroom", "minecraft:mooshroom_spawn_egg"), + ("minecraft:mule", "minecraft:mule_spawn_egg"), + ("minecraft:ocelot", "minecraft:ocelot_spawn_egg"), + ("minecraft:pufferfish", "minecraft:pufferfish_spawn_egg"), + ("minecraft:parrot", "minecraft:parrot_spawn_egg"), + ("minecraft:pig", "minecraft:pig_spawn_egg"), + ("minecraft:polar_bear", "minecraft:polar_bear_spawn_egg"), + ("minecraft:rabbit", "minecraft:rabbit_spawn_egg"), + ("minecraft:sheep", "minecraft:sheep_spawn_egg"), + ("minecraft:shulker", "minecraft:shulker_spawn_egg"), + ("minecraft:silverfish", "minecraft:silverfish_spawn_egg"), + ("minecraft:skeleton", "minecraft:skeleton_spawn_egg"), + ("minecraft:skeleton_horse", "minecraft:skeleton_horse_spawn_egg"), + ("minecraft:slime", "minecraft:slime_spawn_egg"), + ("minecraft:snow_golem", "minecraft:snow_golem_spawn_egg"), + ("minecraft:spider", "minecraft:spider_spawn_egg"), + ("minecraft:squid", "minecraft:squid_spawn_egg"), + ("minecraft:stray", "minecraft:stray_spawn_egg"), + ("minecraft:turtle", "minecraft:turtle_spawn_egg"), + ("minecraft:vex", "minecraft:vex_spawn_egg"), + ("minecraft:villager", "minecraft:villager_spawn_egg"), + ("minecraft:vindication_illager", "minecraft:vindication_illager_spawn_egg"), + ("minecraft:witch", "minecraft:witch_spawn_egg"), + ("minecraft:wither", "minecraft:wither_spawn_egg"), + ("minecraft:wither_skeleton", "minecraft:wither_skeleton_spawn_egg"), + ("minecraft:wolf", "minecraft:wolf_spawn_egg"), + ("minecraft:zombie", "minecraft:zombie_spawn_egg"), + ("minecraft:zombie_horse", "minecraft:zombie_horse_spawn_egg"), + ("minecraft:zombie_pigman", "minecraft:zombie_pigman_spawn_egg"), + ("minecraft:zombie_villager", "minecraft:zombie_villager_spawn_egg"), +]; + +// ------------------------------------------------------------------ 3322 + +const POTION_ITEMS: [&str; 4] = + ["minecraft:potion", "minecraft:splash_potion", "minecraft:lingering_potion", "minecraft:tipped_arrow"]; + +/// `EffectDurationFix.fix`: every effect's factor data counts ticks active. +fn effect_list(list: Tag) -> Tag { + if !is_list(Some(&list)) { + return list; + } + Tag::List( + stream(Some(&list)) + .into_iter() + .map(|mut effect| { + let duration = super::dynamic::as_int(effect.get("Duration"), -1); + update(&mut effect, "FactorCalculationData", |mut data| { + let timestamp = super::dynamic::as_int(data.get("effect_changed_timestamp"), -1); + remove(&mut data, "effect_changed_timestamp"); + set(&mut data, "ticks_active", Tag::Int(timestamp.wrapping_sub(duration))); + data + }); + effect + }) + .collect(), + ) +} + +fn effect_duration(cx: &mut Cx) { + let living = |e: &mut Tag| { + for field in ["Effects", "ActiveEffects", "CustomPotionEffects"] { + update(e, field, effect_list); + } + }; + cx.each(Ty::Entity, living); + cx.each(Ty::Player, living); + item_tag(cx, |id| POTION_ITEMS.contains(&id), |tag| update(tag, "CustomPotionEffects", effect_list)); +} + +// ------------------------------------------------------------------ 3438, 3447 + +const POTTERY_SHARDS: &[(&str, &str)] = &[ + ("minecraft:pottery_shard_archer", "minecraft:archer_pottery_shard"), + ("minecraft:pottery_shard_prize", "minecraft:prize_pottery_shard"), + ("minecraft:pottery_shard_arms_up", "minecraft:arms_up_pottery_shard"), + ("minecraft:pottery_shard_skull", "minecraft:skull_pottery_shard"), +]; + +const POTTERY_SHERDS: &[(&str, &str)] = &[ + ("minecraft:angler_pottery_shard", "minecraft:angler_pottery_sherd"), + ("minecraft:archer_pottery_shard", "minecraft:archer_pottery_sherd"), + ("minecraft:arms_up_pottery_shard", "minecraft:arms_up_pottery_sherd"), + ("minecraft:blade_pottery_shard", "minecraft:blade_pottery_sherd"), + ("minecraft:brewer_pottery_shard", "minecraft:brewer_pottery_sherd"), + ("minecraft:burn_pottery_shard", "minecraft:burn_pottery_sherd"), + ("minecraft:danger_pottery_shard", "minecraft:danger_pottery_sherd"), + ("minecraft:explorer_pottery_shard", "minecraft:explorer_pottery_sherd"), + ("minecraft:friend_pottery_shard", "minecraft:friend_pottery_sherd"), + ("minecraft:heart_pottery_shard", "minecraft:heart_pottery_sherd"), + ("minecraft:heartbreak_pottery_shard", "minecraft:heartbreak_pottery_sherd"), + ("minecraft:howl_pottery_shard", "minecraft:howl_pottery_sherd"), + ("minecraft:miner_pottery_shard", "minecraft:miner_pottery_sherd"), + ("minecraft:mourner_pottery_shard", "minecraft:mourner_pottery_sherd"), + ("minecraft:plenty_pottery_shard", "minecraft:plenty_pottery_sherd"), + ("minecraft:prize_pottery_shard", "minecraft:prize_pottery_sherd"), + ("minecraft:sheaf_pottery_shard", "minecraft:sheaf_pottery_sherd"), + ("minecraft:shelter_pottery_shard", "minecraft:shelter_pottery_sherd"), + ("minecraft:skull_pottery_shard", "minecraft:skull_pottery_sherd"), + ("minecraft:snort_pottery_shard", "minecraft:snort_pottery_sherd"), +]; + +// ------------------------------------------------------------------ 3439 + +/// `LegacyComponentDataFixUtils.createEmptyComponent`. +fn empty_component() -> Tag { + string("{\"text\":\"\"}") +} + +/// `BlockEntitySignDoubleSidedEditableTextFix` (a `NamedEntityWriteReadFix`). +fn sign_double_sided(cx: &mut Cx, id: &str) { + let k = cx.k; + cx.block_entity(id, |be| { + let lines: Vec = + (1..=4).map(|i| be.get(&format!("Text{i}")).cloned().unwrap_or_else(empty_component)).collect(); + let filtered: Vec> = (1..=4).map(|i| be.get(&format!("FilteredText{i}")).cloned()).collect(); + let mut front = Compound::new(); + front.insert("messages".into(), Tag::List(lines.clone())); + front.insert("color".into(), be.get("Color").cloned().unwrap_or_else(|| string("black"))); + front.insert("has_glowing_text".into(), be.get("GlowingText").cloned().unwrap_or(Tag::Byte(0))); + if filtered.iter().any(Option::is_some) { + let messages = + filtered.into_iter().zip(&lines).map(|(f, line)| f.unwrap_or_else(|| line.clone())).collect(); + front.insert("filtered_messages".into(), Tag::List(messages)); + } + set(be, "front_text", Tag::Compound(front)); + let back = Tag::compound([ + ("messages", Tag::List(vec![empty_component(); 4])), + ("color", string("black")), + ("has_glowing_text", Tag::Byte(0)), + ]); + set(be, "back_text", back); + set(be, "is_waxed", Tag::Byte(0)); + set(be, "_filtered_correct", Tag::Byte(1)); + for field in super::fixes_a::SIGN_FIELDS_TO_DROP { + remove(be, field); + } + reread_as(Ty::BlockEntity, k, be); + }); +} + +// ------------------------------------------------------------------ 3440 + +/// `NamespacedTypeRenameFix` over `MULTI_NOISE_BIOME_SOURCE_PARAMETER_LIST`: the +/// preset of a multi-noise biome source in the world generation settings. +fn multi_noise_preset(cx: &mut Cx) { + cx.each(Ty::WorldGenSettings, |v| { + 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; + }; + if as_str(g.get("type")).as_deref() != Some("minecraft:noise") { + continue; + } + let Some(source) = g.get_mut("biome_source") else { + continue; + }; + if as_str(source.get("type")).as_deref() != Some("minecraft:multi_noise") { + continue; + } + if let Some(preset) = as_str(source.get("preset")) + && namespaced(&preset) == "minecraft:overworld_update_1_20" + { + set(source, "preset", string("minecraft:overworld")); + } + } + }); +} + +// ------------------------------------------------------------------ 3450, 3459 + +/// `RemapChunkStatusFix`: statuses namespaced, then mapped. +fn remap_chunk_status(cx: &mut Cx) { + let fix = |v: Tag| match as_str(Some(&v)) { + Some(s) => { + let s = namespaced(&s); + string(match s.as_str() { + "minecraft:liquid_carvers" => "minecraft:carvers", + "minecraft:heightmaps" => "minecraft:spawn", + other => other, + }) + } + None => v, + }; + cx.each(Ty::Chunk, |c| { + update(c, "Status", fix); + if let Some(retrogen) = c.get_mut("below_zero_retrogen") { + update(retrogen, "target_status", fix); + } + }); +} + +/// `LegacyDragonFightFix`. +fn legacy_dragon_fight(cx: &mut Cx) { + cx.each(Ty::Level, |level| { + if level.get("DragonFight").is_some() { + return; + } + let legacy = level + .get("DimensionData") + .and_then(|d| d.get("1")) + .and_then(|d| d.get("DragonFight")) + .cloned() + .unwrap_or_else(super::dynamic::empty_map); + let mut fight = legacy; + update(&mut fight, "ExitPortalLocation", fix_block_pos); + set(level, "DragonFight", fight); + }); +} diff --git a/crates/horizon-world/src/datafix/mod.rs b/crates/horizon-world/src/datafix/mod.rs index a29fc0f21..1e046fced 100644 --- a/crates/horizon-world/src/datafix/mod.rs +++ b/crates/horizon-world/src/datafix/mod.rs @@ -13,6 +13,7 @@ pub mod dynamic; mod fixes_a; mod fixes_b; mod fixes_c; +mod fixes_old; pub mod files; mod items; pub mod json; @@ -25,8 +26,9 @@ 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; +/// Data saved before this DataVersion (1.18.2 is 2975) needs fixes that are not +/// ported (2967 and 2970, and everything before). +pub const OLDEST_SUPPORTED: i32 = 2970; /// `Identifier.tryParse` validity, as `NamespacedSchema.ensureNamespaced` uses it. pub fn identifier(id: &str) -> Option<(String, String)> { @@ -103,7 +105,7 @@ impl Cx<'_> { /// from that read. Each entity keeps that map under this key while fixes run. const LAST_READ: &str = "\u{0}last_read"; -/// Remember an entity's current fields as the map it was read from. +/// Remember a record's current fields as the map it was read from. pub fn mark_read(e: &mut Tag) { if let Tag::Compound(_) = e { let mut raw = e.clone(); @@ -116,7 +118,13 @@ pub fn mark_read(e: &mut Tag) { /// written and read again with the output schema, so it (and every entity inside /// it) now remembers its current fields as the map it was last read from. pub fn reread(k: Key, e: &mut Tag) { - walk(Ty::Entity, e, k, Ty::Entity, &mut mark_read); + reread_as(Ty::Entity, k, e); +} + +/// [`reread`] for a value of another type (a block entity). +pub fn reread_as(ty: Ty, k: Key, v: &mut Tag) { + walk(ty, v, k, Ty::BlockEntity, &mut mark_read); + walk(ty, v, k, Ty::Entity, &mut mark_read); } /// A typed field dropped from an entity: what writing the entity gives back for @@ -158,9 +166,10 @@ pub fn renamer<'a>(table: &'a [(&'a str, &'static str)]) -> impl Fn(&str) -> Opt type FixFn = fn(&mut Cx); -/// Every fix from the 1.20 schemas on, in DataFixers order. +/// Every ported fix (from the 1.19 schemas on), in DataFixers order. fn fixes() -> Vec<(Key, FixFn)> { let mut out = Vec::new(); + fixes_old::register(&mut out); fixes_a::register(&mut out); fixes_b::register(&mut out); fixes_c::register(&mut out); @@ -175,6 +184,7 @@ pub fn update(root: Ty, data: &mut Tag, from: i32, to: i32) { 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)... + walk(root, data, from_key, Ty::BlockEntity, &mut mark_read); walk(root, data, from_key, Ty::Entity, &mut mark_read); read_hooks(root, data, from_key); // ...then every fix above that key, up to the target version... diff --git a/crates/horizon-world/src/datafix/schema.rs b/crates/horizon-world/src/datafix/schema.rs index 33d0963db..5515b6bae 100644 --- a/crates/horizon-world/src/datafix/schema.rs +++ b/crates/horizon-world/src/datafix/schema.rs @@ -509,12 +509,14 @@ impl Walker<'_> { self.field(Ty::ItemStack, s, side); } } - } else { + } else if k >= key(3448, 0) { self.list(Ty::ItemName, v, "sherds"); + } else { + self.list(Ty::ItemName, v, "shards"); } self.field(Ty::ItemStack, v, "item"); } - "minecraft:brushable_block" => self.field(Ty::ItemStack, v, "item"), + "minecraft:brushable_block" | "minecraft:suspicious_sand" => 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 { @@ -749,6 +751,12 @@ impl Walker<'_> { 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:sculk_catalyst" => key(3076, 0), + "minecraft:sculk_shrieker" => key(3078, 0), + "minecraft:hanging_sign" => key(3202, 0), + "minecraft:chiseled_bookshelf" => key(3204, 0), + "minecraft:decorated_pot" => key(3327, 0), + "minecraft:suspicious_sand" => return (key(3327, 0)..key(3438, 0)).contains(&k), "minecraft:crafter" => key(3682, 0), "minecraft:trial_spawner" => key(3689, 0), "minecraft:vault" => key(3807, 0), @@ -758,7 +766,6 @@ pub fn block_entity_known(be: &Tag, k: Key) -> bool { "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, }; diff --git a/tools/datafix_scene.py b/tools/datafix_scene.py index 5a5fd8fda..7f0e047e9 100644 --- a/tools/datafix_scene.py +++ b/tools/datafix_scene.py @@ -58,6 +58,7 @@ ITEMS_A = [ '{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:pig_spawn_egg",Count:1b,tag:{EntityTag:{id:"minecraft:cow"}}}', '{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"}}', @@ -123,6 +124,8 @@ def experiments(version): """Experimental data packs a world of `version` turns on, so that the blocks behind them exist and the feature-flag fixes have flags to remove.""" packs = [] + if 3218 <= version < 3440: + packs.append('update_1_20') if 3698 <= version < 3939: packs.append('update_1_21') if 3700 <= version < 4067: @@ -137,11 +140,27 @@ def items(e, version=None): stacks = ITEMS_A if e == A else ITEMS_B if e == B else ITEMS_C if version is None: return stacks - return [versioned(s, version) for s in stacks] + return [v for v in (versioned(s, version) for s in stacks) if available(v, version)] + + +# Items era A worlds older than each DataVersion do not have. +ITEM_SINCE = { + 'minecraft:goat_horn': 3105, + 'minecraft:decorated_pot': 3327, +} + + +def available(stack, version): + return all(version >= since for item, since in ITEM_SINCE.items() if f'"{item}"' in stack) def versioned(stack, version): """Rewrite the components whose format changed inside an era.""" + if version < 3448: # 1.19.4: pots hold shards, sherds were pottery_shard_* + stack = (stack.replace('sherds:', 'shards:') + .replace('"minecraft:angler_pottery_sherd"', '"minecraft:pottery_shard_prize"') + .replace('"minecraft:arms_up_pottery_sherd"', '"minecraft:pottery_shard_arms_up"') + .replace('"minecraft:skull_pottery_sherd"', '"minecraft:pottery_shard_skull"')) if version >= 4541: # chain renamed to iron_chain stack = stack.replace('"minecraft:chain"', '"minecraft:iron_chain"') if '"minecraft:attribute_modifiers"' in stack: @@ -184,8 +203,13 @@ def block_entities(e, version): '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)] + # Before 1.20 (3439) a sign has one side of loose Text fields. + sign_old = ('{Text1:\'{"text":"one"}\',Text2:\'"two"\',Text3:\'""\',Text4:\'""\',Color:"red",GlowingText:1b,' + 'FilteredText1:\'"x"\'}') + sign = sign_c if e == C else sign_a if version >= 3439 else sign_old + out.append(('oak_sign', sign)) + if version >= 3218: # hanging signs: 1.19.3 with the 1.20 experiment + out.append(('oak_hanging_sign', sign)) if e == A: out += [ ('white_banner', '{Patterns:[{Pattern:"cre",Color:15}],CustomName:\'{"translate":"block.minecraft.ominous_banner"}\'}'), @@ -210,6 +234,9 @@ def block_entities(e, version): ] if version >= 3682: out.append(('crafter', '{Items:[{Slot:0b,id:"minecraft:stone",Count:1b}],triggered:1,crafting_ticks_remaining:2}')) + # Blocks the 1.20 experiment brought in 1.19.3 and 1.19.4. + since = {'decorated_pot': 3327, 'suspicious_sand': 3327, 'chiseled_bookshelf': 3218} + out = [(b, versioned(nbt, version)) for b, nbt in out if version >= since.get(b, 0)] else: out += [ ('white_banner', '{patterns:[{pattern:"minecraft:creeper",color:"black"}],CustomName:%s}' % name("Flag")), @@ -276,7 +303,17 @@ def entities(e, version): ('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}]'), + ('goat', ''), ] + since = {'allay': 3105, 'chest_boat': 3105, 'text_display': 3325, 'item_display': 3325} + out = [(k, x) for k, x in out if version >= since.get(k, 0)] + if version < 3086: # cats before 1.19 have a numeric CatType + out = [(k, 'CatType:1' if k == 'cat' else x) for k, x in out] + if 3105 <= version < 3322: # darkness counts its factor from a timestamp before 1.19.4 + out.append(('pig', 'ActiveEffects:[{Id:33b,Amplifier:0b,Duration:200,ShowParticles:1b,' + 'FactorCalculationData:{padding_duration:22,factor_start:0.0f,factor_target:1.0f,' + 'factor_current:0.0f,effect_changed_timestamp:5,factor_previous_frame:0.0f,' + 'had_effect_last_tick:0b}}]')) return out sword = '{id:"minecraft:iron_sword",count:1}' helmet = '{id:"minecraft:iron_helmet",count:1}' diff --git a/tools/datafix_worlds.py b/tools/datafix_worlds.py index 1b9553977..985881985 100644 --- a/tools/datafix_worlds.py +++ b/tools/datafix_worlds.py @@ -33,8 +33,8 @@ import datafix_scene as scene # noqa: E402 ROOT = Path(__file__).resolve().parent.parent LOCAL = ROOT / '.local' / 'datafix' -VERSIONS = ['1.20.1', '1.20.2', '1.20.4', '1.20.5', '1.20.6', '1.21.1', '1.21.3', '1.21.4', '1.21.5', '1.21.6', '1.21.8', - '1.21.9', '1.21.10', '1.21.11', '26.1', '26.1.2', '26.2'] +VERSIONS = ['1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2', '1.20.4', '1.20.5', '1.20.6', '1.21.1', + '1.21.3', '1.21.4', '1.21.5', '1.21.6', '1.21.8', '1.21.9', '1.21.10', '1.21.11', '26.1', '26.1.2', '26.2'] MANIFEST = 'https://piston-meta.mojang.com/mc/game/version_manifest_v2.json' BOT = 'Upgrader' UUID_BASE = 0x6000 @@ -163,7 +163,13 @@ class Bot: 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 + if protocol <= 758: # 1.18.2: the name only + self.send(0, mcstring(name)) + elif protocol == 759: # 1.19: no signature data + self.send(0, mcstring(name) + b'\x00') + elif protocol == 760: # 1.19.2: no signature data, optional UUID + self.send(0, mcstring(name) + b'\x00' + b'\x01' + offline_uuid(name).bytes) + elif protocol <= 763: # 1.19.3 - 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) From 894113d7f45313be720529ad1604177fe9c54f84 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:53:37 +0200 Subject: [PATCH 11/24] Build golems and withers from dispensers as vanilla does Golems and the wither come from EntityType.create (no spawn finalization, no level random), after clearPatternBlocks sets every pattern cell to air with flags 2 and a destroy event, and updatePatternBlocks follows. A dispensed pumpkin that completes a golem skips setBlock's neighbour updates as vanilla's does, and pumpkins and skulls post block_place. Dispensed skulls face RotationSegment.convertToSegment(facing). Destroy events for air play no break sound. The dispenser fixture plays on normal difficulty like the oracle level. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 6 +- .../src/game/dispense_fixture.rs | 2 + crates/horizon-client/src/game/dispenser.rs | 21 ++++- crates/horizon-client/src/game/golems.rs | 78 ++++++++++++++----- crates/horizon-client/src/game/wither.rs | 27 +++---- 5 files changed, 95 insertions(+), 39 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 8029fa15e..ed10ba195 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1390,10 +1390,14 @@ impl Game<'_> { pos, data, } => { - // LevelRenderer: destroy particles and the block's break sound. + // LevelEventHandler: destroy particles and the block's + // break sound (none for air). let state = BlockStateId(data as u32); let p = [pos.x, pos.y, pos.z]; self.destroy_particles(p, state); + if self.res.facts.is_air(state) { + continue; + } let sounds = self.res.facts.get(state).sounds.clone(); let c = [ f64::from(pos.x) + 0.5, diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index 33bbdb279..340172d61 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -115,6 +115,8 @@ impl Game<'_> { self.orbs.clear(); self.falling.clear(); self.ui.rules = Default::default(); + // The oracle level plays on normal difficulty. + self.ui.difficulty = 2; // The oracle level has world seed 0 and fresh random sequences. self.level.seed = 0; self.rand_sequences = Default::default(); diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index bb987a930..e8a3ecffa 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -1353,8 +1353,8 @@ impl Game<'_> { let rotation = match facing { Direction::Up | Direction::Down => 0, other => { - // RotationSegment.convertToSegment: opposite 2D data value * 4. - (match other.opposite() { + // RotationSegment.convertToSegment: the 2D data value * 4. + (match other { Direction::South => 0, Direction::West => 1, Direction::North => 2, @@ -1373,6 +1373,7 @@ impl Game<'_> { if let Some(entity) = BlockEntity::for_block("minecraft:wither_skeleton_skull") { self.world.block_entities.insert(target, entity); } + self.post_block_game_event("minecraft:block_place", [target.0, target.1, target.2], None, None); self.check_wither_spawn([target.0, target.1, target.2])?; return Ok((shrink(stack), CLICK)); } @@ -1425,8 +1426,20 @@ impl Game<'_> { .registry .default_state("minecraft:carved_pumpkin") { - self.level_set([target.0, target.1, target.2], pumpkin)?; - self.check_golem_spawn([target.0, target.1, target.2])?; + let head = [target.0, target.1, target.2]; + if self.completes_golem(head) { + // CarvedPumpkinBlock.onPlace builds the golem inside + // setBlock; the spot is air again by the time setBlock + // would update neighbours, so it skips them. + let at = Self::pos_of(target); + self.with_level(|b, level, ticks| b.set_block(level, ticks, at, pumpkin, 2 | 16)); + self.process_level_events()?; + self.check_golem_spawn(head)?; + } else { + self.level_set(head, pumpkin)?; + self.check_golem_spawn(head)?; + } + self.post_block_game_event("minecraft:block_place", head, None, None); } return Ok((shrink(stack), CLICK)); } diff --git a/crates/horizon-client/src/game/golems.rs b/crates/horizon-client/src/game/golems.rs index 3d673656b..d048d3d5c 100644 --- a/crates/horizon-client/src/game/golems.rs +++ b/crates/horizon-client/src/game/golems.rs @@ -24,6 +24,7 @@ impl Game<'_> { if self.golem_block(below(1)) == "minecraft:snow_block" && self.golem_block(below(2)) == "minecraft:snow_block" { + // The pattern ("^", "#", "#") is one column. return Some(( "minecraft:snow_golem", vec![head, below(1), below(2)], @@ -38,9 +39,11 @@ impl Game<'_> { && self.golem_block(side(0, 2)) == "minecraft:iron_block" && [-1, 1].iter().all(|&s| air(side(s, 0)) && air(side(s, 2))) { - let mut blocks = vec![head]; - blocks.extend((-1..=1).map(|s| side(s, 1))); - blocks.push(side(0, 2)); + // Every cell of ("~^~", "###", "~#~"), the air ones included. + let blocks = (-1..=1) + .flat_map(|s| (0..3).map(move |row| (s, row))) + .map(|(s, row)| side(s, row)) + .collect(); return Some(("minecraft:iron_golem", blocks, side(0, 2))); } } @@ -58,27 +61,66 @@ impl Game<'_> { let Some((kind, blocks, base)) = self.find_golem(head) else { return self.try_spawn_copper_golem(head); }; - for p in blocks { - let state = self.world.block(p[0], p[1], p[2]); - self.destroy_particles(p, state); - self.level_set(p, self.world.air)?; - } + // CarvedPumpkinBlock.spawnGolemInWorld + self.clear_pattern_blocks(&blocks)?; let at = [ f64::from(base[0]) + 0.5, f64::from(base[1]) + 0.05, f64::from(base[2]) + 0.5, ]; - if let Some(id) = self.spawn_mob(kind, at, false) - && let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) - { - m.yaw = 0.0; - m.body_yaw = 0.0; - m.head_yaw = 0.0; - m.previous_yaw = 0.0; - m.previous_body_yaw = 0.0; - m.previous_head_yaw = 0.0; + self.create_triggered(kind, at, 0.0); + self.update_pattern_blocks(&blocks) + } + /// Whether placing a golem head at `head` completes a snow or iron golem. + pub(super) fn completes_golem(&self, head: [i32; 3]) -> bool { + self.find_golem(head).is_some() + } + /// `EntityType.create(level, TRIGGERED)` and `snapTo`: a built golem or + /// wither has no spawn finalization and draws nothing from the level. + pub(super) fn create_triggered(&mut self, kind: &str, at: [f64; 3], yaw: f32) -> Option { + let t = self.res.mob_types.get(kind).cloned()?; + self.next_entity += 1; + let mut mob = horizon_client::mobs::Mob::new( + self.next_entity, + t, + horizon_core::geometry::Vec3::new(at[0], at[1], at[2]), + yaw, + ); + mob.head_yaw = yaw; + mob.body_yaw = yaw; + mob.previous_yaw = yaw; + mob.previous_body_yaw = yaw; + mob.previous_head_yaw = yaw; + let id = mob.id; + self.mobs.push(mob); + Some(id) + } + /// `CarvedPumpkinBlock.clearPatternBlocks`: every cell of the match + /// becomes air (flags 2) with its destroy event (2001). + pub(super) fn clear_pattern_blocks(&mut self, cells: &[[i32; 3]]) -> Result<()> { + let air = self.world.air; + for &p in cells { + let state = self.world.block(p[0], p[1], p[2]); + let pos = horizon_core::position::BlockPos::new(p[0], p[1], p[2]); + self.with_level(|b, level, ticks| b.set_block(level, ticks, pos, air, 2)); + self.level_events + .push(horizon_world::level::LevelEvent::LevelEvent { + id: 2001, + pos, + data: state.0 as i32, + }); } - Ok(()) + self.process_level_events() + } + /// `CarvedPumpkinBlock.updatePatternBlocks`: neighbours of every cell + /// hear that air arrived. + pub(super) fn update_pattern_blocks(&mut self, cells: &[[i32; 3]]) -> Result<()> { + let air = self.world.air; + for &p in cells { + let pos = horizon_core::position::BlockPos::new(p[0], p[1], p[2]); + self.with_level(|b, level, ticks| b.update_neighbors_at(level, ticks, pos, b.block(air))); + } + self.process_level_events() } /// A pumpkin on a copper block: the block turns into the matching copper /// chest (facing the pumpkin's way) and a copper golem appears where the diff --git a/crates/horizon-client/src/game/wither.rs b/crates/horizon-client/src/game/wither.rs index 3668e5f9f..df24e06d6 100644 --- a/crates/horizon-client/src/game/wither.rs +++ b/crates/horizon-client/src/game/wither.rs @@ -40,35 +40,30 @@ impl Game<'_> { if !ok { continue; } - for row in 0..3 { - for c in 0..3 { - let p = at(c, row); - let state = self.world.block(p[0], p[1], p[2]); - if !self.res.facts.is_air(state) { - self.destroy_particles(p, state); - self.world.set(p[0], p[1], p[2], self.world.air)?; - } - } - } + // WitherSkullBlock.checkSpawn: clear the pattern, the wither + // appears on the middle of the bottom row, then neighbours update. + let cells: Vec<[i32; 3]> = (0..3) + .flat_map(|c| (0..3).map(move |row| (c, row))) + .map(|(c, row)| at(c, row)) + .collect(); + self.clear_pattern_blocks(&cells)?; let base = at(1, 2); let at = [ f64::from(base[0]) + 0.5, f64::from(base[1]) + 0.55, f64::from(base[2]) + 0.5, ]; - if let Some(id) = self.spawn_mob("minecraft:wither", at, true) + let yaw = if ax == 1 { 0.0 } else { 90.0 }; + if let Some(id) = self.create_triggered("minecraft:wither", at, yaw) && let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { - let yaw = if ax == 1 { 0.0 } else { 90.0 }; - m.yaw = yaw; - m.body_yaw = yaw; - m.previous_body_yaw = yaw; - m.head_yaw = yaw; + m.persistent = true; horizon_client::mobs::wither::make_invulnerable(m); let view = self.mob_entity(&self.mobs.iter().find(|m| m.id == id).cloned().unwrap()); self.trigger_entity("minecraft:summoned_entity", "entity", &view); } + self.update_pattern_blocks(&cells)?; return Ok(()); } } From 0de0379cc7b7d8cca968153b9e6a7bba8bb5241c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:53:55 +0200 Subject: [PATCH 12/24] Expect an int page in the click event fix test, as createInt writes Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/datafix/dynamic.rs | 3 +- crates/horizon-world/src/datafix/files.rs | 186 ++++-- crates/horizon-world/src/datafix/fixes_a.rs | 246 ++++++-- crates/horizon-world/src/datafix/fixes_b.rs | 531 ++++++++++++---- crates/horizon-world/src/datafix/fixes_c.rs | 593 ++++++++++++++---- crates/horizon-world/src/datafix/fixes_old.rs | 273 ++++++-- crates/horizon-world/src/datafix/items.rs | 387 ++++++++++-- crates/horizon-world/src/datafix/json.rs | 54 +- crates/horizon-world/src/datafix/mod.rs | 25 +- crates/horizon-world/src/datafix/schema.rs | 110 +++- crates/horizon-world/src/datafix/tests.rs | 29 +- crates/horizon-world/src/datafix/text.rs | 47 +- 12 files changed, 1975 insertions(+), 509 deletions(-) diff --git a/crates/horizon-world/src/datafix/dynamic.rs b/crates/horizon-world/src/datafix/dynamic.rs index 88e089438..a831f07ab 100644 --- a/crates/horizon-world/src/datafix/dynamic.rs +++ b/crates/horizon-world/src/datafix/dynamic.rs @@ -108,7 +108,8 @@ 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) + 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) diff --git a/crates/horizon-world/src/datafix/files.rs b/crates/horizon-world/src/datafix/files.rs index c376a695d..0ee3d385d 100644 --- a/crates/horizon-world/src/datafix/files.rs +++ b/crates/horizon-world/src/datafix/files.rs @@ -61,7 +61,11 @@ pub fn data_version(tag: &Tag, default: i32) -> i32 { /// `FileFixerUpper.requiresFileFixing`. pub fn requires_file_fixing(level_version: i32) -> bool { - let from = if level_version < FILE_FIXER_INTRODUCTION { 0 } else { level_version }; + let from = if level_version < FILE_FIXER_INTRODUCTION { + 0 + } else { + level_version + }; from < *FILE_FIXES.last().unwrap() } @@ -82,7 +86,11 @@ fn move_file(base: &Path, from: &str, to: &str) -> io::Result<()> { if to.exists() { return Err(io::Error::new( io::ErrorKind::AlreadyExists, - format!("cannot move {} to {}: target already exists", from.display(), to.display()), + format!( + "cannot move {} to {}: target already exists", + from.display(), + to.display() + ), )); } if let Some(parent) = to.parent() { @@ -112,7 +120,12 @@ fn delete(base: &Path, relative: &str) -> io::Result<()> { 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()) + .map(|r| { + r.filter_map(Result::ok) + .map(|e| e.path()) + .filter(|p| p.is_dir()) + .collect() + }) .unwrap_or_default(); out.sort(); out @@ -122,12 +135,17 @@ fn subdirectories(path: &Path) -> Vec { 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() + ["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(); + let found: Vec = subdirectories(&root) + .iter() + .flat_map(|ns| subdirectories(ns)) + .collect(); if found.is_empty() { defaults() } else { found } } @@ -136,7 +154,10 @@ fn dimensions(base: &Path) -> Vec { /// `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 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 @@ -148,14 +169,19 @@ fn write_level(base: &Path, mut data: Tag, version: i32) -> io::Result<()> { } /// `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))])) + 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())) + tag.get("data") + .cloned() + .unwrap_or_else(|| Tag::Compound(Compound::new())) })) } @@ -174,7 +200,10 @@ fn fix_4772(base: &Path) -> io::Result<()> { } for (from, to) in [ ("data", "dimensions/minecraft/overworld/data/minecraft"), - ("DIM-1/data", "dimensions/minecraft/the_nether/data/minecraft"), + ( + "DIM-1/data", + "dimensions/minecraft/the_nether/data/minecraft", + ), ("DIM1/data", "dimensions/minecraft/the_end/data/minecraft"), ] { for (a, b) in [ @@ -196,16 +225,26 @@ fn fix_4772(base: &Path) -> io::Result<()> { 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")) + 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")) + (!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")?; + move_file( + &data, + "random_sequences.dat", + "minecraft/random_sequences.dat", + )?; for (from, to) in [ ("", "dimensions/minecraft/overworld"), ("DIM-1", "dimensions/minecraft/the_nether"), @@ -233,7 +272,16 @@ fn fix_4772(base: &Path) -> io::Result<()> { ] { delete(base, f)?; } - for s in ["Monument", "Stronghold", "Mineshaft", "Temple", "Mansion", "Village", "Fortress", "EndCity"] { + for s in [ + "Monument", + "Stronghold", + "Mineshaft", + "Temple", + "Mansion", + "Village", + "Fortress", + "EndCity", + ] { delete(base, &format!("data/{s}.dat"))?; delete(base, &format!("data/{s}_index.dat"))?; } @@ -264,14 +312,20 @@ fn level_dat_to_saved_data(base: &Path) -> io::Result<()> { 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 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"))?; + 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); @@ -279,16 +333,31 @@ fn level_dat_to_saved_data(base: &Path) -> io::Result<()> { 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)?; + 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, + "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"))?; + 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(); @@ -326,12 +395,18 @@ fn fix_4899(base: &Path) -> io::Result<()> { 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)); + 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)); + set( + &mut rules, + "minecraft:spectators_generate_chunks", + Tag::Byte(1), + ); } write_saved_data(&path, rules, V)?; } @@ -363,14 +438,21 @@ fn add_versions(data: &mut Tag) { /// 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 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 }; + let start = if loaded < FILE_FIXER_INTRODUCTION { + 0 + } else { + loaded + }; for version in FILE_FIXES { if version > start && version <= CURRENT { match version { @@ -381,8 +463,12 @@ pub fn fix_world_files(base: &Path) -> io::Result { } } // `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())); + 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")); @@ -437,7 +523,10 @@ fn upgrade_regions(folder: &Path, mut fix: impl FnMut(&mut Tag)) -> io::Result<( }; let mut root = record.nbt(limits)?; fix(&mut root.tag); - records.push((pos, ChunkRecord::from_nbt(&root, record.compression, record.timestamp, limits)?)); + records.push(( + pos, + ChunkRecord::from_nbt(&root, record.compression, record.timestamp, limits)?, + )); } drop(region); let staging = folder.join(".datafix"); @@ -474,7 +563,11 @@ fn upgrade_saved_data(data: &Path) -> io::Result<()> { } files.sort(); for file in files { - let relative = file.strip_prefix(data).unwrap_or(&file).to_string_lossy().replace('\\', "/"); + let relative = file + .strip_prefix(data) + .unwrap_or(&file) + .to_string_lossy() + .replace('\\', "/"); let Some(ty) = super::saved_data_type(&relative) else { continue; }; @@ -493,9 +586,12 @@ 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()) - } + 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), } } @@ -505,7 +601,11 @@ fn tag_to_json(t: &Tag) -> Json { 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()), + Tag::Compound(c) => Json::Obj( + c.iter() + .map(|(k, v)| (k.0.clone(), tag_to_json(v))) + .collect(), + ), other => json::from_nbt(other), } } @@ -513,7 +613,8 @@ fn tag_to_json(t: &Tag) -> Json { 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 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 { @@ -538,10 +639,19 @@ pub fn upgrade_world_data(base: &Path) -> io::Result<()> { 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))?; + 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"))?; } } diff --git a/crates/horizon-world/src/datafix/fixes_a.rs b/crates/horizon-world/src/datafix/fixes_a.rs index 433a2796f..2bfe43af3 100644 --- a/crates/horizon-world/src/datafix/fixes_a.rs +++ b/crates/horizon-world/src/datafix/fixes_a.rs @@ -1,7 +1,7 @@ //! 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, + 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}; @@ -9,32 +9,59 @@ use super::{Cx, FixFn, id_is, items, renamer, reread}; 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(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(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(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")) + 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, 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)); @@ -49,7 +76,15 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { /// `BlockEntitySignDoubleSidedEditableTextFix.FIELDS_TO_DROP`. pub(super) const SIGN_FIELDS_TO_DROP: [&str; 10] = [ - "Text1", "Text2", "Text3", "Text4", "FilteredText1", "FilteredText2", "FilteredText3", "FilteredText4", "Color", + "Text1", + "Text2", + "Text3", + "Text4", + "FilteredText1", + "FilteredText2", + "FilteredText3", + "FilteredText4", + "Color", "GlowingText", ]; @@ -132,7 +167,10 @@ fn scoreboard_display_slots(cx: &mut Cx) { 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()); + let k = SLOT_RENAMES + .iter() + .find(|(o, _)| *o == name) + .map_or(k, |(_, n)| (*n).into()); slots.insert(k, value); } } @@ -180,7 +218,9 @@ const EFFECT_IDS: [&str; 33] = [ /// `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])) + (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) { @@ -206,7 +246,14 @@ fn update_effect_instance(mut v: Tag) -> Tag { 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())); + 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 { @@ -241,7 +288,9 @@ fn mob_effect_ids(cx: &mut Cx) { } update_effect_list(e, "ActiveEffects", "active_effects"); }); - cx.each(Ty::Player, |p| update_effect_list(p, "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; @@ -251,12 +300,20 @@ fn mob_effect_ids(cx: &mut Cx) { }; 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()) + 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" + "minecraft:potion" + | "minecraft:splash_potion" + | "minecraft:lingering_potion" + | "minecraft:tipped_arrow" ) { update_effect_list(tag, "CustomPotionEffects", "custom_potion_effects"); } @@ -268,7 +325,11 @@ fn mob_effect_ids(cx: &mut Cx) { fn primed_tnt_block_state(cx: &mut Cx) { let k = cx.k; cx.entity("minecraft:tnt", |e| { - set(e, "block_state", Tag::compound([("Name", string("minecraft:tnt"))])); + set( + e, + "block_state", + Tag::compound([("Name", string("minecraft:tnt"))]), + ); if let Some(fuse) = e.get("Fuse").cloned() { set(e, "fuse", fuse); } @@ -281,7 +342,11 @@ 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" } + if potion == "minecraft:empty" { + "minecraft:arrow" + } else { + "minecraft:tipped_arrow" + } } else if id_is(e, "minecraft:spectral_arrow") { "minecraft:spectral_arrow" } else { @@ -348,7 +413,11 @@ fn rename_sweeping(cx: &mut Cx) { .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 }; + let new = if namespaced(&id) == "minecraft:sweeping" { + "minecraft:sweeping_edge".into() + } else { + id + }; set(&mut e, "id", string(&new)); } e @@ -364,7 +433,11 @@ fn rename_sweeping(cx: &mut Cx) { /// `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)); + 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, @@ -372,12 +445,20 @@ pub(super) fn fix_block_pos(v: Tag) -> Tag { } /// `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)); + 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()])); + set( + v, + new, + int_list(&[x.int_value(), y.int_value(), z.int_value()]), + ); } } @@ -424,7 +505,13 @@ fn horse_body_armor_entity(e: &mut Tag, field: &str, clear_armor_items: bool) { stream(Some(&list)) .into_iter() .enumerate() - .map(|(i, x)| if i == 2 { Tag::Float(0.085f32.to_bits()) } else { x }) + .map(|(i, x)| { + if i == 2 { + Tag::Float(0.085f32.to_bits()) + } else { + x + } + }) .collect(), ) }); @@ -436,9 +523,14 @@ fn horse_body_armor_entity(e: &mut Tag, field: &str, clear_armor_items: bool) { 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)) + if ![ + "minecraft:llama", + "minecraft:trader_llama", + "minecraft:mule", + "minecraft:donkey", + ] + .iter() + .any(|id| id_is(e, id)) { return; } @@ -461,7 +553,9 @@ fn wolf_health(cx: &mut Cx) { stream(Some(&list)) .into_iter() .map(|mut a| { - if namespaced(&as_string(a.get("Name"), "")) == "minecraft:generic.max_health" { + 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; @@ -477,7 +571,9 @@ fn wolf_health(cx: &mut Cx) { ) }); if adjusted { - super::dynamic::update(e, "Health", |h| Tag::Float((as_float(Some(&h), 0.0) * 2.0).to_bits())); + super::dynamic::update(e, "Health", |h| { + Tag::Float((as_float(Some(&h), 0.0) * 2.0).to_bits()) + }); } }); } @@ -515,8 +611,12 @@ fn block_pos_format(cx: &mut Cx) { } 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.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| { @@ -547,11 +647,14 @@ fn block_pos_format(cx: &mut Cx) { }); } }); - cx.item(|id| id == "minecraft:compass", |s| { - if let Some(tag) = s.get_mut("tag") { - super::dynamic::update(tag, "LodestonePos", fix_block_pos); - } - }); + cx.item( + |id| id == "minecraft:compass", + |s| { + if let Some(tag) = s.get_mut("tag") { + super::dynamic::update(tag, "LodestonePos", fix_block_pos); + } + }, + ); } // ------------------------------------------------------------------ 3814 @@ -677,7 +780,12 @@ pub(super) fn dye_color(id: i32) -> &'static str { 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)), + Some(id) => string( + PATTERN_IDS + .iter() + .find(|(k, _)| *k == id) + .map_or(id.as_str(), |(_, n)| n), + ), None => p, }) }); @@ -690,7 +798,9 @@ pub(super) fn banner_layer(mut v: Tag) -> Tag { 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())) + Some(Tag::List( + stream(Some(&p)).into_iter().map(banner_layer).collect(), + )) }); }); } @@ -765,7 +875,10 @@ fn area_effect_cloud_potion(cx: &mut Cx) { 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 profile = be + .get("SkullOwner") + .or_else(|| be.get("ExtraType")) + .cloned(); let Some(profile) = profile else { return; }; @@ -787,14 +900,23 @@ pub(super) fn component_fix(cx: &mut Cx, id: &str, new_id: &str, f: impl Fn(Tag) } 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) - }); + 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 @@ -838,7 +960,9 @@ fn item_custom_name_to_item_name(cx: &mut Cx) { _ => return, }; if let Some(c) = s.get_mut("components") - && c.get("minecraft:custom_name").and_then(translation_key).is_some_and(|k| expected(&k)) + && c.get("minecraft:custom_name") + .and_then(translation_key) + .is_some_and(|k| expected(&k)) { rename(c, "minecraft:custom_name", "minecraft:item_name"); } @@ -856,7 +980,10 @@ fn banner_custom_name_to_item_name(cx: &mut Cx) { set( be, "components", - Tag::compound([("minecraft:item_name", name), ("minecraft:hide_additional_tooltip", empty_map())]), + Tag::compound([ + ("minecraft:item_name", name), + ("minecraft:hide_additional_tooltip", empty_map()), + ]), ); remove(be, "CustomName"); }); @@ -921,12 +1048,19 @@ pub(super) fn unpack_objective(v: &mut Tag) { }; 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(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))])); + set( + v, + "CriteriaType", + Tag::compound([("type", string(&ty)), ("id", string(&id))]), + ); } /// `Identifier.bySeparator(s, '.')`: namespace before the first dot. @@ -947,7 +1081,11 @@ pub(super) fn repack_objective(v: &mut Tag) { 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)) }; + let name = if ty == "_special" { + id + } else { + format!("{}:{}", pack_with_dot(&ty), pack_with_dot(&id)) + }; set(v, "CriteriaName", string(&name)); remove(v, "CriteriaType"); } diff --git a/crates/horizon-world/src/datafix/fixes_b.rs b/crates/horizon-world/src/datafix/fixes_b.rs index 32cb1646f..2b88bc742 100644 --- a/crates/horizon-world/src/datafix/fixes_b.rs +++ b/crates/horizon-world/src/datafix/fixes_b.rs @@ -1,7 +1,8 @@ //! 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, + 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; @@ -19,23 +20,36 @@ 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(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(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"))])) - }); + 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(4067, 0), |cx| { + feature_flag_remove(cx, &["minecraft:bundle"]) + })); out.push((key(4068, 0), |cx| { - cx.each(Ty::BlockEntity, |be| rename_and_fix(be, "Lock", "lock", fix_lock)); + 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| { @@ -44,7 +58,9 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { } }); })); - out.push((key(4173, 0), |cx| cx.entity("minecraft:tnt_minecart", |e| rename(e, "TNTFuse", "fuse")))); + 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") { @@ -70,39 +86,97 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { }); })); out.push((key(4176, 0), |cx| { - component_fix(cx, "minecraft:lock", "minecraft:lock", |l| (!broken_lock(&l)).then_some(l)); + 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"] { + 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: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))); + 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() } - }); + 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()))); + 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(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()); + 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)); @@ -110,7 +184,8 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { 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()) + (namespaced(id) == "minecraft:nether/use_lodestone") + .then(|| "minecraft:adventure/use_lodestone".into()) }); })); out.push((key(4312, 0), player_equipment)); @@ -160,60 +235,177 @@ fn uuid_text(u: [i32; 4]) -> String { } 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"), + ( + "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"), + ( + "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"), + ( + "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"), + ( + "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"), + ( + "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"), + ( + "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(), + 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]]) } @@ -231,8 +423,13 @@ fn fix_modifiers(modifiers: Vec) -> Vec { 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); + 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"); @@ -316,7 +513,8 @@ fn attribute_modifier_ids(cx: &mut Cx) { 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)); + 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"); @@ -330,13 +528,17 @@ fn jukebox_ticks(cx: &mut Cx) { 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") { + 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", - ))), + Tag::String(crate::nbt::JavaString( + super::json::translatable_component_json("block.minecraft.ominous_banner"), + )), ); set(c, "minecraft:rarity", string("uncommon")); } @@ -346,11 +548,14 @@ fn ominous_banner_rarity(cx: &mut Cx) { fix(c); } }); - cx.item(|id| id == "minecraft:white_banner", |s| { - if let Some(c) = s.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 { @@ -365,7 +570,11 @@ fn attribute_id_prefix(id: &str) -> Option { } /// `AttributesRenameFix`. -pub(super) fn attributes_rename(cx: &mut Cx, old_component_format: bool, rename_fn: &dyn Fn(&str) -> Option) { +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) @@ -377,7 +586,11 @@ pub(super) fn attributes_rename(cx: &mut Cx, old_component_format: bool, rename_ let Some(a) = c.get_mut("minecraft:attribute_modifiers") else { return; }; - let list = if old_component_format { a.get_mut("modifiers") } else { Some(a) }; + 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"); @@ -408,8 +621,14 @@ fn food_to_consumable(cx: &mut Cx) { .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())), + ( + "effects", + Tag::List(e.get("effect").cloned().into_iter().collect()), + ), + ( + "probability", + Tag::Float(as_float(e.get("probability"), 1.0).to_bits()), + ), ]) }) .collect(); @@ -504,7 +723,9 @@ const TRIAL_CONFIGS: &[(&str, &str, &str)] = &[ ]; fn snbt(s: &str) -> Tag { - crate::nbt::parse_snbt(s).map(|(t, _)| t).unwrap_or_else(|_| empty_map()) + crate::nbt::parse_snbt(s) + .map(|(t, _)| t) + .unwrap_or_else(|_| empty_map()) } /// `CompoundTag.merge`. fn merge(into: &mut Tag, other: &Tag) { @@ -560,12 +781,21 @@ fn trial_config_key(normal: &Tag, ominous: &Tag) -> Option<&'static str> { 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 { + 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"))); + set( + be, + "normal_config", + string(&format!("minecraft:{name}/normal")), + ); + set( + be, + "ominous_config", + string(&format!("minecraft:{name}/ominous")), + ); } }); } @@ -618,10 +848,13 @@ fn fix_lock(v: Tag) -> Option { /// `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(); + 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("\"\"") + one(c, "minecraft:custom_name") + && as_str(c.get("minecraft:custom_name")).as_deref() == Some("\"\"") }) } @@ -662,15 +895,23 @@ fn base_value(cx: &mut Cx, entity: &str, attribute: &str, from: f64, to: f64) { 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 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()); + 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 (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())); @@ -687,13 +928,21 @@ fn drop_chances_format(cx: &mut Cx) { } /// `LegacyBlockPropertyRenameAndFix`. -fn block_property_rename(cx: &mut Cx, block: &str, old: &str, new: &str, f: &dyn Fn(&str) -> String) { +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), ""))))); + rename_and_fix(props, old, new, |v| { + Some(string(&f(&as_string(Some(&v), "")))) + }); } }); } @@ -704,7 +953,10 @@ fn forced_chunk_to_ticket(cx: &mut Cx) { 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(), + other => stream(Some(other)) + .iter() + .map(|t| as_long(Some(t), 0)) + .collect(), }; Some(Tag::List( longs @@ -787,8 +1039,16 @@ fn entity_spawner_item_variants(cx: &mut Cx) { 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))); + 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)), + ); } } _ => {} @@ -831,8 +1091,14 @@ 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" => { + "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); @@ -843,7 +1109,11 @@ fn saddle_equipment_slot(cx: &mut Cx) { 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))])); + set( + e, + "saddle", + Tag::compound([("id", string("minecraft:saddle")), ("count", Tag::Int(1))]), + ); saddle_drop_chance(e); } reread(k, e); @@ -910,8 +1180,16 @@ fn thrown_potion_split(cx: &mut Cx) { 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" }; + 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)); }); } @@ -969,9 +1247,19 @@ fn tooltip_display(cx: &mut Cx) { 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: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: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(); @@ -990,7 +1278,10 @@ fn tooltip_display(cx: &mut Cx) { "minecraft:tooltip_display", Tag::compound([ ("hide_tooltip", Tag::Byte(i8::from(hide_tooltip))), - ("hidden_components", Tag::List(hidden.iter().map(|s| string(s)).collect())), + ( + "hidden_components", + Tag::List(hidden.iter().map(|s| string(s)).collect()), + ), ]), ); } @@ -1111,12 +1402,22 @@ fn inline_block_pos_format(cx: &mut Cx) { 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 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"] { + for k in [ + "SpawnX", + "SpawnY", + "SpawnZ", + "SpawnAngle", + "SpawnDimension", + "SpawnForced", + ] { remove(p, k); } set(p, "respawn", respawn); @@ -1126,7 +1427,10 @@ fn inline_block_pos_format(cx: &mut Cx) { p, "entered_nether_pos", Tag::List( - ["x", "y", "z"].iter().map(|k| Tag::Double(as_double(pos.get(k), 0.0).to_bits())).collect(), + ["x", "y", "z"] + .iter() + .map(|k| Tag::Double(as_double(pos.get(k), 0.0).to_bits())) + .collect(), ), ); } @@ -1148,7 +1452,12 @@ fn inline_block_pos_format(cx: &mut Cx) { 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"] { + 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 index 496ddee65..9ef351d38 100644 --- a/crates/horizon-world/src/datafix/fixes_c.rs +++ b/crates/horizon-world/src/datafix/fixes_c.rs @@ -1,7 +1,7 @@ //! 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, + 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}; @@ -20,9 +20,15 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { super::drop_typed(e, "Particle"); }); })); - 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(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| { @@ -46,8 +52,15 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { } }); })); - 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(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| { @@ -64,43 +77,63 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { }); })); 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) - }); + 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))); + 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))); + 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))); + 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| { + 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()) + 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())); + 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)); + 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| { @@ -110,10 +143,14 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { })); 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))); + 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))); + 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| { @@ -130,7 +167,9 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { }); })); 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(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") { @@ -153,11 +192,23 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { cx.rename( Ty::ItemName, renamer(&[ - ("minecraft:ocean_explorer_map", "minecraft:ocean_monument_map"), + ( + "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"), + ( + "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", + ), ]), ); })); @@ -168,14 +219,21 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { }); })); 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")); - } - }); + 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"), + ); + } + }, + ); })); } @@ -199,12 +257,21 @@ fn legacy_world_border(cx: &mut Cx) { ("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_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))), + ( + "warning_blocks", + Tag::Int(as_int(l.get("BorderWarningBlocks"), 5)), + ), + ( + "warning_time", + Tag::Int(as_int(l.get("BorderWarningTime"), 15)), + ), ]); for k in [ "BorderCenterX", @@ -225,7 +292,11 @@ fn legacy_world_border(cx: &mut Cx) { 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 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); @@ -244,65 +315,213 @@ fn world_spawn_data(cx: &mut Cx) { } 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), + ( + "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), + ( + "doImmediateRespawn", + "minecraft:immediate_respawn", + Conv::Bool, + ), ("doInsomnia", "minecraft:spawn_phantoms", Conv::Bool), - ("doLimitedCrafting", "minecraft:limited_crafting", 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), + ( + "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), + ( + "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), + ( + "forgiveDeadPlayers", + "minecraft:forgive_dead_players", + Conv::Bool, + ), ("freezeDamage", "minecraft:freeze_damage", Conv::Bool), - ("globalSoundEvents", "minecraft:global_sound_events", Conv::Bool), + ( + "globalSoundEvents", + "minecraft:global_sound_events", + Conv::Bool, + ), ("keepInventory", "minecraft:keep_inventory", Conv::Bool), - ("lavaSourceConversion", "minecraft:lava_source_conversion", 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), + ( + "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), + ( + "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)), + ( + "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), + ( + "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), + ( + "tntExplosionDropDecay", + "minecraft:tnt_explosion_drop_decay", + Conv::Bool, + ), ("universalAnger", "minecraft:universal_anger", Conv::Bool), - ("waterSourceConversion", "minecraft:water_source_conversion", Conv::Bool), + ( + "waterSourceConversion", + "minecraft:water_source_conversion", + Conv::Bool, + ), ]; #[derive(Clone, Copy)] @@ -342,7 +561,10 @@ 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 away = parse_bool(&as_string( + rules.get("allowFireTicksAwayFromPlayer"), + "false", + )); let radius = if !fire_tick { 0 } else if !away { @@ -351,7 +573,11 @@ fn game_rule_registry(cx: &mut Cx) { -1 }; if radius != 128 { - set(&mut rules, "minecraft:fire_spread_radius_around_player", Tag::Int(radius)); + set( + &mut rules, + "minecraft:fire_spread_radius_around_player", + Tag::Int(radius), + ); } for k in [ "spawnChunkRadius", @@ -375,15 +601,25 @@ fn day_time_to_clock(cx: &mut Cx) { 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)])); + 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: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", @@ -416,8 +652,14 @@ const STONECUTTER_RECIPES: &[(&str, &str)] = &[ "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"), + ( + "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)] = &[ ( @@ -509,21 +751,46 @@ fn level_dat_to_saved_data_preparation(cx: &mut Cx) { 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))), + ( + "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"] { + for k in [ + "WanderingTraderSpawnDelay", + "WanderingTraderSpawnChance", + "WanderingTraderId", + ] { remove(l, k); } let weather = Tag::compound([ - ("clear_weather_time", Tag::Int(as_int(source.get("clearWeatherTime"), 0))), + ( + "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))), + ( + "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))), + ( + "thundering", + boolean(as_bool(source.get("thundering"), false)), + ), ]); - for k in ["clearWeatherTime", "rainTime", "thunderTime", "raining", "thundering"] { + for k in [ + "clearWeatherTime", + "rainTime", + "thunderTime", + "raining", + "thundering", + ] { remove(l, k); } set(l, "weather_data", weather); @@ -542,7 +809,11 @@ fn level_dat_to_saved_data_preparation(cx: &mut Cx) { }) .collect(); remove(l, "ScheduledEvents"); - set(l, "scheduled_events", Tag::compound([("events", Tag::List(events))])); + 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) @@ -580,10 +851,17 @@ 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 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()); + let id = ids + .get(i) + .cloned() + .flatten() + .unwrap_or_else(|| "minecraft:brick".into()); if id.is_empty() { return original; } @@ -604,10 +882,18 @@ fn blending_data(c: &mut Tag) { if !overworld { return; } - const SKIP: [&str; 4] = ["minecraft:empty", "minecraft:structure_starts", "minecraft:structure_references", "minecraft:biomes"]; + 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))]) + 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)); @@ -630,39 +916,89 @@ fn plain_translation_key(v: &Tag) -> Option { } 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"), + ( + "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"); - } - }); + 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) { @@ -686,14 +1022,17 @@ fn merge_terrain_chunk_status(cx: &mut Cx) { 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)); + 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()); + 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/fixes_old.rs b/crates/horizon-world/src/datafix/fixes_old.rs index b3835bc30..82a5a62f3 100644 --- a/crates/horizon-world/src/datafix/fixes_old.rs +++ b/crates/horizon-world/src/datafix/fixes_old.rs @@ -1,7 +1,9 @@ //! Fixes of 1.19 to 1.20 (keys 3076 to 3459): what worlds saved by 1.18.2 to //! 1.19.4 need before the fixes of [`super::fixes_a`]. The options fixes of //! this range (3201, 3214, 3319) are client settings, not world data. -use super::dynamic::{as_number, as_str, is_list, namespaced, remove, rename, set, stream, string, update}; +use super::dynamic::{ + as_number, as_str, is_list, namespaced, remove, rename, set, stream, string, update, +}; use super::fixes_a::fix_block_pos; use super::fixes_b::feature_flag_remove; use super::schema::{Key, Ty, key}; @@ -10,11 +12,18 @@ use crate::nbt::{Compound, Tag}; pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { out.push((key(3077, 0), chunk_delete_ignored_light)); - out.push((key(3084, 0), |cx| cx.rename(Ty::GameEventName, renamer(GAME_EVENTS)))); - out.push((key(3086, 0), |cx| entity_variant(cx, "minecraft:cat", "CatType", cat_type))); + out.push((key(3084, 0), |cx| { + cx.rename(Ty::GameEventName, renamer(GAME_EVENTS)) + })); + out.push((key(3086, 0), |cx| { + entity_variant(cx, "minecraft:cat", "CatType", cat_type) + })); out.push((key(3086, 0), |cx| { criteria_rename(cx, "minecraft:husbandry/complete_catalogue", &|s| { - CAT_TEXTURES.iter().find(|(from, _)| *from == s).map(|(_, to)| (*to).to_owned()) + CAT_TEXTURES + .iter() + .find(|(from, _)| *from == s) + .map(|(_, to)| (*to).to_owned()) }); })); out.push((key(3087, 0), |cx| { @@ -54,7 +63,10 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { })); out.push((key(3097, 0), |cx| { poi_records(cx, |r| { - !matches!(as_str(r.get("type")).as_deref(), Some("minecraft:unemployed" | "minecraft:nitwit") | None) + !matches!( + as_str(r.get("type")).as_deref(), + Some("minecraft:unemployed" | "minecraft:nitwit") | None + ) }) })); out.push((key(3108, 0), |cx| { @@ -82,15 +94,24 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { } }) })); - out.push((key(3438, 0), |cx| cx.rename(Ty::ItemName, renamer(POTTERY_SHARDS)))); + out.push((key(3438, 0), |cx| { + cx.rename(Ty::ItemName, renamer(POTTERY_SHARDS)) + })); out.push((key(3439, 0), |cx| sign_double_sided(cx, "minecraft:sign"))); - out.push((key(3439, 1), |cx| sign_double_sided(cx, "minecraft:hanging_sign"))); + out.push((key(3439, 1), |cx| { + sign_double_sided(cx, "minecraft:hanging_sign") + })); out.push((key(3440, 0), multi_noise_preset)); - out.push((key(3440, 0), |cx| feature_flag_remove(cx, &["minecraft:update_1_20"]))); + out.push((key(3440, 0), |cx| { + feature_flag_remove(cx, &["minecraft:update_1_20"]) + })); out.push((key(3447, 0), |cx| { cx.rename(Ty::ItemName, |id| { let id = namespaced(id); - POTTERY_SHERDS.iter().find(|(from, _)| *from == id).map(|(_, to)| *to) + POTTERY_SHERDS + .iter() + .find(|(from, _)| *from == id) + .map(|(_, to)| *to) }) })); // DecoratedPotFieldRenameFix converts unchecked: the typed shards are written @@ -115,7 +136,10 @@ pub(super) fn register(out: &mut Vec<(Key, FixFn)>) { /// `ChunkDeleteIgnoredLightDataFix`. fn chunk_delete_ignored_light(cx: &mut Cx) { cx.each(Ty::Chunk, |c| { - let light_on = c.get("isLightOn").and_then(as_number).is_some_and(|n| n.double_value() != 0.0); + let light_on = c + .get("isLightOn") + .and_then(as_number) + .is_some_and(|n| n.double_value() != 0.0); if !light_on && let Some(Tag::List(sections)) = c.get_mut("sections") { for s in sections { remove(s, "BlockLight"); @@ -199,7 +223,10 @@ const CAT_TEXTURES: &[(&str, &str)] = &[ ("textures/entity/cat/black.png", "minecraft:black"), ("textures/entity/cat/red.png", "minecraft:red"), ("textures/entity/cat/siamese.png", "minecraft:siamese"), - ("textures/entity/cat/british_shorthair.png", "minecraft:british"), + ( + "textures/entity/cat/british_shorthair.png", + "minecraft:british", + ), ("textures/entity/cat/calico.png", "minecraft:calico"), ("textures/entity/cat/persian.png", "minecraft:persian"), ("textures/entity/cat/ragdoll.png", "minecraft:ragdoll"), @@ -254,7 +281,11 @@ fn goat_horn_id(cx: &mut Cx) { |id| id == "minecraft:goat_horn", |tag| { let variant = super::dynamic::as_int(tag.get("SoundVariant"), 0); - let sound = INSTRUMENTS[if (0..8).contains(&variant) { variant as usize } else { 0 }]; + let sound = INSTRUMENTS[if (0..8).contains(&variant) { + variant as usize + } else { + 0 + }]; remove(tag, "SoundVariant"); set(tag, "instrument", string(sound)); }, @@ -294,7 +325,10 @@ fn poi_records(cx: &mut Cx, keep: impl Fn(&Tag) -> bool) { if let Some(records) = section.get("Records") && is_list(Some(records)) { - let kept: Vec = stream(Some(records)).into_iter().filter(|r| keep(r)).collect(); + let kept: Vec = stream(Some(records)) + .into_iter() + .filter(|r| keep(r)) + .collect(); set(section, "Records", Tag::List(kept)); } } @@ -309,7 +343,10 @@ fn pig_spawn_egg(cx: &mut Cx) { cx.item( |id| id == "minecraft:pig_spawn_egg", |s| { - let entity = s.get("tag").and_then(|t| t.get("EntityTag")).and_then(|e| as_str(e.get("id"))); + let entity = s + .get("tag") + .and_then(|t| t.get("EntityTag")) + .and_then(|e| as_str(e.get("id"))); if let Some(entity) = entity { let egg = SPAWN_EGGS .iter() @@ -329,11 +366,17 @@ const SPAWN_EGGS: &[(&str, &str)] = &[ ("minecraft:cow", "minecraft:cow_spawn_egg"), ("minecraft:creeper", "minecraft:creeper_spawn_egg"), ("minecraft:donkey", "minecraft:donkey_spawn_egg"), - ("minecraft:elder_guardian", "minecraft:elder_guardian_spawn_egg"), + ( + "minecraft:elder_guardian", + "minecraft:elder_guardian_spawn_egg", + ), ("minecraft:ender_dragon", "minecraft:ender_dragon_spawn_egg"), ("minecraft:enderman", "minecraft:enderman_spawn_egg"), ("minecraft:endermite", "minecraft:endermite_spawn_egg"), - ("minecraft:evocation_illager", "minecraft:evocation_illager_spawn_egg"), + ( + "minecraft:evocation_illager", + "minecraft:evocation_illager_spawn_egg", + ), ("minecraft:ghast", "minecraft:ghast_spawn_egg"), ("minecraft:guardian", "minecraft:guardian_spawn_egg"), ("minecraft:horse", "minecraft:horse_spawn_egg"), @@ -353,7 +396,10 @@ const SPAWN_EGGS: &[(&str, &str)] = &[ ("minecraft:shulker", "minecraft:shulker_spawn_egg"), ("minecraft:silverfish", "minecraft:silverfish_spawn_egg"), ("minecraft:skeleton", "minecraft:skeleton_spawn_egg"), - ("minecraft:skeleton_horse", "minecraft:skeleton_horse_spawn_egg"), + ( + "minecraft:skeleton_horse", + "minecraft:skeleton_horse_spawn_egg", + ), ("minecraft:slime", "minecraft:slime_spawn_egg"), ("minecraft:snow_golem", "minecraft:snow_golem_spawn_egg"), ("minecraft:spider", "minecraft:spider_spawn_egg"), @@ -362,21 +408,37 @@ const SPAWN_EGGS: &[(&str, &str)] = &[ ("minecraft:turtle", "minecraft:turtle_spawn_egg"), ("minecraft:vex", "minecraft:vex_spawn_egg"), ("minecraft:villager", "minecraft:villager_spawn_egg"), - ("minecraft:vindication_illager", "minecraft:vindication_illager_spawn_egg"), + ( + "minecraft:vindication_illager", + "minecraft:vindication_illager_spawn_egg", + ), ("minecraft:witch", "minecraft:witch_spawn_egg"), ("minecraft:wither", "minecraft:wither_spawn_egg"), - ("minecraft:wither_skeleton", "minecraft:wither_skeleton_spawn_egg"), + ( + "minecraft:wither_skeleton", + "minecraft:wither_skeleton_spawn_egg", + ), ("minecraft:wolf", "minecraft:wolf_spawn_egg"), ("minecraft:zombie", "minecraft:zombie_spawn_egg"), ("minecraft:zombie_horse", "minecraft:zombie_horse_spawn_egg"), - ("minecraft:zombie_pigman", "minecraft:zombie_pigman_spawn_egg"), - ("minecraft:zombie_villager", "minecraft:zombie_villager_spawn_egg"), + ( + "minecraft:zombie_pigman", + "minecraft:zombie_pigman_spawn_egg", + ), + ( + "minecraft:zombie_villager", + "minecraft:zombie_villager_spawn_egg", + ), ]; // ------------------------------------------------------------------ 3322 -const POTION_ITEMS: [&str; 4] = - ["minecraft:potion", "minecraft:splash_potion", "minecraft:lingering_potion", "minecraft:tipped_arrow"]; +const POTION_ITEMS: [&str; 4] = [ + "minecraft:potion", + "minecraft:splash_potion", + "minecraft:lingering_potion", + "minecraft:tipped_arrow", +]; /// `EffectDurationFix.fix`: every effect's factor data counts ticks active. fn effect_list(list: Tag) -> Tag { @@ -389,9 +451,14 @@ fn effect_list(list: Tag) -> Tag { .map(|mut effect| { let duration = super::dynamic::as_int(effect.get("Duration"), -1); update(&mut effect, "FactorCalculationData", |mut data| { - let timestamp = super::dynamic::as_int(data.get("effect_changed_timestamp"), -1); + let timestamp = + super::dynamic::as_int(data.get("effect_changed_timestamp"), -1); remove(&mut data, "effect_changed_timestamp"); - set(&mut data, "ticks_active", Tag::Int(timestamp.wrapping_sub(duration))); + set( + &mut data, + "ticks_active", + Tag::Int(timestamp.wrapping_sub(duration)), + ); data }); effect @@ -408,39 +475,115 @@ fn effect_duration(cx: &mut Cx) { }; cx.each(Ty::Entity, living); cx.each(Ty::Player, living); - item_tag(cx, |id| POTION_ITEMS.contains(&id), |tag| update(tag, "CustomPotionEffects", effect_list)); + item_tag( + cx, + |id| POTION_ITEMS.contains(&id), + |tag| update(tag, "CustomPotionEffects", effect_list), + ); } // ------------------------------------------------------------------ 3438, 3447 const POTTERY_SHARDS: &[(&str, &str)] = &[ - ("minecraft:pottery_shard_archer", "minecraft:archer_pottery_shard"), - ("minecraft:pottery_shard_prize", "minecraft:prize_pottery_shard"), - ("minecraft:pottery_shard_arms_up", "minecraft:arms_up_pottery_shard"), - ("minecraft:pottery_shard_skull", "minecraft:skull_pottery_shard"), + ( + "minecraft:pottery_shard_archer", + "minecraft:archer_pottery_shard", + ), + ( + "minecraft:pottery_shard_prize", + "minecraft:prize_pottery_shard", + ), + ( + "minecraft:pottery_shard_arms_up", + "minecraft:arms_up_pottery_shard", + ), + ( + "minecraft:pottery_shard_skull", + "minecraft:skull_pottery_shard", + ), ]; const POTTERY_SHERDS: &[(&str, &str)] = &[ - ("minecraft:angler_pottery_shard", "minecraft:angler_pottery_sherd"), - ("minecraft:archer_pottery_shard", "minecraft:archer_pottery_sherd"), - ("minecraft:arms_up_pottery_shard", "minecraft:arms_up_pottery_sherd"), - ("minecraft:blade_pottery_shard", "minecraft:blade_pottery_sherd"), - ("minecraft:brewer_pottery_shard", "minecraft:brewer_pottery_sherd"), - ("minecraft:burn_pottery_shard", "minecraft:burn_pottery_sherd"), - ("minecraft:danger_pottery_shard", "minecraft:danger_pottery_sherd"), - ("minecraft:explorer_pottery_shard", "minecraft:explorer_pottery_sherd"), - ("minecraft:friend_pottery_shard", "minecraft:friend_pottery_sherd"), - ("minecraft:heart_pottery_shard", "minecraft:heart_pottery_sherd"), - ("minecraft:heartbreak_pottery_shard", "minecraft:heartbreak_pottery_sherd"), - ("minecraft:howl_pottery_shard", "minecraft:howl_pottery_sherd"), - ("minecraft:miner_pottery_shard", "minecraft:miner_pottery_sherd"), - ("minecraft:mourner_pottery_shard", "minecraft:mourner_pottery_sherd"), - ("minecraft:plenty_pottery_shard", "minecraft:plenty_pottery_sherd"), - ("minecraft:prize_pottery_shard", "minecraft:prize_pottery_sherd"), - ("minecraft:sheaf_pottery_shard", "minecraft:sheaf_pottery_sherd"), - ("minecraft:shelter_pottery_shard", "minecraft:shelter_pottery_sherd"), - ("minecraft:skull_pottery_shard", "minecraft:skull_pottery_sherd"), - ("minecraft:snort_pottery_shard", "minecraft:snort_pottery_sherd"), + ( + "minecraft:angler_pottery_shard", + "minecraft:angler_pottery_sherd", + ), + ( + "minecraft:archer_pottery_shard", + "minecraft:archer_pottery_sherd", + ), + ( + "minecraft:arms_up_pottery_shard", + "minecraft:arms_up_pottery_sherd", + ), + ( + "minecraft:blade_pottery_shard", + "minecraft:blade_pottery_sherd", + ), + ( + "minecraft:brewer_pottery_shard", + "minecraft:brewer_pottery_sherd", + ), + ( + "minecraft:burn_pottery_shard", + "minecraft:burn_pottery_sherd", + ), + ( + "minecraft:danger_pottery_shard", + "minecraft:danger_pottery_sherd", + ), + ( + "minecraft:explorer_pottery_shard", + "minecraft:explorer_pottery_sherd", + ), + ( + "minecraft:friend_pottery_shard", + "minecraft:friend_pottery_sherd", + ), + ( + "minecraft:heart_pottery_shard", + "minecraft:heart_pottery_sherd", + ), + ( + "minecraft:heartbreak_pottery_shard", + "minecraft:heartbreak_pottery_sherd", + ), + ( + "minecraft:howl_pottery_shard", + "minecraft:howl_pottery_sherd", + ), + ( + "minecraft:miner_pottery_shard", + "minecraft:miner_pottery_sherd", + ), + ( + "minecraft:mourner_pottery_shard", + "minecraft:mourner_pottery_sherd", + ), + ( + "minecraft:plenty_pottery_shard", + "minecraft:plenty_pottery_sherd", + ), + ( + "minecraft:prize_pottery_shard", + "minecraft:prize_pottery_sherd", + ), + ( + "minecraft:sheaf_pottery_shard", + "minecraft:sheaf_pottery_sherd", + ), + ( + "minecraft:shelter_pottery_shard", + "minecraft:shelter_pottery_sherd", + ), + ( + "minecraft:skull_pottery_shard", + "minecraft:skull_pottery_sherd", + ), + ( + "minecraft:snort_pottery_shard", + "minecraft:snort_pottery_sherd", + ), ]; // ------------------------------------------------------------------ 3439 @@ -454,16 +597,32 @@ fn empty_component() -> Tag { fn sign_double_sided(cx: &mut Cx, id: &str) { let k = cx.k; cx.block_entity(id, |be| { - let lines: Vec = - (1..=4).map(|i| be.get(&format!("Text{i}")).cloned().unwrap_or_else(empty_component)).collect(); - let filtered: Vec> = (1..=4).map(|i| be.get(&format!("FilteredText{i}")).cloned()).collect(); + let lines: Vec = (1..=4) + .map(|i| { + be.get(&format!("Text{i}")) + .cloned() + .unwrap_or_else(empty_component) + }) + .collect(); + let filtered: Vec> = (1..=4) + .map(|i| be.get(&format!("FilteredText{i}")).cloned()) + .collect(); let mut front = Compound::new(); front.insert("messages".into(), Tag::List(lines.clone())); - front.insert("color".into(), be.get("Color").cloned().unwrap_or_else(|| string("black"))); - front.insert("has_glowing_text".into(), be.get("GlowingText").cloned().unwrap_or(Tag::Byte(0))); + front.insert( + "color".into(), + be.get("Color").cloned().unwrap_or_else(|| string("black")), + ); + front.insert( + "has_glowing_text".into(), + be.get("GlowingText").cloned().unwrap_or(Tag::Byte(0)), + ); if filtered.iter().any(Option::is_some) { - let messages = - filtered.into_iter().zip(&lines).map(|(f, line)| f.unwrap_or_else(|| line.clone())).collect(); + let messages = filtered + .into_iter() + .zip(&lines) + .map(|(f, line)| f.unwrap_or_else(|| line.clone())) + .collect(); front.insert("filtered_messages".into(), Tag::List(messages)); } set(be, "front_text", Tag::Compound(front)); diff --git a/crates/horizon-world/src/datafix/items.rs b/crates/horizon-world/src/datafix/items.rs index e5a3b2400..9ec13ec3f 100644 --- a/crates/horizon-world/src/datafix/items.rs +++ b/crates/horizon-world/src/datafix/items.rs @@ -1,8 +1,8 @@ //! 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, + 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}; @@ -30,7 +30,10 @@ const ITEM_TO_BLOCK_ENTITY: &[(&str, &str)] = &[ ("minecraft:hopper", "minecraft:hopper"), ("minecraft:banner", "minecraft:banner"), ("minecraft:flower_pot", "minecraft:flower_pot"), - ("minecraft:repeating_command_block", "minecraft:command_block"), + ( + "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"), @@ -71,7 +74,10 @@ const ITEM_TO_BLOCK_ENTITY: &[(&str, &str)] = &[ ("minecraft:standing_sign", "minecraft:sign"), ("minecraft:wall_sign", "minecraft:sign"), ("minecraft:piston_head", "minecraft:piston"), - ("minecraft:daylight_detector_inverted", "minecraft:daylight_detector"), + ( + "minecraft:daylight_detector_inverted", + "minecraft:daylight_detector", + ), ("minecraft:unpowered_comparator", "minecraft:comparator"), ("minecraft:powered_comparator", "minecraft:comparator"), ("minecraft:wall_banner", "minecraft:banner"), @@ -128,33 +134,118 @@ const ITEM_TO_BLOCK_ENTITY: &[(&str, &str)] = &[ /// `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:")) + 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: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"))?; + 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 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", + "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. @@ -195,7 +286,13 @@ impl Stack { 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 }) + Some(Self { + item: namespaced(&item), + count, + components: empty_map(), + remainder, + tag, + }) } fn remove_tag(&mut self, key: &str) -> Option { remove(&mut self.tag, key) @@ -226,7 +323,12 @@ impl Stack { } } /// `fixSubTag`. - fn fix_sub_tag(&mut self, key: &str, skip_missing: bool, f: impl FnOnce(&mut Self, Tag) -> Tag) { + 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; @@ -243,7 +345,8 @@ impl Stack { self.item == id } fn write(mut self) -> Tag { - let mut result = Tag::compound([("id", string(&self.item)), ("count", Tag::Int(self.count))]); + 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); @@ -270,14 +373,66 @@ const BUCKETED_MOBS: [&str; 6] = [ "minecraft:axolotl_bucket", "minecraft:tadpole_bucket", ]; -const BUCKETED_MOB_TAGS: [&str; 10] = - ["NoAI", "Silent", "NoGravity", "Glowing", "Invulnerable", "Health", "Age", "Variant", "HuntingCooldown", "BucketVariantTag"]; +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", + "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). @@ -302,7 +457,11 @@ fn fix_stack(s: &mut Stack) { 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 } + 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) { @@ -312,13 +471,28 @@ fn fix_stack(s: &mut Stack) { } s.set_component("minecraft:unbreakable", c); } - fix_enchantments(s, "Enchantments", "minecraft:enchantments", hide_flags & 1 != 0); + 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); + 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_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 { @@ -331,7 +505,11 @@ fn fix_stack(s: &mut Stack) { } if s.is("minecraft:crossbow") { s.remove_tag("Charged"); - s.move_to_component_default("ChargedProjectiles", "minecraft:charged_projectiles", &Tag::List(vec![])); + 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![])); @@ -351,10 +529,14 @@ fn fix_stack(s: &mut Stack) { } if matches!( s.item.as_str(), - "minecraft:potion" | "minecraft:splash_potion" | "minecraft:lingering_potion" | "minecraft:tipped_arrow" + "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") { + 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"); @@ -366,14 +548,21 @@ fn fix_stack(s: &mut Stack) { if s.is("minecraft:writable_book") && let Some(pages) = fix_book_pages(s) { - s.set_component("minecraft:writable_book_content", Tag::compound([("pages", pages)])); + 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())); + 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"); @@ -408,11 +597,17 @@ fn fix_stack(s: &mut Stack) { } 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 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))]), + Tag::compound([ + ("explosions", Tag::List(explosions)), + ("flight_duration", Tag::Byte(flight as i8)), + ]), ); remove(&mut f, "Explosions"); remove(&mut f, "Flight"); @@ -421,7 +616,10 @@ fn fix_stack(s: &mut Stack) { } 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())); + s.set_component( + "minecraft:firework_explosion", + fix_firework_explosion(e.clone()), + ); for k in ["Type", "Colors", "FadeColors", "Trail", "Flicker"] { remove(&mut e, k); } @@ -466,7 +664,10 @@ fn fix_display(s: &mut Stack, mut d: Tag, hide_flags: i32) -> Tag { 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(); + 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()); @@ -531,19 +732,36 @@ fn fix_block_entity_tag(s: &mut Stack, mut be: Tag, id: &str) -> Tag { 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()))); + 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" => { + "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; + 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)]) }) @@ -626,7 +844,11 @@ fn fix_block_predicate(text: &str) -> Tag { 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 props, + property[..eq].trim(), + string(property[eq + 1..].trim()), + ); } } } @@ -645,7 +867,10 @@ 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 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)); @@ -723,13 +948,19 @@ fn fix_map_decoration(d: &Tag) -> (JavaString, Tag) { ("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())), + ( + "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 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() @@ -744,7 +975,10 @@ fn fix_book_pages(s: &mut Stack) -> Option { .iter() .enumerate() .map(|(i, page)| { - let f = filtered.iter().find(|(k, _)| *k == i.to_string()).map(|(_, v)| v.as_str()); + let f = filtered + .iter() + .find(|(k, _)| *k == i.to_string()) + .map(|(_, v)| v.as_str()); filtered_text(page, f) }) .collect(); @@ -768,7 +1002,11 @@ fn fix_lodestone_tracker(s: &mut Stack) { 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)])); + set( + &mut c, + "target", + Tag::compound([("pos", p), ("dimension", d)]), + ); } if !tracked { set(&mut c, "tracked", Tag::Byte(0)); @@ -796,7 +1034,11 @@ fn fix_firework_explosion(mut e: Tag) -> Tag { /// `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() }; + 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(); @@ -855,7 +1097,8 @@ fn fix_profile_properties(v: Option<&Tag>) -> Option { /// 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))) + 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 @@ -903,7 +1146,10 @@ pub(super) fn unflatten_particle(description: &str) -> Tag { } set(&mut result, "item", stack); } - "minecraft:block" | "minecraft:block_marker" | "minecraft:falling_dust" | "minecraft:dust_pillar" => { + "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))), @@ -1023,7 +1269,10 @@ struct Reader { } impl Reader { fn new(s: &str) -> Self { - Self { s: s.chars().collect(), pos: 0 } + Self { + s: s.chars().collect(), + pos: 0, + } } fn peek(&self) -> Option { self.s.get(self.pos).copied() @@ -1043,7 +1292,10 @@ impl Reader { } fn number_text(&mut self) -> String { let start = self.pos; - while self.peek().is_some_and(|c| c.is_ascii_digit() || c == '.' || c == '-') { + while self + .peek() + .is_some_and(|c| c.is_ascii_digit() || c == '.' || c == '-') + { self.pos += 1; } self.s[start..self.pos].iter().collect() @@ -1059,7 +1311,11 @@ impl Reader { while self.peek().is_some_and(|c| c.is_ascii_digit() || c == '-') { self.pos += 1; } - self.s[start..self.pos].iter().collect::().parse().ok() + self.s[start..self.pos] + .iter() + .collect::() + .parse() + .ok() } fn vector(&mut self) -> Option { let x = self.float()?; @@ -1071,7 +1327,11 @@ impl Reader { 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())])) + 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 { @@ -1100,10 +1360,9 @@ impl Reader { } _ => { let start = self.pos; - while self - .peek() - .is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.' | '+')) - { + 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 index dda40663f..4cab8ff1d 100644 --- a/crates/horizon-world/src/datafix/json.rs +++ b/crates/horizon-world/src/datafix/json.rs @@ -76,7 +76,9 @@ impl Reader<'_> { }; 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('/')) => { + 0x2F if self.s.get(self.pos + 1) == Some(&u('*')) + || self.s.get(self.pos + 1) == Some(&u('/')) => + { if !self.lenient() { return Err(()); } @@ -253,7 +255,21 @@ impl Reader<'_> { 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 + 0x2F | 0x5C + | 0x3B + | 0x23 + | 0x3D + | 0x7B + | 0x7D + | 0x5B + | 0x5D + | 0x3A + | 0x2C + | 0x20 + | 0x09 + | 0x0C + | 0x0D + | 0x0A ) { break; } @@ -268,11 +284,16 @@ impl Reader<'_> { return Err(()); } let text = String::from_utf16_lossy(&lit); - for (word, value) in [("true", Json::Bool(true)), ("false", Json::Bool(false)), ("null", Json::Null)] { + 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() - }) + && text + .chars() + .zip(word.chars()) + .all(|(a, b)| a == b || self.lenient() && a == b.to_ascii_uppercase()) { return Ok(value); } @@ -505,7 +526,11 @@ pub fn from_nbt(tag: &Tag) -> Json { 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::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()), @@ -562,7 +587,10 @@ fn java_fp_string(d: f64, sci: String) -> String { /// A string value as a JSON text component (`{"text": s}`), stable string. pub fn text_component_json(text: &[u16]) -> Vec { - to_stable_string(&Json::Obj(vec![("text".encode_utf16().collect(), Json::Str(text.to_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))])) @@ -590,12 +618,18 @@ mod tests { 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"))]))); + 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"}"#); + assert_eq!( + String::from_utf16(&to_stable_string(&j)).unwrap(), + r#"{"color":"red","text":"a"}"# + ); } #[test] fn java_floating_point_strings() { diff --git a/crates/horizon-world/src/datafix/mod.rs b/crates/horizon-world/src/datafix/mod.rs index 1e046fced..28ec619b3 100644 --- a/crates/horizon-world/src/datafix/mod.rs +++ b/crates/horizon-world/src/datafix/mod.rs @@ -1,20 +1,20 @@ -//! 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 +//! Vanilla's DataFixers from the 1.19 schemas on (`net.minecraft.util.datafix`): +//! the data of worlds saved by 1.18.2 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`]. +//! with the schema just below its own key ([`schema`]). Data older than +//! [`OLDEST_SUPPORTED`] needs fixes that are not ported. The fixes of client +//! files (options, hotbars, debug profiles) are not ported either. pub mod dynamic; +pub mod files; mod fixes_a; mod fixes_b; mod fixes_c; mod fixes_old; -pub mod files; mod items; pub mod json; pub mod schema; @@ -36,7 +36,9 @@ pub fn identifier(id: &str) -> Option<(String, String)> { 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 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'/')); @@ -157,10 +159,15 @@ pub fn id_is(v: &Tag, id: &str) -> bool { } /// `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 { +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) + table + .iter() + .find(|(from, _)| *from == id) + .map(|(_, to)| *to) } } diff --git a/crates/horizon-world/src/datafix/schema.rs b/crates/horizon-world/src/datafix/schema.rs index 5515b6bae..c3f4c8bb0 100644 --- a/crates/horizon-world/src/datafix/schema.rs +++ b/crates/horizon-world/src/datafix/schema.rs @@ -251,7 +251,10 @@ impl Walker<'_> { 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:crafted" + | "minecraft:used" + | "minecraft:broken" + | "minecraft:picked_up" | "minecraft:dropped", ) => Some(Ty::ItemName), Some("minecraft:killed" | "minecraft:killed_by") => Some(Ty::EntityName), @@ -302,7 +305,9 @@ impl Walker<'_> { 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"] { + for slot in [ + "mainhand", "offhand", "feet", "legs", "chest", "head", "body", "saddle", + ] { self.field(Ty::ItemStack, eq, slot); } } @@ -329,7 +334,11 @@ impl Walker<'_> { }; match id { "minecraft:area_effect_cloud" => { - let f = if k >= key(4420, 0) { "custom_particle" } else { "Particle" }; + let f = if k >= key(4420, 0) { + "custom_particle" + } else { + "Particle" + }; self.field(Ty::Particle, v, f); } "minecraft:arrow" | "minecraft:spectral_arrow" => { @@ -372,7 +381,11 @@ impl Walker<'_> { if k < key(4300, 0) { self.field(Ty::ItemStack, v, "SaddleItem"); } - let split = if id == "minecraft:llama" { key(3808, 1) } else { key(3808, 2) }; + let split = if id == "minecraft:llama" { + key(3808, 1) + } else { + key(3808, 2) + }; if k < split { self.field(Ty::ItemStack, v, "DecorItem"); } @@ -425,8 +438,12 @@ impl Walker<'_> { "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") => { + "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"); } _ => {} @@ -460,8 +477,14 @@ impl Walker<'_> { 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" => { + "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), @@ -485,7 +508,9 @@ impl Walker<'_> { 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: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" => { @@ -516,7 +541,9 @@ impl Walker<'_> { } self.field(Ty::ItemStack, v, "item"); } - "minecraft:brushable_block" | "minecraft:suspicious_sand" => self.field(Ty::ItemStack, v, "item"), + "minecraft:brushable_block" | "minecraft:suspicious_sand" => { + 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 { @@ -774,18 +801,57 @@ pub fn block_entity_known(be: &Tag, k: Key) -> bool { /// 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", + "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`. diff --git a/crates/horizon-world/src/datafix/tests.rs b/crates/horizon-world/src/datafix/tests.rs index 1a8c67ec8..99d813dd2 100644 --- a/crates/horizon-world/src/datafix/tests.rs +++ b/crates/horizon-world/src/datafix/tests.rs @@ -21,7 +21,10 @@ fn item_stacks_of_1_20_take_components() { ); 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!( + 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))); @@ -38,14 +41,25 @@ fn level_data_of_1_20_moves_to_the_new_fields() { 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")); + 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]))); + 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] @@ -57,7 +71,10 @@ fn block_states_take_their_new_field_names() { 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_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 index 60bf3aff3..56c4e8da8 100644 --- a/crates/horizon-world/src/datafix/text.rs +++ b/crates/horizon-world/src/datafix/text.rs @@ -1,17 +1,25 @@ //! 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::FixFn; +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))); + 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. @@ -58,7 +66,10 @@ fn legacy_hover_event(v: &mut Tag) { 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)))])), + ( + "contents", + Tag::compound([("text", Tag::String(crate::nbt::JavaString(text)))]), + ), ]) }; set(v, "hoverEvent", new); @@ -69,7 +80,13 @@ fn stringified_flags(v: &mut Tag) { if !is_full(v) { return; } - for flag in ["bold", "italic", "underlined", "strikethrough", "obfuscated"] { + 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"))); } @@ -173,7 +190,9 @@ fn valid_uri(uri: &str) -> bool { 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) { + 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; @@ -182,7 +201,10 @@ fn valid_uri(uri: &str) -> bool { if c == '#' { hashes += 1; } - if c.is_control() || c.is_whitespace() || matches!(c, '"' | '<' | '>' | '\\' | '^' | '`' | '{' | '|' | '}') { + if c.is_control() + || c.is_whitespace() + || matches!(c, '"' | '<' | '>' | '\\' | '^' | '`' | '{' | '|' | '}') + { return false; } i += 1; @@ -207,7 +229,12 @@ mod tests { 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}}"#)); + // The page is written as an int (`createInt`), not as JSON's narrowest number. + let mut expected = j(r#"{"text":"a","click_event":{"action":"change_page"}}"#); + if let Some(click) = expected.get_mut("click_event") { + super::super::dynamic::set(click, "page", Tag::Int(1)); + } + assert_eq!(c, expected); 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"}"#)); From 8eca39aff1baca12c5244568a2f6bc4e063bc351 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:07:06 +0200 Subject: [PATCH 13/24] Equip, saddle and light from dispensers as vanilla does Villagers, piglins, piglin brutes, allays, dolphins, foxes and pandas can pick up loot from construction (their constructors call setCanPickUpLoot), so they take dispensed equipment without spawn finalization. Nautilus saddles play item.nautilus_saddle_equip. The oracle level is the overworld, so fire in an obsidian frame lights a portal there too, and it answers getEntity(id) and level-wide entity queries, which leash cutting needs. Unseeded item motion is compared within its full spread (item motion plus a shearing sheep's push). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/dispenser.rs | 4 ++-- crates/horizon-client/src/mobs.rs | 12 ++++++++++++ .../parity/VanillaBlockUpdateOracle.java | 19 +++++++++++++++++++ .../parity/VanillaDispenserOracle.java | 1 + tools/test_dispenser_parity.py | 9 ++++++--- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index e8a3ecffa..bfbea7038 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -283,9 +283,9 @@ fn equip_sound(m: &Mob, slot: Slot, default: Option) -> Option { _ if equine(s) => "minecraft:entity.horse.saddle".into(), _ if horizon_client::mobs::nautilus::is_nautilus(s) => { if m.fluids.eye_in_water { - "minecraft:entity.nautilus.saddle_underwater_equip".into() + "minecraft:item.nautilus_saddle_underwater_equip".into() } else { - "minecraft:entity.nautilus.saddle_equip".into() + "minecraft:item.nautilus_saddle_equip".into() } } _ => return default, diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index ff0e37fa5..eb186d7ae 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -994,6 +994,18 @@ impl Mob { }, kind, }; + // setCanPickUpLoot(true) in the Villager, AbstractPiglin, Allay, + // Dolphin, Fox and Panda constructors. + mob.extra.pick_up_loot = matches!( + mob.species(), + Species::Villager + | Species::Piglin + | Species::PiglinBrute + | Species::Allay + | Species::Dolphin + | Species::Fox + | Species::Panda + ); ai::register(&mut mob); mob } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java index 21908b3e2..037788547 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java @@ -217,6 +217,23 @@ public final class VanillaBlockUpdateOracle { } } } + /** ServerLevel.getEntity(id) over the level's entities. */ + @Override public Entity getEntity(int id) { + if (entities == null) return null; + for (Entity e : entities) if (e.getId() == id && !e.isRemoved()) return e; + return null; + } + /** Every entity of the level (no area), as ServerLevel's entity getter would. */ + @Override public void getEntities(net.minecraft.world.level.entity.EntityTypeTest type, java.util.function.Predicate filter, List out, int max) { + if (entities == null) return; + for (Entity e : List.copyOf(entities)) { + T t = type.tryCast(e); + if (t != null && !e.isRemoved() && filter.test(t)) { + out.add(t); + if (out.size() >= max) return; + } + } + } @Override public List getEntitiesOfClass(Class type, AABB box, java.util.function.Predicate filter) { return getEntities(net.minecraft.world.level.entity.EntityTypeTest.forClass(type), box, filter); } @@ -297,6 +314,8 @@ public final class VanillaBlockUpdateOracle { return attributes; } @Override public net.minecraft.world.level.dimension.DimensionType dimensionType() { return OVERWORLD_TYPE; } + /** The overworld (fire in an obsidian frame lights a portal here). */ + @Override public net.minecraft.resources.ResourceKey dimension() { return net.minecraft.world.level.Level.OVERWORLD; } @Override public long getOverworldClockTime() { return DAY.totalTicks(); } @Override public float getMoonBrightness(BlockPos p) { return net.minecraft.world.level.dimension.DimensionType.MOON_BRIGHTNESS_PER_PHASE[environmentAttributes().getValue(net.minecraft.world.attribute.EnvironmentAttributes.MOON_PHASE, p).index()]; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index 0aba1dce4..85ab872e5 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -84,6 +84,7 @@ public final class VanillaDispenserOracle { } catch (Throwable e) { Throwable c = e; while (c.getCause() != null) c = c.getCause(); + if (System.getenv("ORACLE_TRACE") != null) c.printStackTrace(); StackTraceElement[] st = c.getStackTrace(); result.clear(); result.add("error " + c.getClass().getSimpleName() + " " + (st.length > 0 ? st[0].toString() : "") + " " + c.getMessage()); diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index 4dbb83b13..85d15a100 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -272,8 +272,9 @@ def cases(facts, count, seed, only): if target['type'] == 'minecraft:sulfur_cube' and rng.random() < 0.5: target['equip'] = {'body': plain(rng.choice(SWALLOWED))} # Leads: tied to an earlier target, or that target holds this one's. - if entities and rng.random() < 0.3: - target['leash'] = rng.randrange(len(entities)) + holders = [i for i, e in enumerate(entities) if e['type'] != 'minecraft:armor_stand'] + if holders and rng.random() < 0.3: + target['leash'] = rng.choice(holders) entities.append(target) if bare == 'shears' and len(entities) == 1 and rng.random() < 0.3: # A holder out of the dispenser's reach whose lead runs to the target. @@ -320,7 +321,9 @@ def numbers_close(java, rust): for x, y in zip(a, b): if x.startswith('~'): try: - if abs(float(x[1:]) - float(y)) > 0.25: + # An item's own motion spans 0.2 per axis, and a shearing + # sheep pushes each wool with its own random by up to 0.1 more. + if abs(float(x[1:]) - float(y)) > 0.4: return False except ValueError: return False From e1eabdc7816cc0f437a6d569732cce2065300bec Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:07:10 +0200 Subject: [PATCH 14/24] Pass the save upgrade harness from 1.18.2: block entity data stays empty A block entity read inside an item stack remembers the map it was read from; that memory is not part of the value, so an item's block entity tag with only its id still counts as empty and is dropped, as in vanilla. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/datafix/dynamic.rs | 4 +++- crates/horizon-world/src/datafix/mod.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/horizon-world/src/datafix/dynamic.rs b/crates/horizon-world/src/datafix/dynamic.rs index a831f07ab..23e50e602 100644 --- a/crates/horizon-world/src/datafix/dynamic.rs +++ b/crates/horizon-world/src/datafix/dynamic.rs @@ -214,8 +214,10 @@ pub fn copy_field(from: &Tag, field: &str, to: &mut Tag, to_field: &str) { set(to, to_field, v.clone()); } } +/// `Dynamic.equals(emptyMap())`: the map a record remembers it was read from +/// (see `datafix::mark_read`) is not part of the value. pub fn is_empty_map(tag: &Tag) -> bool { - matches!(tag, Tag::Compound(c) if c.is_empty()) + matches!(tag, Tag::Compound(c) if c.keys().all(|k| *k == key(super::LAST_READ))) } /// `Dynamic.orElseEmptyMap` of an optional field. pub fn or_empty_map(tag: Option<&Tag>) -> Tag { diff --git a/crates/horizon-world/src/datafix/mod.rs b/crates/horizon-world/src/datafix/mod.rs index 28ec619b3..ad5c6f342 100644 --- a/crates/horizon-world/src/datafix/mod.rs +++ b/crates/horizon-world/src/datafix/mod.rs @@ -105,7 +105,7 @@ impl Cx<'_> { /// writing lays the typed fields over it. A typed field that a fix drops (by /// casting to a type without it) therefore comes back on write with its value /// from that read. Each entity keeps that map under this key while fixes run. -const LAST_READ: &str = "\u{0}last_read"; +pub(crate) const LAST_READ: &str = "\u{0}last_read"; /// Remember a record's current fields as the map it was read from. pub fn mark_read(e: &mut Tag) { From 36209e7827adc630cb3b2e2725c758fcef789b62 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:10:30 +0200 Subject: [PATCH 15/24] Place dispensed shulker boxes only where DirectionalPlaceContext can A dispenser places a shulker box in the block in front or nowhere, and only when that block's canBeReplaced accepts it (a snow layer only when it is a single layer). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/dispenser.rs | 8 +++---- crates/horizon-world/src/level/place.rs | 23 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index bfbea7038..d673b1f6f 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -1463,10 +1463,6 @@ impl Game<'_> { .facts .is_air(self.block_at((front.0, front.1 - 1, front.2))); let clicked_face = if below_empty { facing } else { Direction::Up }; - let front_state = self.block_at(front); - if !(self.res.facts.is_air(front_state) || self.res.facts.get(front_state).replaceable) { - return Ok((Some(stack), FAIL)); - } let ctx = PlaceContext { clicked_pos: Self::pos_of(front), face: clicked_face, @@ -1482,6 +1478,10 @@ impl Game<'_> { item: stack.item.to_string(), can_use_game_master_blocks: false, }; + // DirectionalPlaceContext places at the front block or nowhere. + if !self.with_level(|b, level, ticks| b.directional_can_place(level, ticks, &ctx)) { + return Ok((Some(stack), FAIL)); + } let outcome = self.with_level(|b, level, ticks| b.place_block(level, ticks, &ctx)); let PlaceOutcome::Placed { pos: placed, state } = outcome else { self.process_level_events()?; diff --git a/crates/horizon-world/src/level/place.rs b/crates/horizon-world/src/level/place.rs index 9ab3f8917..16b2aad5e 100644 --- a/crates/horizon-world/src/level/place.rs +++ b/crates/horizon-world/src/level/place.rs @@ -1595,6 +1595,29 @@ impl Behaviours { ) -> PlaceOutcome { self.sim(level, ticks).place(ctx) } + /// `DirectionalPlaceContext.canPlace`: whether the block at the clicked + /// position can be replaced by the item (a dispenser places only there). + pub fn directional_can_place( + &self, + level: &mut L, + ticks: &mut Ticks, + ctx: &PlaceContext, + ) -> bool { + let sim = self.sim(level, ticks); + let inner = Ctx { + pos: ctx.clicked_pos, + hit_pos: ctx.clicked_pos, + face: ctx.face, + hit: ctx.hit, + inside: ctx.inside, + yaw: ctx.yaw, + pitch: ctx.pitch, + sneaking: ctx.sneaking, + item: ctx.item.clone(), + replace_clicked: true, + }; + sim.can_be_replaced(sim.state(ctx.clicked_pos), &inner) + } /// The position and state `BlockItem.place` would place for `ctx`, without /// writing anything (None when vanilla's placement fails). pub fn placement_state( From 93b785a2efcd68c9ec75ab46a3da6179b7ccae94 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:28:53 +0200 Subject: [PATCH 16/24] Describe save upgrades from 1.18.2 in the status table Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 863822f8f..384566d2a 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ round-trip harness has not been rerun. | Rendering | 0.75 | Entity models match over 150 layers; block entity renderers and HUD overlays not compared | | Lighting | 1 | None: saved and relit light matches vanilla, slabs and stairs included | | World generation | 1 | None: 2,352 whole chunks over 225 structure sites, in all three dimensions, identical to vanilla | -| Region files and saves | 0.75 | Save round-trip harness fails; older DataVersions are not upgraded | +| Region files and saves | 0.75 | Saves round-trip with vanilla, and worlds from 1.18.2 to 26.2 upgrade as vanilla's DataFixers do over 22 sample worlds; worlds older than 1.18.2 are not upgraded | | Entities and physics | 0.75 | Attributes and dimensions match; AI, loot and spawn rules not compared; drowned, villagers, traders and the wither unverified | | Interaction and inventory | 1 | None: menu clicks match over 6,000 click sequences | | Menus, HUD and chat | 0.75 | Commands parse as vanilla over 39,685 inputs, but /item modify, /spectate, /waypoint and /dialog do not run; no tab list or world border rendering | @@ -46,7 +46,7 @@ round-trip harness has not been rerun. ### Next steps -1. Rerun the save round-trip harness and upgrade saves from older DataVersions. +1. Upgrade saves older than 1.18.2 (the DataFixers before DataVersion 2970). 2. Draw every item through the item model pipeline, and add the resource pack screen. 3. An audio harness: sound engine attenuation, weighted sound events, music and ambience. 4. Mob AI, loot and spawn-rule harnesses, and an oracle for drowned, villagers, traders and the wither. From 6605e6cdf16e1a91cd14731f39ff6f7755842d7f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:32:28 +0200 Subject: [PATCH 17/24] Run the dispenser fixture in the oracle's plains light and level The fixture's chunks are plains (empty chunks read as badlands, which has no bone meal flowers) and carry the case's sky and block light, as the oracle level does, so grass bone meal places flowers and crops check their light like vanilla's. The oracle's client sounds include the lava fizz (1501). Cases build two-block plants whole: a lone lower half is not a world vanilla makes. Co-Authored-By: Claude Opus 5.5 --- .../src/game/dispense_fixture.rs | 22 ++++++++++++++++--- .../parity/VanillaDispenserOracle.java | 1 + tools/test_dispenser_parity.py | 15 +++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index 340172d61..7ad14cb07 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -92,11 +92,16 @@ impl Game<'_> { fn fixture_reset(&mut self, seed: i64) { self.world = World::new(self.res.facts.air, seed); + // The oracle level is plains everywhere (bone meal's flowers, freezing). + let plains = u32::from(self.res.biomes.id("minecraft:plains")); for cx in -2..=2 { for cz in -2..=2 { - self.world - .chunks - .insert((cx, cz), Arc::new(Chunk::empty(cx, cz))); + let mut chunk = Chunk::empty(cx, cz); + for section in &mut chunk.sections { + Arc::make_mut(section).biomes = + horizon_core::paletted::PalettedContainer::new(horizon_client::chunk::BIOME_STRATEGY, plains); + } + self.world.chunks.insert((cx, cz), Arc::new(chunk)); } } self.block_ticks = Ticks::new(0); @@ -125,6 +130,17 @@ impl Game<'_> { fn dispense_case(&mut self, case: &Value) -> Result> { let seed = case["seed"].as_i64().unwrap_or(0); self.fixture_reset(seed); + // The oracle level answers one sky and one block light everywhere. + let light = case["light"].as_array(); + let level = |i: usize, default: i64| { + light.and_then(|l| l.get(i)).and_then(Value::as_i64).unwrap_or(default) as u8 + }; + let packed = (level(0, 15) << 4) | level(1, 0); + for chunk in self.world.chunks.values_mut() { + for section in &mut Arc::make_mut(chunk).sections { + Arc::make_mut(section).light = horizon_client::chunk::Light::Uniform(packed); + } + } self.world.game_time = case["time"].as_i64().unwrap_or(0); // The oracle level's clock stands at day time 6000. self.world.day_time = 6000; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index 85ab872e5..2bc24d850 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -46,6 +46,7 @@ public final class VanillaDispenserOracle { case 1018 -> "minecraft:entity.blaze.shoot"; case 1051 -> "minecraft:entity.wind_charge.throw"; case 1505 -> "minecraft:item.bone_meal.use"; + case 1501 -> "minecraft:block.lava.extinguish"; case 1009 -> data == 0 ? "minecraft:block.fire.extinguish" : data == 1 ? "minecraft:block.generic.extinguish_fire" : null; case 2001 -> { BlockState state = Block.stateById(data); diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index 85d15a100..b7a076784 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -211,8 +211,17 @@ def build_scene(cells, states, rng, scene, front, facing): h.put(cells, at, states.get('air')) +def halves(states, name): + """Whether a block comes in an upper and a lower half (tall plants, dripleaves, pitchers).""" + name = name if ':' in name else 'minecraft:' + name + return any(states.facts.states[i][4].get('properties', {}).get('half') == 'upper' for i in states.facts.by_block[name]) + + def front_state(states, rng, name): """Mostly the default state, sometimes a random one (charges, honey, ages, lit...).""" + if halves(states, name): + # A lone half is not a world vanilla makes; the pair is placed by put_front. + return states.get(name) if name in ('respawn_anchor', 'beehive', 'bee_nest', 'campfire', 'candle', 'wheat', 'sweet_berry_bush', 'rail', 'powered_rail', 'candle_cake', 'soul_campfire') or rng.random() < 0.3: return states.any(rng, name) @@ -237,6 +246,12 @@ def cases(facts, count, seed, only): below = (dx, dy - 1, dz) palette = FRONT_FOR[bare] if bare in FRONT_FOR and rng.random() < 0.8 else FRONT[kind] front_name = rng.choice(palette) + above = (dx, dy + 1, dz) + if halves(states, front_name) and front_name != 'pitcher_crop': + if above == (0, 0, 0): + front_name = 'air' + else: + h.put(cells, above, states.get(front_name, half='upper')) if front_name != 'air': h.put(cells, front, front_state(states, rng, front_name)) if below != (0, 0, 0) and rng.random() < 0.8: From 3a875c709e10fd41e51584b6011dbf5eea4e9347 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:39:26 +0200 Subject: [PATCH 18/24] Refuse only worlds older than 1.18.2 when opening them Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/save.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 32f904b5b..cf337a5be 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -116,7 +116,7 @@ impl SessionLock { /// (`FileFixerUpper`), then the DataFixers upgrades of every chunk, entity chunk, /// POI chunk, player, saved data, advancements and statistics file. Vanilla runs /// the latter as it reads each file; Horizon runs them all on opening, which reads -/// the same data. Worlds older than the ported fixes (1.20) are refused. +/// the same data. Worlds older than the ported fixes (1.18.2) are refused. pub fn upgrade_older_world(world: &Path) -> Result<()> { let Some(root) = read_dat(&world.join("level.dat")) else { return Ok(()); @@ -130,7 +130,7 @@ pub fn upgrade_older_world(world: &Path) -> Result<()> { } if version < i64::from(horizon_world::datafix::OLDEST_SUPPORTED) { return Err(format!( - "{} was saved with DataVersion {version}; worlds older than DataVersion {} (1.20) \ + "{} was saved with DataVersion {version}; worlds older than DataVersion {} (1.18.2) \ cannot be upgraded yet (tools/upgrade_world.py runs vanilla's own upgrader)", world.display(), horizon_world::datafix::OLDEST_SUPPORTED @@ -2199,10 +2199,10 @@ mod tests { super::read_dat(&dir.join("level.dat")) .and_then(|r| super::number(r.get("Data")?.get("DataVersion"))) }; - // Older than the ported fixes: refused, left as it is. - super::write_dat(&dir.join("level.dat"), level(3000)).unwrap(); + // 1.18, older than the ported fixes: refused, left as it is. + super::write_dat(&dir.join("level.dat"), level(2860)).unwrap(); assert!(super::upgrade_older_world(&dir).is_err()); - assert_eq!(version(&dir), Some(3000)); + assert_eq!(version(&dir), Some(2860)); // 1.21.1: upgraded to the current version. super::write_dat(&dir.join("level.dat"), level(3955)).unwrap(); super::upgrade_older_world(&dir).unwrap(); From 877165a6c9fd70ce3ac1d988ca10636e383302fd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:41:30 +0200 Subject: [PATCH 19/24] Read region headers as vanilla's RegionFile does in the region tests Invalid allocations are dropped rather than refused, overlaps are not checked, and an empty or truncated file is a region without chunks. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/tests/world_io.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/horizon-world/tests/world_io.rs b/crates/horizon-world/tests/world_io.rs index bbbfab360..65b5006a8 100644 --- a/crates/horizon-world/tests/world_io.rs +++ b/crates/horizon-world/tests/world_io.rs @@ -120,12 +120,21 @@ fn malformed_region(path: &Path, first: u32, second: u32, payload_len: u32, code } #[test] -fn corrupt_allocations_lengths_codecs_and_missing_external_files_fail() { +fn corrupt_allocations_are_dropped_and_bad_payloads_fail() { let dir = Directory::new(); - for (first, second) in [(257, 0), (512, 0), (1025, 0), (513, 513)] { + // RegionFile's constructor drops (and logs) entries whose sector overlaps the + // header, that have no sectors or that start past the end of the file; it + // does not check overlaps between chunks. + for (first, second, chunks) in [(257, 0, 0), (512, 0, 0), (1025, 0, 0), (513, 513, 2)] { malformed_region(&dir.region(), first, second, 1, 3); - assert!(RegionFile::open(dir.region()).is_err()); + let reader = RegionFile::open(dir.region()).unwrap(); + assert_eq!(reader.chunks().count(), chunks); } + // An empty or truncated file reads as a region without chunks. + fs::write(dir.region(), b"").unwrap(); + assert_eq!(RegionFile::open(dir.region()).unwrap().chunks().count(), 0); + fs::write(dir.region(), [0, 0, 2, 1]).unwrap(); + assert_eq!(RegionFile::open(dir.region()).unwrap().chunks().count(), 0); for (len, codec) in [ (0, 3), (4093, 3), From a47f1ec58b200f588590635d7101ce31f511e308 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:50:20 +0200 Subject: [PATCH 20/24] Dispense mobs with their jockeys as vanilla's spawn eggs do A dispensed spawn egg's mob gets the riders finalizeSpawn adds (chicken jockeys, spider jockeys, strider jockeys). The harness leaves which mob comes out a baby and rides to the spawning port's finalization draws, as it already does for the level random of spawning cases. Cases no longer grow plants out of a dispenser or build a door from mismatched halves, and the oracle runs every case three times so an unseeded value is marked even when two runs happen to agree. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/dispenser.rs | 6 +++- .../parity/VanillaDispenserOracle.java | 12 ++++---- tools/test_dispenser_parity.py | 28 ++++++++++++++++++- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index d673b1f6f..b943956e0 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -729,7 +729,11 @@ impl Game<'_> { f64::from(p.1) + y_off, f64::from(p.2) + 0.5, ]; - self.spawn_mob(&name, at, true) + let id = self.spawn_mob(&name, at, true)?; + // finalizeSpawn's riders (chicken jockeys, spider jockeys...) come + // with a dispensed mob as with any spawn that is not natural. + self.spawn_jockeys(id, false); + Some(id) } /// `EquipmentDispenseItemBehavior.dispenseEquipment`: the first living /// entity in front that `canEquipWithDispenser` gets one of the item. diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index 2bc24d850..ed8c01096 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -31,8 +31,8 @@ import net.minecraft.world.level.levelgen.LegacyRandomSource; * result lists block changes, container contents, spawned entities with their * position and motion, the targets' state afterwards, level events, sounds * (those the client plays for level events included), game events and the - * level random. Every case runs twice: numbers that differ between the runs - * come from an entity's own unseeded random and are marked with "~". + * level random. Every case runs three times: numbers that differ between the + * runs come from an entity's own unseeded random and are marked with "~". * Used by tools/test_dispenser_parity.py. */ public final class VanillaDispenserOracle { @@ -71,9 +71,10 @@ public final class VanillaDispenserOracle { int index = 0; for (var line : (Iterable) lines::iterator) { var f = JsonParser.parseString(line).getAsJsonObject(); - List a = attempt(f), b = attempt(f); + // Three runs: a value from an unseeded random rarely repeats thrice. + List a = attempt(f), b = attempt(f), c = attempt(f); out.print("case " + index++ + "\n"); - for (String s : merge(a, b)) out.print(s + "\n"); + for (String s : merge(merge(a, b), c)) out.print(s + "\n"); } } } @@ -108,7 +109,8 @@ public final class VanillaDispenserOracle { out.add(x); continue; } - for (int j = 0; j < p.length; j++) if (!p[j].equals(q[j])) p[j] = "~" + p[j]; + // A token already marked in an earlier merge stays marked. + for (int j = 0; j < p.length; j++) if (!p[j].equals(q[j]) && !p[j].startsWith("~")) p[j] = "~" + p[j]; out.add(String.join(" ", p)); } return out; diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index b7a076784..61f74d496 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -107,6 +107,11 @@ FRONT_FOR = { } # Structures a behaviour completes (golems, the wither, a nether portal). SCENES = {'carved_pumpkin': ['snow_golem', 'iron_golem'], 'wither_skeleton_skull': ['wither'], 'flint_and_steel': ['portal']} +# Plants that need soil (or a log) under them to exist at all. +ROOTED = {'wheat', 'carrots', 'potatoes', 'beetroots', 'oak_sapling', 'birch_sapling', 'spruce_sapling', 'cherry_sapling', + 'mangrove_propagule', 'sweet_berry_bush', 'cocoa', 'bamboo_sapling', 'bamboo', 'pumpkin_stem', 'melon_stem', + 'torchflower_crop', 'pitcher_crop', 'small_dripleaf', 'big_dripleaf', 'warped_fungus', 'brown_mushroom', 'kelp', + 'sea_pickle', 'seagrass', 'short_grass', 'fern', 'dandelion', 'pink_petals', 'twisting_vines', 'azalea'} BELOW = ['air', 'stone', 'water', 'rail', 'grass_block', 'soul_sand', 'dirt', 'farmland', 'netherrack'] TARGETS = { @@ -246,12 +251,17 @@ def cases(facts, count, seed, only): below = (dx, dy - 1, dz) palette = FRONT_FOR[bare] if bare in FRONT_FOR and rng.random() < 0.8 else FRONT[kind] front_name = rng.choice(palette) + if facing == 'up' and front_name in ROOTED: + # Nothing grows out of a dispenser: vanilla never lets these stand on one. + front_name = 'air' above = (dx, dy + 1, dz) if halves(states, front_name) and front_name != 'pitcher_crop': if above == (0, 0, 0): front_name = 'air' else: - h.put(cells, above, states.get(front_name, half='upper')) + # The upper half matches the lower one in everything but its half. + lower = states.facts.states[states.get(front_name)][4].get('properties', {}) + h.put(cells, above, states.get(front_name, **{**lower, 'half': 'upper'})) if front_name != 'air': h.put(cells, front, front_state(states, rng, front_name)) if below != (0, 0, 0) and rng.random() < 0.8: @@ -313,6 +323,16 @@ FINALIZED = ('_spawn_egg', 'salmon_bucket', 'cod_bucket', 'pufferfish_bucket', ' def finalizes(case): return any(any(tag in i[4] for tag in FINALIZED) for i in case['items'] if i[:3] == [0, 0, 0]) +def spawned_type(case): + item = next(i[4] for i in case['items'] if i[:3] == [0, 0, 0]).split('"')[1].split(':')[1] + return 'minecraft:' + item.removesuffix('_spawn_egg').removesuffix('_bucket') + + +def rider(line, spawned): + parts = line.split(' ') + return parts[0] == 'N' and parts[1] not in (spawned, 'minecraft:item') + + def voice(line): parts = line.split(' ') if parts[0] == 'S' and len(parts) == 3 and parts[2].lstrip('~').startswith('minecraft:entity.'): @@ -403,6 +423,12 @@ def main(): continue if finalizes(case): unseeded += 1 + # Riders finalizeSpawn adds (a baby zombie's chicken, a spider's + # skeleton) depend on whether the mob came out a baby and on the + # jockey odds, both finalization draws: only the egg's own mob is compared. + spawned = spawned_type(case) + a = [line for line in a if not rider(line, spawned)] + b = [line for line in b if not rider(line, spawned)] # EntityType.spawn plays the new mob's ambient sound: which one (a # wolf pants or barks, in its sound variant) comes from the mob's # own unseeded random, so only that a voice plays, and where, is compared. From 54a92f23f0a3754a72cca0f7649bea05df040d7b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:52:18 +0200 Subject: [PATCH 21/24] Give every block update oracle level a chunk generator Bone meal and saplings reach for getChunkSource().getGenerator(); the shared oracle level now answers it, as the dispenser oracle did. Co-Authored-By: Claude Opus 5.5 --- .../parity/VanillaBlockUpdateOracle.java | 26 +++++++++++++++++++ .../parity/VanillaDispenserOracle.java | 25 ------------------ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java index 037788547..606e2ae34 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java @@ -517,8 +517,34 @@ public final class VanillaBlockUpdateOracle { return new BlockPos(a.get(0).getAsInt(), a.get(1).getAsInt(), a.get(2).getAsInt()); } + /** + * The chunk source bone meal reaches for: saplings, grass and moss place + * their features with `getChunkSource().getGenerator()`, which none of + * them reads beyond passing it along. + */ + static final class Chunks extends net.minecraft.server.level.ServerChunkCache { + static net.minecraft.world.level.chunk.ChunkGenerator generator; + + private Chunks() { + super(null, null, null, null, null, null, 0, 0, false, null); + } + + @Override + public net.minecraft.world.level.chunk.ChunkGenerator getGenerator() { + return generator; + } + + /** Leads send entity link packets; nobody is tracking. */ + @Override + public void sendToTrackingPlayers(final net.minecraft.world.entity.Entity entity, final net.minecraft.network.protocol.Packet packet) { + } + } + static World build(JsonObject f) throws Exception { World w = make(World.class); + // Saplings, grass and moss place features through the chunk source's generator. + if (Chunks.generator == null) Chunks.generator = make(net.minecraft.world.level.levelgen.FlatLevelSource.class); + set(net.minecraft.server.level.ServerLevel.class, w, "chunkSource", make(Chunks.class)); w.cells = new HashMap<>(); w.log = new StringBuilder(); w.blockEvents = new java.util.LinkedHashSet<>(); diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index ed8c01096..26bb9a745 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -138,29 +138,6 @@ public final class VanillaDispenserOracle { throw new IllegalStateException("no slots in " + be); } - /** - * The chunk source bone meal reaches for: saplings, grass and moss place - * their features with `getChunkSource().getGenerator()`, which none of - * them reads beyond passing it along. - */ - static final class Chunks extends net.minecraft.server.level.ServerChunkCache { - static net.minecraft.world.level.chunk.ChunkGenerator generator; - - private Chunks() { - super(null, null, null, null, null, null, 0, 0, false, null); - } - - @Override - public net.minecraft.world.level.chunk.ChunkGenerator getGenerator() { - return generator; - } - - /** Leads send entity link packets; nobody is tracking. */ - @Override - public void sendToTrackingPlayers(final Entity entity, final net.minecraft.network.protocol.Packet packet) { - } - } - static Method dispenseFrom(Class type) throws Exception { for (Class c = type; c != null; c = c.getSuperclass()) { try { @@ -218,8 +195,6 @@ public final class VanillaDispenserOracle { w.entities = new ArrayList<>(); w.registries = ParityData.access; w.server.loot.delegate = ParityData.loot; - if (Chunks.generator == null) Chunks.generator = VanillaBlockUpdateOracle.make(net.minecraft.world.level.levelgen.FlatLevelSource.class); - VanillaBlockUpdateOracle.set(ServerLevel.class, w, "chunkSource", VanillaBlockUpdateOracle.make(Chunks.class)); VanillaBlockUpdateOracle.set(Level.class, w, "damageSources", new net.minecraft.world.damagesource.DamageSources(ParityData.access)); if (f.has("rules")) { for (var rule : f.getAsJsonObject("rules").entrySet()) { From ae3f129c8eded1b0efb7bef2f3f303064d050f54 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:54:06 +0200 Subject: [PATCH 22/24] Format the dispenser parity work Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/boats.rs | 12 +- .../src/game/dispense_fixture.rs | 59 +++++--- crates/horizon-client/src/game/dispenser.rs | 135 ++++++++++++++---- crates/horizon-client/src/game/golems.rs | 4 +- crates/horizon-client/src/game/persist.rs | 11 +- crates/horizon-client/src/game/sculk.rs | 7 +- crates/horizon-client/src/items.rs | 13 +- crates/horizon-world/src/level/hooks.rs | 6 +- crates/horizon-world/src/level/portal.rs | 5 +- 9 files changed, 194 insertions(+), 58 deletions(-) diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index a1d0458bf..fbac6ab14 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -175,7 +175,10 @@ impl BoatModels { } } } - Self { models, chest_models } + Self { + models, + chest_models, + } } } impl Game<'_> { @@ -639,9 +642,10 @@ impl Game<'_> { if self.boats[i].chest && (sneaking || self.riding.is_some()) { let b = &self.boats[i]; let key = (i32::MAX - 15, super::minecarts::CART_KEY_Y, b.id as i32); - self.world - .block_entities - .insert(key, horizon_client::containers::BlockEntity::Chest(b.items.clone())); + self.world.block_entities.insert( + key, + horizon_client::containers::BlockEntity::Chest(b.items.clone()), + ); self.container = Some(key); self.screen = super::Screen::Inventory; self.capture(false); diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index 7ad14cb07..a906ba259 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -65,11 +65,12 @@ enum Target { Stand(u32), } - impl Game<'_> { /// Run every case of `spec` ("CASES,OUTPUT") and write the results. pub(super) fn dispense_fixture(&mut self, spec: &str) -> Result<()> { - let (input, output) = spec.split_once(',').ok_or("HORIZON_DISPENSE_FIXTURE=CASES,OUTPUT")?; + let (input, output) = spec + .split_once(',') + .ok_or("HORIZON_DISPENSE_FIXTURE=CASES,OUTPUT")?; // Saplings and feature placers grow from the live feature registry. self.install_live_features(); let text = std::fs::read_to_string(input)?; @@ -98,8 +99,10 @@ impl Game<'_> { for cz in -2..=2 { let mut chunk = Chunk::empty(cx, cz); for section in &mut chunk.sections { - Arc::make_mut(section).biomes = - horizon_core::paletted::PalettedContainer::new(horizon_client::chunk::BIOME_STRATEGY, plains); + Arc::make_mut(section).biomes = horizon_core::paletted::PalettedContainer::new( + horizon_client::chunk::BIOME_STRATEGY, + plains, + ); } self.world.chunks.insert((cx, cz), Arc::new(chunk)); } @@ -133,7 +136,10 @@ impl Game<'_> { // The oracle level answers one sky and one block light everywhere. let light = case["light"].as_array(); let level = |i: usize, default: i64| { - light.and_then(|l| l.get(i)).and_then(Value::as_i64).unwrap_or(default) as u8 + light + .and_then(|l| l.get(i)) + .and_then(Value::as_i64) + .unwrap_or(default) as u8 }; let packed = (level(0, 15) << 4) | level(1, 0); for chunk in self.world.chunks.values_mut() { @@ -187,7 +193,12 @@ impl Game<'_> { targets.push(self.fixture_target(e)?); } // Leashable.setLeashedTo: a lead from a target to an earlier one. - for (e, t) in case["entities"].as_array().into_iter().flatten().zip(&targets) { + for (e, t) in case["entities"] + .as_array() + .into_iter() + .flatten() + .zip(&targets) + { let (Some(holder), Target::Mob(id)) = (e["leash"].as_u64(), t) else { continue; }; @@ -294,16 +305,13 @@ impl Game<'_> { let flag = |name: &str| e[name].as_bool().unwrap_or(false); let ageable = !matches!( s, - Species::Zombie - | Species::Skeleton - | Species::Bogged - | Species::SnowGolem + Species::Zombie | Species::Skeleton | Species::Bogged | Species::SnowGolem ); if flag("baby") && ageable { mob.extra.age = -24000; } - let tamable = body_in_head(s) - || matches!(s, Species::Cat | Species::Parrot | Species::Ocelot); + let tamable = + body_in_head(s) || matches!(s, Species::Cat | Species::Parrot | Species::Ocelot); if flag("tame") && tamable { mob.extra.tame = true; } @@ -393,7 +401,12 @@ impl Game<'_> { if let Some(r) = ready { line.push_str(if r { " ready" } else { " unready" }); } - if x.chested && matches!(s, Species::Donkey | Species::Mule | Species::Llama | Species::TraderLlama) { + if x.chested + && matches!( + s, + Species::Donkey | Species::Mule | Species::Llama | Species::TraderLlama + ) + { line.push_str(" chest"); } if x.leash.is_some() { @@ -426,9 +439,18 @@ impl Game<'_> { let (p, v) = (m.position, m.velocity); out.push(line(&m.kind.id, [p.x, p.y, p.z], [v.x, v.y, v.z], "")); } - for s in self.armor_stands.iter().filter(|s| !old_stands.contains(&s.id)) { + for s in self + .armor_stands + .iter() + .filter(|s| !old_stands.contains(&s.id)) + { let (p, v) = (s.position, s.velocity); - out.push(line("minecraft:armor_stand", [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + out.push(line( + "minecraft:armor_stand", + [p.x, p.y, p.z], + [v.x, v.y, v.z], + "", + )); } for d in &self.dropped { let (p, v) = (d.position, d.velocity); @@ -459,7 +481,12 @@ impl Game<'_> { out.push(line(kind, [p.x, p.y, p.z], [v.x, v.y, v.z], "")); } for (p, v) in self.rockets.iter().map(|r| r.motion()) { - out.push(line("minecraft:firework_rocket", [p.x, p.y, p.z], [v.x, v.y, v.z], "")); + out.push(line( + "minecraft:firework_rocket", + [p.x, p.y, p.z], + [v.x, v.y, v.z], + "", + )); } for t in &self.tnts { let (p, v) = (t.position, t.velocity); diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index b943956e0..f6a7e5a58 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -129,7 +129,8 @@ pub(super) fn entity_random() -> LegacyRandom { let mut current = UNIQUIFIER.load(Ordering::Relaxed); let seed = loop { let next = current.wrapping_mul(STEP); - match UNIQUIFIER.compare_exchange_weak(current, next, Ordering::Relaxed, Ordering::Relaxed) { + match UNIQUIFIER.compare_exchange_weak(current, next, Ordering::Relaxed, Ordering::Relaxed) + { Ok(_) => break next, Err(actual) => current = actual, } @@ -603,7 +604,9 @@ impl Game<'_> { "minecraft:honeycomb" => self.dispense_honeycomb(pos, facing, stack)?, "minecraft:potion" => self.dispense_potion(pos, facing, stack)?, _ if item.ends_with("shulker_box") => self.dispense_shulker_box(pos, facing, stack)?, - _ if super::boats::any_boat_wood(&item).is_some() => self.dispense_boat(pos, facing, stack), + _ if super::boats::any_boat_wood(&item).is_some() => { + self.dispense_boat(pos, facing, stack) + } _ if super::minecarts::CartKind::from_id(&item).is_some() => { self.dispense_minecart(pos, facing, stack) } @@ -686,7 +689,9 @@ impl Game<'_> { /// top of the block (`setPos(x + 0.5, y + 1, z + 0.5)`) falls one block /// against the collision shapes overlapping that block alone. fn drop_offset(&self, width: f64, height: f64, p: Pos) -> f64 { - use horizon_world::{block_collision_scan::BlockCollisionScan, collision_context::CollisionContext}; + use horizon_world::{ + block_collision_scan::BlockCollisionScan, collision_context::CollisionContext, + }; let h = width / 2.0; let (x, z) = (f64::from(p.0) + 0.5, f64::from(p.2) + 0.5); let y = f64::from(p.1) + 1.0; @@ -696,7 +701,11 @@ impl Game<'_> { ); let cell = Aabb::new( Vec3::new(f64::from(p.0), f64::from(p.1), f64::from(p.2)), - Vec3::new(f64::from(p.0) + 1.0, f64::from(p.1) + 1.0, f64::from(p.2) + 1.0), + Vec3::new( + f64::from(p.0) + 1.0, + f64::from(p.1) + 1.0, + f64::from(p.2) + 1.0, + ), ); let mut scene = SceneCollisions { world: &self.world, @@ -704,9 +713,17 @@ impl Game<'_> { }; let mut amount = -1.0; // Level.getCollisions(null, aabb): CollisionContext.empty(). - if let Ok(scan) = BlockCollisionScan::new(&mut scene, CollisionContext::empty(false), cell, false, 1_000_000) { + if let Ok(scan) = BlockCollisionScan::new( + &mut scene, + CollisionContext::empty(false), + cell, + false, + 1_000_000, + ) { for hit in scan.flatten() { - amount = hit.shape.collide(horizon_core::geometry::Axis::Y, entity, amount); + amount = hit + .shape + .collide(horizon_core::geometry::Axis::Y, entity, amount); } } 1.0 + amount @@ -762,9 +779,9 @@ impl Game<'_> { // LivingEntity.canEquipWithDispenser: alive, the slot usable, the // item's allowed entities, the slot empty and the dispenser rule. let target = self.targets_at(front).into_iter().find(|t| match *t { - Target::Player => slot.armor_index().is_some_and(|i| { - allows("minecraft:player") && self.armor[i].is_none() - }), + Target::Player => slot + .armor_index() + .is_some_and(|i| allows("minecraft:player") && self.armor[i].is_none()), Target::Stand(i) => slot.stand_index().is_some_and(|s| { allows("minecraft:armor_stand") && self.armor_stands[i].equipment[s].is_none() }), @@ -802,7 +819,11 @@ impl Game<'_> { mob_set_slot(m, slot, one); // setGuaranteedDrop / setPersistenceRequired. m.persistent = true; - let sound = if m.silent { None } else { equip_sound(m, slot, sound) }; + let sound = if m.silent { + None + } else { + equip_sound(m, slot, sound) + }; (Some(m.position), sound) } }; @@ -1010,7 +1031,10 @@ impl Game<'_> { return (shrink(stack), CLICK); } } - (self.default_execute(pos, facing, stack), optional_event("chest")) + ( + self.default_execute(pos, facing, stack), + optional_event("chest"), + ) } /// `BoatDispenseItemBehavior`. fn dispense_boat(&mut self, pos: Pos, facing: Direction, stack: ItemStack) -> Dispensed { @@ -1183,7 +1207,12 @@ impl Game<'_> { if let Some(id) = self.spawn_mob_at(kind, target, true) { self.mob_placed(id)?; } - self.post_block_game_event("minecraft:entity_place", [target.0, target.1, target.2], None, None); + self.post_block_game_event( + "minecraft:entity_place", + [target.0, target.1, target.2], + None, + None, + ); } let bucket = ItemStack::new("minecraft:bucket", 1); Ok(( @@ -1203,7 +1232,12 @@ impl Game<'_> { self.process_level_events()?; match picked { Some(filled) => { - self.post_block_game_event("minecraft:fluid_pickup", [target.x, target.y, target.z], None, None); + self.post_block_game_event( + "minecraft:fluid_pickup", + [target.x, target.y, target.z], + None, + None, + ); let filled = ItemStack::new(filled, 1); Ok(( self.consume_with_remainder(pos, facing, stack, filled), @@ -1253,15 +1287,28 @@ impl Game<'_> { } return Ok((self.hurt_and_break(stack, 1), CLICK)); } - if self.with_level(|b, level, ticks| b.fire_can_be_placed_at(level, ticks, target_pos, facing)) { - let fire = self.with_level(|b, level, ticks| b.fire_state_for(level, ticks, target_pos)); + if self + .with_level(|b, level, ticks| b.fire_can_be_placed_at(level, ticks, target_pos, facing)) + { + let fire = + self.with_level(|b, level, ticks| b.fire_state_for(level, ticks, target_pos)); self.level_set([target.0, target.1, target.2], fire)?; - self.post_block_game_event("minecraft:block_place", [target.0, target.1, target.2], None, None); + self.post_block_game_event( + "minecraft:block_place", + [target.0, target.1, target.2], + None, + None, + ); } else if self.can_light(state, &name) { if let Some(lit) = self.with_props(state, &[("lit", "true")]) { self.level_set([target.0, target.1, target.2], lit)?; } - self.post_block_game_event("minecraft:block_change", [target.0, target.1, target.2], None, None); + self.post_block_game_event( + "minecraft:block_change", + [target.0, target.1, target.2], + None, + None, + ); } else if &*name == "minecraft:tnt" { if self.ui.rules.bool("tnt_explodes") { self.spawn_primed_tnt(target_pos, false); @@ -1282,8 +1329,11 @@ impl Game<'_> { let lit = self.prop(state, "lit"); let waterlogged = self.prop(state, "waterlogged"); let dry_unlit = lit.as_deref() == Some("false") && waterlogged.as_deref() == Some("false"); - ((self.in_block_tag("minecraft:campfires", name) || self.in_block_tag("minecraft:candles", name)) && dry_unlit) - || (self.in_block_tag("minecraft:candle_cakes", name) && lit.as_deref() == Some("false")) + ((self.in_block_tag("minecraft:campfires", name) + || self.in_block_tag("minecraft:candles", name)) + && dry_unlit) + || (self.in_block_tag("minecraft:candle_cakes", name) + && lit.as_deref() == Some("false")) } fn dispense_bone_meal( &mut self, @@ -1336,7 +1386,12 @@ impl Game<'_> { 1.0, 1.0, ); - self.post_block_game_event("minecraft:entity_place", [front.0, front.1, front.2], None, None); + self.post_block_game_event( + "minecraft:entity_place", + [front.0, front.1, front.2], + None, + None, + ); (shrink(stack), CLICK) } @@ -1377,7 +1432,12 @@ impl Game<'_> { if let Some(entity) = BlockEntity::for_block("minecraft:wither_skeleton_skull") { self.world.block_entities.insert(target, entity); } - self.post_block_game_event("minecraft:block_place", [target.0, target.1, target.2], None, None); + self.post_block_game_event( + "minecraft:block_place", + [target.0, target.1, target.2], + None, + None, + ); self.check_wither_spawn([target.0, target.1, target.2])?; return Ok((shrink(stack), CLICK)); } @@ -1436,7 +1496,9 @@ impl Game<'_> { // setBlock; the spot is air again by the time setBlock // would update neighbours, so it skips them. let at = Self::pos_of(target); - self.with_level(|b, level, ticks| b.set_block(level, ticks, at, pumpkin, 2 | 16)); + self.with_level(|b, level, ticks| { + b.set_block(level, ticks, at, pumpkin, 2 | 16) + }); self.process_level_events()?; self.check_golem_spawn(head)?; } else { @@ -1609,7 +1671,11 @@ impl Game<'_> { // BeehiveBlock.dropHoneycomb (harvest/beehive), then // releaseBeesAndResetHoneyLevel and the shear event. let p = [target.0, target.1, target.2]; - for stack in self.sequence_loot("minecraft:harvest/beehive", "minecraft:harvest/beehive", None) { + for stack in self.sequence_loot( + "minecraft:harvest/beehive", + "minecraft:harvest/beehive", + None, + ) { self.pop_resource(p, stack); } if let Some(reset) = self.with_props(state, &[("honey_level", "0")]) { @@ -1714,7 +1780,8 @@ impl Game<'_> { .play("minecraft:entity.sulfur_cube.eject", sound_at, 1.0, 1.0); } Species::Sheep if !m.extra.sheared && !m.is_baby() => { - let color = horizon_client::mobs::render::DYE_NAMES[m.extra.color as usize & 15]; + let color = + horizon_client::mobs::render::DYE_NAMES[m.extra.color as usize & 15]; self.audio .play("minecraft:entity.sheep.shear", sound_at, 1.0, 1.0); let drops = self.sequence_loot( @@ -1799,7 +1866,12 @@ impl Game<'_> { } _ => continue, } - self.post_block_game_event("minecraft:shear", [target.0, target.1, target.2], None, None); + self.post_block_game_event( + "minecraft:shear", + [target.0, target.1, target.2], + None, + None, + ); return Ok(true); } Ok(false) @@ -1816,7 +1888,9 @@ impl Game<'_> { (Holder::Mob(m.id), b.center(), Some(i)) } Target::Player => { - let Some(v) = self.player_view() else { return false }; + let Some(v) = self.player_view() else { + return false; + }; (Holder::Player, v.bounds.center(), None) } Target::Stand(_) => return false, @@ -1826,7 +1900,9 @@ impl Game<'_> { .mobs .iter() .enumerate() - .filter(|(_, m)| !m.removed && m.extra.leash == Some(holder) && overlaps(m.bounds(), area)) + .filter(|(_, m)| { + !m.removed && m.extra.leash == Some(holder) && overlaps(m.bounds(), area) + }) .map(|(j, m)| (m.id, j)) .collect(); leashed.sort_unstable(); @@ -1963,7 +2039,10 @@ impl Game<'_> { let Some(waxed) = waxed.filter(|_| name.contains("copper") || name.contains("lightning_rod")) else { - return Ok((self.default_execute(pos, facing, stack), optional_event("honeycomb"))); + return Ok(( + self.default_execute(pos, facing, stack), + optional_event("honeycomb"), + )); }; set_success("honeycomb", true); self.level_set([target.0, target.1, target.2], waxed)?; diff --git a/crates/horizon-client/src/game/golems.rs b/crates/horizon-client/src/game/golems.rs index d048d3d5c..a67e05af8 100644 --- a/crates/horizon-client/src/game/golems.rs +++ b/crates/horizon-client/src/game/golems.rs @@ -118,7 +118,9 @@ impl Game<'_> { let air = self.world.air; for &p in cells { let pos = horizon_core::position::BlockPos::new(p[0], p[1], p[2]); - self.with_level(|b, level, ticks| b.update_neighbors_at(level, ticks, pos, b.block(air))); + self.with_level(|b, level, ticks| { + b.update_neighbors_at(level, ticks, pos, b.block(air)) + }); } self.process_level_events() } diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 957ede43a..de2ff8573 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -204,9 +204,14 @@ impl Game<'_> { if let Some((position, velocity, yaw)) = entity_nbt::boat_from_nbt(&tag) { self.next_entity += 1; let id = self.next_entity; - let (wood, chest) = super::boats::any_boat_wood(k).unwrap_or(("oak", false)); - let mut b = - super::boats::Boat::new(id, std::sync::Arc::from(wood), position, yaw); + let (wood, chest) = + super::boats::any_boat_wood(k).unwrap_or(("oak", false)); + let mut b = super::boats::Boat::new( + id, + std::sync::Arc::from(wood), + position, + yaw, + ); if chest { b = b.with_chest(); b.items = horizon_client::save::decode_items( diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 65245eaab..61ff84bd9 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -438,7 +438,12 @@ impl Game<'_> { /// `Level.gameEvent`: tell the listeners within reach. pub(super) fn post_game_event(&mut self, event: &'static str, pos: [f64; 3], ctx: Context) { super::dispense_fixture::record(|| { - format!("G {},{},{} {event}", pos[0].floor() as i32, pos[1].floor() as i32, pos[2].floor() as i32) + format!( + "G {},{},{} {event}", + pos[0].floor() as i32, + pos[1].floor() as i32, + pos[2].floor() as i32 + ) }); if self.remote.is_some() || !self.sculk.tables.built { return; diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index 4912b5408..bfecad8ad 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -527,7 +527,11 @@ impl Items { self.loot.contains_key(id) } /// The server random sequence the block's loot table draws from. - pub fn block_loot_sequence(&self, registry: &BlockRegistry, state: BlockStateId) -> Option { + pub fn block_loot_sequence( + &self, + registry: &BlockRegistry, + state: BlockStateId, + ) -> Option { let block = registry.state(state)?; let name = block.name.trim_start_matches("minecraft:"); let standing = name.replacen("_wall_", "_", 1); @@ -913,7 +917,12 @@ impl LootContext<'_> { _ => false, } } - fn choose(&self, entries: &[Value], random: &mut impl RandomSource, produced: &mut Vec) { + fn choose( + &self, + entries: &[Value], + random: &mut impl RandomSource, + produced: &mut Vec, + ) { let mut candidates = Vec::new(); for e in entries { self.expand(e, random, &mut candidates); diff --git a/crates/horizon-world/src/level/hooks.rs b/crates/horizon-world/src/level/hooks.rs index 66114eee4..ee1e3cdd9 100644 --- a/crates/horizon-world/src/level/hooks.rs +++ b/crates/horizon-world/src/level/hooks.rs @@ -459,8 +459,10 @@ impl Sim<'_, L> { if same { return; } - if matches!(self.level.dimension(), super::Dimension::Overworld | super::Dimension::Nether) - && let Some(shape) = self.find_empty_portal_shape(pos, true) + if matches!( + self.level.dimension(), + super::Dimension::Overworld | super::Dimension::Nether + ) && let Some(shape) = self.find_empty_portal_shape(pos, true) { self.create_portal_blocks(&shape); return; diff --git a/crates/horizon-world/src/level/portal.rs b/crates/horizon-world/src/level/portal.rs index 403cd49c6..b59dd477c 100644 --- a/crates/horizon-world/src/level/portal.rs +++ b/crates/horizon-world/src/level/portal.rs @@ -160,7 +160,10 @@ impl Sim<'_, L> { return true; } // isPortal - if !matches!(self.level.dimension(), super::Dimension::Overworld | super::Dimension::Nether) { + if !matches!( + self.level.dimension(), + super::Dimension::Overworld | super::Dimension::Nether + ) { return false; } for face in Direction::ALL { From 99057953e0988c5765f62fd1457e6da927ca1801 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 17:51:49 +0200 Subject: [PATCH 23/24] Mark dispensers as matching vanilla in the parity table Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9d20a6f5c..9696517eb 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ round-trip harness has not been rerun. | Interaction and inventory | 1 | None: menu clicks match over 6,000 click sequences | | Menus, HUD and chat | 0.75 | Commands parse as vanilla over 39,685 inputs, but /item modify, /spectate, /waypoint and /dialog do not run; no tab list or world border rendering | | Audio | 0.75 | Sound engine, music and ambience not compared with vanilla | -| Block behaviours and ticking | 0.75 | Block updates, random ticks and redstone match vanilla; dispensers lack several behaviours | +| Block behaviours and ticking | 0.75 | Block updates, random ticks, redstone and dispensers match vanilla; a container replaced by another block keeps its contents instead of spilling them, and there are no copper golems | ### Next steps @@ -50,7 +50,7 @@ round-trip harness has not been rerun. 2. Draw every item through the item model pipeline, and add the resource pack screen. 3. An audio harness: sound engine attenuation, weighted sound events, music and ambience. 4. A mob AI harness, and an oracle for drowned, villagers, traders and the wither. -5. The missing commands and dispenser behaviours, the tab list and world border rendering. +5. The missing commands, containers spilling when replaced, copper golems, the tab list and world border rendering. 6. Test LAN hosting with a vanilla client, and add a dedicated server mode. ```sh From 82dceec5e76908d9c6a12fbc4ad6478f46034cc0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:36:09 +0200 Subject: [PATCH 24/24] Keep the datafix sample worlds with the shared vanilla data, and generate them when missing The old server jars and the 22 worlds made from them are inputs, but they lived in each worktree's .local and were lost when its outputs were cleared. They now sit beside the shared vanilla data, written atomically, and the harness generates any that are missing (under a lock, since several worktrees may run it at once). Co-Authored-By: Claude Opus 5.5 --- tools/datafix_worlds.py | 22 +++++++++++++++------- tools/test_datafix_parity.py | 20 ++++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tools/datafix_worlds.py b/tools/datafix_worlds.py index 985881985..a89581e84 100644 --- a/tools/datafix_worlds.py +++ b/tools/datafix_worlds.py @@ -2,10 +2,11 @@ """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, +.local/datafix/servers/VERSION/server.jar, beside the shared vanilla data) 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. +so that player data exists. The saved world is kept in datafix/worlds/VERSION beside the shared vanilla data +(.local/datafix in a plain checkout), where every worktree finds it. datafix_worlds.py [--force] [VERSION...] (default: every version below) """ @@ -33,6 +34,9 @@ import datafix_scene as scene # noqa: E402 ROOT = Path(__file__).resolve().parent.parent LOCAL = ROOT / '.local' / 'datafix' +# Old server jars and their worlds are inputs, not outputs: keep them beside the +# vanilla data, which parallel worktrees share (.local/minecraft links there). +SHARED = (ROOT / '.local' / 'minecraft').resolve().parent / 'datafix' VERSIONS = ['1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2', '1.20.4', '1.20.5', '1.20.6', '1.21.1', '1.21.3', '1.21.4', '1.21.5', '1.21.6', '1.21.8', '1.21.9', '1.21.10', '1.21.11', '26.1', '26.1.2', '26.2'] MANIFEST = 'https://piston-meta.mojang.com/mc/game/version_manifest_v2.json' @@ -41,7 +45,7 @@ UUID_BASE = 0x6000 def server_jar(version): - path = LOCAL / 'servers' / version / 'server.jar' + path = SHARED / 'servers' / version / 'server.jar' if not path.exists(): path.parent.mkdir(parents=True, exist_ok=True) manifest = json.load(urllib.request.urlopen(MANIFEST)) @@ -271,7 +275,7 @@ def uuid_nbt(i): def build(version, force=False): jar, data_version, protocol = server_jar(version) - target = LOCAL / 'worlds' / version + target = SHARED / 'worlds' / version if target.exists() and not force: print(f'{version}: kept {target}') return [] @@ -310,11 +314,15 @@ def build(version, force=False): bot.leave() time.sleep(2) server.stop() - shutil.rmtree(target, ignore_errors=True) + # Copy aside, then rename: an interrupted copy must not look like a finished world. + partial = target.with_name(target.name + '.partial') + shutil.rmtree(partial, ignore_errors=True) target.parent.mkdir(parents=True, exist_ok=True) - shutil.copytree(work / 'world', target) - (target / 'datafix-source.json').write_text(json.dumps( + shutil.copytree(work / 'world', partial) + (partial / 'datafix-source.json').write_text(json.dumps( {'version': version, 'data_version': data_version, 'problems': problems}, indent=1)) + shutil.rmtree(target, ignore_errors=True) + partial.rename(target) for command, out in problems: print(f'{version}: command failed: {command}\n {out}') print(f'{version}: DataVersion {data_version}, world in {target}') diff --git a/tools/test_datafix_parity.py b/tools/test_datafix_parity.py index da3e6250f..d3fd6957e 100644 --- a/tools/test_datafix_parity.py +++ b/tools/test_datafix_parity.py @@ -2,8 +2,9 @@ """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: +For every sample world in datafix/worlds beside the shared vanilla data (made by +tools/datafix_worlds.py with the official server jars of 1.18.2 to 26.2, and +generated automatically when missing), 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 @@ -31,7 +32,7 @@ import anvil_dump # noqa: E402 ROOT = Path(__file__).resolve().parent.parent LOCAL = ROOT / '.local' / 'datafix' -WORLDS = LOCAL / 'worlds' +WORLDS = (ROOT / '.local' / 'minecraft').resolve().parent / 'datafix' / 'worlds' RUN = LOCAL / 'run' IGNORED = { @@ -184,10 +185,17 @@ def main(): 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 [] + import datafix_worlds + import fcntl + WORLDS.parent.mkdir(parents=True, exist_ok=True) + with open(WORLDS.parent / 'generate.lock', 'w') as lock: + fcntl.flock(lock, fcntl.LOCK_EX) # another worktree may be generating them + missing = [v for v in datafix_worlds.VERSIONS if not (WORLDS / v / 'level.dat').exists()] + if missing: + print('generating sample worlds:', ' '.join(missing), flush=True) + subprocess.run([sys.executable, str(ROOT / 'tools' / 'datafix_worlds.py'), *missing], cwd=ROOT, check=True) + worlds = sorted(p.name for p in WORLDS.iterdir() if p.is_dir()) 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