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:
Ludovic Marie
2026-10-10 03:50:14 +02:00
parent 2b618b1dfe
commit 7fa9a83542
10 changed files with 1866 additions and 45 deletions
@@ -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, &current);
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, &current);
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)));
}
}
+1 -1
View File
@@ -111,7 +111,7 @@ impl BlockEntity {
lit_remaining: 0,
lit_total: 0,
cooking_timer: 0,
cooking_total: 200,
cooking_total: 0,
speed: 1.0,
}));
}
+49 -11
View File
@@ -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)));
}
+38 -19
View File
@@ -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
+10
View File
@@ -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()?;
+74 -8
View File
@@ -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));
+1
View File
@@ -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;
+46 -6
View File
@@ -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(&current)
});
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, &current)
}
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)));