diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index a42bc8ba6..d9cf20db5 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -918,6 +918,43 @@ pub fn item_entity_from_nbt(tag: &Tag, id: u32, bob_offset: f32) -> Option, +) -> Tag { + let base = Base { + id: "minecraft:ominous_item_spawner", + uuid, + position, + motion: Vec3::ZERO, + rotation: [0.0, 0.0], + on_ground: false, + fall_distance: 0.0, + }; + let mut tag = base.tag(raw); + put(&mut tag, "item", item_to_nbt(item)); + put( + &mut tag, + "spawn_item_after_ticks", + Tag::Long(spawn_item_after), + ); + tag +} +/// `OminousItemSpawner.readAdditionalSaveData`: the item, where it floats +/// and `spawn_item_after_ticks` (0 when absent). None without an item: +/// such a spawner would spawn nothing. +pub fn ominous_item_spawner_from_nbt(tag: &Tag) -> Option<(crate::items::ItemStack, Vec3, i64)> { + let item = tag.get("item").and_then(item_from_nbt)?; + let after = number(tag.get("spawn_item_after_ticks")).unwrap_or(0); + Some((item, position(tag)?, after)) +} + /// `ExperienceOrb`: Health, Age, Value, Count. pub fn orb_to_nbt(o: &ExperienceOrb, uuid: [i32; 4]) -> Tag { let base = Base { @@ -1505,6 +1542,38 @@ mod tests { use super::*; use crate::items::ItemStack; + /// The item, the spawn delay and the fields Horizon doesn't model come + /// back; a compound without an item is no spawner. + #[test] + fn ominous_item_spawner_round_trip() { + let item = ItemStack { + item: Arc::from("minecraft:ominous_trial_key"), + count: 1, + damage: 0, + extra: None, + }; + let raw = Tag::compound([("CustomName", string("kept"))]); + let at = Vec3::new(10.5, 64.25, -3.5); + let tag = ominous_item_spawner_to_nbt(&item, at, 97, [1, 2, 3, 4], Some(&raw)); + assert_eq!( + kind(&tag).as_deref(), + Some("minecraft:ominous_item_spawner") + ); + assert_eq!(uuid(&tag), Some([1, 2, 3, 4])); + assert!(matches!( + tag.get("spawn_item_after_ticks"), + Some(Tag::Long(97)) + )); + assert!(tag.get("CustomName").is_some()); + let (back, pos, after) = ominous_item_spawner_from_nbt(&tag).unwrap(); + assert_eq!( + (back.item.as_ref(), back.count), + ("minecraft:ominous_trial_key", 1) + ); + assert_eq!((pos, after), (at, 97)); + assert!(ominous_item_spawner_from_nbt(&raw).is_none()); + } + #[test] fn falling_block_round_trip_keeps_impact_distance_and_settings() { let state = Tag::compound([("Name", string("minecraft:damaged_anvil"))]); diff --git a/crates/horizon-client/src/game/ominous_item_spawner.rs b/crates/horizon-client/src/game/ominous_item_spawner.rs index 1470a3124..8084faf9e 100644 --- a/crates/horizon-client/src/game/ominous_item_spawner.rs +++ b/crates/horizon-client/src/game/ominous_item_spawner.rs @@ -1,8 +1,7 @@ //! `OminousItemSpawner`: the item an ominous trial spawner drops on its //! fight. It floats, growing and turning (`OminousItemSpawnerRenderer`), for //! 60 to 120 ticks, then falls as an item or is fired straight down as a -//! projectile (`spawnItem`). Not saved: a spawner left in an unloaded chunk -//! is lost. +//! projectile (`spawnItem`). Saved with its chunk's entities (persist.rs). use super::Game; use horizon_assets::item_model::DisplayContext; use horizon_client::{ @@ -22,10 +21,14 @@ const TICKS_SCALING: f32 = 50.0; const ROTATION_SPEED: f32 = 40.0; pub(super) struct OminousItemSpawner { - item: ItemStack, - position: [f64; 3], - spawn_item_after: i64, + pub(super) item: ItemStack, + pub(super) position: [f64; 3], + pub(super) spawn_item_after: i64, tick_count: i64, + /// Its UUID, and the compound it was loaded from (fields Horizon doesn't + /// model survive a save). + pub(super) uuid: [i32; 4], + pub(super) raw: Option>, } /// `ProjectileItem.DispenseConfig` of the items fired down: power, @@ -66,6 +69,26 @@ impl Game<'_> { position, spawn_item_after: i64::from(delay), tick_count: 0, + uuid: horizon_client::save::random_uuid(), + raw: None, + }); + } + /// A saved spawner, as `readAdditionalSaveData` leaves it: its tick + /// count starts again from 0. + pub(super) fn load_ominous_item_spawner(&mut self, tag: horizon_world::nbt::Tag) { + let Some((item, at, spawn_item_after)) = + horizon_client::entity_nbt::ominous_item_spawner_from_nbt(&tag) + else { + return; + }; + self.ominous_spawners.push(OminousItemSpawner { + item, + position: [at.x, at.y, at.z], + spawn_item_after, + tick_count: 0, + uuid: horizon_client::entity_nbt::uuid(&tag) + .unwrap_or_else(horizon_client::save::random_uuid), + raw: Some(Box::new(tag)), }); } /// Every item spawner's tick: `commonTick`'s count, `tickServer` (the diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 3e824e72e..eb30ab5ed 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -274,6 +274,7 @@ impl Game<'_> { self.carts.push(c); } } + "minecraft:ominous_item_spawner" => self.load_ominous_item_spawner(tag), "minecraft:ender_dragon" => { let seed = self.world.random.next_long(); if self.dragon.is_none() @@ -685,6 +686,19 @@ impl Game<'_> { .push(c.to_nbt(uuid, raw.as_ref())); } } + for s in &self.ominous_spawners { + let at = Vec3::new(s.position[0], s.position[1], s.position[2]); + if keys.contains(&chunk_of(at)) { + let tag = entity_nbt::ominous_item_spawner_to_nbt( + &s.item, + at, + s.spawn_item_after, + s.uuid, + s.raw.as_deref(), + ); + out.entry(chunk_of(at)).or_default().push(tag); + } + } for c in &self.carts { if keys.contains(&chunk_of(c.position)) { let uuid = self @@ -774,6 +788,8 @@ impl Game<'_> { keep }); self.crystals.retain(|c| !gone(c.position)); + self.ominous_spawners + .retain(|s| !gone(Vec3::new(s.position[0], s.position[1], s.position[2]))); if self.dragon.as_ref().is_some_and(|d| gone(d.position)) { self.dragon = None; }