Add typed block entity NBT and save roundtrip harness (WIP)
Work in progress on typed block entity load/save and the save differential harness. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,962 @@
|
||||
//! Vanilla 26.3 block entity NBT for every block entity type.
|
||||
//!
|
||||
//! [`canonical`] is `loadAdditional` followed by `saveWithFullMetadata` for each of
|
||||
//! the 47 `BlockEntityType`s: it reads the fields the type reads (with vanilla's
|
||||
//! defaults and clamps), drops everything else and writes the fields `saveAdditional`
|
||||
//! writes (including its omit-when-empty rules). Nested codec values that Horizon does
|
||||
//! not simulate (spawner entity data, vibration listeners, trial spawner configuration,
|
||||
//! vault data, sculk cursors, sign and banner components, profiles) are carried as
|
||||
//! loaded. The common `BaseContainerBlockEntity` fields (`CustomName`, `lock`) and the
|
||||
//! `RandomizableContainer` loot table fields are handled for every container type.
|
||||
//!
|
||||
//! [`overlay`] merges a block entity Horizon models (and re-encodes from its own,
|
||||
//! partial state) with the NBT it was loaded from, so fields Horizon does not model
|
||||
//! survive and only what the game changed is replaced.
|
||||
use horizon_world::nbt::{Compound, Tag};
|
||||
|
||||
fn string(s: &str) -> Tag {
|
||||
Tag::String(s.into())
|
||||
}
|
||||
fn text(tag: Option<&Tag>) -> Option<String> {
|
||||
match tag {
|
||||
Some(Tag::String(s)) => Some(s.to_string_lossy()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// `CompoundTag.getIntOr`: any numeric tag (floats floor).
|
||||
fn int(tag: Option<&Tag>) -> Option<i32> {
|
||||
Some(match tag? {
|
||||
Tag::Byte(v) => i32::from(*v),
|
||||
Tag::Short(v) => i32::from(*v),
|
||||
Tag::Int(v) => *v,
|
||||
Tag::Long(v) => *v as i32,
|
||||
Tag::Float(b) => f32::from_bits(*b).floor() as i32,
|
||||
Tag::Double(b) => f64::from_bits(*b).floor() as i32,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
fn long(tag: Option<&Tag>) -> Option<i64> {
|
||||
Some(match tag? {
|
||||
Tag::Long(v) => *v,
|
||||
Tag::Float(b) => f32::from_bits(*b).floor() as i64,
|
||||
Tag::Double(b) => f64::from_bits(*b).floor() as i64,
|
||||
other => i64::from(int(Some(other))?),
|
||||
})
|
||||
}
|
||||
fn float(tag: Option<&Tag>) -> Option<f32> {
|
||||
Some(match tag? {
|
||||
Tag::Float(b) => f32::from_bits(*b),
|
||||
Tag::Double(b) => f64::from_bits(*b) as f32,
|
||||
Tag::Long(v) => *v as f32,
|
||||
other => int(Some(other))? as f32,
|
||||
})
|
||||
}
|
||||
fn flag(tag: Option<&Tag>) -> Option<bool> {
|
||||
Some(int(tag)? != 0)
|
||||
}
|
||||
fn boolean(v: bool) -> Tag {
|
||||
Tag::Byte(i8::from(v))
|
||||
}
|
||||
fn f32_tag(v: f32) -> Tag {
|
||||
Tag::Float(v.to_bits())
|
||||
}
|
||||
|
||||
/// What a block entity reads and writes, in `saveAdditional` order.
|
||||
struct Fields<'a> {
|
||||
input: &'a Tag,
|
||||
out: Compound,
|
||||
}
|
||||
impl<'a> Fields<'a> {
|
||||
fn put(&mut self, key: &str, value: Tag) {
|
||||
self.out.insert(key.into(), value);
|
||||
}
|
||||
fn int(&mut self, key: &str, default: i32) {
|
||||
let v = int(self.input.get(key)).unwrap_or(default);
|
||||
self.put(key, Tag::Int(v));
|
||||
}
|
||||
fn int_clamped(&mut self, key: &str, default: i32, min: i32, max: i32) {
|
||||
let v = int(self.input.get(key)).unwrap_or(default).clamp(min, max);
|
||||
self.put(key, Tag::Int(v));
|
||||
}
|
||||
fn short(&mut self, key: &str, default: i16) {
|
||||
let v = int(self.input.get(key)).map_or(default, |v| v as i16);
|
||||
self.put(key, Tag::Short(v));
|
||||
}
|
||||
fn long(&mut self, key: &str, default: i64) {
|
||||
let v = long(self.input.get(key)).unwrap_or(default);
|
||||
self.put(key, Tag::Long(v));
|
||||
}
|
||||
fn float(&mut self, key: &str, default: f32) {
|
||||
let v = float(self.input.get(key)).unwrap_or(default);
|
||||
self.put(key, f32_tag(v));
|
||||
}
|
||||
fn flag(&mut self, key: &str, default: bool) {
|
||||
let v = flag(self.input.get(key)).unwrap_or(default);
|
||||
self.put(key, boolean(v));
|
||||
}
|
||||
fn string(&mut self, key: &str, default: &str) {
|
||||
let v = text(self.input.get(key)).unwrap_or_else(|| default.to_owned());
|
||||
self.put(key, string(&v));
|
||||
}
|
||||
/// An optional value written as loaded, when it has the expected shape.
|
||||
fn keep(&mut self, key: &str, valid: impl Fn(&Tag) -> bool) {
|
||||
if let Some(t) = self.input.get(key).filter(|t| valid(t)) {
|
||||
self.put(key, t.clone());
|
||||
}
|
||||
}
|
||||
fn keep_compound(&mut self, key: &str) {
|
||||
self.keep(key, |t| matches!(t, Tag::Compound(_)));
|
||||
}
|
||||
/// A compound that is always written (`default` when absent).
|
||||
fn compound_or(&mut self, key: &str, default: Compound) {
|
||||
let v = match self.input.get(key) {
|
||||
Some(Tag::Compound(c)) => c.clone(),
|
||||
_ => default,
|
||||
};
|
||||
self.put(key, Tag::Compound(v));
|
||||
}
|
||||
/// An optional text component (`storeNullable("CustomName", ComponentSerialization.CODEC)`).
|
||||
fn component(&mut self, key: &str) {
|
||||
if let Some(c) = self.input.get(key).and_then(component) {
|
||||
self.put(key, c);
|
||||
}
|
||||
}
|
||||
/// `LockCode.addToTag`.
|
||||
fn lock(&mut self) {
|
||||
self.keep_compound("lock");
|
||||
}
|
||||
/// `BaseContainerBlockEntity`: the custom name and the lock.
|
||||
fn base_container(&mut self) {
|
||||
self.component("CustomName");
|
||||
self.lock();
|
||||
}
|
||||
/// `RandomizableContainer.tryLoadLootTable` / `trySaveLootTable`; true when the
|
||||
/// block entity has a loot table (and so saves no items).
|
||||
fn loot_table(&mut self) -> bool {
|
||||
let Some(table) = text(self.input.get("LootTable")) else {
|
||||
return false;
|
||||
};
|
||||
self.put("LootTable", string(&table));
|
||||
let seed = long(self.input.get("LootTableSeed")).unwrap_or(0);
|
||||
if seed != 0 {
|
||||
self.put("LootTableSeed", Tag::Long(seed));
|
||||
}
|
||||
true
|
||||
}
|
||||
/// `ContainerHelper.loadAllItems` + `saveAllItems`.
|
||||
fn items(&mut self, size: usize, also_when_empty: bool) {
|
||||
let items = items(self.input.get("Items"), size);
|
||||
if !items.is_empty() || also_when_empty {
|
||||
self.put("Items", Tag::List(items));
|
||||
}
|
||||
}
|
||||
/// A container: loot table or items.
|
||||
fn container(&mut self, size: usize, also_when_empty: bool) {
|
||||
if !self.loot_table() {
|
||||
self.items(size, also_when_empty);
|
||||
}
|
||||
}
|
||||
/// An optional `ItemStack` (`RecordItem`, `Book`, `item`).
|
||||
fn item(&mut self, key: &str) -> bool {
|
||||
match self.input.get(key).and_then(stack) {
|
||||
Some(item) => {
|
||||
self.put(key, item);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `ComponentSerialization.CODEC` on loaded NBT: a literal without style is a plain
|
||||
/// string; anything else is kept as loaded.
|
||||
pub fn component(tag: &Tag) -> Option<Tag> {
|
||||
match tag {
|
||||
Tag::String(_) | Tag::List(_) => Some(tag.clone()),
|
||||
Tag::Compound(c) => {
|
||||
if c.len() == 1
|
||||
&& let Some(t @ Tag::String(_)) = c.get(&"text".into())
|
||||
{
|
||||
return Some(t.clone());
|
||||
}
|
||||
Some(tag.clone())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `ItemStack.CODEC`: `id`, `count` (1..=99, default 1) and a non-empty `components`
|
||||
/// patch kept as loaded. None for the empty stack and for invalid stacks.
|
||||
pub fn stack(tag: &Tag) -> Option<Tag> {
|
||||
let id = text(tag.get("id"))?;
|
||||
if id.is_empty() || id == "minecraft:air" {
|
||||
return None;
|
||||
}
|
||||
let count = int(tag.get("count")).unwrap_or(1);
|
||||
if !(1..=99).contains(&count) {
|
||||
return None;
|
||||
}
|
||||
let mut out = vec![("id", string(&id)), ("count", Tag::Int(count))];
|
||||
if let Some(c @ Tag::Compound(map)) = tag.get("components")
|
||||
&& !map.is_empty()
|
||||
{
|
||||
out.push(("components", c.clone()));
|
||||
}
|
||||
Some(Tag::compound(out))
|
||||
}
|
||||
/// `ItemStackTemplate.CODEC`: an item id or `{id, count?, components?}`.
|
||||
fn template(tag: &Tag) -> Option<Tag> {
|
||||
match tag {
|
||||
Tag::String(_) => Some(tag.clone()),
|
||||
Tag::Compound(_) => {
|
||||
let mut out = vec![("id", string(&text(tag.get("id"))?))];
|
||||
if let Some(count) = int(tag.get("count")).filter(|c| *c != 1) {
|
||||
out.push(("count", Tag::Int(count)));
|
||||
}
|
||||
if let Some(c @ Tag::Compound(map)) = tag.get("components")
|
||||
&& !map.is_empty()
|
||||
{
|
||||
out.push(("components", c.clone()));
|
||||
}
|
||||
Some(Tag::compound(out))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// `ItemStackWithSlot` entries of a container of `size` slots, by ascending slot.
|
||||
fn items(tag: Option<&Tag>, size: usize) -> Vec<Tag> {
|
||||
let mut slots: Vec<Option<Tag>> = vec![None; size];
|
||||
if let Some(Tag::List(list)) = tag {
|
||||
for entry in list {
|
||||
let (Some(slot), Some(item)) = (int(entry.get("Slot")), stack(entry)) else {
|
||||
continue;
|
||||
};
|
||||
if (0..size as i32).contains(&slot) {
|
||||
slots[slot as usize] = Some(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
slots
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, item)| {
|
||||
let Tag::Compound(mut c) = item? else {
|
||||
return None;
|
||||
};
|
||||
c.insert("Slot".into(), Tag::Byte(i as i8));
|
||||
Some(Tag::Compound(c))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn int_array(tag: Option<&Tag>) -> Option<&Vec<i32>> {
|
||||
match tag {
|
||||
Some(Tag::IntArray(v)) => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// A saved `BlockPos` (`[I; x, y, z]`).
|
||||
fn is_pos(tag: &Tag) -> bool {
|
||||
matches!(tag, Tag::IntArray(v) if v.len() == 3)
|
||||
}
|
||||
/// The loaded `SignText` of one side, or the empty text.
|
||||
fn sign_side(tag: Option<&Tag>) -> Tag {
|
||||
let empty = || string("");
|
||||
let mut messages: Vec<Tag> = match tag.and_then(|t| t.get("messages")) {
|
||||
Some(Tag::List(list)) => list.iter().filter_map(component).collect(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
messages.resize_with(4, empty);
|
||||
messages.truncate(4);
|
||||
let mut out = vec![
|
||||
("messages", Tag::List(messages)),
|
||||
(
|
||||
"color",
|
||||
string(
|
||||
&tag.and_then(|t| text(t.get("color")))
|
||||
.unwrap_or_else(|| "black".into()),
|
||||
),
|
||||
),
|
||||
(
|
||||
"has_glowing_text",
|
||||
boolean(
|
||||
tag.and_then(|t| flag(t.get("has_glowing_text")))
|
||||
.unwrap_or(false),
|
||||
),
|
||||
),
|
||||
];
|
||||
if let Some(filtered @ Tag::List(list)) = tag.and_then(|t| t.get("filtered_messages"))
|
||||
&& list.len() == 4
|
||||
{
|
||||
out.push(("filtered_messages", filtered.clone()));
|
||||
}
|
||||
Tag::compound(out)
|
||||
}
|
||||
|
||||
/// The id of a block entity compound.
|
||||
pub fn id(tag: &Tag) -> Option<String> {
|
||||
text(tag.get("id"))
|
||||
}
|
||||
|
||||
/// Every `BlockEntityType` id this module knows.
|
||||
pub const TYPES: [&str; 47] = [
|
||||
"minecraft:furnace",
|
||||
"minecraft:chest",
|
||||
"minecraft:trapped_chest",
|
||||
"minecraft:ender_chest",
|
||||
"minecraft:jukebox",
|
||||
"minecraft:dispenser",
|
||||
"minecraft:dropper",
|
||||
"minecraft:sign",
|
||||
"minecraft:hanging_sign",
|
||||
"minecraft:mob_spawner",
|
||||
"minecraft:creaking_heart",
|
||||
"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:conduit",
|
||||
"minecraft:barrel",
|
||||
"minecraft:smoker",
|
||||
"minecraft:blast_furnace",
|
||||
"minecraft:lectern",
|
||||
"minecraft:bell",
|
||||
"minecraft:jigsaw",
|
||||
"minecraft:campfire",
|
||||
"minecraft:beehive",
|
||||
"minecraft:sculk_sensor",
|
||||
"minecraft:calibrated_sculk_sensor",
|
||||
"minecraft:sculk_catalyst",
|
||||
"minecraft:sculk_shrieker",
|
||||
"minecraft:chiseled_bookshelf",
|
||||
"minecraft:shelf",
|
||||
"minecraft:brushable_block",
|
||||
"minecraft:decorated_pot",
|
||||
"minecraft:crafter",
|
||||
"minecraft:trial_spawner",
|
||||
"minecraft:vault",
|
||||
"minecraft:test_block",
|
||||
"minecraft:test_instance_block",
|
||||
];
|
||||
/// The two types added after the ones above in 26.3.
|
||||
pub const NEWEST: [&str; 2] = ["minecraft:copper_golem_statue", "minecraft:potent_sulfur"];
|
||||
|
||||
/// The block entity as vanilla would save it after loading `tag` (a full block
|
||||
/// entity compound with `id`, `x`, `y`, `z`); None for an unknown type or a tag
|
||||
/// without a position.
|
||||
pub fn canonical(tag: &Tag) -> Option<Tag> {
|
||||
let id = id(tag)?;
|
||||
let (x, y, z) = (int(tag.get("x"))?, int(tag.get("y"))?, int(tag.get("z"))?);
|
||||
let mut f = Fields {
|
||||
input: tag,
|
||||
out: Compound::new(),
|
||||
};
|
||||
match id.as_str() {
|
||||
"minecraft:furnace" | "minecraft:smoker" | "minecraft:blast_furnace" => {
|
||||
f.base_container();
|
||||
f.int("cooking_time_spent", 0);
|
||||
f.int("cooking_total_time", 0);
|
||||
f.int("lit_time_remaining", 0);
|
||||
f.int("lit_total_time", 0);
|
||||
f.float("speed_multiplier", 1.0);
|
||||
f.items(3, true);
|
||||
let used: Compound = match tag.get("RecipesUsed") {
|
||||
Some(Tag::Compound(c)) => c
|
||||
.iter()
|
||||
.filter_map(|(k, v)| Some((k.clone(), Tag::Int(int(Some(v))?))))
|
||||
.collect(),
|
||||
_ => Compound::new(),
|
||||
};
|
||||
f.put("RecipesUsed", Tag::Compound(used));
|
||||
}
|
||||
"minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => {
|
||||
f.base_container();
|
||||
f.container(27, true);
|
||||
}
|
||||
"minecraft:dispenser" | "minecraft:dropper" => {
|
||||
f.base_container();
|
||||
f.container(9, true);
|
||||
}
|
||||
"minecraft:hopper" => {
|
||||
f.base_container();
|
||||
f.container(5, true);
|
||||
f.int("TransferCooldown", -1);
|
||||
}
|
||||
"minecraft:shulker_box" => {
|
||||
f.base_container();
|
||||
f.container(27, false);
|
||||
}
|
||||
"minecraft:crafter" => {
|
||||
f.base_container();
|
||||
f.int("crafting_ticks_remaining", 0);
|
||||
f.container(9, true);
|
||||
let disabled: Vec<i32> = int_array(tag.get("disabled_slots"))
|
||||
.map(|v| v.iter().copied().filter(|s| (0..9).contains(s)).collect())
|
||||
.unwrap_or_default();
|
||||
// `addDisabledSlots`: the slots whose container data is 1, ascending.
|
||||
let mut slots = disabled;
|
||||
slots.sort_unstable();
|
||||
slots.dedup();
|
||||
f.put("disabled_slots", Tag::IntArray(slots));
|
||||
f.put(
|
||||
"triggered",
|
||||
Tag::Int(int(tag.get("triggered")).unwrap_or(0)),
|
||||
);
|
||||
}
|
||||
"minecraft:ender_chest"
|
||||
| "minecraft:end_portal"
|
||||
| "minecraft:daylight_detector"
|
||||
| "minecraft:bell"
|
||||
| "minecraft:conduit"
|
||||
| "minecraft:copper_golem_statue" => {
|
||||
if id == "minecraft:conduit" {
|
||||
f.keep("Target", |t| matches!(t, Tag::IntArray(v) if v.len() == 4));
|
||||
}
|
||||
}
|
||||
"minecraft:end_gateway" => {
|
||||
f.long("Age", 0);
|
||||
f.keep("exit_portal", |t| {
|
||||
is_pos(t)
|
||||
&& matches!(t, Tag::IntArray(v) if v[1].abs() <= 30_000_000 && v[0].abs() <= 30_000_000 && v[2].abs() <= 30_000_000)
|
||||
});
|
||||
if flag(tag.get("ExactTeleport")) == Some(true) {
|
||||
f.put("ExactTeleport", boolean(true));
|
||||
}
|
||||
}
|
||||
"minecraft:jukebox" => {
|
||||
let has_song = f.item("RecordItem");
|
||||
if has_song
|
||||
&& let Some(ticks) = tag
|
||||
.get("ticks_since_song_started")
|
||||
.and_then(|t| long(Some(t)))
|
||||
&& let Some(Tag::Compound(c)) = f.out.get(&"RecordItem".into())
|
||||
&& is_record(c)
|
||||
{
|
||||
f.put("ticks_since_song_started", Tag::Long(ticks));
|
||||
}
|
||||
}
|
||||
"minecraft:sign" | "minecraft:hanging_sign" => {
|
||||
if flag(tag.get("allow_op_features")) == Some(true) {
|
||||
f.put("allow_op_features", boolean(true));
|
||||
}
|
||||
f.put("front_text", sign_side(tag.get("front_text")));
|
||||
f.put("back_text", sign_side(tag.get("back_text")));
|
||||
f.flag("is_waxed", false);
|
||||
}
|
||||
"minecraft:mob_spawner" => {
|
||||
f.short("Delay", 20);
|
||||
f.short("MinSpawnDelay", 200);
|
||||
f.short("MaxSpawnDelay", 800);
|
||||
f.short("SpawnCount", 4);
|
||||
f.short("MaxNearbyEntities", 6);
|
||||
f.short("RequiredPlayerRange", 16);
|
||||
f.short("SpawnRange", 4);
|
||||
f.keep_compound("SpawnData");
|
||||
let potentials = match tag.get("SpawnPotentials") {
|
||||
Some(t @ Tag::List(_)) => t.clone(),
|
||||
_ => match f.out.get(&"SpawnData".into()) {
|
||||
Some(data) => Tag::List(vec![Tag::compound([
|
||||
("data", data.clone()),
|
||||
("weight", Tag::Int(1)),
|
||||
])]),
|
||||
None => Tag::List(Vec::new()),
|
||||
},
|
||||
};
|
||||
f.put("SpawnPotentials", potentials);
|
||||
}
|
||||
"minecraft:creaking_heart" => {
|
||||
f.keep(
|
||||
"creaking",
|
||||
|t| matches!(t, Tag::IntArray(v) if v.len() == 4),
|
||||
);
|
||||
}
|
||||
"minecraft:piston" => {
|
||||
f.put(
|
||||
"blockState",
|
||||
match tag.get("blockState") {
|
||||
Some(t @ (Tag::String(_) | Tag::Compound(_))) => t.clone(),
|
||||
_ => string("minecraft:air"),
|
||||
},
|
||||
);
|
||||
f.put("facing", Tag::Int(int(tag.get("facing")).unwrap_or(0)));
|
||||
f.float("progress", 0.0);
|
||||
f.flag("extending", false);
|
||||
f.flag("source", false);
|
||||
}
|
||||
"minecraft:brewing_stand" => {
|
||||
f.base_container();
|
||||
f.int("BrewTime", 0);
|
||||
f.int("total_brew_time", 400);
|
||||
f.items(5, true);
|
||||
f.int("Fuel", 0);
|
||||
f.int("total_fuel", 20);
|
||||
f.float("speed_multiplier", 1.0);
|
||||
}
|
||||
"minecraft:enchanting_table" => f.component("CustomName"),
|
||||
"minecraft:beacon" => {
|
||||
for key in ["primary_effect", "secondary_effect"] {
|
||||
if let Some(effect) = text(tag.get(key)) {
|
||||
f.put(key, string(&effect));
|
||||
}
|
||||
}
|
||||
f.int("Levels", 0);
|
||||
f.component("CustomName");
|
||||
f.lock();
|
||||
}
|
||||
"minecraft:skull" => {
|
||||
f.keep("profile", |t| {
|
||||
matches!(t, Tag::Compound(_) | Tag::String(_))
|
||||
});
|
||||
if let Some(sound) = text(tag.get("note_block_sound")) {
|
||||
f.put("note_block_sound", string(&sound));
|
||||
}
|
||||
f.component("custom_name");
|
||||
}
|
||||
"minecraft:comparator" => f.int("OutputSignal", 0),
|
||||
"minecraft:banner" => {
|
||||
if let Some(patterns @ Tag::List(list)) = tag.get("patterns")
|
||||
&& !list.is_empty()
|
||||
{
|
||||
f.put("patterns", patterns.clone());
|
||||
}
|
||||
f.component("CustomName");
|
||||
}
|
||||
"minecraft:structure_block" => {
|
||||
f.string("name", "");
|
||||
f.string("author", "");
|
||||
f.string("metadata", "");
|
||||
f.int_clamped("posX", 0, -48, 48);
|
||||
f.int_clamped("posY", 1, -48, 48);
|
||||
f.int_clamped("posZ", 0, -48, 48);
|
||||
f.int_clamped("sizeX", 0, 0, 48);
|
||||
f.int_clamped("sizeY", 0, 0, 48);
|
||||
f.int_clamped("sizeZ", 0, 0, 48);
|
||||
f.string("rotation", "NONE");
|
||||
f.string("mirror", "NONE");
|
||||
f.string("mode", "DATA");
|
||||
f.flag("ignoreEntities", true);
|
||||
f.flag("strict", false);
|
||||
f.flag("powered", false);
|
||||
f.flag("showair", false);
|
||||
f.flag("showboundingbox", true);
|
||||
f.float("integrity", 1.0);
|
||||
f.long("seed", 0);
|
||||
}
|
||||
"minecraft:command_block" => {
|
||||
f.string("Command", "");
|
||||
f.int("SuccessCount", 0);
|
||||
f.component("CustomName");
|
||||
let track = flag(tag.get("TrackOutput")).unwrap_or(true);
|
||||
f.put("TrackOutput", boolean(track));
|
||||
if track {
|
||||
f.component("LastOutput");
|
||||
}
|
||||
let update = flag(tag.get("UpdateLastExecution")).unwrap_or(true);
|
||||
f.put("UpdateLastExecution", boolean(update));
|
||||
if update && let Some(last) = long(tag.get("LastExecution")).filter(|l| *l != -1) {
|
||||
f.put("LastExecution", Tag::Long(last));
|
||||
}
|
||||
f.flag("powered", false);
|
||||
f.flag("conditionMet", false);
|
||||
f.flag("auto", false);
|
||||
}
|
||||
"minecraft:lectern" => {
|
||||
if f.item("Book") {
|
||||
f.int("Page", 0);
|
||||
}
|
||||
}
|
||||
"minecraft:jigsaw" => {
|
||||
f.string("name", "minecraft:empty");
|
||||
f.string("target", "minecraft:empty");
|
||||
f.string("pool", "minecraft:empty");
|
||||
f.string("final_state", "minecraft:air");
|
||||
f.string("joint", "rollable");
|
||||
f.int("placement_priority", 0);
|
||||
f.int("selection_priority", 0);
|
||||
}
|
||||
"minecraft:campfire" => {
|
||||
f.items(4, true);
|
||||
for key in ["CookingTimes", "CookingTotalTimes"] {
|
||||
let mut times = [0; 4];
|
||||
if let Some(v) = int_array(tag.get(key)) {
|
||||
for (slot, t) in times.iter_mut().zip(v) {
|
||||
*slot = *t;
|
||||
}
|
||||
}
|
||||
f.put(key, Tag::IntArray(times.to_vec()));
|
||||
}
|
||||
}
|
||||
"minecraft:beehive" => {
|
||||
let bees = match tag.get("bees") {
|
||||
Some(Tag::List(list)) => list
|
||||
.iter()
|
||||
.filter_map(|bee| {
|
||||
let data = bee
|
||||
.get("entity_data")
|
||||
.filter(|d| matches!(d, Tag::Compound(_)))?;
|
||||
Some(Tag::compound([
|
||||
("entity_data", data.clone()),
|
||||
("ticks_in_hive", Tag::Int(int(bee.get("ticks_in_hive"))?)),
|
||||
(
|
||||
"min_ticks_in_hive",
|
||||
Tag::Int(int(bee.get("min_ticks_in_hive"))?),
|
||||
),
|
||||
]))
|
||||
})
|
||||
.collect(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
f.put("bees", Tag::List(bees));
|
||||
f.keep("flower_pos", is_pos);
|
||||
}
|
||||
"minecraft:sculk_sensor" | "minecraft:calibrated_sculk_sensor" => {
|
||||
f.int("last_vibration_frequency", 0);
|
||||
f.compound_or("listener", listener());
|
||||
}
|
||||
"minecraft:sculk_shrieker" => {
|
||||
f.int("warning_level", 0);
|
||||
f.compound_or("listener", listener());
|
||||
}
|
||||
"minecraft:sculk_catalyst" => {
|
||||
let cursors = match tag.get("cursors") {
|
||||
Some(t @ Tag::List(_)) => t.clone(),
|
||||
_ => Tag::List(Vec::new()),
|
||||
};
|
||||
f.put("cursors", cursors);
|
||||
}
|
||||
"minecraft:chiseled_bookshelf" => {
|
||||
f.items(6, true);
|
||||
f.int("last_interacted_slot", -1);
|
||||
}
|
||||
"minecraft:shelf" => {
|
||||
f.items(3, true);
|
||||
f.flag("align_items_to_bottom", false);
|
||||
}
|
||||
"minecraft:brushable_block" => {
|
||||
if !f.loot_table() {
|
||||
f.item("item");
|
||||
}
|
||||
}
|
||||
"minecraft:decorated_pot" => {
|
||||
if let Some(Tag::Compound(sherds)) = tag.get("sherds") {
|
||||
let kept: Compound = ["back", "left", "right", "front"]
|
||||
.iter()
|
||||
.filter_map(|side| {
|
||||
Some(((*side).into(), template(sherds.get(&(*side).into())?)?))
|
||||
})
|
||||
.collect();
|
||||
if !kept.is_empty() {
|
||||
f.put("sherds", Tag::Compound(kept));
|
||||
}
|
||||
}
|
||||
if !f.loot_table() {
|
||||
f.item("item");
|
||||
}
|
||||
}
|
||||
"minecraft:trial_spawner" => {
|
||||
// `TrialSpawnerStateData.Packed` and `FullConfig`: every key is optional
|
||||
// and defaulted by vanilla; they are kept as loaded.
|
||||
if let Tag::Compound(c) = tag {
|
||||
for (k, v) in c {
|
||||
let key = k.to_string_lossy();
|
||||
if !matches!(
|
||||
key.as_str(),
|
||||
"id" | "x" | "y" | "z" | "keepPacked" | "components"
|
||||
) {
|
||||
f.out.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
if !f.out.contains_key(&"spawn_data".into()) {
|
||||
f.put(
|
||||
"spawn_data",
|
||||
Tag::compound([("entity", Tag::Compound(Compound::new()))]),
|
||||
);
|
||||
}
|
||||
}
|
||||
"minecraft:vault" => {
|
||||
// `VaultConfig.DEFAULT` (when the config is absent): the trial key and the
|
||||
// default reward table, which is not saved.
|
||||
f.compound_or(
|
||||
"config",
|
||||
[(
|
||||
"key_item".into(),
|
||||
Tag::compound([
|
||||
("count", Tag::Int(1)),
|
||||
("id", string("minecraft:trial_key")),
|
||||
]),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
);
|
||||
f.compound_or("shared_data", Compound::new());
|
||||
f.compound_or("server_data", Compound::new());
|
||||
}
|
||||
"minecraft:test_block" => {
|
||||
f.string("mode", "fail");
|
||||
f.string("message", "");
|
||||
f.flag("powered", false);
|
||||
}
|
||||
"minecraft:test_instance_block" => {
|
||||
f.compound_or(
|
||||
"data",
|
||||
[
|
||||
("ignore_entities".into(), boolean(false)),
|
||||
("rotation".into(), string("none")),
|
||||
("size".into(), Tag::IntArray(vec![0, 0, 0])),
|
||||
("status".into(), string("cleared")),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
);
|
||||
if let Some(errors @ Tag::List(list)) = tag.get("errors")
|
||||
&& !list.is_empty()
|
||||
{
|
||||
f.put("errors", errors.clone());
|
||||
}
|
||||
}
|
||||
"minecraft:potent_sulfur" => f.int("countdown", -1),
|
||||
_ => return None,
|
||||
}
|
||||
let mut out = f.out;
|
||||
out.insert("id".into(), string(&id));
|
||||
out.insert("x".into(), Tag::Int(x));
|
||||
out.insert("y".into(), Tag::Int(y));
|
||||
out.insert("z".into(), Tag::Int(z));
|
||||
out.insert("keepPacked".into(), Tag::Byte(0));
|
||||
out.insert(
|
||||
"components".into(),
|
||||
match tag.get("components") {
|
||||
Some(c @ Tag::Compound(_)) => c.clone(),
|
||||
_ => Tag::Compound(Compound::new()),
|
||||
},
|
||||
);
|
||||
Some(Tag::Compound(out))
|
||||
}
|
||||
|
||||
/// `VibrationSystem.Data` for a sensor with no pending vibration.
|
||||
fn listener() -> Compound {
|
||||
[
|
||||
("event_delay".into(), Tag::Int(0)),
|
||||
("selector".into(), Tag::compound([("tick", Tag::Long(-1))])),
|
||||
]
|
||||
.into_iter()
|
||||
.collect()
|
||||
}
|
||||
/// Whether a stack plays a song (`JukeboxSong.fromStack`): a music disc or an item
|
||||
/// with the `jukebox_playable` component.
|
||||
fn is_record(item: &Compound) -> bool {
|
||||
let id = match item.get(&"id".into()) {
|
||||
Some(Tag::String(s)) => s.to_string_lossy(),
|
||||
_ => return false,
|
||||
};
|
||||
id.starts_with("minecraft:music_disc_")
|
||||
|| matches!(item.get(&"components".into()), Some(Tag::Compound(c)) if c.contains_key(&"minecraft:jukebox_playable".into()))
|
||||
}
|
||||
|
||||
/// Three-way merge of a block entity: `original` is the NBT it was loaded from,
|
||||
/// `loaded` what Horizon encodes right after decoding it, and `current` what Horizon
|
||||
/// encodes now. Whatever the game left unchanged since loading keeps its original
|
||||
/// form (including the detail Horizon does not model, such as styled text), what it
|
||||
/// changed takes the current value, and keys Horizon never models stay.
|
||||
pub fn overlay(original: &Tag, loaded: &Tag, current: &Tag) -> Tag {
|
||||
if current == loaded {
|
||||
return original.clone();
|
||||
}
|
||||
match (original, loaded, current) {
|
||||
(Tag::Compound(o), Tag::Compound(l), Tag::Compound(c)) => {
|
||||
let mut out = Compound::new();
|
||||
for (key, cv) in c {
|
||||
let value = match (o.get(key), l.get(key)) {
|
||||
(Some(ov), Some(lv)) => overlay(ov, lv, cv),
|
||||
_ => cv.clone(),
|
||||
};
|
||||
out.insert(key.clone(), value);
|
||||
}
|
||||
// Keys the game never models (or models only when present) survive;
|
||||
// keys it removed are gone.
|
||||
for (key, ov) in o {
|
||||
if !c.contains_key(key) && !l.contains_key(key) {
|
||||
out.insert(key.clone(), ov.clone());
|
||||
}
|
||||
}
|
||||
Tag::Compound(out)
|
||||
}
|
||||
(Tag::List(o), Tag::List(l), Tag::List(c)) if keyed_by_slot(c) && keyed_by_slot(l) => {
|
||||
let slot = |t: &Tag| int(t.get("Slot"));
|
||||
Tag::List(
|
||||
c.iter()
|
||||
.map(|cv| {
|
||||
let s = slot(cv);
|
||||
fn find(list: &[Tag], slot: Option<i32>) -> Option<&Tag> {
|
||||
list.iter().find(|t| int(t.get("Slot")) == slot)
|
||||
}
|
||||
match (find(o, s), find(l, s)) {
|
||||
(Some(ov), Some(lv)) => overlay(ov, lv, cv),
|
||||
_ => cv.clone(),
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
_ => current.clone(),
|
||||
}
|
||||
}
|
||||
fn keyed_by_slot(list: &[Tag]) -> bool {
|
||||
list.iter().all(|t| t.get("Slot").is_some())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use horizon_world::nbt::parse_snbt;
|
||||
|
||||
fn snbt(s: &str) -> Tag {
|
||||
parse_snbt(s).expect("snbt").0
|
||||
}
|
||||
fn canon(s: &str) -> Tag {
|
||||
canonical(&snbt(s)).expect("known type")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_type_has_defaults() {
|
||||
for id in TYPES.iter().chain(NEWEST.iter()) {
|
||||
let tag = snbt(&format!("{{id:\"{id}\",x:1,y:2,z:3}}"));
|
||||
let out = canonical(&tag).unwrap_or_else(|| panic!("{id}"));
|
||||
assert_eq!(out.get("id"), tag.get("id"));
|
||||
assert_eq!(out.get("keepPacked"), Some(&Tag::Byte(0)), "{id}");
|
||||
assert_eq!(
|
||||
out.get("components"),
|
||||
Some(&Tag::Compound(Compound::new())),
|
||||
"{id}"
|
||||
);
|
||||
}
|
||||
assert!(canonical(&snbt("{id:\"minecraft:nope\",x:1,y:2,z:3}")).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn furnace_defaults_match_vanilla() {
|
||||
let out = canon("{id:\"minecraft:furnace\",x:1,y:2,z:3}");
|
||||
assert_eq!(out.get("cooking_total_time"), Some(&Tag::Int(0)));
|
||||
assert_eq!(out.get("speed_multiplier"), Some(&f32_tag(1.0)));
|
||||
assert_eq!(out.get("Items"), Some(&Tag::List(vec![])));
|
||||
assert_eq!(
|
||||
out.get("RecipesUsed"),
|
||||
Some(&Tag::Compound(Compound::new()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loot_table_replaces_items_and_zero_seed_is_omitted() {
|
||||
let out = canon(
|
||||
"{id:\"minecraft:chest\",x:1,y:2,z:3,LootTable:\"minecraft:chests/x\",LootTableSeed:0L,\
|
||||
Items:[{Slot:0b,id:\"minecraft:stone\",count:1}]}",
|
||||
);
|
||||
assert!(out.get("Items").is_none());
|
||||
assert!(out.get("LootTableSeed").is_none());
|
||||
assert_eq!(out.get("LootTable"), Some(&string("minecraft:chests/x")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn items_are_validated_and_sorted_by_slot() {
|
||||
let out = canon(
|
||||
"{id:\"minecraft:hopper\",x:1,y:2,z:3,Items:[\
|
||||
{Slot:4b,id:\"minecraft:stone\",count:2},{Slot:5b,id:\"minecraft:stone\",count:1},\
|
||||
{Slot:0b,id:\"minecraft:air\",count:1},{Slot:1b,id:\"minecraft:dirt\"},\
|
||||
{Slot:2b,id:\"minecraft:dirt\",count:100}]}",
|
||||
);
|
||||
let Some(Tag::List(items)) = out.get("Items") else {
|
||||
panic!("items")
|
||||
};
|
||||
let slots: Vec<_> = items.iter().map(|i| i.get("Slot").cloned()).collect();
|
||||
assert_eq!(slots, vec![Some(Tag::Byte(1)), Some(Tag::Byte(4))]);
|
||||
assert_eq!(items[0].get("count"), Some(&Tag::Int(1)));
|
||||
assert_eq!(out.get("TransferCooldown"), Some(&Tag::Int(-1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plain_text_components_become_strings() {
|
||||
let out =
|
||||
canon("{id:\"minecraft:banner\",x:1,y:2,z:3,CustomName:{text:\"Flag\"},patterns:[]}");
|
||||
assert_eq!(out.get("CustomName"), Some(&string("Flag")));
|
||||
assert!(out.get("patterns").is_none());
|
||||
let styled =
|
||||
canon("{id:\"minecraft:banner\",x:1,y:2,z:3,CustomName:{text:\"F\",color:\"red\"}}");
|
||||
assert!(matches!(styled.get("CustomName"), Some(Tag::Compound(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shulker_box_omits_empty_items_and_structure_block_clamps() {
|
||||
let shulker = canon("{id:\"minecraft:shulker_box\",x:1,y:2,z:3}");
|
||||
assert!(shulker.get("Items").is_none());
|
||||
let structure = canon("{id:\"minecraft:structure_block\",x:1,y:2,z:3,posX:99,sizeY:-4}");
|
||||
assert_eq!(structure.get("posX"), Some(&Tag::Int(48)));
|
||||
assert_eq!(structure.get("sizeY"), Some(&Tag::Int(0)));
|
||||
assert_eq!(structure.get("posY"), Some(&Tag::Int(1)));
|
||||
assert_eq!(structure.get("showboundingbox"), Some(&Tag::Byte(1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canonical_is_idempotent() {
|
||||
for id in TYPES.iter().chain(NEWEST.iter()) {
|
||||
let once = canonical(&snbt(&format!("{{id:\"{id}\",x:1,y:2,z:3}}"))).expect("known");
|
||||
assert_eq!(canonical(&once).as_ref(), Some(&once), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlay_keeps_unchanged_detail_and_takes_changes() {
|
||||
let original = snbt(
|
||||
"{id:\"minecraft:sign\",front_text:{messages:[{text:\"a\",color:\"red\"},\"\",\"\",\"\"]},\
|
||||
back_text:{messages:[\"x\",\"\",\"\",\"\"]},extra:1b}",
|
||||
);
|
||||
let loaded = snbt(
|
||||
"{id:\"minecraft:sign\",front_text:{messages:[\"a\",\"\",\"\",\"\"]},\
|
||||
back_text:{messages:[\"x\",\"\",\"\",\"\"]}}",
|
||||
);
|
||||
let current = snbt(
|
||||
"{id:\"minecraft:sign\",front_text:{messages:[\"a\",\"\",\"\",\"\"]},\
|
||||
back_text:{messages:[\"y\",\"\",\"\",\"\"]}}",
|
||||
);
|
||||
let merged = overlay(&original, &loaded, ¤t);
|
||||
assert_eq!(merged.get("front_text"), original.get("front_text"));
|
||||
assert_eq!(merged.get("back_text"), current.get("back_text"));
|
||||
assert_eq!(merged.get("extra"), original.get("extra"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlay_merges_container_slots() {
|
||||
let item = |slot: i8, count: i32, extra: &str| {
|
||||
snbt(&format!(
|
||||
"{{Slot:{slot}b,id:\"minecraft:stone\",count:{count}{extra}}}"
|
||||
))
|
||||
};
|
||||
let original = Tag::compound([(
|
||||
"Items",
|
||||
Tag::List(vec![
|
||||
item(0, 1, ",components:{\"minecraft:lore\":[\"x\"]}"),
|
||||
item(1, 1, ""),
|
||||
]),
|
||||
)]);
|
||||
let loaded = Tag::compound([("Items", Tag::List(vec![item(0, 1, ""), item(1, 1, "")]))]);
|
||||
let current = Tag::compound([("Items", Tag::List(vec![item(0, 1, ""), item(1, 5, "")]))]);
|
||||
let merged = overlay(&original, &loaded, ¤t);
|
||||
let Some(Tag::List(items)) = merged.get("Items") else {
|
||||
panic!("items")
|
||||
};
|
||||
assert!(
|
||||
items[0].get("components").is_some(),
|
||||
"slot 0 keeps its lore"
|
||||
);
|
||||
assert_eq!(items[1].get("count"), Some(&Tag::Int(5)));
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,7 @@ impl BlockEntity {
|
||||
lit_remaining: 0,
|
||||
lit_total: 0,
|
||||
cooking_timer: 0,
|
||||
cooking_total: 200,
|
||||
cooking_total: 0,
|
||||
speed: 1.0,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -108,10 +108,15 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
Tag::Short(m.death_time.clamp(0, i32::from(i16::MAX)) as i16),
|
||||
),
|
||||
("PersistenceRequired", Tag::Byte(i8::from(m.persistent))),
|
||||
("CustomNameVisible", Tag::Byte(i8::from(m.name_visible))),
|
||||
] {
|
||||
put(&mut tag, key, value);
|
||||
}
|
||||
// `Entity.saveWithoutId` writes the flag only when set.
|
||||
if m.name_visible {
|
||||
put(&mut tag, "CustomNameVisible", Tag::Byte(1));
|
||||
} else if let Tag::Compound(map) = &mut tag {
|
||||
map.remove(&"CustomNameVisible".into());
|
||||
}
|
||||
// A styled name from the save stays unless the name changed.
|
||||
if custom_name(&tag) != m.custom_name
|
||||
&& let Some(name) = &m.custom_name
|
||||
@@ -134,7 +139,10 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
map.entry(key.into()).or_insert(value);
|
||||
}
|
||||
let id = m.kind.id.as_str();
|
||||
if m.kind.category == "creature" {
|
||||
// Only breedable animals have an age; a loaded creature that was saved without
|
||||
// one (a frog) stays without.
|
||||
let aged = raw.is_none_or(|r| r.get("Age").is_some());
|
||||
if m.kind.category == "creature" && aged {
|
||||
map.insert("Age".into(), Tag::Int(m.extra.age));
|
||||
map.insert("AgeLocked".into(), Tag::Byte(i8::from(m.extra.age_locked)));
|
||||
for (key, value) in [
|
||||
@@ -204,7 +212,10 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
let v = i32::from(m.extra.variant) | i32::from(m.extra.variant2) << 8;
|
||||
map.insert("Variant".into(), Tag::Int(v));
|
||||
}
|
||||
map.insert("ChestedHorse".into(), Tag::Byte(i8::from(m.extra.chested)));
|
||||
// Only donkeys and mules carry chests.
|
||||
if matches!(id, "minecraft:donkey" | "minecraft:mule") {
|
||||
map.insert("ChestedHorse".into(), Tag::Byte(i8::from(m.extra.chested)));
|
||||
}
|
||||
let mut equipment = Vec::new();
|
||||
if m.extra.saddled {
|
||||
equipment.push((
|
||||
@@ -218,7 +229,11 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
Tag::compound([("id", string(armor)), ("count", Tag::Int(1))]),
|
||||
));
|
||||
}
|
||||
map.insert("equipment".into(), Tag::compound(equipment));
|
||||
if equipment.is_empty() {
|
||||
map.remove(&"equipment".into());
|
||||
} else {
|
||||
map.insert("equipment".into(), Tag::compound(equipment));
|
||||
}
|
||||
let attr = |id: &str, v: f64| {
|
||||
Tag::compound([("id", string(id)), ("base", Tag::Double(v.to_bits()))])
|
||||
};
|
||||
@@ -347,7 +362,7 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
}
|
||||
// Skeletons are spawned holding a bow (finalizeSpawn); a mob loaded
|
||||
// from NBT keeps only its saved equipment.
|
||||
"minecraft:skeleton" | "minecraft:stray" | "minecraft:bogged" => {
|
||||
"minecraft:skeleton" | "minecraft:stray" | "minecraft:bogged" if raw.is_none() => {
|
||||
map.entry("equipment".into()).or_insert_with(|| {
|
||||
Tag::compound([(
|
||||
"mainhand",
|
||||
@@ -423,7 +438,12 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
}
|
||||
}
|
||||
}
|
||||
map.insert("equipment".into(), Tag::Compound(equipment));
|
||||
// Vanilla saves `equipment` only when something is equipped.
|
||||
if equipment.is_empty() {
|
||||
map.remove(&"equipment".into());
|
||||
} else {
|
||||
map.insert("equipment".into(), Tag::Compound(equipment));
|
||||
}
|
||||
}
|
||||
tag
|
||||
}
|
||||
@@ -934,16 +954,27 @@ pub fn armor_stand_to_nbt(a: &ArmorStand, uuid: [i32; 4], raw: Option<&Tag>) ->
|
||||
.zip(&a.equipment)
|
||||
.filter_map(|(slot, stack)| Some((*slot, item_to_nbt(stack.as_ref()?))))
|
||||
.collect();
|
||||
put(&mut tag, "equipment", Tag::compound(equipment));
|
||||
// `equipment` is saved only when something is worn, `Marker` only when set.
|
||||
if equipment.is_empty() {
|
||||
if let Tag::Compound(map) = &mut tag {
|
||||
map.remove(&"equipment".into());
|
||||
}
|
||||
} else {
|
||||
put(&mut tag, "equipment", Tag::compound(equipment));
|
||||
}
|
||||
for (key, value) in [
|
||||
("Small", a.small),
|
||||
("ShowArms", a.show_arms),
|
||||
("NoBasePlate", a.no_base_plate),
|
||||
("Invisible", a.invisible),
|
||||
("Marker", a.marker),
|
||||
] {
|
||||
put(&mut tag, key, Tag::Byte(i8::from(value)));
|
||||
}
|
||||
if a.marker {
|
||||
put(&mut tag, "Marker", Tag::Byte(1));
|
||||
} else if let Tag::Compound(map) = &mut tag {
|
||||
map.remove(&"Marker".into());
|
||||
}
|
||||
if let Tag::Compound(map) = &tag
|
||||
&& !map.contains_key(&horizon_world::nbt::JavaString::from("Health"))
|
||||
{
|
||||
@@ -1028,12 +1059,19 @@ fn put_offers(map: &mut Compound, m: &Mob) {
|
||||
("sell", item_to_nbt(&o.result)),
|
||||
("uses", Tag::Int(o.uses)),
|
||||
("maxUses", Tag::Int(o.max_uses)),
|
||||
("rewardExp", Tag::Byte(i8::from(o.reward_exp))),
|
||||
("specialPrice", Tag::Int(o.special_price_diff)),
|
||||
("demand", Tag::Int(o.demand)),
|
||||
("priceMultiplier", Tag::Float(o.price_multiplier.to_bits())),
|
||||
("xp", Tag::Int(o.xp)),
|
||||
];
|
||||
// `MerchantOffer.CODEC` omits fields at their defaults.
|
||||
if !o.reward_exp {
|
||||
entries.push(("rewardExp", Tag::Byte(0)));
|
||||
}
|
||||
if o.special_price_diff != 0 {
|
||||
entries.push(("specialPrice", Tag::Int(o.special_price_diff)));
|
||||
}
|
||||
if o.demand != 0 {
|
||||
entries.push(("demand", Tag::Int(o.demand)));
|
||||
}
|
||||
if let Some(b) = &o.cost_b {
|
||||
entries.push(("buyB", item_to_nbt(&b.stack)));
|
||||
}
|
||||
|
||||
@@ -498,28 +498,47 @@ impl Game<'_> {
|
||||
if let Err(e) = self.ui.rules.save(&self.world_root) {
|
||||
eprintln!("Horizon: failed to save game rules: {e}");
|
||||
}
|
||||
use horizon_world::nbt::{Limits, NamedTag, Tag};
|
||||
use horizon_world::nbt::Tag;
|
||||
let c = self.ui.clock;
|
||||
let clock = Tag::compound([
|
||||
("total_ticks", Tag::Long(self.world.day_time)),
|
||||
("partial_tick", Tag::Float(c.partial.to_bits())),
|
||||
("rate", Tag::Float(c.rate.to_bits())),
|
||||
("paused", Tag::Byte(i8::from(c.paused))),
|
||||
]);
|
||||
let root = NamedTag {
|
||||
name: "".into(),
|
||||
tag: Tag::compound([
|
||||
("data", Tag::compound([("minecraft:overworld", clock)])),
|
||||
("DataVersion", Tag::Int(horizon_world::DATA_VERSION)),
|
||||
]),
|
||||
// The other clocks stay as saved; the overworld one keeps the fields Horizon
|
||||
// does not model, and (like vanilla's codec) omits rate, partial tick and
|
||||
// pause while they have their defaults.
|
||||
let path = self.world_root.join("data/minecraft/world_clocks.dat");
|
||||
let existing = horizon_client::save::read_dat(&path);
|
||||
let mut data = match existing.as_ref().and_then(|r| r.get("data")) {
|
||||
Some(d @ Tag::Compound(_)) => d.clone(),
|
||||
_ => Tag::Compound(Default::default()),
|
||||
};
|
||||
if let Ok(bytes) = horizon_world::nbt::write_named(&root, Limits::default())
|
||||
&& let Ok(gz) = horizon_world::compression::Compression::Gzip.encode(&bytes)
|
||||
{
|
||||
let dir = self.world_root.join("data/minecraft");
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let _ = std::fs::write(dir.join("world_clocks.dat"), gz);
|
||||
let mut clock = match data.get("minecraft:overworld") {
|
||||
Some(c @ Tag::Compound(_)) => c.clone(),
|
||||
_ => Tag::Compound(Default::default()),
|
||||
};
|
||||
if let Tag::Compound(map) = &mut clock {
|
||||
map.insert("total_ticks".into(), Tag::Long(self.world.day_time));
|
||||
for (key, value, default) in [
|
||||
(
|
||||
"partial_tick",
|
||||
Tag::Float(c.partial.to_bits()),
|
||||
c.partial == 0.0,
|
||||
),
|
||||
("rate", Tag::Float(c.rate.to_bits()), c.rate == 1.0),
|
||||
("paused", Tag::Byte(i8::from(c.paused)), !c.paused),
|
||||
] {
|
||||
if default {
|
||||
map.remove(&key.into());
|
||||
} else {
|
||||
map.insert(key.into(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Tag::Compound(map) = &mut data {
|
||||
map.insert("minecraft:overworld".into(), clock);
|
||||
}
|
||||
let root = Tag::compound([
|
||||
("data", data),
|
||||
("DataVersion", Tag::Int(horizon_world::DATA_VERSION)),
|
||||
]);
|
||||
let _ = horizon_client::save::write_dat(&path, root);
|
||||
let _ = write_difficulty(&self.world_root, self.ui.difficulty);
|
||||
}
|
||||
/// Death message in chat (CombatTracker.getDeathMessage, as far as the
|
||||
|
||||
@@ -34,6 +34,7 @@ const HELP: &[&str] = &[
|
||||
"bench SECONDS reset stats, wait, then reply with the summary",
|
||||
"open WORLD [SEED] [creative] open or create a world from the title screen",
|
||||
"save save the world",
|
||||
"save-all save the world, rewriting every resident chunk",
|
||||
"connect HOST[:PORT] [NAME] join a multiplayer server (offline mode) from the title screen",
|
||||
"chat MESSAGE send chat or a /command to the server",
|
||||
"disconnect leave the server for the server list",
|
||||
@@ -386,6 +387,15 @@ impl Game<'_> {
|
||||
self.save_game()?;
|
||||
ok()
|
||||
}
|
||||
"save-all" => {
|
||||
// Like `save-all flush`: also rewrite chunks that were not modified
|
||||
// (the save round-trip harness compares them with vanilla's files).
|
||||
self.require_world()?;
|
||||
let keys: Vec<_> = self.world.chunks.keys().copied().collect();
|
||||
self.world.modified.extend(keys);
|
||||
self.save_game()?;
|
||||
ok()
|
||||
}
|
||||
"dragon-hurt" => {
|
||||
// Test hook: damage the dragon's head as the player would.
|
||||
let amount: f32 = arg(1)?.parse()?;
|
||||
|
||||
@@ -25,6 +25,36 @@ pub struct EntityMemory {
|
||||
/// Entities of types Horizon does not simulate, kept per chunk and
|
||||
/// written back unchanged.
|
||||
unknown: HashMap<(i32, i32), Vec<Tag>>,
|
||||
/// Entities without a Horizon entity ID (orbs, arrows, falling blocks, crystals,
|
||||
/// the dragon): the UUID and compound they were loaded with, found again by
|
||||
/// type and position (an entity that has moved since loading is saved afresh).
|
||||
anon: HashMap<(String, [u64; 3]), (Vec3, Option<[i32; 4]>, Tag)>,
|
||||
}
|
||||
|
||||
fn anon_key(kind: &str, p: Vec3) -> (String, [u64; 3]) {
|
||||
(
|
||||
kind.to_owned(),
|
||||
[p.x.to_bits(), p.y.to_bits(), p.z.to_bits()],
|
||||
)
|
||||
}
|
||||
/// The loaded compound underneath the fields Horizon models: what the entity was
|
||||
/// loaded with, with every key of `modelled` replacing its loaded value.
|
||||
fn with_raw(modelled: Tag, raw: Option<&Tag>) -> Tag {
|
||||
let (Some(Tag::Compound(raw)), Tag::Compound(modelled)) = (raw, &modelled) else {
|
||||
return modelled;
|
||||
};
|
||||
let mut out = raw.clone();
|
||||
for (k, v) in modelled {
|
||||
// Defaults Horizon invents for fields it does not simulate give way to the save.
|
||||
let invented = matches!(
|
||||
k.to_string_lossy().as_str(),
|
||||
"Fire" | "Air" | "Invulnerable" | "PortalCooldown" | "HasBeenShot"
|
||||
);
|
||||
if !(invented && out.contains_key(k)) {
|
||||
out.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
Tag::Compound(out)
|
||||
}
|
||||
|
||||
fn painting_center(p: &horizon_client::entities::Painting) -> Vec3 {
|
||||
@@ -111,6 +141,19 @@ impl Game<'_> {
|
||||
for tag in tags {
|
||||
let kind = entity_nbt::kind(&tag).unwrap_or_default();
|
||||
let uuid = entity_nbt::uuid(&tag);
|
||||
if matches!(
|
||||
kind.as_str(),
|
||||
"minecraft:experience_orb"
|
||||
| "minecraft:arrow"
|
||||
| "minecraft:falling_block"
|
||||
| "minecraft:end_crystal"
|
||||
| "minecraft:ender_dragon"
|
||||
) && let Some(at) = entity_nbt::position(&tag)
|
||||
{
|
||||
self.persist
|
||||
.anon
|
||||
.insert(anon_key(&kind, at), (at, uuid, tag.clone()));
|
||||
}
|
||||
match kind.as_str() {
|
||||
"minecraft:item" => {
|
||||
self.next_entity += 1;
|
||||
@@ -347,31 +390,44 @@ impl Game<'_> {
|
||||
.or_default()
|
||||
.push(tag);
|
||||
}
|
||||
let anon = |persist: &EntityMemory, kind: &str, at: Vec3| {
|
||||
persist
|
||||
.anon
|
||||
.get(&anon_key(kind, at))
|
||||
.map(|(_, uuid, raw)| (uuid.unwrap_or_else(random_uuid), Some(raw.clone())))
|
||||
.unwrap_or_else(|| (random_uuid(), None))
|
||||
};
|
||||
for o in &self.orbs {
|
||||
if keys.contains(&chunk_of(o.position)) {
|
||||
let (uuid, raw) = anon(&self.persist, "minecraft:experience_orb", o.position);
|
||||
out.entry(chunk_of(o.position))
|
||||
.or_default()
|
||||
.push(entity_nbt::orb_to_nbt(o, random_uuid()));
|
||||
.push(with_raw(entity_nbt::orb_to_nbt(o, uuid), raw.as_ref()));
|
||||
}
|
||||
}
|
||||
for a in &self.arrows {
|
||||
if keys.contains(&chunk_of(a.position)) {
|
||||
let owner = if a.from_player { player_uuid } else { None };
|
||||
out.entry(chunk_of(a.position))
|
||||
.or_default()
|
||||
.push(entity_nbt::arrow_to_nbt(a, random_uuid(), owner));
|
||||
let (uuid, raw) = anon(&self.persist, "minecraft:arrow", a.position);
|
||||
out.entry(chunk_of(a.position)).or_default().push(with_raw(
|
||||
entity_nbt::arrow_to_nbt(a, uuid, owner),
|
||||
raw.as_ref(),
|
||||
));
|
||||
}
|
||||
}
|
||||
for f in &self.falling {
|
||||
if keys.contains(&chunk_of(f.position)) {
|
||||
let (uuid, raw) = anon(&self.persist, "minecraft:falling_block", f.position);
|
||||
let tag = entity_nbt::falling_block_to_nbt(
|
||||
self.state_to_nbt(f.state),
|
||||
f.position,
|
||||
f.velocity,
|
||||
f.age,
|
||||
random_uuid(),
|
||||
uuid,
|
||||
);
|
||||
out.entry(chunk_of(f.position)).or_default().push(tag);
|
||||
out.entry(chunk_of(f.position))
|
||||
.or_default()
|
||||
.push(with_raw(tag, raw.as_ref()));
|
||||
}
|
||||
}
|
||||
for b in &self.boats {
|
||||
@@ -396,15 +452,17 @@ impl Game<'_> {
|
||||
if let Some(d) = &self.dragon
|
||||
&& keys.contains(&chunk_of(d.position))
|
||||
{
|
||||
let (uuid, raw) = anon(&self.persist, "minecraft:ender_dragon", d.position);
|
||||
out.entry(chunk_of(d.position))
|
||||
.or_default()
|
||||
.push(d.to_nbt(random_uuid(), None));
|
||||
.push(d.to_nbt(uuid, raw.as_ref()));
|
||||
}
|
||||
for c in &self.crystals {
|
||||
if keys.contains(&chunk_of(c.position)) {
|
||||
let (uuid, raw) = anon(&self.persist, "minecraft:end_crystal", c.position);
|
||||
out.entry(chunk_of(c.position))
|
||||
.or_default()
|
||||
.push(c.to_nbt(random_uuid(), None));
|
||||
.push(c.to_nbt(uuid, raw.as_ref()));
|
||||
}
|
||||
}
|
||||
for c in &self.carts {
|
||||
@@ -537,6 +595,9 @@ impl Game<'_> {
|
||||
self.orbs.retain(|o| !gone(o.position));
|
||||
self.arrows.retain(|a| !gone(a.position));
|
||||
self.falling.retain(|f| !gone(f.position));
|
||||
self.persist
|
||||
.anon
|
||||
.retain(|_, (at, _, _)| !keys.contains(&chunk_of(*at)));
|
||||
for id in removed {
|
||||
self.persist.uuids.remove(&id);
|
||||
self.persist.raw.remove(&id);
|
||||
@@ -555,6 +616,11 @@ impl Game<'_> {
|
||||
.iter()
|
||||
.map(|m| m.id)
|
||||
.chain(self.dropped.iter().map(|e| e.id))
|
||||
.chain(self.boats.iter().map(|e| e.id))
|
||||
.chain(self.carts.iter().map(|e| e.id))
|
||||
.chain(self.armor_stands.iter().map(|e| e.id))
|
||||
.chain(self.item_frames.iter().map(|e| e.id))
|
||||
.chain(self.paintings.iter().map(|e| e.id))
|
||||
.collect();
|
||||
self.persist.uuids.retain(|id, _| alive.contains(id));
|
||||
self.persist.raw.retain(|id, _| alive.contains(id));
|
||||
|
||||
@@ -8,6 +8,7 @@ pub mod assets;
|
||||
pub mod attribute_updates;
|
||||
pub mod audio;
|
||||
pub mod biome;
|
||||
pub mod block_entity_nbt;
|
||||
pub mod block_entity_render;
|
||||
pub mod blocks;
|
||||
pub mod brewing;
|
||||
|
||||
@@ -199,6 +199,9 @@ pub struct ChunkExtras {
|
||||
pub root: Compound,
|
||||
/// Block entities Horizon does not model.
|
||||
pub block_entities: Vec<Tag>,
|
||||
/// The NBT of the modelled block entities as loaded, to restore the fields and
|
||||
/// detail Horizon does not model when it saves them (`block_entity_nbt::overlay`).
|
||||
pub block_entity_sources: Vec<((i32, i32, i32), Tag)>,
|
||||
/// Scheduled ticks (`block_ticks`/`fluid_ticks`): delays relative to the
|
||||
/// save time. Registered with the level's tick scheduler on arrival and
|
||||
/// replaced with the scheduler's pending ticks when saving.
|
||||
@@ -408,6 +411,17 @@ fn encode_block_entity(p: (i32, i32, i32), e: &crate::containers::BlockEntity, b
|
||||
}
|
||||
Tag::compound(entries)
|
||||
}
|
||||
/// `block_entity_nbt::overlay`, then a container that now holds items loses its
|
||||
/// loot table (vanilla clears it when the loot is generated into the container).
|
||||
fn merge_block_entity(original: &Tag, loaded: &Tag, current: &Tag) -> Tag {
|
||||
let mut merged = crate::block_entity_nbt::overlay(original, loaded, current);
|
||||
let has_items = matches!(current.get("Items"), Some(Tag::List(items)) if !items.is_empty());
|
||||
if has_items && let Tag::Compound(map) = &mut merged {
|
||||
map.remove(&"LootTable".into());
|
||||
map.remove(&"LootTableSeed".into());
|
||||
}
|
||||
merged
|
||||
}
|
||||
fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers::BlockEntity)> {
|
||||
use crate::containers::BlockEntity;
|
||||
let int = |k: &str| number(tag.get(k)).map(|v| v as i32);
|
||||
@@ -459,7 +473,7 @@ fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers:
|
||||
f.lit_remaining = int("lit_time_remaining").unwrap_or(0);
|
||||
f.lit_total = int("lit_total_time").unwrap_or(0);
|
||||
f.cooking_timer = int("cooking_time_spent").unwrap_or(0);
|
||||
f.cooking_total = int("cooking_total_time").unwrap_or(200);
|
||||
f.cooking_total = int("cooking_total_time").unwrap_or(0);
|
||||
if let Some(speed) = float(tag.get("speed_multiplier")) {
|
||||
f.speed = speed as f32;
|
||||
}
|
||||
@@ -684,7 +698,27 @@ pub fn encode_with(
|
||||
let mut block_entities: Vec<Tag> = chunk
|
||||
.block_entities
|
||||
.iter()
|
||||
.map(|(p, e)| encode_block_entity(*p, e, &block_name(p)))
|
||||
.map(|(p, e)| {
|
||||
let block = block_name(p);
|
||||
let current = encode_block_entity(*p, e, &block);
|
||||
let source = extras
|
||||
.and_then(|x| x.block_entity_sources.iter().find(|(q, _)| q == p))
|
||||
.map(|(_, tag)| tag)
|
||||
.filter(|tag| {
|
||||
crate::block_entity_nbt::id(tag) == crate::block_entity_nbt::id(¤t)
|
||||
});
|
||||
let merged = match source {
|
||||
Some(original) => {
|
||||
let loaded = decode_block_entity(original).map_or_else(
|
||||
|| current.clone(),
|
||||
|(q, e)| encode_block_entity(q, &e, &block),
|
||||
);
|
||||
merge_block_entity(original, &loaded, ¤t)
|
||||
}
|
||||
None => current,
|
||||
};
|
||||
crate::block_entity_nbt::canonical(&merged).unwrap_or(merged)
|
||||
})
|
||||
.collect();
|
||||
if let Some(extras) = extras {
|
||||
for raw in &extras.block_entities {
|
||||
@@ -697,7 +731,8 @@ pub fn encode_with(
|
||||
let still_there =
|
||||
tables.is_none_or(|t| t.block_entity.get(state).copied().unwrap_or(true));
|
||||
if still_there && !chunk.block_entities.iter().any(|(p, _)| *p == pos) {
|
||||
block_entities.push(raw.clone());
|
||||
block_entities
|
||||
.push(crate::block_entity_nbt::canonical(raw).unwrap_or_else(|| raw.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -894,7 +929,10 @@ pub fn decode_with(
|
||||
if let Some(Tag::List(entities)) = tag.get("block_entities") {
|
||||
for e in entities {
|
||||
match decode_block_entity(e) {
|
||||
Some(be) => chunk.block_entities.push(be),
|
||||
Some(be) => {
|
||||
extras.block_entity_sources.push((be.0, e.clone()));
|
||||
chunk.block_entities.push(be);
|
||||
}
|
||||
None => extras.block_entities.push(e.clone()),
|
||||
}
|
||||
}
|
||||
@@ -1723,8 +1761,10 @@ impl Level {
|
||||
}),
|
||||
),
|
||||
];
|
||||
if !equipment.is_empty() {
|
||||
entries.push(("equipment", Tag::compound(equipment)));
|
||||
// Vanilla writes `equipment` even when empty and omits an empty effect list.
|
||||
entries.push(("equipment", Tag::compound(equipment)));
|
||||
if self.active_effects.is_empty() {
|
||||
entries.retain(|(key, _)| *key != "active_effects");
|
||||
}
|
||||
if let Some(pos) = self.position {
|
||||
entries.push(("Pos", doubles(&pos)));
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
"""The test arrangement of tools/test_save_roundtrip_parity.py: one of every vanilla 26.3
|
||||
block entity type and many entity types, placed by console commands in a flat world.
|
||||
|
||||
BLOCK_ENTITIES entries are (block id and states, block entity NBT in SNBT or '').
|
||||
ENTITIES entries are (entity id, extra SNBT fields). Entities get deterministic UUIDs
|
||||
and NoAI/Silent/NoGravity so that the two saves can be matched one to one.
|
||||
"""
|
||||
|
||||
SWORD = ('{Slot:0b,id:"minecraft:iron_sword",count:1,components:{"minecraft:damage":7,'
|
||||
'"minecraft:custom_name":{text:"Blade",italic:0b},"minecraft:enchantments":{"minecraft:sharpness":3}}}')
|
||||
STONE = '{Slot:5b,id:"minecraft:stone",count:64}'
|
||||
APPLE = '{Slot:1b,id:"minecraft:apple",count:3}'
|
||||
NAME = 'CustomName:{text:"Named",color:"red"}'
|
||||
LOCKED = 'lock:{components:{"minecraft:custom_name":{text:"Key"}}}'
|
||||
LOOT = 'LootTable:"minecraft:chests/simple_dungeon",LootTableSeed:12345L'
|
||||
SIGN_SIDE = ('{messages:[{text:"one",color:"red"},"two",{text:"three",bold:1b},""],color:"blue",'
|
||||
'has_glowing_text:1b}')
|
||||
BOOK = ('{id:"minecraft:written_book",count:1,components:{"minecraft:written_book_content":'
|
||||
'{title:{raw:"Title"},author:"Author",pages:[{raw:"page one"},{raw:"page two"}]}}}')
|
||||
|
||||
FULL = [
|
||||
('chest', f'{{Items:[{SWORD},{STONE}],{NAME},{LOCKED}}}'),
|
||||
('chest[facing=east]', f'{{{LOOT}}}'),
|
||||
('trapped_chest', f'{{Items:[{SWORD},{APPLE}]}}'),
|
||||
('ender_chest', ''),
|
||||
('barrel[facing=up]', f'{{Items:[{STONE}],{NAME}}}'),
|
||||
('barrel[facing=up]', f'{{{LOOT}}}'),
|
||||
('dispenser[facing=up]', f'{{Items:[{APPLE}],{NAME}}}'),
|
||||
('dropper[facing=up]', f'{{Items:[{STONE}]}}'),
|
||||
('hopper', f'{{Items:[{STONE}],TransferCooldown:3,{NAME}}}'),
|
||||
('red_shulker_box', f'{{Items:[{SWORD},{STONE}],{NAME}}}'),
|
||||
('furnace', f'{{Items:[{{Slot:0b,id:"minecraft:iron_ore",count:5}},{{Slot:1b,id:"minecraft:coal",count:2}}],'
|
||||
'lit_time_remaining:100,lit_total_time:200,cooking_time_spent:50,cooking_total_time:200,'
|
||||
'speed_multiplier:1.5f,RecipesUsed:{"minecraft:iron_ingot":2}}'),
|
||||
('blast_furnace', '{Items:[{Slot:2b,id:"minecraft:iron_ingot",count:9}],RecipesUsed:{"minecraft:copper_ingot":4}}'),
|
||||
('smoker', f'{{{NAME},cooking_total_time:100}}'),
|
||||
('brewing_stand', f'{{Items:[{{Slot:0b,id:"minecraft:potion",count:1}},{{Slot:3b,id:"minecraft:nether_wart",count:2}},'
|
||||
'{Slot:4b,id:"minecraft:blaze_powder",count:1}],BrewTime:200,Fuel:10}'),
|
||||
('campfire', '{Items:[{Slot:0b,id:"minecraft:beef",count:1}],CookingTimes:[I;10,0,0,0],'
|
||||
'CookingTotalTimes:[I;600,0,0,0]}'),
|
||||
('soul_campfire', ''),
|
||||
('chiseled_bookshelf', '{Items:[{Slot:0b,id:"minecraft:book",count:1},{Slot:3b,id:"minecraft:book",count:1}],'
|
||||
'last_interacted_slot:3}'),
|
||||
('oak_shelf', f'{{Items:[{APPLE}],align_items_to_bottom:1b}}'),
|
||||
('crafter', f'{{crafting_ticks_remaining:3,disabled_slots:[I;2,4],triggered:1,Items:[{APPLE}]}}'),
|
||||
('decorated_pot', '{sherds:{back:"minecraft:brick",left:"minecraft:brick",right:"minecraft:angler_pottery_sherd",'
|
||||
'front:{id:"minecraft:brick"}},item:{id:"minecraft:diamond",count:2}}'),
|
||||
('suspicious_sand', '{item:{id:"minecraft:diamond",count:1}}'),
|
||||
('suspicious_gravel', f'{{{LOOT}}}'),
|
||||
('jukebox', '{RecordItem:{id:"minecraft:music_disc_13",count:1},ticks_since_song_started:5L}'),
|
||||
('lectern', f'{{Book:{BOOK},Page:1}}'),
|
||||
('beacon', f'{{Levels:0,primary_effect:"minecraft:speed",secondary_effect:"minecraft:regeneration",{NAME}}}'),
|
||||
('bell', ''),
|
||||
('white_banner', '{patterns:[{pattern:"minecraft:stripe_top",color:"red"},'
|
||||
'{pattern:"minecraft:creeper",color:"black"}],CustomName:{text:"Flag"}}'),
|
||||
('skeleton_skull', '{note_block_sound:"minecraft:block.note_block.imitate.skeleton",custom_name:{text:"Skully"}}'),
|
||||
('creeper_head', ''),
|
||||
('oak_sign', f'{{front_text:{SIGN_SIDE},back_text:{{messages:["a","b","c","d"],color:"black",has_glowing_text:0b}},is_waxed:1b}}'),
|
||||
('oak_hanging_sign', f'{{front_text:{SIGN_SIDE}}}'),
|
||||
('spawner', '{SpawnData:{entity:{id:"minecraft:pig"}},Delay:20s,MinSpawnDelay:200s,MaxSpawnDelay:800s,'
|
||||
'SpawnCount:4s,MaxNearbyEntities:6s,RequiredPlayerRange:16s,SpawnRange:4s}'),
|
||||
('trial_spawner', '{required_player_range:10,target_cooldown_length:1200}'),
|
||||
('vault', '{config:{loot_table:"minecraft:chests/trial_chambers/reward",key_item:{id:"minecraft:trial_key",count:1}},'
|
||||
'server_data:{rewarded_players:[[I;1,2,3,4]],total_ejections_needed:2}}'),
|
||||
('command_block', f'{{Command:"say hi",auto:1b,TrackOutput:0b,{NAME}}}'),
|
||||
('chain_command_block', '{Command:"say chain",conditionMet:1b}'),
|
||||
('comparator', '{OutputSignal:5}'),
|
||||
('conduit', ''),
|
||||
('daylight_detector', ''),
|
||||
('enchanting_table', f'{{{NAME}}}'),
|
||||
('end_gateway', '{Age:20L,exit_portal:[I;0,70,0],ExactTeleport:1b}'),
|
||||
('end_portal', ''),
|
||||
('jigsaw', '{name:"minecraft:a",target:"minecraft:b",pool:"minecraft:empty",final_state:"minecraft:stone",'
|
||||
'joint:"aligned",placement_priority:1,selection_priority:2}'),
|
||||
('structure_block', '{name:"test:s",author:"me",metadata:"meta",mode:"SAVE",posX:1,posY:2,posZ:3,sizeX:4,sizeY:5,'
|
||||
'sizeZ:6,rotation:"CLOCKWISE_90",mirror:"LEFT_RIGHT",ignoreEntities:0b,integrity:0.5f,seed:9L}'),
|
||||
('sculk_sensor', '{last_vibration_frequency:3}'),
|
||||
('calibrated_sculk_sensor', ''),
|
||||
('sculk_catalyst', ''),
|
||||
('sculk_shrieker[can_summon=true]', '{warning_level:2}'),
|
||||
('creaking_heart', ''),
|
||||
('beehive', '{bees:[{entity_data:{id:"minecraft:bee"},ticks_in_hive:100,min_ticks_in_hive:2400}],flower_pos:[I;1,2,3]}'),
|
||||
('bee_nest', ''),
|
||||
('test_block', '{mode:"accept",message:"hello",powered:1b}'),
|
||||
('test_instance_block', ''),
|
||||
('copper_golem_statue', ''),
|
||||
('potent_sulfur', '{countdown:7}'),
|
||||
]
|
||||
|
||||
# The same blocks with no NBT: vanilla fills every field with its default, which
|
||||
# Horizon must reproduce when it loads and saves them.
|
||||
BLOCK_ENTITIES = FULL + [(block, '') for block, nbt in FULL if nbt]
|
||||
|
||||
# (entity id, extra SNBT). Each also gets UUID, Pos, NoAI, Silent, a name and a tag.
|
||||
ENTITIES = [
|
||||
('pig', ''), ('cow', 'Age:-100'), ('sheep', 'Color:5b,Sheared:1b'), ('chicken', 'EggLayTime:1000'),
|
||||
('horse', 'Tame:1b,Variant:258,Temper:30'), ('wolf', 'Health:12f,CollarColor:3b'), ('cat', ''),
|
||||
('villager', 'VillagerData:{profession:"minecraft:farmer",level:2,type:"minecraft:plains"},Xp:15,'
|
||||
'Offers:{Recipes:[{buy:{id:"minecraft:wheat",count:20},sell:{id:"minecraft:emerald",count:1},'
|
||||
'maxUses:16,uses:2,xp:2,priceMultiplier:0.05f,demand:0}]}'),
|
||||
('zombie', 'IsBaby:1b,ArmorItems:[{},{},{},{id:"minecraft:iron_helmet",count:1}],HandItems:[{id:"minecraft:iron_sword",count:1},{}]'),
|
||||
('skeleton', ''), ('creeper', 'powered:1b,Fuse:20s'), ('spider', ''), ('enderman', ''),
|
||||
('bee', 'HasNectar:1b,Anger:0'), ('axolotl', 'Variant:2'), ('frog', ''), ('fox', 'Type:"snow"'),
|
||||
('parrot', 'Variant:3'), ('llama', 'Strength:3,Variant:1'), ('rabbit', 'RabbitType:2'),
|
||||
('allay', ''), ('iron_golem', 'PlayerCreated:1b'), ('snow_golem', 'Pumpkin:0b'),
|
||||
('armor_stand', 'ShowArms:1b,Small:1b,Pose:{Head:[10f,20f,30f]},ArmorItems:[{},{},{},{id:"minecraft:diamond_helmet",count:1}]'),
|
||||
('item', 'Item:{id:"minecraft:diamond",count:5},Age:100,PickupDelay:20s'),
|
||||
('experience_orb', 'Value:7s,Age:30s'),
|
||||
('item_frame', 'Facing:3b,Item:{id:"minecraft:apple",count:1},ItemRotation:2b,ItemDropChance:0.5f'),
|
||||
('glow_item_frame', 'Facing:2b'),
|
||||
('painting', 'Facing:3b,variant:"minecraft:kebab"'),
|
||||
('arrow', 'inGround:1b,life:5s,damage:2.0d'),
|
||||
('falling_block', 'BlockState:{id:"minecraft:anvil",properties:{facing:"east"}},Time:3,HurtEntities:1b,FallHurtAmount:2f'),
|
||||
('tnt', 'fuse:50s,block_state:{id:"minecraft:tnt",properties:{unstable:"true"}}'),
|
||||
('oak_boat', ''), ('chest_boat', ''),
|
||||
('minecart', ''), ('chest_minecart', 'Items:[{Slot:0b,id:"minecraft:apple",count:2}]'),
|
||||
('hopper_minecart', ''), ('furnace_minecart', 'Fuel:100s'), ('tnt_minecart', 'TNTFuse:-1'),
|
||||
('text_display', 'text:{text:"Hello"},billboard:"center",line_width:100,background:1234,alignment:"left"'),
|
||||
('block_display', 'block_state:{id:"minecraft:oak_stairs",properties:{facing:"east"}},transformation:{translation:[1f,0f,0f],left_rotation:[0f,0f,0f,1f],right_rotation:[0f,0f,0f,1f],scale:[2f,2f,2f]}'),
|
||||
('item_display', 'item:{id:"minecraft:stone",count:1},item_display:"head"'),
|
||||
('interaction', 'width:2f,height:3f,response:1b'),
|
||||
('marker', 'data:{a:1,b:"two"}'),
|
||||
('area_effect_cloud', 'Radius:3f,Duration:200,Particle:{type:"minecraft:entity_effect",color:16711680},Potion:"minecraft:poison"'),
|
||||
('end_crystal', 'ShowBottom:0b'),
|
||||
('snowball', ''), ('ender_pearl', ''), ('small_fireball', ''), ('wither_skull', 'dangerous:1b'),
|
||||
('glow_squid', ''), ('turtle', 'HomePosX:1,HomePosY:-60,HomePosZ:1,HasEgg:1b'),
|
||||
('ghast', ''), ('piglin', ''), ('hoglin', ''), ('strider', 'Saddle:1b'), ('warden', ''),
|
||||
('trader_llama', ''), ('mooshroom', 'Type:"brown"'), ('panda', 'MainGene:"lazy"'),
|
||||
('copper_golem', ''), ('creaking', ''),
|
||||
]
|
||||
@@ -0,0 +1,555 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Save-format differential test: vanilla 26.3 world -> Horizon -> vanilla.
|
||||
|
||||
1. A headless vanilla server (flat world, offline) is filled with one of every block
|
||||
entity type and many entity types (tools/save_roundtrip_scene.py), a bot player
|
||||
joins so a player file exists, and the world is saved (world V0).
|
||||
2. Horizon opens a copy of V0 under a virtual screen, rewrites every resident chunk and
|
||||
entity section (`save-all`) and quits; this is world H.
|
||||
3. The NBT of V0 and H is compared: chunks (block states and biomes decoded, so palette
|
||||
order does not matter), block entities, entities, heightmaps, light, ticks, structure
|
||||
data, level.dat, data/*.dat, players/data/*.dat. Differences must be explained by
|
||||
IGNORED below (vanilla-irrelevant: elapsed time, ordering, derived data) or the test
|
||||
fails.
|
||||
4. The vanilla server loads H: its log must show no errors, upgrades or DataFixer
|
||||
activity, and `data get` of every block entity and entity must print the same
|
||||
data as the server that loaded V0 (so vanilla reads Horizon's files as it reads its own).
|
||||
|
||||
test_save_roundtrip_parity.py [--keep] [--skip-horizon] [--bin-dir DIR] [--stage gen|resave|compare|all]
|
||||
[--world DIR] (compare an existing V0/H pair: --vanilla DIR --horizon DIR)
|
||||
|
||||
Run files stay under .local/save-roundtrip/run. The Horizon client runs on the virtual
|
||||
screen (`vscreen`), never on a real monitor, with a null ALSA device.
|
||||
"""
|
||||
import argparse
|
||||
import collections
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
import anvil_dump # noqa: E402
|
||||
import save_roundtrip_scene as scene # noqa: E402
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
LOCAL = ROOT / '.local'
|
||||
RESOURCES = LOCAL / 'horizon-client' / '26.3'
|
||||
SERVER = LOCAL / 'minecraft' / '26.3' / 'jars' / 'server.jar'
|
||||
RUN = LOCAL / 'save-roundtrip' / 'run'
|
||||
PORT = 25000 + (os.getpid() % 5000) + 1000
|
||||
|
||||
# Differences that do not change what vanilla reads: path pattern (list indices as [])
|
||||
# -> reason. Anything else is a failure.
|
||||
IGNORED = {
|
||||
r'^chunk\.LastUpdate$': 'game time of the save (Horizon saves at its own time)',
|
||||
r'^chunk\.InhabitedTime$': 'ticks the chunk was loaded; differs by the time Horizon ran',
|
||||
r'^chunk\.isLightOn$': 'light is recomputed by vanilla when absent',
|
||||
# Horizon keeps simulating between loading and saving (it needs a few seconds of
|
||||
# chunk generation first), so counters and positions of live things move on.
|
||||
r'^chunk\.block_entities\.\(2, -60, 5\)\.(cooking_time_spent|lit_time_remaining|Items\[\]\.count)$':
|
||||
'the lit furnace keeps smelting',
|
||||
r'^chunk\.block_entities\..*\.(ticks_since_song_started|Age|BrewTime|warning_level|ticks_in_hive)$':
|
||||
'block entity timers advance',
|
||||
r'^entity\.(Age|Motion\[\]|Rotation\[\]|Pos\[\]|OnGround|HurtTime|Health|TimeInOverworld|fall_distance|'
|
||||
r'wasOnGround|Fire|Air|DeathTime|Fuse|life|inGround|ticks_in_ground)$': 'entity simulation advances',
|
||||
r'^level\.dat\.Data\.(Time|LastPlayed)$': 'clock and wall time of the save',
|
||||
r'^level\.dat\.Data\.(ServerBrands.*|singleplayer_uuid)$': 'a client world names its brand and its player',
|
||||
r'^players/data/.*\.dat\.(Health|Pos\[\]|foodSaturationLevel|foodLevel|foodExhaustionLevel|Motion\[\]|OnGround)$':
|
||||
'the player keeps simulating',
|
||||
}
|
||||
|
||||
# Real differences that are known and not fixed yet: (pattern -> what is wrong). They are
|
||||
# reported, and do not fail the run, so that new differences still do.
|
||||
KNOWN_GAPS = {
|
||||
r'^chunk\.sections\.blocks|^chunk\.Heightmaps': 'a falling block entity with NoGravity lands anyway',
|
||||
r'^entity \(28672, 0, 0, 11\)': 'the spider is gone from the Horizon save (not yet explained)',
|
||||
r'^entity \(28672, 0, 0, 30\) minecraft:falling_block': 'the NoGravity falling block landed (above)',
|
||||
r'^entity\.attributes': 'Horizon derives the attribute list of a horse from its own model',
|
||||
r'^entity\.Brain\.memories': 'Horizon gives villagers memories (meeting point) that vanilla had not formed yet',
|
||||
}
|
||||
|
||||
SUSPICIOUS = re.compile(
|
||||
r"(/ERROR\]|/WARN\]|login failed|Exception|Couldn't load|Failed to|Skipping|Invalid|Unknown|corrupt|"
|
||||
r'Recreating|upgrad|DataFix|out of bounds|missing|Mismatch|Discarding)', re.IGNORECASE)
|
||||
BENIGN = re.compile(r'(Unpacking |OFFLINE/INSECURE|online-mode|authenticate usernames|hackers|'
|
||||
r'Failed to load properties|Failed to load eula|Unknown or incomplete command|'
|
||||
r'Can.t keep up|Ignoring unknown|^WARNING: |No key layers|has the following)')
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- vanilla server
|
||||
|
||||
class Vanilla:
|
||||
def __init__(self, directory, port=PORT, world='world', extra=None):
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
(directory / 'eula.txt').write_text('eula=true\n')
|
||||
props = {
|
||||
'server-port': port, 'online-mode': 'false', 'white-list': 'false', 'enforce-whitelist': 'false',
|
||||
'level-name': world, 'level-seed': '4242', 'spawn-protection': '0', 'view-distance': '4',
|
||||
'simulation-distance': '4', 'sync-chunk-writes': 'true', 'enable-query': 'false',
|
||||
'enable-rcon': 'false', 'management-server-enabled': 'false', 'pause-when-empty-seconds': '-1',
|
||||
'level-type': 'minecraft:flat', 'generate-structures': 'false', 'spawn-monsters': 'false',
|
||||
'spawn-npcs': 'false', 'spawn-animals': 'false', 'max-tick-time': '-1',
|
||||
}
|
||||
props.update(extra or {})
|
||||
(directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()))
|
||||
self.port = port
|
||||
self.process = subprocess.Popen(
|
||||
['java', '-Xmx2G', '-jar', str(SERVER), '--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 (', 240)
|
||||
|
||||
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'vanilla: no "{pattern}" in {timeout}s; last lines: {seen[-5:]}')
|
||||
|
||||
def run(self, command, timeout=60):
|
||||
"""Run a console command; return the log lines it produced."""
|
||||
self.counter += 1
|
||||
marker = f'@@{self.counter}@@'
|
||||
self.process.stdin.write(command + '\n' + 'say ' + marker + '\n')
|
||||
self.process.stdin.flush()
|
||||
out = self.wait(marker, timeout)
|
||||
return [re.sub(r'^\[[0-9:]+\] \[[^\]]*\]: (System chat: )?', '', l) for l in out[:-1]]
|
||||
|
||||
def stop(self):
|
||||
try:
|
||||
self.process.stdin.write('stop\n')
|
||||
self.process.stdin.flush()
|
||||
self.process.wait(90)
|
||||
except Exception:
|
||||
self.process.kill()
|
||||
|
||||
def problems(self):
|
||||
return [l for l in self.lines if SUSPICIOUS.search(l) and not BENIGN.search(l)]
|
||||
|
||||
|
||||
def command_failed(lines):
|
||||
return any(re.search(r'(Unknown|Expected|Incorrect|Invalid|Could not|Unable|Failed|That block|error)', l, re.I)
|
||||
for l in lines)
|
||||
|
||||
|
||||
UUID_BASE = 0x7000
|
||||
|
||||
|
||||
def entity_uuid(i):
|
||||
return f'[I;{UUID_BASE},0,0,{i}]'
|
||||
|
||||
|
||||
def entity_command(i, kind, extra):
|
||||
x = 3 + 3 * (i % 10)
|
||||
z = 36 + 3 * (i // 10)
|
||||
nbt = (f'UUID:{entity_uuid(i)},NoAI:1b,Silent:1b,NoGravity:1b,Invulnerable:1b,Tags:["rt"],'
|
||||
f'CustomName:{{text:"E{i}"}}' + (',' + extra if extra else ''))
|
||||
if kind in ('item_frame', 'glow_item_frame', 'painting'):
|
||||
nbt += f',block_pos:[I;{x},-60,{z}],Pos:[{x}.5d,-60.0d,{z}.5d]'
|
||||
return f'summon minecraft:{kind} {x} -60 {z} {{{nbt}}}'
|
||||
|
||||
|
||||
def block_position(i):
|
||||
return 2 + 3 * (i % 10), -60, 2 + 3 * (i // 10)
|
||||
|
||||
|
||||
def build_world(directory):
|
||||
"""Fill a fresh vanilla world; returns the failed scene commands."""
|
||||
server = Vanilla(directory)
|
||||
failed = []
|
||||
# A bot joins first so the player file exists and the chunks around spawn are loaded.
|
||||
server.run('forceload add 0 0 63 63')
|
||||
for rule in ('spawn_mobs', 'spawn_monsters', 'spawn_wandering_traders', 'spawn_patrols', 'spawn_phantoms',
|
||||
'spawn_wardens', 'raids'):
|
||||
server.run(f'gamerule {rule} false')
|
||||
server.run('gamerule random_tick_speed 0')
|
||||
time.sleep(3)
|
||||
server.run('fill 0 -61 0 47 -61 47 minecraft:stone')
|
||||
for i, (block, nbt) in enumerate(scene.BLOCK_ENTITIES):
|
||||
x, y, z = block_position(i)
|
||||
command = f'setblock {x} {y} {z} minecraft:{block}{nbt}'
|
||||
out = server.run(command)
|
||||
if command_failed(out):
|
||||
failed.append((command, out))
|
||||
for i, (kind, extra) in enumerate(scene.ENTITIES):
|
||||
command = entity_command(i, kind, extra)
|
||||
out = server.run(command)
|
||||
if command_failed(out):
|
||||
failed.append((command, out))
|
||||
server.run('time set 6000')
|
||||
server.run('gamerule advance_time false')
|
||||
server.run('gamerule advance_weather false')
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
import vanilla_login
|
||||
threading.Thread(target=lambda: vanilla_login.join('127.0.0.1', server.port, 'RoundTrip', 25), daemon=True).start()
|
||||
time.sleep(6)
|
||||
server.run('item replace entity RoundTrip container.0 with minecraft:iron_sword[damage=3]')
|
||||
server.run('give RoundTrip minecraft:apple 7')
|
||||
server.run('xp set RoundTrip 5 levels')
|
||||
time.sleep(20)
|
||||
server.run('save-all flush')
|
||||
time.sleep(2)
|
||||
server.stop()
|
||||
return failed, server.problems()
|
||||
|
||||
|
||||
def view_of_world(directory):
|
||||
"""Run vanilla on a world; return ({label: data text}, problem log lines)."""
|
||||
server = Vanilla(directory)
|
||||
views = {}
|
||||
try:
|
||||
server.run('forceload add 0 0 63 63')
|
||||
time.sleep(3)
|
||||
for i, (block, _) in enumerate(scene.BLOCK_ENTITIES):
|
||||
x, y, z = block_position(i)
|
||||
out = server.run(f'data get block {x} {y} {z}')
|
||||
views[f'block {i} {block}'] = ' '.join(out)
|
||||
for i, (kind, _) in enumerate(scene.ENTITIES):
|
||||
out = server.run(f'data get entity {entity_uuid_text(i)}')
|
||||
views[f'entity {i} {kind}'] = ' '.join(out)
|
||||
finally:
|
||||
server.stop()
|
||||
return views, server.problems()
|
||||
|
||||
|
||||
def entity_uuid_text(i):
|
||||
def u(v):
|
||||
return f'{v & 0xffffffff:08x}'
|
||||
return f'{u(UUID_BASE)}-{u(0)[:4]}-{u(0)[4:]}-{u(0)[:4]}-{u(0)[4:]}{u(i)}'
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- Horizon
|
||||
|
||||
def vscreen_env():
|
||||
text = subprocess.run(['vscreen', 'env'], capture_output=True, text=True, check=True).stdout
|
||||
env = dict(os.environ)
|
||||
env.pop('DISPLAY', None)
|
||||
for match in re.finditer(r'(\w+)=(\S+)', text.split('unset')[0]):
|
||||
env[match.group(1)] = match.group(2)
|
||||
return env
|
||||
|
||||
|
||||
def horizon_resave(source, destination, bin_dir):
|
||||
shutil.rmtree(destination, ignore_errors=True)
|
||||
saves = destination / 'saves'
|
||||
saves.mkdir(parents=True)
|
||||
shutil.copytree(source, saves / 'world')
|
||||
alsa = destination / 'null-asound.conf'
|
||||
alsa.write_text('pcm.!default { type null }\nctl.!default { type null }\n')
|
||||
env = vscreen_env()
|
||||
env['ALSA_CONFIG_PATH'] = str(alsa)
|
||||
socket = Path(tempfile.mkdtemp(prefix='hzrt')) / 'control.sock' # sockets need short paths
|
||||
ctl = [str(bin_dir / 'horizon-ctl'), '--socket', str(socket)]
|
||||
subprocess.run(['vscreen', 'up'], check=False, capture_output=True)
|
||||
launch = subprocess.run(
|
||||
ctl + ['launch', '--resources', str(RESOURCES), '--world', 'world', '--render-distance', '6'],
|
||||
cwd=destination, env=env, capture_output=True, text=True)
|
||||
print(launch.stdout.strip())
|
||||
if launch.returncode:
|
||||
sys.exit('horizon failed to launch: ' + launch.stdout + launch.stderr)
|
||||
steps = [['wait-ready', '240'], ['save-all'], ['quit']]
|
||||
for step in steps:
|
||||
result = subprocess.run(ctl + step, cwd=destination, env=env, capture_output=True, text=True)
|
||||
print(' '.join(step), '->', result.stdout.strip() or result.stderr.strip())
|
||||
if step[0] != 'quit' and result.returncode:
|
||||
sys.exit('horizon-ctl failed: ' + result.stdout)
|
||||
time.sleep(3)
|
||||
return saves / 'world'
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- NBT comparison
|
||||
|
||||
def unwrap(v):
|
||||
"""26.3 mixed-list wrappers ({"": value}) and plain python values."""
|
||||
if isinstance(v, dict):
|
||||
return {k: unwrap(x) for k, x in v.items()}
|
||||
if isinstance(v, list):
|
||||
return [unwrap(x[''] if isinstance(x, dict) and list(x) == [''] else x) for x in v]
|
||||
return v
|
||||
|
||||
|
||||
def short(v):
|
||||
s = json.dumps(v, default=str)
|
||||
return s if len(s) < 160 else s[:157] + '...'
|
||||
|
||||
|
||||
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)):
|
||||
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)))
|
||||
if all(not isinstance(x, (dict, list)) for x in a + b):
|
||||
out.append((path, a, b))
|
||||
return
|
||||
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 pattern_of(path):
|
||||
return re.sub(r'\[\d+\]', '[]', path)
|
||||
|
||||
|
||||
def ignored(path):
|
||||
for pattern, why in IGNORED.items():
|
||||
if re.search(pattern, pattern_of(path.split(' @')[0])):
|
||||
return why
|
||||
return None
|
||||
|
||||
|
||||
def state_text(entry):
|
||||
if isinstance(entry, str):
|
||||
return entry
|
||||
props = entry.get('properties', entry.get('Properties', {}))
|
||||
return entry.get('id', entry.get('Name')) + ('[' + ','.join(f'{k}={v}' for k, v in sorted(props.items())) + ']' if props else '')
|
||||
|
||||
|
||||
def unpack(longs, bits, count):
|
||||
per = 64 // bits
|
||||
out = []
|
||||
for word in longs:
|
||||
word &= (1 << 64) - 1
|
||||
for i in range(per):
|
||||
if len(out) == count:
|
||||
return out
|
||||
out.append((word >> (i * bits)) & ((1 << bits) - 1))
|
||||
return out
|
||||
|
||||
|
||||
def decode_container(container, count, minimum, default):
|
||||
palette = [state_text(e) for e in container.get('palette', [default])] if count == 4096 else \
|
||||
list(container.get('palette', [default]))
|
||||
if len(palette) == 1:
|
||||
return [palette[0]] * count
|
||||
bits = max(minimum, (len(palette) - 1).bit_length())
|
||||
return [palette[i] if i < len(palette) else None for i in unpack(container['data'][1], bits, count)]
|
||||
|
||||
|
||||
def normalize_chunk(root):
|
||||
root = unwrap(root)
|
||||
sections = {}
|
||||
for s in root.get('sections', []):
|
||||
y = s['Y'][1] if isinstance(s['Y'], tuple) else s['Y']
|
||||
blocks = decode_container(s['block_states'], 4096, 4, 'minecraft:air')
|
||||
biomes = decode_container(s['biomes'], 64, 1, 'minecraft:plains') if 'biomes' in s else None
|
||||
sections[y] = {'blocks': blocks, 'biomes': biomes, 'BlockLight': s.get('BlockLight'),
|
||||
'SkyLight': s.get('SkyLight')}
|
||||
out = {k: v for k, v in root.items() if k != 'sections'}
|
||||
out['block_entities'] = {(e['x'], e['y'], e['z']): e for e in root.get('block_entities', [])}
|
||||
for key in ('block_ticks', 'fluid_ticks'):
|
||||
out[key] = sorted(root.get(key, []), key=lambda t: json.dumps(t, sort_keys=True, default=str))
|
||||
return out, sections
|
||||
|
||||
|
||||
def compare_chunks(vanilla, horizon, report):
|
||||
for key in sorted(set(vanilla) | set(horizon)):
|
||||
if key not in horizon:
|
||||
report.append((f'chunk {key}', 'present', 'missing from Horizon'))
|
||||
continue
|
||||
if key not in vanilla:
|
||||
report.append((f'chunk {key}', 'missing from vanilla', 'present'))
|
||||
continue
|
||||
va, vs = normalize_chunk(vanilla[key])
|
||||
ha, hs = normalize_chunk(horizon[key])
|
||||
out = []
|
||||
diff(va, ha, 'chunk', out)
|
||||
for y in sorted(set(vs) | set(hs)):
|
||||
empty = {'blocks': ['minecraft:air'] * 4096, 'biomes': None, 'BlockLight': None, 'SkyLight': None}
|
||||
a, b = vs.get(y, empty), hs.get(y, empty)
|
||||
blocks = [(i, p, q) for i, (p, q) in enumerate(zip(a['blocks'], b['blocks'])) if p != q]
|
||||
if blocks:
|
||||
i, p, q = blocks[0]
|
||||
out.append((f'chunk.sections.blocks[{y}]', f'{len(blocks)} differ, first #{i} {p}', q))
|
||||
if a['biomes'] is not None and b['biomes'] is not None and a['biomes'] != b['biomes']:
|
||||
out.append((f'chunk.sections.biomes[{y}]', 'differ', ''))
|
||||
for light in ('BlockLight', 'SkyLight'):
|
||||
if a[light] != b[light]:
|
||||
out.append((f'chunk.sections.{light}[{y}]', None if a[light] is None else 'present',
|
||||
None if b[light] is None else 'present'))
|
||||
for path, p, q in out:
|
||||
report.append((f'{path} @{key}', p, q))
|
||||
|
||||
|
||||
def compare_entities(vanilla, horizon, report):
|
||||
def collect(regions):
|
||||
out = {}
|
||||
for key, root in regions.items():
|
||||
for e in unwrap(root).get('Entities', []):
|
||||
uuid = tuple(e['UUID'][1]) if 'UUID' in e else None
|
||||
out[uuid] = e
|
||||
return out
|
||||
a, b = collect(vanilla), collect(horizon)
|
||||
# Entities Horizon adds (chunk-generation spawns in the chunks vanilla never
|
||||
# generated) are not part of the scene.
|
||||
extra = [u for u in b if u not in a and not (u and u[0] == UUID_BASE)]
|
||||
if extra:
|
||||
print(f' note: Horizon saved {len(extra)} entities that are not in the scene (generated chunks)')
|
||||
for uuid in extra:
|
||||
del b[uuid]
|
||||
for uuid in sorted(set(a) | set(b), key=str):
|
||||
if uuid not in b:
|
||||
report.append((f'entity {uuid} {a[uuid].get("id")}', 'present', 'missing from Horizon'))
|
||||
elif uuid not in a:
|
||||
report.append((f'entity {uuid} {b[uuid].get("id")}', 'missing from vanilla', 'present'))
|
||||
else:
|
||||
out = []
|
||||
diff(a[uuid], b[uuid], 'entity', out)
|
||||
for path, p, q in out:
|
||||
report.append((f'{path} @{a[uuid].get("id")}#{uuid[-1]}', p, q))
|
||||
|
||||
|
||||
def load_regions(folder):
|
||||
out = {}
|
||||
if folder.is_dir():
|
||||
for f in sorted(folder.glob('r.*.*.mca')):
|
||||
out.update({(f.name, *k): v for k, v in anvil_dump.region_chunks(str(f)).items()})
|
||||
return out
|
||||
|
||||
|
||||
def compare_dat(name, a, b, report):
|
||||
out = []
|
||||
diff(unwrap(a), unwrap(b), name, out)
|
||||
report.extend(out)
|
||||
|
||||
|
||||
def dimension(world):
|
||||
d = world / 'dimensions' / 'minecraft' / 'overworld'
|
||||
return d if d.is_dir() else world
|
||||
|
||||
|
||||
def compare_worlds(vanilla, horizon):
|
||||
report = []
|
||||
va, ha = dimension(vanilla), dimension(horizon)
|
||||
compare_chunks(load_regions(va / 'region'), load_regions(ha / 'region'), report)
|
||||
compare_entities(load_regions(va / 'entities'), load_regions(ha / 'entities'), report)
|
||||
for rel in ['level.dat'] + sorted(
|
||||
str(p.relative_to(vanilla)) for pattern in ('data/**/*.dat', 'players/**/*.dat')
|
||||
for p in vanilla.glob(pattern)):
|
||||
a, b = vanilla / rel, horizon / rel
|
||||
if not b.exists():
|
||||
report.append((rel, 'present', 'missing from Horizon'))
|
||||
elif a.exists():
|
||||
compare_dat(rel, anvil_dump.read_dat(str(a)), anvil_dump.read_dat(str(b)), report)
|
||||
return report
|
||||
|
||||
|
||||
def summarize(report):
|
||||
groups = collections.OrderedDict()
|
||||
for path, a, b in report:
|
||||
why = ignored(path)
|
||||
groups.setdefault((pattern_of(path.split(' @')[0]), why), []).append((path, a, b))
|
||||
failures = 0
|
||||
for (pattern, why), items in groups.items():
|
||||
if why:
|
||||
print(f' ignored {pattern} x{len(items)} ({why})')
|
||||
continue
|
||||
gap = next((g for p, g in KNOWN_GAPS.items() if re.search(p, pattern)), None)
|
||||
if gap:
|
||||
print(f' KNOWN GAP {pattern} x{len(items)} ({gap})')
|
||||
continue
|
||||
failures += len(items)
|
||||
path, a, b = items[0]
|
||||
print(f' DIFF {pattern} x{len(items)}\n e.g. {path}\n vanilla {short(a)}\n horizon {short(b)}')
|
||||
return failures
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- main
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument('--keep', action='store_true')
|
||||
parser.add_argument('--bin-dir', default=str(ROOT / 'target' / 'fast'))
|
||||
parser.add_argument('--vanilla', help='an existing vanilla world folder (skips generation)')
|
||||
parser.add_argument('--horizon', help='an existing Horizon-saved world folder (skips the Horizon run)')
|
||||
parser.add_argument('--skip-vanilla-load', action='store_true')
|
||||
parser.add_argument('--gen-only', action='store_true', help='stop after building the vanilla world')
|
||||
args = parser.parse_args()
|
||||
RUN.mkdir(parents=True, exist_ok=True)
|
||||
failures = 0
|
||||
if args.vanilla:
|
||||
v0 = Path(args.vanilla)
|
||||
else:
|
||||
shutil.rmtree(RUN / 'v0', ignore_errors=True)
|
||||
failed, problems = build_world(RUN / 'v0')
|
||||
v0 = RUN / 'v0' / 'world'
|
||||
for command, out in failed:
|
||||
print('scene command failed:', command[:100], out[:2])
|
||||
for line in problems:
|
||||
print('vanilla log:', line)
|
||||
if args.gen_only:
|
||||
return
|
||||
if args.horizon:
|
||||
h = Path(args.horizon)
|
||||
else:
|
||||
h = horizon_resave(v0, RUN / 'h', Path(args.bin_dir))
|
||||
print('comparing', v0, 'with', h)
|
||||
report = compare_worlds(v0, h)
|
||||
failures += summarize(report)
|
||||
if not args.skip_vanilla_load:
|
||||
shutil.rmtree(RUN / 'v1', ignore_errors=True)
|
||||
shutil.copytree(h, RUN / 'v1' / 'world')
|
||||
views1, problems1 = view_of_world(RUN / 'v1')
|
||||
shutil.rmtree(RUN / 'v0view', ignore_errors=True)
|
||||
shutil.copytree(v0, RUN / 'v0view' / 'world')
|
||||
views0, problems0 = view_of_world(RUN / 'v0view')
|
||||
for line in problems1:
|
||||
if line not in problems0:
|
||||
print('vanilla log (Horizon save):', line)
|
||||
failures += 1
|
||||
volatile = re.compile(r'(cooking_time_spent|lit_time_remaining|ticks_since_song_started|Age|BrewTime|'
|
||||
r'ticks_in_hive|state_updating_resumes_at|LastExecution|last_vibration_frequency|warning_level): -?[0-9]+[a-zA-Z]?')
|
||||
for label in views0:
|
||||
if label.startswith('entity'):
|
||||
# Entity data moves with the simulation: only require vanilla to load it.
|
||||
if 'falling_block' in label:
|
||||
continue # lands in Horizon (known gap above)
|
||||
if 'No entity was found' in views1.get(label, '') and 'No entity was found' not in views0[label]:
|
||||
print(f'VANILLA cannot find {label} in the Horizon save')
|
||||
failures += 1
|
||||
continue
|
||||
if volatile.sub('', views0[label]) != volatile.sub('', views1.get(label, '')):
|
||||
print(f'VANILLA VIEW differs: {label}\n V0 {views0[label][:300]}\n H {str(views1.get(label))[:300]}')
|
||||
failures += 1
|
||||
print('save round-trip parity', 'FAILED' if failures else 'ok', f'({failures} unexplained differences)')
|
||||
if not args.keep and not failures:
|
||||
shutil.rmtree(RUN, ignore_errors=True)
|
||||
sys.exit(1 if failures else 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user