Merge pull request #1001 from ludovic111/fix/guest-projectile-owner

🤖 Agent: merged after verify passed. Remote players' projectiles record their owner: ender pearls teleport and hurt the thrower, mobs target the shooter; docs/server.md row updated; part of #484.
This commit is contained in:
Ludovic Marie
2026-10-19 11:52:38 +02:00
committed by GitHub
7 changed files with 103 additions and 9 deletions
+48 -4
View File
@@ -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;
@@ -125,6 +125,8 @@ pub(super) struct GuestSwap {
packets: Vec<play::Clientbound>,
/// 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;
@@ -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();
+4
View File
@@ -67,6 +67,9 @@ pub struct Projectile {
pub id: u32,
pub kind: ProjectileKind,
pub owner: Option<Who>,
/// On a server, the guest (by connection) who shot it: `Who::Player`
/// then means them, not the host.
pub guest: Option<u32>,
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,
+1 -1
View File
@@ -92,7 +92,7 @@ not tick cost, so it is in scope here (see section 9).
| Secure chat | **Works** (`SignedMessageChain`, `LastSeenMessagesValidator`, `MessageSignatureCache`): the chat session update and its profile key, checked against the services' `playerCertificateKeys`; signed chat, `say`, `me` and `msg`, relayed with their signatures and last-seen lists; `enforce-secure-profile` (with `online-mode`) refuses unsigned chat; and vanilla's log lines (`[Not Secure]`, the decoder's warnings, the kicks). Offline servers and players without a chat session chat unsigned, as on vanilla. `test_secure_chat_parity.py` runs the same signed and unsigned players against vanilla's server and Horizon's, with test keys and a mock services server. |
| Nether and End | **Players can't change dimension.** The engine keeps one dimension loaded (`Game::enter_dimension` moves only the local single-player player), and the hub serves one `dimension` to everyone. |
| Refused logins | Logged as vanilla does: `Disconnecting <name> (/address): <reason>`, then `<name> (/address) lost connection: <reason>`, and the session errors (`Username '<name>' tried to join with an invalid session`, `Couldn't verify username because servers are unavailable`). |
| Gameplay for remote players | The LAN guest leftovers apply: an item used on a block is used twice, the recipe book doesn't work, status effects don't apply to guests, arrows don't record their owner, team friendly fire is ignored (#484). Block desync and short freezes are reported on a same-machine server (#500). |
| Gameplay for remote players | Checked against vanilla's server with a vanilla-codec client (#484, `tools/test_dedicated_server_parity.py`): a right click with an item on a block uses it once, a dead player's commands run, players on a team without friendly fire don't hurt each other, and a player's projectiles are theirs (their ender pearl takes them). The recipe book, status effects and block sync while digging (#500) work. Left: a player's projectiles don't hit other players (no bow PvP between guests), and a mob a guest kills counts in the host's statistics and advancements, not the guest's (`mob_died` runs as the host). |
| Defaults that surprise owners | `white-list=true` is vanilla 26.3's default, so a fresh server refuses everyone until they are added. That matches vanilla, so the first-run flow (section 3) asks about the whitelist up front. |
| Management | Only stdin. There is no service, no backups, no status beyond `list`, no restart after a crash, and a server can't be managed while it runs in the background. |
| Packaging | The server is the full game client. Resources come from `horizon-prepare`, which needs a JDK and the official launcher's client install, both awkward on a headless VPS. |
@@ -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;
@@ -499,13 +504,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()));
@@ -566,6 +578,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();
@@ -745,6 +759,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;
}
}
+4 -2
View File
@@ -17,7 +17,8 @@ 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:
@@ -40,7 +41,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`;