[PR #4239] [MERGED] Spectators on a server see through the entity they spectate #5508

Closed
opened 2026-11-03 05:13:04 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/4239
Author: @d3grando
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: fix-3518-set-camera


📝 Commits (1)

  • 3c6999d Spectators on a server see through the entity they spectate

📊 Changes

7 files changed (+253 additions, -47 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+12 -2)
📝 crates/horizon-client/src/game/creatures.rs (+5 -0)
📝 crates/horizon-client/src/game/fov.rs (+8 -0)
📝 crates/horizon-client/src/game/perspective.rs (+55 -42)
📝 crates/horizon-client/src/game/remote.rs (+29 -3)
➕ crates/horizon-client/src/game/remote_camera.rs (+108 -0)
📝 crates/horizon-protocol/src/packets/play.rs (+36 -0)

📄 Description

🤖 Opened by d3grando's AI agent (Claude Code, Claude Opus 5.5), not by d3grando personally.

What and why

Part of #3518 (the ClientPacketListener.handleSetCamera row).

On a server, /spectate <entity> or a spectator's click sends ClientboundSetCameraPacket, and the server then keeps the player on that entity every tick (ServerPlayer.tick). Horizon decoded that packet as unknown, so the view stayed on the player. With a vanilla 26.3 server.jar:

  • the view stayed at the player's eye height inside the mob's body, and the pig's back filled the bottom of the screen;
  • the view did not turn when the mob turned (after /tp of the pig to yaw -90 the view still faced the old way);
  • the client kept sending its own position, so the server logged Dev moved too quickly! and pulled the player back;
  • a spectator's left click on a mob did nothing: Horizon sent no packet for it.

This PR ports the client side:

  • SetCamera (clientbound) and SpectatorAction (serverbound, OPTIONAL_VAR_INT) packets in horizon-protocol.
  • handleSetCamera → Minecraft.setCameraEntity. The camera entity's id is kept until the player's own id comes back. Login and respawn also reset it, as handleLogin/handleRespawn do. An id the level doesn't hold is ignored, as getEntity returning null is.
  • While another entity is the camera:
    • the view is aligned with it (Camera.alignWithEntity): its interpolated position, its view rotation (for a living entity the head's yaw, LivingEntity.getViewYRot) and its eye height, eased by Camera.tick, and its FOV modifier is 1;
    • it isn't drawn in first person (LevelRenderer);
    • the crosshair pick starts from its eyes along its head's view and skips it (Minecraft.pick, LocalPlayer.raycastHitResult);
    • the player neither moves nor sends moves (LocalPlayer.applyInput/aiStep/sendPosition check isControlledCamera). The input packet still goes out, so sneaking lets go of the camera as on vanilla.
  • A spectator's attack click sends ServerboundSpectatorActionPacket with the entity under the crosshair, or empty for a click on nothing (Minecraft.startAttack → MultiPlayerGameMode.spectate/spectatorNoAction).

The camera follows mobs and players, which live in Game::mobs. Spectating a boat, minecart, armor stand or display still keeps the view on the player as before (those types have no per-tick previous position/rotation here). That is left for a follow-up. Horizon's own server (single player's server thread, LAN) never sends SetCamera and ignores SpectatorAction (handleSpectatorAction is a separate ledger row), so single player is unchanged.

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Vanilla reference

Read with javap -c -p from the 26.3 jars:

  • ClientboundSetCameraPacket (VarInt cameraId, getEntity), ServerboundSpectatorActionPacket (ByteBufCodecs.OPTIONAL_VAR_INT)
  • ClientPacketListener.handleSetCamera, handleLogin, handleRespawn; Minecraft.setCameraEntity, pick, startAttack
  • Camera.alignWithEntity, Camera.tick, Camera.tickFov, LivingEntity.getViewYRot (yHeadRot)
  • LocalPlayer.isControlledCamera, applyInput, aiStep, sendPosition
  • MultiPlayerGameMode.spectate, spectatorNoAction
  • the server side for the test: ServerPlayer.setCamera (teleport plus SetCamera) and ServerPlayer.tick (snap to the camera, sneak lets go)

Tests: packets::play::tests::set_camera_is_the_camera_entity_id and spectator_action_is_an_optional_entity_id check the wire forms; game::remote_camera::tests::set_camera_follows_vanilla checks the camera choice. No parity harness covers a vanilla server's camera packets. The behaviour was checked in game against vanilla 26.3 server.jar (below).

Self-check

tools/pr_ready.py doesn't run on Windows (it needs SIGHUP), so these are the steps it runs, by hand:

cargo fmt --all -- --check                                             # ok
cargo clippy -p horizon-protocol --all-targets -- -D warnings          # ok
cargo clippy -p horizon-client --all-targets -- -D warnings            # ok
cargo test -p horizon-protocol --lib                                   # 112 passed
cargo test -p horizon-client --bin horizon-client -- remote perspective fov camera   # 37 passed, 1 ignored

Fails before, passes after. Only the two packet definitions and their enum entries were reverted; the tests stay as they are:

test packets::play::tests::set_camera_is_the_camera_entity_id ... FAILED        (Unknown(95, [172, 2]))
test packets::play::tests::spectator_action_is_an_optional_entity_id ... FAILED
test result: FAILED. 0 passed; 2 failed
# with the fix
test result: ok. 2 passed; 0 failed

In game (Windows, --profile fast, joined to vanilla 26.3 server.jar in offline mode, flat world, noon):

  • /gamemode spectator, then /spectate on a NoAI pig.
  • /tp the pig 35 blocks away: the view moves with it.
  • /tp the pig to yaw -90: the view turns to the diamond wall it faces.
  • Holding W for ~2 minutes while spectating: the player stays put, and the server logs no "moved too quickly" (before: one such warning and a pull-back).
  • Sneaking: the server lets go, the view returns to the player at the pig's place, and W moves the player again.
  • Left-clicking the pig as a spectator: the server attaches the camera (the player's server position becomes the pig's).
  • /spectate on a pig with AI in a one-block cage: the view looks around with the pig's head while its Rotation (yRot) stays [0.0f, 0.0f], as vanilla's LivingEntity.getViewYRot (yHeadRot) has it, and the pig's body is not drawn.

Screenshots

Same scene and time of day, spectating a pig on a vanilla server after /tp turned the pig to face a diamond wall (yaw -90):

  • Before: the view still faces the gold wall in the old direction, from the player's eye height.
  • After: the view turns with the pig to the diamond wall, from the pig's eye height, and the pig's body isn't drawn over the view.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/4239 **Author:** [@d3grando](https://github.com/d3grando) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `fix-3518-set-camera` --- ### 📝 Commits (1) - [`3c6999d`](https://github.com/horizon-rust/horizon/commit/3c6999d700465d6f3d3b49dafd5efce6675fcdc8) Spectators on a server see through the entity they spectate ### 📊 Changes **7 files changed** (+253 additions, -47 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+12 -2) 📝 `crates/horizon-client/src/game/creatures.rs` (+5 -0) 📝 `crates/horizon-client/src/game/fov.rs` (+8 -0) 📝 `crates/horizon-client/src/game/perspective.rs` (+55 -42) 📝 `crates/horizon-client/src/game/remote.rs` (+29 -3) ➕ `crates/horizon-client/src/game/remote_camera.rs` (+108 -0) 📝 `crates/horizon-protocol/src/packets/play.rs` (+36 -0) </details> ### 📄 Description > 🤖 Opened by d3grando's AI agent (Claude Code, Claude Opus 5.5), not by d3grando personally. ## What and why Part of #3518 (the `ClientPacketListener.handleSetCamera` row). On a server, `/spectate <entity>` or a spectator's click sends `ClientboundSetCameraPacket`, and the server then keeps the player on that entity every tick (`ServerPlayer.tick`). Horizon decoded that packet as unknown, so the view stayed on the player. With a vanilla 26.3 `server.jar`: - the view stayed at the player's eye height inside the mob's body, and the pig's back filled the bottom of the screen; - the view did not turn when the mob turned (after `/tp` of the pig to yaw -90 the view still faced the old way); - the client kept sending its own position, so the server logged `Dev moved too quickly!` and pulled the player back; - a spectator's left click on a mob did nothing: Horizon sent no packet for it. This PR ports the client side: - `SetCamera` (clientbound) and `SpectatorAction` (serverbound, `OPTIONAL_VAR_INT`) packets in `horizon-protocol`. - `handleSetCamera` → `Minecraft.setCameraEntity`. The camera entity's id is kept until the player's own id comes back. Login and respawn also reset it, as `handleLogin`/`handleRespawn` do. An id the level doesn't hold is ignored, as `getEntity` returning null is. - While another entity is the camera: - the view is aligned with it (`Camera.alignWithEntity`): its interpolated position, its view rotation (for a living entity the head's yaw, `LivingEntity.getViewYRot`) and its eye height, eased by `Camera.tick`, and its FOV modifier is 1; - it isn't drawn in first person (`LevelRenderer`); - the crosshair pick starts from its eyes along its head's view and skips it (`Minecraft.pick`, `LocalPlayer.raycastHitResult`); - the player neither moves nor sends moves (`LocalPlayer.applyInput`/`aiStep`/`sendPosition` check `isControlledCamera`). The input packet still goes out, so sneaking lets go of the camera as on vanilla. - A spectator's attack click sends `ServerboundSpectatorActionPacket` with the entity under the crosshair, or empty for a click on nothing (`Minecraft.startAttack` → `MultiPlayerGameMode.spectate`/`spectatorNoAction`). The camera follows mobs and players, which live in `Game::mobs`. Spectating a boat, minecart, armor stand or display still keeps the view on the player as before (those types have no per-tick previous position/rotation here). That is left for a follow-up. Horizon's own server (single player's server thread, LAN) never sends `SetCamera` and ignores `SpectatorAction` (`handleSpectatorAction` is a separate ledger row), so single player is unchanged. ## Kind - [x] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [ ] perf: faster, or less memory - [ ] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Vanilla reference Read with `javap -c -p` from the 26.3 jars: - `ClientboundSetCameraPacket` (VarInt `cameraId`, `getEntity`), `ServerboundSpectatorActionPacket` (`ByteBufCodecs.OPTIONAL_VAR_INT`) - `ClientPacketListener.handleSetCamera`, `handleLogin`, `handleRespawn`; `Minecraft.setCameraEntity`, `pick`, `startAttack` - `Camera.alignWithEntity`, `Camera.tick`, `Camera.tickFov`, `LivingEntity.getViewYRot` (`yHeadRot`) - `LocalPlayer.isControlledCamera`, `applyInput`, `aiStep`, `sendPosition` - `MultiPlayerGameMode.spectate`, `spectatorNoAction` - the server side for the test: `ServerPlayer.setCamera` (teleport plus `SetCamera`) and `ServerPlayer.tick` (snap to the camera, sneak lets go) Tests: `packets::play::tests::set_camera_is_the_camera_entity_id` and `spectator_action_is_an_optional_entity_id` check the wire forms; `game::remote_camera::tests::set_camera_follows_vanilla` checks the camera choice. No parity harness covers a vanilla server's camera packets. The behaviour was checked in game against vanilla 26.3 `server.jar` (below). ## Self-check `tools/pr_ready.py` doesn't run on Windows (it needs SIGHUP), so these are the steps it runs, by hand: ``` cargo fmt --all -- --check # ok cargo clippy -p horizon-protocol --all-targets -- -D warnings # ok cargo clippy -p horizon-client --all-targets -- -D warnings # ok cargo test -p horizon-protocol --lib # 112 passed cargo test -p horizon-client --bin horizon-client -- remote perspective fov camera # 37 passed, 1 ignored ``` Fails before, passes after. Only the two packet definitions and their enum entries were reverted; the tests stay as they are: ``` test packets::play::tests::set_camera_is_the_camera_entity_id ... FAILED (Unknown(95, [172, 2])) test packets::play::tests::spectator_action_is_an_optional_entity_id ... FAILED test result: FAILED. 0 passed; 2 failed # with the fix test result: ok. 2 passed; 0 failed ``` In game (Windows, `--profile fast`, joined to vanilla 26.3 `server.jar` in offline mode, flat world, noon): - `/gamemode spectator`, then `/spectate` on a NoAI pig. - `/tp` the pig 35 blocks away: the view moves with it. - `/tp` the pig to yaw -90: the view turns to the diamond wall it faces. - Holding W for ~2 minutes while spectating: the player stays put, and the server logs no "moved too quickly" (before: one such warning and a pull-back). - Sneaking: the server lets go, the view returns to the player at the pig's place, and W moves the player again. - Left-clicking the pig as a spectator: the server attaches the camera (the player's server position becomes the pig's). - `/spectate` on a pig with AI in a one-block cage: the view looks around with the pig's head while its `Rotation` (`yRot`) stays `[0.0f, 0.0f]`, as vanilla's `LivingEntity.getViewYRot` (`yHeadRot`) has it, and the pig's body is not drawn. ## Screenshots Same scene and time of day, spectating a pig on a vanilla server after `/tp` turned the pig to face a diamond wall (yaw -90): - Before: the view still faces the gold wall in the old direction, from the player's eye height. - After: the view turns with the pig to the diamond wall, from the pig's eye height, and the pig's body isn't drawn over the view. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 05:13:04 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#5508
No description provided.