From 4d3f3b639183a2550713c818b862fd9d6b4d57bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Dr=C3=A4ger?= <20014538+sdraeger@users.noreply.github.com> Date: Sat, 24 Oct 2026 17:02:57 -0700 Subject: [PATCH 1/2] Load fireballs from NBT as live projectiles --- crates/horizon-client/src/entity_nbt.rs | 91 +++++++++++++++++++ crates/horizon-client/src/game/commands.rs | 39 ++++---- .../horizon-client/src/game/commands/data.rs | 5 +- .../horizon-client/src/game/lan_entities.rs | 15 +-- crates/horizon-client/src/game/persist.rs | 13 ++- crates/horizon-client/src/game/scoreboard.rs | 2 +- crates/horizon-client/src/projectiles.rs | 28 +++++- 7 files changed, 153 insertions(+), 40 deletions(-) diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 49ea776ba..3c014e59b 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -1155,6 +1155,62 @@ pub fn arrow_from_nbt( Some((p, tag.get("inBlockState").cloned())) } +/// `LargeFireball` and `SmallFireball`: `AbstractHurtingProjectile`'s +/// `acceleration_power`, `Projectile`'s owner and flags, `ExplosionPower`. +pub fn fireball_to_nbt( + p: &Projectile, + uuid: [i32; 4], + owner: Option<[i32; 4]>, + raw: Option<&Tag>, +) -> Option { + let power = match p.kind { + ProjectileKind::LargeFireball { power } => Some(power), + ProjectileKind::SmallFireball => None, + _ => return None, + }; + let base = Base { + id: p.kind.id(), + uuid, + position: p.position, + motion: p.velocity, + rotation: [p.yaw, p.pitch], + on_ground: false, + fall_distance: 0.0, + }; + let mut tag = base.tag(raw); + put( + &mut tag, + "acceleration_power", + Tag::Double(p.acceleration.to_bits()), + ); + put(&mut tag, "LeftOwner", Tag::Byte(i8::from(p.left_owner))); + if let Some(power) = power { + put(&mut tag, "ExplosionPower", Tag::Byte(power as i8)); + } + if let Some(owner) = owner { + put(&mut tag, "Owner", Tag::IntArray(owner.to_vec())); + } + Some(tag) +} + +pub fn fireball_from_nbt(tag: &Tag, player: Option<[i32; 4]>) -> Option { + let kind = match kind(tag)?.as_str() { + "minecraft:fireball" => ProjectileKind::LargeFireball { + power: number(tag.get("ExplosionPower")).unwrap_or(1) as i32, + }, + "minecraft:small_fireball" => ProjectileKind::SmallFireball, + _ => return None, + }; + let owner = uuid_from_tag(tag.get("Owner")); + let from_player = player.is_some() && owner == player; + let mut p = Projectile::new(kind, position(tag)?, from_player.then_some(Who::Player)); + p.velocity = motion(tag); + [p.yaw, p.pitch] = rotation(tag); + p.left_owner = flag(tag, "LeftOwner"); + p.acceleration = float(tag.get("acceleration_power")).unwrap_or(0.1); + Some(p) +} + /// `FallingBlockEntity`: BlockState, Time, DropItem, HurtEntities. #[allow(clippy::too_many_arguments)] pub fn falling_block_to_nbt( @@ -2139,3 +2195,38 @@ mod sheep_nbt_tests { assert_eq!((back.extra.color, back.extra.sheared), (14, true)); } } + +#[cfg(test)] +mod fireball_nbt_tests { + use super::*; + #[test] + fn a_fireball_round_trips_its_flight() { + let raw = Tag::compound([ + ("id", Tag::String("minecraft:fireball".into())), + ("Pos", doubles(&[1.0, 70.0, -2.5])), + ("Motion", doubles(&[0.0, -0.5, 1.0])), + ("ExplosionPower", Tag::Byte(4)), + ("acceleration_power", Tag::Double(0.0f64.to_bits())), + ]); + let p = fireball_from_nbt(&raw, None).unwrap(); + assert_eq!(p.kind, ProjectileKind::LargeFireball { power: 4 }); + assert_eq!((p.velocity.z, p.acceleration), (1.0, 0.0)); + let back = fireball_to_nbt(&p, [1, 2, 3, 4], None, None).unwrap(); + assert_eq!(number(back.get("ExplosionPower")), Some(4)); + assert_eq!(float(back.get("acceleration_power")), Some(0.0)); + assert_eq!( + read_doubles::<3>(back.get("Motion")), + Some([0.0, -0.5, 1.0]) + ); + // A default fireball keeps vanilla's power and acceleration. + let plain = Tag::compound([ + ("id", Tag::String("minecraft:fireball".into())), + ("Pos", doubles(&[0.0, 0.0, 0.0])), + ]); + let p = fireball_from_nbt(&plain, None).unwrap(); + assert_eq!( + (p.kind, p.acceleration), + (ProjectileKind::LargeFireball { power: 1 }, 0.1) + ); + } +} diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index a229cbdc9..3cb3fcbf5 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -42,7 +42,8 @@ pub(super) enum Target { Mob(u32), Item(u32), Orb(usize), - Arrow(usize), + /// By index in `creatures.projectiles`. + Projectile(usize), Stand(u32), Frame(u32), Painting(u32), @@ -327,7 +328,7 @@ impl Game<'_> { .orbs .get(i) .map(|o| [o.position.x, o.position.y, o.position.z]), - Target::Arrow(i) => self + Target::Projectile(i) => self .creatures .projectiles .get(i) @@ -382,7 +383,11 @@ impl Game<'_> { }), Target::Item(_) => "minecraft:item".into(), Target::Orb(_) => "minecraft:experience_orb".into(), - Target::Arrow(_) => "minecraft:arrow".into(), + Target::Projectile(i) => self + .creatures + .projectiles + .get(i) + .map_or_else(String::new, |p| p.kind.id().into()), Target::Stand(_) => "minecraft:armor_stand".into(), Target::Frame(id) => { if self.item_frames.iter().any(|f| f.id == id && f.glow) { @@ -503,6 +508,7 @@ impl Game<'_> { _ => {} } if s.kind != 's' { + use horizon_client::projectiles::ProjectileKind as K; candidates.extend(self.player_candidates(s)); candidates.extend( self.mobs @@ -517,8 +523,12 @@ impl Game<'_> { .projectiles .iter() .enumerate() - .filter(|(_, p)| p.arrow.is_some()) - .map(|(i, _)| Target::Arrow(i)), + // The kinds /data can write (entity_nbt). + .filter(|(_, p)| { + p.arrow.is_some() + || matches!(p.kind, K::LargeFireball { .. } | K::SmallFireball) + }) + .map(|(i, _)| Target::Projectile(i)), ); candidates.extend(self.armor_stands.iter().map(|a| Target::Stand(a.id))); candidates.extend(self.item_frames.iter().map(|f| Target::Frame(f.id))); @@ -1116,7 +1126,7 @@ impl Game<'_> { o.previous = v; } } - Target::Arrow(i) => { + Target::Projectile(i) => { if let Some(a) = self.creatures.projectiles.get_mut(i) { a.position = v; a.previous = v; @@ -1540,14 +1550,10 @@ impl Game<'_> { self.orbs[i].age = u32::MAX / 2; } } - Target::Arrow(i) => { - if let Some(a) = self - .creatures - .projectiles - .get_mut(i) - .and_then(|p| p.arrow.as_mut()) - { - a.removed = true; + // cmd_kill goes from the highest index down. + Target::Projectile(i) => { + if i < self.creatures.projectiles.len() { + self.creatures.projectiles.remove(i); } } Target::Stand(id) => self.armor_stands.retain(|a| a.id != id), @@ -1569,7 +1575,7 @@ impl Game<'_> { let mut ordered = targets.clone(); ordered.sort_by_key(|t| { std::cmp::Reverse(match t { - Target::Orb(i) | Target::Arrow(i) => *i, + Target::Orb(i) | Target::Projectile(i) => *i, _ => 0, }) }); @@ -1577,9 +1583,6 @@ impl Game<'_> { self.kill_target(t); } self.orbs.retain(|o| o.age < u32::MAX / 2); - self.creatures - .projectiles - .retain(|p| !p.arrow.as_ref().is_some_and(|a| a.removed)); let message = if targets.len() == 1 { Text::translate("commands.kill.success.single", &[names[0].clone()]) } else { diff --git a/crates/horizon-client/src/game/commands/data.rs b/crates/horizon-client/src/game/commands/data.rs index 89e3d6397..da6f6f3bc 100644 --- a/crates/horizon-client/src/game/commands/data.rs +++ b/crates/horizon-client/src/game/commands/data.rs @@ -113,7 +113,7 @@ impl Game<'_> { self.other_to_nbt(&o, uuid) } Target::Orb(i) => entity_nbt::orb_to_nbt(self.orbs.get(i)?, random_uuid()), - Target::Arrow(i) => { + Target::Projectile(i) => { let p = self.creatures.projectiles.get(i)?; let owner = if p.owner == Some(horizon_client::mobs::Who::Player) { player_uuid @@ -125,7 +125,8 @@ impl Game<'_> { .as_ref() .and_then(|a| a.last_state) .map(|s| self.state_to_nbt(s)); - entity_nbt::arrow_to_nbt(p, random_uuid(), owner, in_block, None)? + entity_nbt::arrow_to_nbt(p, random_uuid(), owner, in_block, None) + .or_else(|| entity_nbt::fireball_to_nbt(p, random_uuid(), owner, None))? } }) } diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index db0c3e8b2..dd272719a 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -321,20 +321,7 @@ impl Game<'_> { } for p in self.creatures.projectiles.iter().filter(|p| p.id != 0) { use horizon_client::projectiles::ProjectileKind as K; - let kind = match &p.kind { - K::Arrow { .. } => "minecraft:arrow", - K::Snowball => "minecraft:snowball", - K::Egg => "minecraft:egg", - K::EnderPearl => "minecraft:ender_pearl", - K::SmallFireball => "minecraft:small_fireball", - K::LargeFireball { .. } => "minecraft:fireball", - K::LlamaSpit => "minecraft:llama_spit", - K::WindCharge => "minecraft:wind_charge", - K::BreezeWindCharge => "minecraft:breeze_wind_charge", - K::ShulkerBullet { .. } => "minecraft:shulker_bullet", - K::WitherSkull { .. } => "minecraft:wither_skull", - K::Potion { .. } => "minecraft:splash_potion", - }; + let kind = p.kind.id(); let mut e = state(p.id, kind, at(p.position), p.yaw, false); e.pitch = p.pitch; e.velocity = at(p.velocity); diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index ad9d7fe7e..d3afbf6ce 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -156,6 +156,8 @@ impl Game<'_> { kind.as_str(), "minecraft:experience_orb" | "minecraft:arrow" + | "minecraft:fireball" + | "minecraft:small_fireball" | "minecraft:falling_block" | "minecraft:end_crystal" | "minecraft:ender_dragon" @@ -195,6 +197,11 @@ impl Game<'_> { self.creatures.projectiles.push(p); } } + "minecraft:fireball" | "minecraft:small_fireball" => { + if let Some(p) = entity_nbt::fireball_from_nbt(&tag, player_uuid) { + self.creatures.projectiles.push(p); + } + } "minecraft:falling_block" => { if let Some((state, position, velocity, age, fall_distance)) = entity_nbt::falling_block_from_nbt(&tag) @@ -642,13 +649,15 @@ impl Game<'_> { } else { None }; - let (uuid, raw) = anon(&self.persist, "minecraft:arrow", p.position); + let (uuid, raw) = anon(&self.persist, p.kind.id(), p.position); let in_block = p .arrow .as_ref() .and_then(|a| a.last_state) .map(|s| self.state_to_nbt(s)); - if let Some(tag) = entity_nbt::arrow_to_nbt(p, uuid, owner, in_block, raw.as_ref()) { + let tag = entity_nbt::arrow_to_nbt(p, uuid, owner, in_block, raw.as_ref()) + .or_else(|| entity_nbt::fireball_to_nbt(p, uuid, owner, raw.as_ref())); + if let Some(tag) = tag { out.entry(chunk_of(p.position)).or_default().push(tag); } } diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index 4905fb80f..227a09a6c 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -180,7 +180,7 @@ impl Game<'_> { | Target::Cart(id) | Target::Other(id) => self.persist.raw.get(&id), Target::Player => self.level.player_extra.as_ref(), - Target::Guest(_) | Target::Orb(_) | Target::Arrow(_) => None, + Target::Guest(_) | Target::Orb(_) | Target::Projectile(_) => None, }; match raw.and_then(|r| r.get("Tags")) { Some(Tag::List(l)) => l diff --git a/crates/horizon-client/src/projectiles.rs b/crates/horizon-client/src/projectiles.rs index 093482a08..8244da4d9 100644 --- a/crates/horizon-client/src/projectiles.rs +++ b/crates/horizon-client/src/projectiles.rs @@ -61,6 +61,25 @@ pub enum ProjectileKind { color: u32, }, } +impl ProjectileKind { + /// Its entity type. + pub fn id(&self) -> &'static str { + match self { + Self::Arrow { .. } => "minecraft:arrow", + Self::Snowball => "minecraft:snowball", + Self::Egg => "minecraft:egg", + Self::EnderPearl => "minecraft:ender_pearl", + Self::SmallFireball => "minecraft:small_fireball", + Self::LargeFireball { .. } => "minecraft:fireball", + Self::LlamaSpit => "minecraft:llama_spit", + Self::WindCharge => "minecraft:wind_charge", + Self::BreezeWindCharge => "minecraft:breeze_wind_charge", + Self::ShulkerBullet { .. } => "minecraft:shulker_bullet", + Self::WitherSkull { .. } => "minecraft:wither_skull", + Self::Potion { .. } => "minecraft:splash_potion", + } + } +} #[derive(Clone, Debug, PartialEq)] pub struct Projectile { /// The entity id a server tracks it by (0 until it is tracked). @@ -83,6 +102,8 @@ pub struct Projectile { pub bonus: crate::mobs::gear::ArrowBonus, /// Arrows' `AbstractArrow` state (ticked by `arrows::tick`). pub arrow: Option>, + /// `AbstractHurtingProjectile.accelerationPower`. + pub acceleration: f64, } /// What a projectile struck this tick. #[derive(Clone, Copy, Debug, PartialEq)] @@ -146,6 +167,7 @@ impl Projectile { stuck: false, left_owner: false, bonus: Default::default(), + acceleration: 0.1, arrow: kind_is_arrow.then(|| { // Entity.random: seeded unpredictably, the position stands in. Box::new(crate::arrows::ArrowState::new( @@ -571,9 +593,9 @@ impl Projectile { let v = self.velocity; let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1.0e-9); self.velocity = Vec3::new( - (v.x + v.x / len * 0.1) * inertia, - (v.y + v.y / len * 0.1) * inertia, - (v.z + v.z / len * 0.1) * inertia, + (v.x + v.x / len * self.acceleration) * inertia, + (v.y + v.y / len * self.acceleration) * inertia, + (v.z + v.z / len * self.acceleration) * inertia, ); } // Arrows tick with `arrows::tick`. From b7fcd446b6eed116764021d0c13e20e795a63b3c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 09:36:32 +0100 Subject: [PATCH 2/2] Fireballs: LeftOwner and HasBeenShot are saved as Projectile writes them Projectile.addAdditionalSaveData writes LeftOwner only when it is set and HasBeenShot always (true from the first tick on); fireball_to_nbt wrote LeftOwner:0b and no HasBeenShot. Same shape as arrow_to_nbt. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entity_nbt.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 3c014e59b..bf0764a6b 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -1183,7 +1183,17 @@ pub fn fireball_to_nbt( "acceleration_power", Tag::Double(p.acceleration.to_bits()), ); - put(&mut tag, "LeftOwner", Tag::Byte(i8::from(p.left_owner))); + // Projectile: LeftOwner only when set; HasBeenShot once it has ticked + // (a fireball saved before its first tick keeps what it was loaded with). + if let Tag::Compound(map) = &mut tag { + map.remove(&"LeftOwner".into()); + } + if p.left_owner { + put(&mut tag, "LeftOwner", Tag::Byte(1)); + } + if p.age > 0 || tag.get("HasBeenShot").is_none() { + put(&mut tag, "HasBeenShot", Tag::Byte(i8::from(p.age > 0))); + } if let Some(power) = power { put(&mut tag, "ExplosionPower", Tag::Byte(power as i8)); } @@ -2214,6 +2224,9 @@ mod fireball_nbt_tests { let back = fireball_to_nbt(&p, [1, 2, 3, 4], None, None).unwrap(); assert_eq!(number(back.get("ExplosionPower")), Some(4)); assert_eq!(float(back.get("acceleration_power")), Some(0.0)); + // Projectile.addAdditionalSaveData: no LeftOwner unless set, HasBeenShot always. + assert!(back.get("LeftOwner").is_none()); + assert_eq!(number(back.get("HasBeenShot")), Some(0)); assert_eq!( read_doubles::<3>(back.get("Motion")), Some([0.0, -0.5, 1.0])