diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index d82e8a5aa..db793d993 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1661,10 +1661,48 @@ impl Game<'_> { } self.creatures.projectiles.append(&mut projectiles); for (p, hit) in hits { - self.projectile_hit(&p, hit)?; + // A guest's projectile lands as theirs: a pearl takes them, what + // it hurts answers to them. One hitting the host (`Who::Player` + // here) stays the host's hit. + match p.guest { + Some(conn) + if !matches!( + hit, + Hit::Entity { + who: Who::Player, + .. + } + ) => + { + self.guest_projectile_hit(conn, &p, hit)?; + } + _ => self.projectile_hit(&p, hit)?, + } } Ok(()) } + /// `projectile_hit` for a projectile the guest `conn` shot, run as them; + /// as the host's when they have left. + fn guest_projectile_hit(&mut self, conn: u32, p: &Projectile, hit: Hit) -> Result<()> { + let Some(mut host) = self.lan.take() else { + return self.projectile_hit(p, hit); + }; + let result = self.as_guest(&mut host, conn, |g| g.projectile_hit(p, hit)); + // The mob hit now answers to the guest (LivingEntity.lastHurtByPlayer). + if result.is_some() + && let Hit::Entity { + who: Who::Mob(id), .. + } = hit + && self.mobs.iter().any(|m| m.id == id) + { + host.mob_players.insert(id, conn); + } + self.lan = Some(host); + match result { + Some(r) => r, + None => self.projectile_hit(p, hit), + } + } /// What an arrow's tick asked for: its particles and sounds. fn arrow_event(&mut self, at: Vec3, event: horizon_client::arrows::ArrowEvent) { use horizon_client::arrows::{ArrowEvent, ArrowParticle, ArrowSound, BlockReaction}; @@ -1968,9 +2006,15 @@ impl Game<'_> { m.damage(0.0, source); } if p.owner == Some(Who::Player) && self.player.is_some() { - // ThrownEnderpearl: 5% endermite, teleport, 5 damage. - if self.world.random.next_float() < 0.05 { - self.spawn_mob("minecraft:endermite", atp, false); + // ThrownEnderpearl: 5% endermite where the player was + // (when monsters spawn), teleport, 5 damage. + if self.world.random.next_float() < 0.05 + && self.ui.rules.bool("spawn_monsters") + && self.ui.difficulty != 0 + && let Some(pl) = self.player.as_ref() + { + let from = pl.position; + self.spawn_mob("minecraft:endermite", [from.x, from.y, from.z], false); } if let Some(pl) = self.player.as_mut() { pl.position = at; diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 5f98a401f..6f1c17c3a 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -125,6 +125,8 @@ pub(super) struct GuestSwap { packets: Vec, /// The host's count of swings started, kept while the guest acts. swings: u32, + /// How many projectiles there were: those added since are the guest's. + projectiles: usize, } impl GuestPlay { @@ -298,6 +300,7 @@ impl Game<'_> { ); let packets = std::mem::take(&mut self.guest_packets); let swings = self.swings_started; + let projectiles = self.creatures.projectiles.len(); Some(GuestSwap { conn, i, @@ -341,6 +344,7 @@ impl Game<'_> { acting, packets, swings, + projectiles, }) } @@ -390,7 +394,14 @@ impl Game<'_> { acting, packets, swings, + projectiles, } = swap; + // What the guest shot is theirs (Projectile.setOwner). + for p in self.creatures.projectiles.iter_mut().skip(projectiles) { + if p.owner == Some(horizon_client::mobs::Who::Player) && p.guest.is_none() { + p.guest = Some(conn); + } + } // The guest's swings are theirs: the host's own count stays the host's. play.swings += self.swings_started.wrapping_sub(swings); self.swings_started = swings; diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index 7a1236e52..020ec6adc 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -324,7 +324,11 @@ impl Game<'_> { let mut e = state(p.id, kind, at(p.position), p.yaw, false); e.pitch = p.pitch; e.velocity = at(p.velocity); - e.data = owner(p.owner); + // Projectile.getAddEntityPacket: the owner's id. + e.data = match p.guest.and_then(|conn| hub.guest(conn)) { + Some(g) => g.entity_id, + None => owner(p.owner), + }; match &p.kind { K::Arrow { critical, .. } => { let a = p.arrow.as_deref(); diff --git a/crates/horizon-client/src/projectiles.rs b/crates/horizon-client/src/projectiles.rs index c2d4b4e3a..cc44e7511 100644 --- a/crates/horizon-client/src/projectiles.rs +++ b/crates/horizon-client/src/projectiles.rs @@ -67,6 +67,9 @@ pub struct Projectile { pub id: u32, pub kind: ProjectileKind, pub owner: Option, + /// On a server, the guest (by connection) who shot it: `Who::Player` + /// then means them, not the host. + pub guest: Option, pub position: Vec3, pub previous: Vec3, pub velocity: Vec3, @@ -133,6 +136,7 @@ impl Projectile { id: 0, kind, owner, + guest: None, position, previous: position, velocity: Vec3::ZERO, 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 bd450e965..3ed121fe9 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java @@ -464,6 +464,11 @@ public final class VanillaProtocolClient { int furnaceState = 0; // Snowballs the server spawned since the last right click. int snowballs = 0; + // The player's own id, and what the ender pearl it throws does to it. + int playerId = -1; + boolean pearl = false; + int pearlTeleports = 0; + float pearlHealth = -1; // The villager the server's console summoned beside the spawn. int villager = -1; net.minecraft.world.phys.Vec3 villagerAt = null; @@ -496,13 +501,20 @@ public final class VanillaProtocolClient { loadStart = true; } } + if (packet instanceof net.minecraft.network.protocol.game.ClientboundLoginPacket p) playerId = p.playerId(); + // The first health after the pearl: the hurt (regeneration follows each server's ticks). + if (pearl && pearlHealth < 0 && packet instanceof net.minecraft.network.protocol.game.ClientboundSetHealthPacket p) { + pearlHealth = p.getHealth(); + } if (packet instanceof ClientboundKeepAlivePacket p) { send(new ServerboundKeepAlivePacket(p.getId())); } else if (packet instanceof ClientboundPingPacket p) { send(new ServerboundPongPacket(p.getId())); } else if (packet instanceof ClientboundPlayerPositionPacket p) { position = PositionMoveRotation.calculateAbsolute(position, p.change(), p.relatives()); - record("play-move", text(position.position()) + " " + position.yRot() + " " + position.xRot()); + // Where a pearl lands follows its random inaccuracy: only counted. + if (pearl) pearlTeleports++; + else record("play-move", text(position.position()) + " " + position.yRot() + " " + position.xRot()); if (respawning) respawnAt = position; send(new ServerboundAcceptTeleportationPacket(p.id(), position.position().x, position.position().y, position.position().z, position.yRot(), position.xRot())); @@ -560,6 +572,8 @@ public final class VanillaProtocolClient { } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket p && p.getType() == net.minecraft.world.entity.EntityTypes.SNOWBALL) { snowballs++; + // Projectile.getAddEntityPacket: its owner's id. + record("play-use", "snowball owner is the player: " + (p.getData() == playerId)); } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket p && p.getType() == net.minecraft.world.entity.EntityTypes.VILLAGER) { villager = p.getId(); @@ -736,6 +750,21 @@ public final class VanillaProtocolClient { stepAt = now; } else if (step == 16 + REPLIES.length && now - stepAt > 2000) { record("play-use", "off hand on a block: " + snowballs + " snowball(s)"); + // An ender pearl thrown at its feet (no endermite: ThrownEnderpearl spawns one 5% of the time). + send(new ServerboundChatCommandPacket("gamerule spawn_monsters false")); + send(new ServerboundChatCommandPacket("item replace entity @s weapon.offhand with minecraft:air")); + send(new ServerboundChatCommandPacket("item replace entity @s weapon.mainhand with minecraft:ender_pearl")); + step++; + stepAt = now; + } else if (step == 17 + REPLIES.length && now - stepAt > 1500) { + pearl = true; + send(new net.minecraft.network.protocol.game.ServerboundUseItemPacket(net.minecraft.world.InteractionHand.MAIN_HAND, 7, + position.yRot(), 90.0F)); + step++; + stepAt = now; + } else if (step == 18 + REPLIES.length && now - stepAt > 2500) { + // ThrownEnderpearl.onHit: the thrower is teleported and takes 5 damage. + record("play-pearl", "teleported " + pearlTeleports + " time(s), health " + pearlHealth); return; } } diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index e23891fa7..83feb250f 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -16,7 +16,8 @@ chunks load start" event and the chunk under the player), sets a furnace beside itself, gives itself raw iron and coal, sets and asks its own score (`@s`), opens the furnace, shift-clicks the ore and the coal in and the ingot out, right-clicks the ground with a -snowball in the main hand, then in the off hand, and leaves. The +snowball in the main hand, then in the off hand, throws an ender pearl at +its feet, and leaves. The console then runs `list` and `say` and `stop`. Compared, all of them exactly: @@ -37,7 +38,8 @@ exactly: shift-click changes and its lit and cooking totals; - the snowballs each right click throws: one, though the client sends the block's part and the item's of each hand as separate packets - (handleUseItemOn, handleUseItem); + (handleUseItemOn, handleUseItem), and that the player is their owner; +- the ender pearl's teleport of the player who threw it, and the 5 damage; - the console lines about the player (an operator's command feedback, which vanilla broadcasts to the console, included), the chat, `list`, `say` and `stop`;