Multiplayer: keep firework flight duration, boost gliding players

Two halves of FireworkRocketEntity on a server were missing:

- SetCreativeModeSlot encoded only damage, so a creative-picked rocket
  lost its minecraft:fireworks component (flight_duration and explosions
  gone); the wire codec also had no decoder for it or firework_explosion,
  so a stack carrying them failed to decode at all. The components now
  follow Fireworks.STREAM_CODEC/FireworkExplosion.STREAM_CODEC both ways
  (component ids 71/70).
- A remote rocket never read DATA_ATTACHED_TO_TARGET,
  DATA_ID_FIREWORKS_ITEM or DATA_SHOT_AT_ANGLE, so the server-attached
  boost rocket was an inert marker: attached_to now comes from
  EntityDataSerializers.OPTIONAL_UNSIGNED_INT (the id plus one), the item
  feeds the explosion list, and FireworkRocketEntity.tick's
  isFallFlying boost applies to the local player.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
socksprox
2026-10-25 01:18:53 +02:00
parent a4fa1a770d
commit 372f918e44
4 changed files with 207 additions and 3 deletions
+7 -2
View File
@@ -25,9 +25,12 @@ pub(super) struct Rocket {
pub(super) velocity: Vec3,
life: i32,
lifetime: i32,
explosions: Vec<FireworkExplosion>,
pub(super) explosions: Vec<FireworkExplosion>,
/// Boosting the player (attachedToEntity).
pub(super) attached: bool,
/// `DATA_ATTACHED_TO_TARGET`: the server entity the rocket follows;
/// boosting happens when it is the local player.
pub(super) attached_to: Option<u32>,
/// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on
/// the first block it hits.
pub(super) angled: bool,
@@ -48,6 +51,7 @@ impl Rocket {
lifetime: i32::MAX,
explosions,
attached: false,
attached_to: None,
angled: false,
item,
}
@@ -63,7 +67,7 @@ impl Rocket {
(self.position, self.velocity)
}
}
fn fireworks_of(stack: &ItemStack) -> (u8, Vec<FireworkExplosion>) {
pub(super) fn fireworks_of(stack: &ItemStack) -> (u8, Vec<FireworkExplosion>) {
// The item's default component: flight 1, no explosions.
stack
.data()
@@ -89,6 +93,7 @@ impl Game<'_> {
lifetime,
explosions,
attached,
attached_to: None,
angled: false,
item: stack.clone(),
}
+48
View File
@@ -596,6 +596,28 @@ impl Game<'_> {
out.tidy();
Some(out)
}
/// A wire `FireworkExplosion` as vanilla's NBT codec writes it
/// (`FireworkExplosion.Shape`'s BY_ID order).
fn fireworks_tag(e: &horizon_protocol::item::FireworkExplosion) -> Tag {
const SHAPES: [&str; 5] = ["small_ball", "large_ball", "star", "creeper", "burst"];
let ints = |c: &[i32]| Tag::List(c.iter().map(|c| Tag::Int(*c)).collect());
Tag::compound([
(
"shape",
Tag::String(
SHAPES
.get(e.shape.max(0) as usize)
.copied()
.unwrap_or("small_ball")
.into(),
),
),
("colors", ints(&e.colors)),
("fade_colors", ints(&e.fade)),
("has_trail", Tag::Byte(i8::from(e.trail))),
("has_twinkle", Tag::Byte(i8::from(e.twinkle))),
])
}
/// The item's patched components as vanilla's `components` compound.
fn protocol_components(&self, s: &Stack) -> Option<Tag> {
use horizon_protocol::item::{Component, component};
@@ -633,6 +655,14 @@ impl Game<'_> {
("pages", Tag::List(b.pages.clone())),
("resolved", Tag::Byte(i8::from(b.resolved))),
]),
Component::Fireworks { flight, explosions } => Tag::compound([
("flight_duration", Tag::Byte(*flight as i8)),
(
"explosions",
Tag::List(explosions.iter().map(Self::fireworks_tag).collect()),
),
]),
Component::FireworkExplosion(e) => Self::fireworks_tag(e),
Component::Enchantments(list) => {
// Enchantments are a synced (data-driven) registry: the
// ids index the server's RegistryData, not the builtin
@@ -719,6 +749,24 @@ impl Game<'_> {
horizon_protocol::item::Component::Int(s.damage as i32),
));
}
if let Some((flight, explosions)) = s.data().and_then(|d| d.fireworks.clone()) {
stack.added.push((
horizon_protocol::item::component::FIREWORKS,
horizon_protocol::item::Component::Fireworks {
flight: i32::from(flight),
explosions: explosions
.iter()
.map(|e| horizon_protocol::item::FireworkExplosion {
shape: e.shape as i32,
colors: e.colors.iter().map(|c| *c as i32).collect(),
fade: e.fade.iter().map(|c| *c as i32).collect(),
trail: e.trail,
twinkle: e.twinkle,
})
.collect(),
},
));
}
Some(stack)
}
/// A line in the connection's chat, with its styles, click and hover
@@ -2,7 +2,7 @@
//! end crystals on a server's client (`ClientPacketListener.handleAddEntity`
//! and `handleSetEntityData`): the server simulates them, the client draws
//! them where it says and interpolates their moves.
use super::{FallingBlock, Game};
use super::{FallingBlock, Game, fireworks::fireworks_of, view_vector};
use horizon_client::projectiles::{Projectile, ProjectileKind};
use horizon_core::{geometry::Vec3, position::BlockPos};
use horizon_protocol::entity_data::Value;
@@ -117,6 +117,37 @@ impl Game<'_> {
pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) {
let id = id as u32;
self.remote_hook_data(id, values);
if self.rockets.iter().any(|r| r.id == id) {
for (index, _, value) in values {
match (index, value) {
// FireworkRocketEntity.DATA_ID_FIREWORKS_ITEM: its
// explosions for `createFireworks`.
(8, Value::Item(stack)) => {
if let Some(stack) = self.to_stack(stack)
&& let Some(r) = self.rockets.iter_mut().find(|r| r.id == id)
{
let (_, explosions) = fireworks_of(&stack);
r.explosions = explosions;
r.item = stack;
}
}
// DATA_ATTACHED_TO_TARGET (OptionalInt: the id plus 1).
(9, Value::Id(v)) => {
let v = (*v > 0).then(|| (*v - 1) as u32);
if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) {
r.attached_to = v;
}
}
// DATA_SHOT_AT_ANGLE.
(10, Value::Bool(a)) => {
if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) {
r.angled = *a;
}
}
_ => {}
}
}
}
if self.potions.has_thrown(id) {
// ThrowableItemProjectile.DATA_ITEM_STACK: the potion it is.
for (index, _, value) in values {
@@ -199,9 +230,30 @@ impl Game<'_> {
let id = t.id;
t.remote_step(|at| step(id, at));
}
// FireworkRocketEntity.tick: a rocket attached to the gliding local
// player boosts it client-side too (attachedToEntity.isFallFlying).
let me = self
.remote
.as_ref()
.and_then(|r| u32::try_from(r.entity_id).ok());
for r in &mut self.rockets {
let id = r.id;
r.remote_step(|at| step(id, at));
if me.is_some() && r.attached_to == me {
if let Some(p) = self.player.as_mut()
&& p.fall_flying
{
let look = view_vector(self.yaw, self.pitch);
let m = p.velocity;
p.velocity = Vec3::new(
m.x + look[0] * 0.1 + (look[0] * 1.5 - m.x) * 0.5,
m.y + look[1] * 0.1 + (look[1] * 1.5 - m.y) * 0.5,
m.z + look[2] * 0.1 + (look[2] * 1.5 - m.z) * 0.5,
);
r.position = p.position;
r.velocity = p.velocity;
}
}
}
for c in &mut self.crystals {
c.time += 1;
+99
View File
@@ -35,6 +35,8 @@ pub mod component {
pub const CHARGED_PROJECTILES: i32 = 51;
pub const WRITABLE_BOOK_CONTENT: i32 = 56;
pub const WRITTEN_BOOK_CONTENT: i32 = 57;
pub const FIREWORK_EXPLOSION: i32 = 70;
pub const FIREWORKS: i32 = 71;
}
/// A written book's content (`WrittenBookContent.STREAM_CODEC`): the
@@ -78,6 +80,48 @@ pub enum Component {
WrittenBook(WrittenBook),
/// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates.
Stacks(Vec<Stack>),
/// `Fireworks`: flight duration and its explosions.
Fireworks {
flight: i32,
explosions: Vec<FireworkExplosion>,
},
/// `FireworkExplosion` (a firework star).
FireworkExplosion(FireworkExplosion),
}
/// `FireworkExplosion.STREAM_CODEC`: shape id, colors, fade colors, trail
/// and twinkle (FireworkExplosion.Shape's BY_ID order).
#[derive(Clone, Debug, PartialEq)]
pub struct FireworkExplosion {
pub shape: i32,
pub colors: Vec<i32>,
pub fade: Vec<i32>,
pub trail: bool,
pub twinkle: bool,
}
impl FireworkExplosion {
fn read(r: &mut Reader) -> Result<Self> {
let ints = |r: &mut Reader| -> Result<Vec<i32>> {
let n = r.count(1024)?;
(0..n).map(|_| r.i32()).collect()
};
Ok(Self {
shape: r.varint()?,
colors: ints(r)?,
fade: ints(r)?,
trail: r.bool()?,
twinkle: r.bool()?,
})
}
fn write(&self, w: &mut Writer) {
w.varint(self.shape);
w.varint(self.colors.len() as i32);
self.colors.iter().for_each(|c| w.i32(*c));
w.varint(self.fade.len() as i32);
self.fade.iter().for_each(|c| w.i32(*c));
w.bool(self.trail);
w.bool(self.twinkle);
}
}
/// `Filterable.streamCodec`: the raw value, then an optional filtered one
@@ -224,6 +268,17 @@ impl Component {
hidden: (0..n).map(|_| r.varint()).collect::<Result<_>>()?,
}
}
FIREWORKS => {
let flight = r.varint()?;
let n = r.count(256)?;
Self::Fireworks {
flight,
explosions: (0..n)
.map(|_| FireworkExplosion::read(r))
.collect::<Result<_>>()?,
}
}
FIREWORK_EXPLOSION => Self::FireworkExplosion(FireworkExplosion::read(r)?),
other => return Err(Error::new(format!("unsupported item component {other}"))),
})
}
@@ -294,6 +349,12 @@ impl Component {
w.varint(stacks.len() as i32);
stacks.iter().for_each(|s| Template::write(s, w));
}
Self::Fireworks { flight, explosions } => {
w.varint(*flight);
w.varint(explosions.len() as i32);
explosions.iter().for_each(|e| e.write(w));
}
Self::FireworkExplosion(e) => e.write(w),
Self::WritableBook(pages) => {
w.varint(pages.len() as i32);
for page in pages {
@@ -544,6 +605,44 @@ mod tests {
round::<Option<Hashed>, HashedSlot>(None);
}
#[test]
fn fireworks_components_round_trip() {
let mut rocket = Stack::new(42, 1);
rocket.added.push((
component::FIREWORKS,
Component::Fireworks {
flight: 3,
explosions: vec![
FireworkExplosion {
shape: 4,
colors: vec![0xff0000, 0x00ff00],
fade: vec![0x0000ff],
trail: true,
twinkle: false,
},
FireworkExplosion {
shape: 0,
colors: vec![],
fade: vec![],
trail: false,
twinkle: true,
},
],
},
));
rocket.added.push((
component::FIREWORK_EXPLOSION,
Component::FireworkExplosion(FireworkExplosion {
shape: 2,
colors: vec![7],
fade: vec![],
trail: false,
twinkle: false,
}),
));
round::<Option<Stack>, Slot>(Some(rocket));
}
#[test]
fn charged_projectiles_are_item_stack_templates() {
// A crossbow (id 7) loaded with one arrow (id 9): ChargedProjectiles