Ominous item spawners are saved with their chunk
OminousItemSpawner.addAdditionalSaveData keeps its item and spawn_item_after_ticks; Horizon saved neither, so an ominous trial spawner's key or potion floating mid-wave vanished when the world closed or its chunk unloaded. They are now written with the chunk's entities (UUID and unmodelled fields kept), read back as live spawners whose tick count starts again from 0 (Entity.tickCount isn't saved), and leave the level with their chunk. Fixes #462 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -918,6 +918,43 @@ pub fn item_entity_from_nbt(tag: &Tag, id: u32, bob_offset: f32) -> Option<ItemE
|
||||
})
|
||||
}
|
||||
|
||||
/// `OminousItemSpawner.addAdditionalSaveData`: its item (left out when
|
||||
/// empty) and `spawn_item_after_ticks`. Its tick count is the entity's
|
||||
/// `tickCount`, which isn't saved: a reloaded spawner counts from 0 again.
|
||||
pub fn ominous_item_spawner_to_nbt(
|
||||
item: &crate::items::ItemStack,
|
||||
position: Vec3,
|
||||
spawn_item_after: i64,
|
||||
uuid: [i32; 4],
|
||||
raw: Option<&Tag>,
|
||||
) -> 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"))]);
|
||||
|
||||
@@ -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<Box<horizon_world::nbt::Tag>>,
|
||||
}
|
||||
|
||||
/// `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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user