From 43e8b54918739c858df6a14cd7a9e6c4baf2c6bb Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:57:33 +0200 Subject: [PATCH] Server: track boats, minecarts, armor stands, frames and paintings to clients A server sent its clients only mobs and dropped items. Boats, every kind of minecart, armor stands, item frames, paintings and the entities Horizon keeps without simulating are now tracked as ServerEntity does: the AddEntity data (hanging entities at their block, with their direction), the synched data that differs from the defaults, equipment, passengers (ClientboundSetPassengersPacket at pairing and on change; a passenger syncs only its rotation and resyncs its position when it dismounts), and the type's update interval and range. Also vanilla-exact on the way, as the new harness found: - mobs pair with their health (always non-default) and Mob flags (no AI, left-handed); - /summon without NBT summons any type from an empty compound; - armor stands move before gravity (LivingEntity.travelInAir), so a stand at rest keeps vanilla's -0.0784 vertical motion. tools/test_server_entities_parity.py compares, from a vanilla-codec client, what vanilla's dedicated server and Horizon's track for a scene of 20 entities. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entities.rs | 17 +- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/commands.rs | 8 +- crates/horizon-client/src/game/lan.rs | 18 +- .../horizon-client/src/game/lan_entities.rs | 259 ++++++++++++++++++ crates/horizon-client/src/game/minecarts.rs | 4 + crates/horizon-client/src/lan/entity.rs | 183 +++++++++---- crates/horizon-client/src/lan/hub.rs | 7 +- crates/horizon-protocol/src/packets/play.rs | 9 + .../horizonlab/parity/EntityTranscript.java | 160 +++++++++++ .../parity/VanillaProtocolClient.java | 36 ++- tools/test_server_entities_parity.py | 174 ++++++++++++ 12 files changed, 811 insertions(+), 65 deletions(-) create mode 100644 crates/horizon-client/src/game/lan_entities.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java create mode 100644 tools/test_server_entities_parity.py diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs index 4ceafb3d0..5c905ac54 100644 --- a/crates/horizon-client/src/entities.rs +++ b/crates/horizon-client/src/entities.rs @@ -535,15 +535,16 @@ impl ArmorStand { Vec3::new(p.x + w, p.y + h, p.z + w), ) } - /// LivingEntity.travel with gravity 0.08 and 0.98 drag, falling until it - /// lands (armor stands have no other movement). + /// LivingEntity.travelInAir (armor stands have no other movement): move + /// by the velocity (a landing stops the fall, + /// `Block.updateEntityMovementAfterFallOn`), then gravity 0.08 and the + /// 0.98 drag, so a stand at rest on the ground keeps -0.0784 as vanilla's. pub fn tick(&mut self, scene: &mut crate::collisions::SceneCollisions) { if self.marker { return; } - let mut v = self.velocity; - v.y -= 0.08; - let wanted = v; + let wanted = self.velocity; + let mut y = wanted.y; if let Ok(moved) = scene.resolve(self.bounds(), wanted, 0.0, self.on_ground) { self.position = Vec3::new( self.position.x + moved.x, @@ -551,9 +552,11 @@ impl ArmorStand { self.position.z + moved.z, ); self.on_ground = wanted.y < 0.0 && moved.y > wanted.y; - v = Vec3::new(0.0, if moved.y != wanted.y { 0.0 } else { v.y * 0.98 }, 0.0); + if moved.y != wanted.y { + y = 0.0; + } } - self.velocity = v; + self.velocity = Vec3::new(0.0, (y - 0.08) * f64::from(0.98f32), 0.0); } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e126c533a..436802be2 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -64,6 +64,7 @@ mod jukebox; mod lan; mod lan_actions; mod lan_crafting; +mod lan_entities; mod lan_mobs; mod lan_pvp; mod lan_screen; diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 3ca938d6a..27f89b243 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -1481,7 +1481,13 @@ impl Game<'_> { self.carts.push(cart); name = Text::translate(&format!("entity.minecraft.{k}"), &[]); } - _ => return Err(err("commands.summon.failed", &[])), + // SummonCommand.createEntity: any other type from an empty compound. + _ => { + let Some(target) = self.summon_with_nbt(&kind, pos, "{}")? else { + return Err(err("commands.summon.failed", &[])); + }; + name = self.target_name(target); + } } } self.feedback(Text::translate("commands.summon.success", &[name])); diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 57c7b9ee0..209c34286 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -73,7 +73,7 @@ pub(super) struct Owner { } /// A stable UUID for an entity that has none. -fn entity_uuid(id: u32) -> u128 { +pub(super) fn entity_uuid(id: u32) -> u128 { 0x4856_5a4e_0000_0000_0000_0000_0000_0000u128 | u128::from(id) } @@ -144,11 +144,15 @@ fn mob_meta(m: &Mob, hub: &Hub) -> Vec<(u8, i32, Value)> { meta.push((3, serializer::BOOLEAN, Value::Bool(true))); } } - if m.health < m.kind.max_health { - meta.push((9, serializer::FLOAT, Value::Float(m.health.max(0.0)))); + // LivingEntity.DATA_HEALTH_ID: defined as 1, then set to the max health. + let health = m.health.max(0.0); + if health != 1.0 { + meta.push((9, serializer::FLOAT, Value::Float(health))); } - if kind == "minecraft:rabbit" && m.no_ai { - meta.push((15, serializer::BYTE, Value::Byte(1))); + // Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2. + let flags = i8::from(m.no_ai) | i8::from(m.extra.left_handed) << 1; + if flags != 0 { + meta.push((15, serializer::BYTE, Value::Byte(flags))); } if m.is_baby() && let Some(index) = baby_index(kind) @@ -713,7 +717,7 @@ impl Game<'_> { }) } - /// Mobs and items as the entities guests track. + /// The entities guests track: mobs, items and the rest (game/lan_entities.rs). fn lan_entities(&self, hub: &mut Hub) -> Vec { let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len() + self.orbs.len()); for m in self.mobs.iter().filter(|m| !m.removed) { @@ -770,6 +774,7 @@ impl Game<'_> { equipment: Vec::new(), }); } + self.lan_object_states(hub, &mut out); out } @@ -785,6 +790,7 @@ impl Game<'_> { if state.is_some() || host.hub.config.dedicated { let has_guests = !host.hub.guests.is_empty(); let entities = if has_guests { + self.lan_riding(state.as_ref().map(|s| s.entity_id), &mut host.hub.riding); self.lan_entities(&mut host.hub) } else { Vec::new() diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs new file mode 100644 index 000000000..9064447aa --- /dev/null +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -0,0 +1,259 @@ +//! The entities beyond mobs and items that a server tracks to its clients +//! (`ChunkMap.TrackedEntity`, `ServerEntity`): boats, minecarts, armor +//! stands, item frames, paintings and the entities Horizon keeps without +//! simulating them, each as vanilla pairs it (`getAddEntityPacket` and the +//! synched data that differs from the defaults), and who rides what. +use super::Game; +use horizon_client::lan::{ + entity::{EntityState, Riding}, + hub::Hub, +}; +use horizon_protocol::entity_data::{Value, serializer}; +use horizon_world::nbt::Tag; + +/// `ArmorStand`'s default poses (head, body, left arm, right arm, left leg, +/// right leg), DATA_HEAD_POSE to DATA_RIGHT_LEG_POSE (entries 16 to 21), +/// under their `Pose` keys. +const STAND_POSES: [(&str, [f32; 3]); 6] = [ + ("Head", [0.0, 0.0, 0.0]), + ("Body", [0.0, 0.0, 0.0]), + ("LeftArm", [-10.0, 0.0, -10.0]), + ("RightArm", [-15.0, 0.0, 10.0]), + ("LeftLeg", [-1.0, 0.0, -1.0]), + ("RightLeg", [1.0, 0.0, 1.0]), +]; + +fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) -> EntityState { + EntityState { + id: id as i32, + uuid: super::lan::entity_uuid(id), + kind: kind.to_owned(), + position, + yaw, + pitch: 0.0, + head_yaw: 0.0, + on_ground, + velocity: [0.0; 3], + data: 0, + meta: Vec::new(), + equipment: Vec::new(), + } +} + +/// `VehicleEntity`'s DATA_ID_HURT, DATA_ID_HURTDIR and DATA_ID_DAMAGE +/// (entries 8 to 10) when they differ from their defaults (0, 1, 0). +fn vehicle_meta(hurt_time: i32, hurt_dir: i32, damage: f32) -> Vec<(u8, i32, Value)> { + let mut meta = Vec::new(); + if hurt_time != 0 { + meta.push((8, serializer::INT, Value::Int(hurt_time))); + } + if hurt_dir != 1 { + meta.push((9, serializer::INT, Value::Int(hurt_dir))); + } + if damage != 0.0 { + meta.push((10, serializer::FLOAT, Value::Float(damage))); + } + meta +} + +/// An armor stand's pose from its compound (`ArmorStandPose`), each part +/// that differs from the default. +fn stand_poses(raw: Option<&Tag>) -> Vec<(u8, i32, Value)> { + let Some(pose) = raw.and_then(|t| t.get("Pose")) else { + return Vec::new(); + }; + let mut meta = Vec::new(); + for (i, (key, default)) in STAND_POSES.iter().enumerate() { + let Some(Tag::List(values)) = pose.get(key) else { + continue; + }; + let mut r = *default; + for (k, v) in values.iter().take(3).enumerate() { + if let Tag::Float(bits) = v { + r[k] = f32::from_bits(*bits); + } + } + if r != *default { + meta.push(( + 16 + i as u8, + serializer::ROTATIONS, + Value::Floats(r.to_vec()), + )); + } + } + meta +} + +impl Game<'_> { + /// Who rides what (`Entity.getPassengers`): mobs on mobs, and the + /// host in its boat or minecart. + pub(super) fn lan_riding(&self, host_entity: Option, riding: &mut Riding) { + riding.clear(); + for m in self.mobs.iter().filter(|m| !m.removed) { + if let Some(vehicle) = m.extra.mount { + riding.add(vehicle as i32, m.id as i32); + } + } + if let (Some(host), Some(vehicle)) = (host_entity, self.riding) { + riding.add(vehicle as i32, host); + } + } + + /// Boats, minecarts, armor stands, item frames, paintings and the + /// entities kept unsimulated, as guests track them. + pub(super) fn lan_object_states(&self, hub: &mut Hub, out: &mut Vec) { + for b in &self.boats { + let p = b.position; + let mut e = state(b.id, &b.entity_type(), [p.x, p.y, p.z], b.yaw, b.on_ground); + e.velocity = [b.velocity.x, b.velocity.y, b.velocity.z]; + e.meta = vehicle_meta(b.hurt_time, b.hurt_dir, b.damage); + // AbstractBoat.DATA_ID_PADDLE_LEFT and DATA_ID_PADDLE_RIGHT. + for (k, paddling) in b.paddling.iter().enumerate() { + if *paddling { + e.meta + .push((11 + k as u8, serializer::BOOLEAN, Value::Bool(true))); + } + } + out.push(e); + } + for c in &self.carts { + let p = c.position; + let mut e = state(c.id, c.kind.id(), [p.x, p.y, p.z], c.yaw, c.on_ground()); + e.velocity = [c.velocity.x, c.velocity.y, c.velocity.z]; + e.meta = vehicle_meta(c.hurt_time, c.hurt_dir, c.damage); + match c.kind { + // MinecartFurnace.DATA_ID_FUEL. + super::minecarts::CartKind::Furnace if c.fuel > 0 => { + e.meta.push((13, serializer::BOOLEAN, Value::Bool(true))); + } + // MinecartCommandBlock.DATA_ID_COMMAND_NAME. + super::minecarts::CartKind::CommandBlock => { + if let Some(Tag::String(command)) = + self.persist.raw.get(&c.id).and_then(|t| t.get("Command")) + { + let command = command.to_string_lossy(); + if !command.is_empty() { + e.meta + .push((13, serializer::STRING, Value::String(command))); + } + } + } + _ => {} + } + out.push(e); + } + for a in &self.armor_stands { + let p = a.position; + let mut e = state( + a.id, + "minecraft:armor_stand", + [p.x, p.y, p.z], + a.yaw, + a.on_ground, + ); + e.head_yaw = a.yaw; + e.velocity = [a.velocity.x, a.velocity.y, a.velocity.z]; + if a.fire_ticks > 0 { + e.meta.push((0, serializer::BYTE, Value::Byte(1))); + } + // LivingEntity.DATA_HEALTH_ID: 1 until the stand's max health is set. + e.meta.push((9, serializer::FLOAT, Value::Float(a.health))); + // ArmorStand.DATA_CLIENT_FLAGS: small 1, arms 4, no base plate 8, marker 16. + let flags = i8::from(a.small) + | i8::from(a.show_arms) << 2 + | i8::from(a.no_base_plate) << 3 + | i8::from(a.marker) << 4; + if flags != 0 { + e.meta.push((15, serializer::BYTE, Value::Byte(flags))); + } + e.meta.extend(stand_poses(self.persist.raw.get(&a.id))); + // Stored main hand, off hand, feet, legs, chest, head: the wire's slots 0 to 5. + for (slot, item) in a.equipment.iter().enumerate() { + if item.is_some() { + e.equipment.push((slot as u8, hub.stack(item))); + } + } + out.push(e); + } + for f in &self.item_frames { + // ItemFrame.getAddEntityPacket: the block it hangs in and its direction. + let mut e = state(f.id, f.kind(), f.pos.map(f64::from), 0.0, false); + e.pitch = match f.facing { + 0 => 90.0, + 1 => -90.0, + _ => 0.0, + }; + e.yaw = match f.facing { + 2 => 180.0, + 4 => 90.0, + 5 => 270.0, + _ => 0.0, + }; + e.data = i32::from(f.facing); + // HangingEntity.DATA_DIRECTION, south by default. + if f.facing != 3 { + e.meta + .push((8, serializer::DIRECTION, Value::Id(i32::from(f.facing)))); + } + if f.item.is_some() { + e.meta + .push((9, serializer::ITEM_STACK, Value::Item(hub.stack(&f.item)))); + } + if f.rotation != 0 { + e.meta + .push((10, serializer::INT, Value::Int(i32::from(f.rotation)))); + } + out.push(e); + } + for p in &self.paintings { + let mut e = state(p.id, "minecraft:painting", p.pos.map(f64::from), 0.0, false); + // Direction.get2DDataValue (south 0, west 1, north 2, east 3) to its + // 3D data value and yaw. + let facing = [3, 4, 2, 5][usize::from(p.facing & 3)]; + e.yaw = f32::from(p.facing & 3) * 90.0; + e.data = facing; + if facing != 3 { + e.meta.push((8, serializer::DIRECTION, Value::Id(facing))); + } + // Painting.DATA_PAINTING_VARIANT_ID, a registry holder (id + 1), + // set unless it is the registry's first variant (VariantUtils.getAny). + if let Some(id) = hub.registry_id("minecraft:painting_variant", &p.variant) + && id != 0 + { + e.meta + .push((9, serializer::PAINTING_VARIANT, Value::Id(id + 1))); + } + out.push(e); + } + for o in &self.others { + let p = o.position; + let mut e = state(o.id, &o.kind, [p.x, p.y, p.z], o.yaw, false); + e.pitch = o.pitch; + e.head_yaw = o.yaw; + out.push(e); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn armor_stand_poses_send_only_what_differs() { + let tag = + horizon_world::nbt::parse_snbt("{Pose:{Head:[10f,20f,0f],LeftArm:[-10f,0f,-10f]}}") + .unwrap() + .0; + let meta = stand_poses(Some(&tag)); + assert_eq!( + meta, + vec![( + 16, + serializer::ROTATIONS, + Value::Floats(vec![10.0, 20.0, 0.0]) + )] + ); + assert!(stand_poses(None).is_empty()); + } +} diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index de440433d..1e0b42b50 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -157,6 +157,10 @@ pub(super) struct Rail { powered: bool, } impl Minecart { + /// `Entity.onGround`. + pub(super) fn on_ground(&self) -> bool { + self.on_ground + } pub fn new(id: u32, kind: CartKind, position: Vec3) -> Self { Self { id, diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index a37010d79..178f9bd44 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -11,7 +11,7 @@ use horizon_protocol::{ packets::play::{ self, AddEntity, EntityPositionSync, MoveEntity, MoveEntityPos, MoveEntityPosRot, MoveEntityRot, PositionPath, RotateHead, SetEntityData, SetEntityMotion, SetEquipment, - VecDelta, + SetPassengers, VecDelta, }, }; @@ -54,6 +54,40 @@ struct Sent { /// The type's update interval (0: never) and whether velocity is tracked. interval: u32, track_deltas: bool, + /// lastPassengers, and wasRiding: a passenger's position isn't synced, + /// and the first update after it dismounts is a full position sync. + passengers: Vec, + was_riding: bool, +} + +/// Who rides what this tick (`Entity.getPassengers`, `isPassenger`). +#[derive(Default)] +pub struct Riding { + /// Each vehicle's passengers, in order. + pub passengers: crate::fast_hash::FxHashMap>, + /// Each passenger's vehicle. + pub vehicle: crate::fast_hash::FxHashMap, +} + +impl Riding { + pub fn clear(&mut self) { + self.passengers.clear(); + self.vehicle.clear(); + } + /// `passenger` rides `vehicle`, after the ones already riding it. + pub fn add(&mut self, vehicle: i32, passenger: i32) { + self.passengers.entry(vehicle).or_default().push(passenger); + self.vehicle.insert(passenger, vehicle); + } + pub fn passengers(&self, id: i32) -> &[i32] { + self.passengers.get(&id).map_or(&[], Vec::as_slice) + } + fn set_passengers(&self, vehicle: i32) -> play::Clientbound { + play::Clientbound::SetPassengers(SetPassengers { + vehicle, + passengers: self.passengers(vehicle).to_vec(), + }) + } } #[derive(Default)] @@ -94,7 +128,12 @@ impl Tracker { } /// The packets that introduce an entity (ServerEntity.sendPairingData). - pub fn spawn(&mut self, e: &EntityState, entity_type: i32) -> Vec { + pub fn spawn( + &mut self, + e: &EntityState, + entity_type: i32, + riding: &Riding, + ) -> Vec { let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw)); let head = pack_degrees(e.head_yaw); let (_, interval, track_deltas) = tracking::settings(&e.kind); @@ -121,6 +160,13 @@ impl Tracker { slots: e.equipment.clone(), })); } + let passengers = riding.passengers(e.id).to_vec(); + if !passengers.is_empty() { + out.push(riding.set_passengers(e.id)); + } + if let Some(&vehicle) = riding.vehicle.get(&e.id) { + out.push(riding.set_passengers(vehicle)); + } self.sent.insert( e.id, Sent { @@ -135,6 +181,8 @@ impl Tracker { teleport_delay: 0, interval, track_deltas, + passengers, + was_riding: false, }, ); out @@ -144,17 +192,43 @@ impl Tracker { /// rotation every update interval (positions at least every 60 ticks, a /// full position sync every 400 or when the entity lands or leaves the /// ground), velocity for types that track it, changed data at once. - pub fn update(&mut self, e: &EntityState) -> Vec { + pub fn update(&mut self, e: &EntityState, riding: &Riding) -> Vec { let Some(s) = self.sent.get_mut(&e.id) else { return Vec::new(); }; let mut out = Vec::new(); + let passengers = riding.passengers(e.id); + if passengers != s.passengers.as_slice() { + out.push(riding.set_passengers(e.id)); + s.passengers = passengers.to_vec(); + } let dirty = e.meta != s.meta; let due = s.interval != 0 && s.tick.is_multiple_of(s.interval); if due || dirty { let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw)); let turned = |a: i8, b: i8| (i32::from(a) - i32::from(b)).abs() >= 1; let rotate = turned(rotation.1, s.rotation.1) || turned(rotation.0, s.rotation.0); + if riding.vehicle.contains_key(&e.id) { + // A passenger: its rotation alone (its vehicle places it). + if rotate { + out.push(play::Clientbound::MoveEntityRot(MoveEntityRot( + MoveEntity { + id: e.id, + delta: None, + rotation: Some((rotation.1, rotation.0)), + on_ground: e.on_ground, + }, + ))); + s.rotation = rotation; + } + s.base = e.position; + s.was_riding = true; + Self::send_dirty(e, s, dirty, &mut out); + Self::send_head(e, s, &mut out); + s.tick += 1; + Self::send_equipment(e, s, &mut out); + return out; + } s.teleport_delay += 1; let d: f64 = (0..3).map(|k| (e.position[k] - s.base[k]).powi(2)).sum(); let send_position = d >= 7.629_394_5e-6 || s.tick.is_multiple_of(60); @@ -166,9 +240,11 @@ impl Tracker { on_ground: e.on_ground, }; // ServerEntity.createMovePacket. - let packet = if s.teleport_delay > 400 || s.was_on_ground != e.on_ground { + let packet = if s.teleport_delay > 400 || s.was_riding || s.was_on_ground != e.on_ground + { s.was_on_ground = e.on_ground; s.teleport_delay = 0; + s.was_riding = false; Some(play::Clientbound::EntityPositionSync(sync)) } else if send_position { let delta = [0, 1, 2].map(|k| encode(e.position[k]) - encode(s.base[k])); @@ -227,33 +303,47 @@ impl Tracker { s.rotation = rotation; } } - // ServerEntity.sendDirtyEntityData. - if dirty { - let changed: Vec<_> = e - .meta - .iter() - .filter(|m| !s.meta.contains(m)) - .cloned() - .collect(); - if !changed.is_empty() { - out.push(play::Clientbound::SetEntityData(SetEntityData { - id: e.id, - values: changed, - })); - } - s.meta = e.meta.clone(); - } - let head = pack_degrees(e.head_yaw); - if turned(head, s.head) { - out.push(play::Clientbound::RotateHead(RotateHead { - id: e.id, - head_yaw: head, - })); - s.head = head; - } + Self::send_dirty(e, s, dirty, &mut out); + Self::send_head(e, s, &mut out); } s.tick += 1; - // LivingEntity.detectEquipmentUpdates: changed slots every tick. + Self::send_equipment(e, s, &mut out); + out + } + + /// ServerEntity.sendDirtyEntityData. + fn send_dirty(e: &EntityState, s: &mut Sent, dirty: bool, out: &mut Vec) { + if !dirty { + return; + } + let changed: Vec<_> = e + .meta + .iter() + .filter(|m| !s.meta.contains(m)) + .cloned() + .collect(); + if !changed.is_empty() { + out.push(play::Clientbound::SetEntityData(SetEntityData { + id: e.id, + values: changed, + })); + } + s.meta = e.meta.clone(); + } + + fn send_head(e: &EntityState, s: &mut Sent, out: &mut Vec) { + let head = pack_degrees(e.head_yaw); + if (i32::from(head) - i32::from(s.head)).abs() >= 1 { + out.push(play::Clientbound::RotateHead(RotateHead { + id: e.id, + head_yaw: head, + })); + s.head = head; + } + } + + /// LivingEntity.detectEquipmentUpdates: changed slots every tick. + fn send_equipment(e: &EntityState, s: &mut Sent, out: &mut Vec) { if e.equipment != s.equipment { // Cleared slots are sent as empty stacks. let mut slots: Vec<(u8, Option)> = e @@ -275,7 +365,6 @@ impl Tracker { } s.equipment = e.equipment.clone(); } - out } } @@ -303,24 +392,24 @@ mod tests { #[test] fn moves_go_out_every_update_interval() { let mut t = Tracker::default(); - let spawn = t.spawn(&state(0.0), 100); + let spawn = t.spawn(&state(0.0), 100, &Riding::default()); assert!(matches!(spawn[0], play::Clientbound::AddEntity(_))); // Tick 0 is due and a multiple of 60: the position goes out unchanged. assert!(matches!( - t.update(&state(0.0))[..], + t.update(&state(0.0), &Riding::default())[..], [play::Clientbound::MoveEntityPos(_)] )); // A zombie updates every third tick. - assert!(t.update(&state(0.5)).is_empty()); - assert!(t.update(&state(0.5)).is_empty()); + assert!(t.update(&state(0.5), &Riding::default()).is_empty()); + assert!(t.update(&state(0.5), &Riding::default()).is_empty()); assert!(matches!( - t.update(&state(0.5))[..], + t.update(&state(0.5), &Riding::default())[..], [play::Clientbound::MoveEntityPos(_)] )); for _ in 0..2 { - t.update(&state(40.0)); + t.update(&state(40.0), &Riding::default()); } - let jump = t.update(&state(40.0)); + let jump = t.update(&state(40.0), &Riding::default()); assert!(matches!(jump[0], play::Clientbound::EntityPositionSync(_))); } @@ -328,13 +417,13 @@ mod tests { fn landing_sends_a_position_sync() { let mut t = Tracker::default(); let mut e = state(0.0); - t.spawn(&e, 100); - t.update(&e); + t.spawn(&e, 100, &Riding::default()); + t.update(&e, &Riding::default()); e.on_ground = false; - t.update(&e); - t.update(&e); + t.update(&e, &Riding::default()); + t.update(&e, &Riding::default()); assert!(matches!( - t.update(&e)[..], + t.update(&e, &Riding::default())[..], [play::Clientbound::EntityPositionSync(_)] )); } @@ -343,11 +432,11 @@ mod tests { fn metadata_and_equipment_send_only_the_changes() { let mut t = Tracker::default(); let mut e = state(0.0); - t.spawn(&e, 100); - t.update(&e); + t.spawn(&e, 100, &Riding::default()); + t.update(&e, &Riding::default()); e.meta.push((0, 0, Value::Byte(1))); e.equipment.push((0, Some(Stack::new(5, 1)))); - let out = t.update(&e); + let out = t.update(&e, &Riding::default()); assert!( out.iter() .any(|p| matches!(p, play::Clientbound::SetEntityData(_))) @@ -356,9 +445,9 @@ mod tests { out.iter() .any(|p| matches!(p, play::Clientbound::SetEquipment(_))) ); - assert!(t.update(&e).is_empty()); + assert!(t.update(&e, &Riding::default()).is_empty()); e.equipment.clear(); - let out = t.update(&e); + let out = t.update(&e, &Riding::default()); let Some(play::Clientbound::SetEquipment(eq)) = out.first() else { panic!("expected equipment"); }; diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index b18187835..d710c239e 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -124,6 +124,8 @@ pub struct Hub { /// The `player_movement_check` and `elytra_movement_check` game rules, /// set by the game before each tick. pub movement_checks: (bool, bool), + /// Who rides what, set by the game before each tick. + pub riding: super::entity::Riding, last_clock: i64, last_rate: f32, ticking: (f32, bool), @@ -237,6 +239,7 @@ impl Hub { sync, ticks: 0, movement_checks: (true, true), + riding: Default::default(), last_clock: i64::MIN, last_rate: 0.0, ticking: (20.0, false), @@ -1501,14 +1504,14 @@ impl Hub { continue; } if self.guests[gi].tracker.is_tracking(e.id) { - let packets = self.guests[gi].tracker.update(e); + let packets = self.guests[gi].tracker.update(e, &self.riding); self.guests[gi].send_all(&packets); } else if visible(&self.guests[gi], ei) { let Some(kind) = self.entity_type(&e.kind) else { continue; }; // Players need their list entry first; guests got it at join. - let packets = self.guests[gi].tracker.spawn(e, kind); + let packets = self.guests[gi].tracker.spawn(e, kind, &self.riding); self.guests[gi].send_all(&packets); } } diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 95ce869a4..40b70624a 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -590,6 +590,10 @@ packet!(SetCursorItem { item: Option => Slot }); packet!(SetDefaultSpawnPosition { pos: GlobalPos => GlobalPos, yaw: f32 => Float, pitch: f32 => Float }); packet!(SetEntityData { id: i32 => VarInt, values: Vec<(u8, i32, Value)> => EntityData }); packet!(SetEntityMotion { id: i32 => VarInt, velocity: [f64; 3] => LpVec3 }); +packet!( + /// ClientboundSetPassengersPacket: a vehicle and every entity riding it. + SetPassengers { vehicle: i32 => VarInt, passengers: Vec => List } +); /// ClientboundSetEquipmentPacket: (EquipmentSlot ordinal, stack) pairs. pub struct Equipment; impl Codec)>> for Equipment { @@ -1426,6 +1430,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { SetHealth(SetHealth) = SET_HEALTH, SetHeldSlot(SetHeldSlot) = SET_HELD_SLOT, SetObjective(SetObjective) = SET_OBJECTIVE, + SetPassengers(SetPassengers) = SET_PASSENGERS, SetPlayerInventory(SetPlayerInventory) = SET_PLAYER_INVENTORY, SetPlayerTeam(SetPlayerTeam) = SET_PLAYER_TEAM, SetScore(SetScore) = SET_SCORE, @@ -2052,6 +2057,10 @@ mod tests { id: 2, velocity: [0.0; 3], }), + Clientbound::SetPassengers(SetPassengers { + vehicle: 2, + passengers: vec![3, 300], + }), Clientbound::SetEquipment(SetEquipment { id: 2, slots: vec![(0, None), (5, Some(Stack::new(1, 1)))], diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java new file mode 100644 index 000000000..4d7cc9f2c --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java @@ -0,0 +1,160 @@ +package dev.horizonlab.parity; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; +import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket; +import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket; +import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; +import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; +import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket; +import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket; +import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket; +import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket; +import net.minecraft.network.protocol.game.VecDeltaCodec; +import net.minecraft.world.entity.EntityTypes; +import net.minecraft.world.phys.Vec3; + +/** + * What a vanilla client knows about the entities a server tracks to it, as + * ClientPacketListener would apply the packets: each entity's spawn packet, + * the entity data it was paired with and all it was sent since, its + * equipment, attributes and passengers, and where the position syncs leave + * it (VecDeltaCodec, as handleMoveEntity and handleEntityPositionSync). + * Entity ids differ between servers: an entity is named by its type and the + * block it was added in, passengers by those names. Players are left out. + */ +final class EntityTranscript { + private static final class Tracked { + final String type; + final String add; + final VecDeltaCodec codec = new VecDeltaCodec(); + String name; + String pairing; + final Map data = new TreeMap<>(); + final Map equipment = new TreeMap<>(); + final Map attributes = new TreeMap<>(); + int[] passengers = new int[0]; + float yRot; + float xRot; + float headRot; + boolean onGround; + String motion; + boolean removed; + + Tracked(ClientboundAddEntityPacket p) { + type = BuiltInRegistries.ENTITY_TYPE.getKey(p.getType()).toString(); + Vec3 at = new Vec3(p.getX(), p.getY(), p.getZ()); + codec.setBase(at); + name = type + " " + (int) Math.floor(at.x) + "," + (int) Math.floor(at.y) + "," + (int) Math.floor(at.z); + add = "at " + at + " rot " + p.getXRot() + "," + p.getYRot() + " head " + p.getYHeadRot() + " data " + p.getData() + + " motion " + p.getMovement(); + yRot = p.getYRot(); + xRot = p.getXRot(); + headRot = p.getYHeadRot(); + motion = String.valueOf(p.getMovement()); + } + } + + private final Map byId = new HashMap<>(); + private final List all = new ArrayList<>(); + private int unknown; + + /** Applies one packet; false when it isn't about a tracked entity. */ + boolean accept(Packet packet) { + if (packet instanceof ClientboundAddEntityPacket p) { + if (p.getType() == EntityTypes.PLAYER) return true; + Tracked t = new Tracked(p); + byId.put(p.getId(), t); + all.add(t); + return true; + } + if (packet instanceof ClientboundRemoveEntitiesPacket p) { + p.entityIds().forEach(id -> { + Tracked t = byId.remove(id); + if (t != null) t.removed = true; + }); + return true; + } + Tracked t = switch (packet) { + case ClientboundSetEntityDataPacket p -> byId.get(p.id()); + case ClientboundSetEquipmentPacket p -> byId.get(p.getEntity()); + case ClientboundUpdateAttributesPacket p -> byId.get(p.getEntityId()); + case ClientboundSetPassengersPacket p -> byId.get(p.getVehicle()); + case ClientboundEntityPositionSyncPacket p -> byId.get(p.id()); + case ClientboundSetEntityMotionPacket p -> byId.get(p.id()); + case ClientboundTeleportEntityPacket p -> byId.get(p.id()); + case ClientboundMoveEntityPacket p -> byId.get(VanillaProtocolClient.moveEntityId(p)); + case ClientboundRotateHeadPacket p -> byId.get(VanillaProtocolClient.rotateHeadId(p)); + default -> null; + }; + if (t == null) { + return false; + } + switch (packet) { + case ClientboundSetEntityDataPacket p -> { + for (var v : p.packedItems()) t.data.put(v.id(), net.minecraft.network.syncher.EntityDataSerializers.getSerializedId(v.serializer()) + " " + v.value()); + if (t.pairing == null) { + StringBuilder b = new StringBuilder(); + for (var v : p.packedItems()) b.append(v.id()).append('=').append(v.value()).append(' '); + t.pairing = b.toString().trim(); + } + } + case ClientboundSetEquipmentPacket p -> p.getSlots().forEach(s -> t.equipment.put(s.getFirst().getName(), String.valueOf(s.getSecond()))); + case ClientboundUpdateAttributesPacket p -> p.getValues().forEach(a -> t.attributes.put(a.attribute().getRegisteredName(), + a.base() + " " + a.modifiers())); + case ClientboundSetPassengersPacket p -> t.passengers = p.getPassengers(); + case ClientboundEntityPositionSyncPacket p -> { + t.codec.setBase(p.position().endPosition()); + t.yRot = p.yRot(); + t.xRot = p.xRot(); + t.onGround = p.onGround(); + } + case ClientboundMoveEntityPacket p -> { + if (p.hasPosition()) t.codec.setBase(p.getPositionDelta().decode(t.codec).endPosition()); + if (p.hasRotation()) { + t.yRot = p.getYRot(); + t.xRot = p.getXRot(); + } + t.onGround = p.isOnGround(); + } + case ClientboundSetEntityMotionPacket p -> t.motion = String.valueOf(p.movement()); + case ClientboundTeleportEntityPacket p -> { + t.codec.setBase(p.change().position()); + t.onGround = p.onGround(); + } + case ClientboundRotateHeadPacket p -> t.headRot = p.getYHeadRot(); + default -> { + } + } + return true; + } + + /** One line per entity, sorted, each field named. */ + List lines() { + Map seen = new HashMap<>(); + for (Tracked t : all) { + int n = seen.merge(t.name, 1, Integer::sum); + if (n > 1) t.name += " #" + n; + } + Map names = new HashMap<>(); + byId.forEach((id, t) -> names.put(id, t.name)); + List out = new ArrayList<>(); + for (Tracked t : all) { + List riders = new ArrayList<>(); + for (int id : t.passengers) riders.add(names.getOrDefault(id, "player or unknown")); + out.add("entity " + t.name + " | add " + t.add + " | pairing " + t.pairing + " | data " + t.data + " | equipment " + t.equipment + + " | attributes " + t.attributes + " | passengers " + riders + " | now " + t.codec.getBase() + " rot " + t.xRot + "," + + t.yRot + " head " + t.headRot + " ground " + t.onGround + " motion " + t.motion + (t.removed ? " | removed" : "")); + } + out.sort(null); + return out; + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java index f87e9cf0f..ca366705b 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java @@ -113,6 +113,8 @@ public final class VanillaProtocolClient { private ProtocolInfo inbound; private ProtocolInfo outbound; private String state; + /** With a fifth argument {@code entities}: the entity transcript mode (EntityTranscript). */ + private EntityTranscript entities; public static void main(String[] args) throws Exception { ParityData.init(); @@ -121,6 +123,7 @@ public final class VanillaProtocolClient { String name = args[2]; Path output = Path.of(args[3]); VanillaProtocolClient client = new VanillaProtocolClient(); + if (args.length > 4 && args[4].equals("entities")) client.entities = new EntityTranscript(); try { client.status(host, port); client.join(host, port, name); @@ -405,6 +408,18 @@ public final class VanillaProtocolClient { play(); } + /** The entity mode's step, and how long it records the entities for (ms). */ + private static final int ENTITIES = 1000; + private static final long ENTITY_WAIT = 20_000; + + static int moveEntityId(net.minecraft.network.protocol.game.ClientboundMoveEntityPacket p) { + return (int) field(p, net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.class, "entityId"); + } + + static int rotateHeadId(net.minecraft.network.protocol.game.ClientboundRotateHeadPacket p) { + return (int) field(p, p.getClass(), "entityId"); + } + private static final String[] REPLIES = {"random value 7..7", "random value x", "trigger nope"}; private void play() throws Exception { @@ -461,6 +476,7 @@ public final class VanillaProtocolClient { if (r != null) { Packet packet = r.packet; counts.merge(r.type, 1, Integer::sum); + if (entities != null && step >= ENTITIES) entities.accept(packet); if (r.type.equals("minecraft:chunk_batch_start")) ordered = false; if (respawning && !respawned) { String line = respawnLine(r); @@ -581,8 +597,20 @@ public final class VanillaProtocolClient { commandTree(p); } } + if (entities != null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { + // Entity mode: the harness's console summons the scene once it reads this + // line; everything tracked from then on is recorded until the client leaves. + record("play-chunks", String.valueOf(chunks.size())); + send(new ServerboundChatPacket("summon the entities", Instant.now(), 0L, Optional.empty(), + new LastSeenMessages.Update(0, new BitSet(), (byte) 0))); + step = ENTITIES; + stepAt = now; + } else if (step == ENTITIES && now - stepAt > ENTITY_WAIT) { + for (String line : entities.lines()) record("play-entities", line); + return; + } // Once the chunks stop coming: chat, then /list, then leave. - if (loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { + if (entities == null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { record("play-chunks", chunks.size() + " " + chunks); BLOCK_ENTITIES.forEach((k, v) -> record("play-block-entities", k + " " + v)); // Which light layers exist depends on the neighbouring @@ -910,8 +938,12 @@ public final class VanillaProtocolClient { } private static Object field(Object owner, String name) { + return field(owner, owner.getClass(), name); + } + + private static Object field(Object owner, Class declaring, String name) { try { - var f = owner.getClass().getDeclaredField(name); + var f = declaring.getDeclaredField(name); f.setAccessible(true); return f.get(owner); } catch (ReflectiveOperationException e) { diff --git a/tools/test_server_entities_parity.py b/tools/test_server_entities_parity.py new file mode 100644 index 000000000..f97e3f15a --- /dev/null +++ b/tools/test_server_entities_parity.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Entities other than mobs and items, tracked to a vanilla client, against vanilla's dedicated server. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first, with the same server.properties (offline, view distance 4). A vanilla-codec +client (VanillaProtocolClient in its entity mode) joins, and once its chunks are in it asks for the scene: the +console freezes the ticks, builds a wall and a rail and summons, around the spawn, every kind of minecart (one +set rolling along the rail), boats and a raft (one falling onto the ground), armor stands (posed, small, with +arms, wearing a helmet, one falling), a chicken jockey, item frames (one holding a turned diamond) and paintings. The client records +for 20 seconds, the ticks running again after 3, what ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn +packet (position, rotations, data, motion), the entity data it was paired with and all it was sent since, its +equipment and passengers, and where its position syncs leave it. Both transcripts must match exactly, +entity by entity (named by type and the block it was added in). Attributes are recorded but not compared yet: +Horizon sends no ClientboundUpdateAttributesPacket, for mobs either. +""" +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-entities-parity' +PLAYER = 'Tester' + +# Around the spawn at 0 -60 0 on the flat world's grass: a wall to the north for the frames and paintings, +# a rail to the south. +SCENE = [ + 'fill -7 -60 -6 7 -57 -6 minecraft:stone', + 'fill -7 -60 9 7 -60 9 minecraft:rail', + # Minecarts. + 'summon minecraft:minecart -5.5 -60 3.5', + 'summon minecraft:chest_minecart -3.5 -60 3.5', + 'summon minecraft:furnace_minecart -1.5 -60 3.5', + 'summon minecraft:hopper_minecart 1.5 -60 3.5', + 'summon minecraft:tnt_minecart 3.5 -60 3.5', + 'summon minecraft:command_block_minecart 5.5 -60 1.5 {Command:"say hi"}', + 'summon minecraft:minecart -6.5 -60 9.5 {Motion:[0.5d,0d,0d]}', + # Boats. + 'summon minecraft:oak_boat -5.5 -60 6.5 {Rotation:[45f,0f]}', + 'summon minecraft:birch_chest_boat -2.5 -60 6.5', + 'summon minecraft:bamboo_raft 0.5 -60 6.5', + 'summon minecraft:oak_boat 4.5 -56 6.5', + # Armor stands. + 'summon minecraft:armor_stand -2.5 -60 -2.5 {Rotation:[90f,0f]}', + 'summon minecraft:armor_stand 6.5 -57 -2.5', + 'summon minecraft:armor_stand -4.5 -60 -2.5 {ShowArms:1b,Small:1b,NoBasePlate:1b,' + 'Pose:{Head:[10f,20f,0f],RightArm:[-30f,0f,15f]},equipment:{head:{id:"minecraft:diamond_helmet",count:1}}}', + # A chicken jockey (passengers). + 'summon minecraft:chicken -6.5 -60 -2.5 {NoAI:1b,Passengers:[{id:"minecraft:husk",IsBaby:1b,NoAI:1b}]}', + # Frames and paintings on the wall's south face. + 'summon minecraft:item_frame -3.5 -59 -4.97 {block_pos:[I;-4,-59,-5],Facing:3b,' + 'Item:{id:"minecraft:diamond",count:1},ItemRotation:3b}', + 'summon minecraft:glow_item_frame -1.5 -59 -4.97 {block_pos:[I;-2,-59,-5],Facing:3b}', + 'summon minecraft:painting 1.5 -59 -4.97 {block_pos:[I;1,-59,-5],facing:0b,variant:"minecraft:kebab"}', + 'summon minecraft:painting 4 -59 -4.97 {block_pos:[I;4,-59,-5],facing:0b,variant:"minecraft:courbet"}', +] + + +def scene_lines(lines): + """The transcript's entities in the scene's area (the world's own animals and slimes wander elsewhere), + without their attributes: ClientboundUpdateAttributesPacket isn't sent by Horizon yet, for mobs either.""" + out = [] + for line in lines: + m = re.match(r'play-entities entity \S+ (-?\d+),-?\d+,(-?\d+)', line) + if m and -8 <= int(m.group(1)) <= 8 and -7 <= int(m.group(2)) <= 10: + out.append(re.sub(r' \| attributes \{[^|]*\}', '', line)) + return out + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}-client.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), PLAYER, str(out), 'entities'], + cwd=lab.ROOT, stdout=log, stderr=subprocess.STDOUT, text=True) + try: + server.wait_for(rf'<{PLAYER}> summon the entities', timeout=180) + # Frozen, so that every entity is paired as it was summoned whenever each server tracks it in + # its tick; then they fall and roll. + server.console('tick freeze') + for command in SCENE: + server.console(command) + time.sleep(3) + server.console('tick unfreeze') + client.wait(timeout=120) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + lines = out.read_text(encoding='utf-8').splitlines() + (build / f'{label}.txt').write_text('\n'.join(lines) + '\n', encoding='utf-8') + return lines + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + # SERVER_ENTITIES_SIDES=vanilla (or horizon) runs one side and reads the other's last transcript. + sides = [s for s in os.environ.get('SERVER_ENTITIES_SIDES', 'vanilla,horizon').split(',') if s] + if 'horizon' in sides: + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.VanillaProtocolClient'] + + world = generate_world(vanilla_server, build) + results = {} + for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]: + if label in sides: + results[label] = run_side(label, command, client, build, world) + else: + results[label] = (build / f'{label}.txt').read_text(encoding='utf-8').splitlines() + vanilla, horizon = scene_lines(results['vanilla']), scene_lines(results['horizon']) + errors = [l for l in results['vanilla'] + results['horizon'] if l.startswith('error')] + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=0, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:8000]) + if len(vanilla) < len(SCENE) - 2: + raise AssertionError(f'only {len(vanilla)} entities tracked: the scene did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + entities=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, item frames and paintings tracked to a ' + 'vanilla-codec client by a dedicated server: spawn packets, entity data, equipment, passengers and the ' + 'positions the syncs leave them at.')) + print(f'server entities: {len(vanilla)} entities identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}')