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 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-10 22:24:03 +02:00
parent a903cb4dd5
commit 68092c4c7f
17 changed files with 7784 additions and 0 deletions
@@ -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<String> = 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
}
}
}
+238
View File
@@ -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<Number> {
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<bool> {
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<String> {
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<Tag> {
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<Tag> {
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<Tag>) {
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(),
}
}
+570
View File
@@ -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<String>) -> 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<Option<Tag>> {
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::<Result<_, _>>()?;
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<PathBuf> {
let mut out: Vec<PathBuf> = 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<PathBuf> {
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<PathBuf> = 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<Option<Tag>> {
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<Option<Tag>> {
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<String>) -> io::Result<()> {
if !folder.is_dir() {
return Ok(());
}
let mut names: Vec<String> = 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<i32> {
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<Vec<(String, String)>> {
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<PathBuf> {
let mut out: Vec<PathBuf> = 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<u16> = 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<i32> {
let version = fix_world_files(base)?;
upgrade_world_data(base)?;
Ok(version)
}
+937
View File
@@ -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<Tag> = 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<Tag> {
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<Tag>) {
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<Tag>) {
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<String> {
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<String> {
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(),
}
}
File diff suppressed because it is too large Load Diff
+694
View File
@@ -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<i32> {
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<Tag> = 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<Option<String>> = 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<String> {
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");
}
}
}
}
});
}
File diff suppressed because it is too large Load Diff
+610
View File
@@ -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<u16>),
Arr(Vec<Json>),
/// Insertion order, a repeated key replacing the value in place.
Obj(Vec<(Vec<u16>, Json)>),
}
impl Json {
pub fn get(&self, key: &str) -> Option<&Json> {
let key: Vec<u16> = 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<u16>, Json)>, key: Vec<u16>, 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<T> = Result<T, ()>;
const fn u(c: char) -> u16 {
c as u16
}
impl Reader<'_> {
fn lenient(&self) -> bool {
self.mode == Mode::Lenient
}
fn peek_char(&self) -> Option<u16> {
self.s.get(self.pos).copied()
}
/// `JsonReader.nextNonWhitespace`, with comments when lenient.
fn skip_ws(&mut self) -> R<Option<u16>> {
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<Json> {
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<Json> {
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<Json> {
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<Vec<u16>> {
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<u16> {
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<Json> {
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<Json> {
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<Json> {
parse_mode(s, Mode::Lenient)
}
/// `StrictJsonParser.parse`.
pub fn parse_strict(s: &[u16]) -> Option<Json> {
parse_mode(s, Mode::Strict)
}
/// `GsonHelper.toStableString`: keys in natural (UTF-16) order, Gson escaping.
pub fn to_stable_string(json: &Json) -> Vec<u16> {
let mut out = Vec::new();
write(json, &mut out);
out
}
fn write(json: &Json, out: &mut Vec<u16>) {
let push = |out: &mut Vec<u16>, 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<u16>, 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<u16>) {
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<Tag> {
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<i64> {
let (mantissa, exponent) = match literal.find(['e', 'E']) {
Some(i) => (&literal[..i], literal[i + 1..].parse::<i64>().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<exp>.
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<width$}", width = e + 1), String::new())
};
let frac = if frac.is_empty() { "0".into() } else { frac };
format!("{sign}{int}.{frac}")
} else {
let zeros = "0".repeat((-exp - 1) as usize);
format!("{sign}0.{zeros}{digits}")
}
} else {
let (first, rest) = digits.split_at(1);
let rest = if rest.is_empty() { "0" } else { rest };
format!("{sign}{first}.{rest}E{exp}")
}
}
/// A string value as a JSON text component (`{"text": s}`), stable string.
pub fn text_component_json(text: &[u16]) -> Vec<u16> {
to_stable_string(&Json::Obj(vec![("text".encode_utf16().collect(), Json::Str(text.to_vec()))]))
}
pub fn translatable_component_json(key: &str) -> Vec<u16> {
to_stable_string(&Json::object(vec![("translate", Json::string(key))]))
}
#[cfg(test)]
mod tests {
use super::*;
fn w(s: &str) -> Vec<u16> {
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");
}
}
+233
View File
@@ -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<Ty> {
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;
+787
View File
@@ -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);
}
+63
View File
@@ -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)));
}
+225
View File
@@ -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<String> {
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::<Vec<u16>>();
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<Tag> {
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<i32> {
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<char> = 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::<Vec<_>>()).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);
}
}
+1
View File
@@ -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;
@@ -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<String, String> generators = generators(out);
Path dimensions = out.resolve("dimensions");
if (Files.isDirectory(dimensions)) {
List<Path> dims;
try (Stream<Path> 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<Level> 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<String, String> generators(Path world) throws Exception {
Map<String, String> 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<Level> 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<ChunkPos, CompoundTag> 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<Path> files;
try (Stream<Path> 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<Path> list(Path folder, String suffix) throws Exception {
if (!Files.isDirectory(folder)) {
return List.of();
}
try (Stream<Path> 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<Path> 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);
}
}
}
}
}
+343
View File
@@ -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
+325
View File
@@ -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()
+214
View File
@@ -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()