From 34f9cb83fd58915f8734d2daf9c1a846a7690ef1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 9 Oct 2026 05:20:25 +0200 Subject: [PATCH] Add adventure and spectator modes Spectators fly through blocks without overlays, a hotbar or a hand, and do not interact, pick up items or trigger plates; adventure players cannot build. The game mode is saved and restored. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 77 +++++++++++++++---- crates/horizon-client/src/game/boats.rs | 2 +- crates/horizon-client/src/game/commands.rs | 7 +- crates/horizon-client/src/game/creatures.rs | 3 +- crates/horizon-client/src/game/game_mode.rs | 72 +++++++++++++++++ crates/horizon-client/src/game/minecarts.rs | 2 +- crates/horizon-client/src/game/remote.rs | 6 +- crates/horizon-client/src/game/render_tick.rs | 7 +- crates/horizon-client/src/motion.rs | 15 +++- crates/horizon-client/src/save.rs | 30 ++++++-- 10 files changed, 183 insertions(+), 38 deletions(-) create mode 100644 crates/horizon-client/src/game/game_mode.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 5c068ad13..93addb69a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -19,6 +19,7 @@ mod fireworks; mod fishing; mod fov; mod frames; +mod game_mode; mod growth; mod jukebox; mod lightning; @@ -460,6 +461,8 @@ struct Game<'a> { destroy_delay: u32, use_delay: u32, creative: bool, + /// The game mode; `creative` mirrors `mode == Creative`. + mode: horizon_client::command::args::GameMode, keys: HashSet, left: bool, right: bool, @@ -994,7 +997,7 @@ impl Game<'_> { self.dropped = items; return; }; - if p.health.dead_or_dying() { + if p.health.dead_or_dying() || self.spectator() { self.dropped = items; return; } @@ -1054,7 +1057,9 @@ impl Game<'_> { ) -> R { let darken = self.sky_darken(); let (rain, thunder) = self.world.weather.levels(1.0); - let player_box = self.player.as_ref().map(|p| { + // Spectators ignore block triggers (Entity.isIgnoringBlockTriggers). + let spectator = self.spectator(); + let player_box = self.player.as_ref().filter(|_| !spectator).map(|p| { let w = f64::from(p.dimensions.width) / 2.0; ( [p.position.x - w, p.position.y, p.position.z - w], @@ -1565,6 +1570,10 @@ impl Game<'_> { Ok(()) } fn use_item(&mut self) -> Result<()> { + // Spectators only look (MultiPlayerGameMode.useItemOn/useItem pass). + if self.spectator() { + return Ok(()); + } if self.interact_frame() || self.interact_armor_stand() || self.interact_boat() @@ -1598,7 +1607,7 @@ impl Game<'_> { let player = PlayerContext { yaw: self.yaw, pitch: self.pitch, - may_build: true, + may_build: self.may_build(), can_eat: self.creative || self .player @@ -1693,6 +1702,9 @@ impl Game<'_> { self.use_offhand(); return Ok(()); } + if !self.may_build() { + return Ok(()); + } let face = match hit.face { [0, 1, 0] => Direction::Up, [0, -1, 0] => Direction::Down, @@ -1885,6 +1897,11 @@ impl Game<'_> { self.mining = None; return Ok(()); }; + // GameType.isBlockPlacingRestricted: adventure and spectator. + if !self.may_build() { + self.mining = None; + return Ok(()); + } if self.creative { if self.destroy_delay == 0 { self.break_block(hit.pos)?; @@ -2092,8 +2109,10 @@ impl Game<'_> { }; let mut player = PlayerMotion::new(self.motion, feet)?; player.yaw = self.yaw; - player.may_fly = self.creative; - player.invulnerable = self.creative; + let spectator = self.mode == horizon_client::command::args::GameMode::Spectator; + player.may_fly = self.creative || spectator; + player.invulnerable = self.creative || spectator; + player.spectator = spectator; player.pitch = self.pitch; // LivingEntity.canGlideUsing: a glider in its slot that will not break next. player.glider = self.armor[1].as_ref().is_some_and(|s| { @@ -2175,8 +2194,10 @@ impl Game<'_> { self.previous_position = player.position; } player.yaw = self.yaw; - player.may_fly = self.creative; - player.invulnerable = self.creative; + let spectator = self.mode == horizon_client::command::args::GameMode::Spectator; + player.may_fly = self.creative || spectator; + player.invulnerable = self.creative || spectator; + player.spectator = spectator; player.pitch = self.pitch; // LivingEntity.canGlideUsing: a glider in its slot that will not break next. player.glider = self.armor[1].as_ref().is_some_and(|s| { @@ -2659,7 +2680,12 @@ impl Game<'_> { (w - 15.0) / 2.0, (h - 15.0) / 2.0, ); - // Hotbar. + } + // Gui: spectators get the (closed) spectator menu instead of the + // hotbar and no player health. + let spectator = self.spectator(); + if !spectator { + let atlas = &self.res.atlas; batch.sprite(atlas, "gui/sprites/hud/hotbar", w / 2.0 - 91.0, h - 22.0); batch.sprite( atlas, @@ -2669,7 +2695,7 @@ impl Game<'_> { ); } // Hud.extractItemHotbar: the off hand left of the hotbar (right-handed). - if let Some(stack) = self.offhand.clone() { + if let Some(stack) = self.offhand.clone().filter(|_| !spectator) { batch.sprite( &self.res.atlas, "gui/sprites/hud/hotbar_offhand_left", @@ -2679,7 +2705,7 @@ impl Game<'_> { self.draw_item(&mut batch, &stack, w / 2.0 - 91.0 - 26.0, h - 19.0); } for i in 0..9 { - if let Some(stack) = self.inventory[i].clone() { + if let Some(stack) = self.inventory[i].clone().filter(|_| !spectator) { self.draw_item( &mut batch, &stack, @@ -2694,6 +2720,7 @@ impl Game<'_> { self.draw_titles(&mut batch); let atlas = &self.res.atlas; if !self.creative + && !spectator && let Some(p) = &self.player { // Experience bar and hearts/food (Gui.renderPlayerHealth). @@ -3957,6 +3984,9 @@ impl Game<'_> { } /// Player.attack with the attack-strength charge, crits and knockback. fn attack(&mut self) -> bool { + if self.spectator() { + return false; + } // A painting nearer than the targeted block and mob breaks instead. if let Some((i, d)) = self.painting_hit() && self.mob_hit().is_none_or(|m| d < m.1) @@ -4404,7 +4434,7 @@ impl Game<'_> { thunder_level: if thunder { (1.0, 1.0) } else { (0.0, 0.0) }, }; } - self.creative = level.creative; + self.set_mode_id(level.mode()); let mut inventory = vec![None; 36]; self.armor = [None, None, None, None]; self.offhand = None; @@ -5365,7 +5395,11 @@ impl Game<'_> { if self.xp_delay > 0 { self.xp_delay -= 1; } - let player = self.player.as_ref().filter(|p| !p.health.dead_or_dying()); + let spectator = self.spectator(); + let player = self + .player + .as_ref() + .filter(|p| !p.health.dead_or_dying() && !spectator); let target = player.map(|p| { [ p.position.x, @@ -5582,10 +5616,11 @@ impl Game<'_> { /// space, converted to camera-relative world space. fn hand_geometry(&mut self, alpha: f64) -> Vec { let mut out = Vec::new(); - if self - .player - .as_ref() - .is_none_or(|p| p.health.dead_or_dying()) + if self.spectator() + || self + .player + .as_ref() + .is_none_or(|p| p.health.dead_or_dying()) || self.screen == Screen::Dead { return out; @@ -6284,8 +6319,10 @@ impl Game<'_> { self.save_maps(); self.save_fight(); self.level.selected_slot = self.selected as i32; + let mode = self.mode_id(); let level = &mut self.level; level.creative = self.creative; + level.game_mode = mode; level.time = self.world.game_time; level.weather = self.world.weather; level.day_time = self.world.day_time; @@ -6416,7 +6453,7 @@ impl Game<'_> { sky.sky_light_factor = 1.0; } // LevelRenderer.renderHitOutline: the outline shape's edges. - let outline = if self.screen == Screen::Playing { + let outline = if self.screen == Screen::Playing && !self.spectator() { self.pick() .map(|h| { let state = self.world.block(h.pos[0], h.pos[1], h.pos[2]); @@ -6576,6 +6613,7 @@ impl Game<'_> { KeyCode::KeyE if self.screen == Screen::Creative && self.creative_screen.tab().search => {} KeyCode::KeyE => match self.screen { + Screen::Playing if self.spectator() => {} Screen::Playing => { self.screen = if self.creative { Screen::Creative @@ -7295,6 +7333,11 @@ pub fn run() -> Result<()> { destroy_delay: 0, use_delay: 0, creative, + mode: if creative { + horizon_client::command::args::GameMode::Creative + } else { + horizon_client::command::args::GameMode::Survival + }, keys: HashSet::new(), left: false, right: false, diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index f426fa059..bbd20997e 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -427,7 +427,7 @@ impl Game<'_> { } /// `getDismountLocationForPassenger`: in front of the player if there is /// floor there, else on top of the boat. - fn dismount(&mut self, i: usize) { + pub(super) fn dismount(&mut self, i: usize) { self.riding = None; let b = &self.boats[i]; let r = self.yaw.to_radians(); diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 6b23bd624..8be55e8ed 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -503,12 +503,11 @@ impl Game<'_> { } } fn set_game_mode(&mut self, mode: GameMode) -> bool { - let creative = mode == GameMode::Creative; - if self.creative == creative && matches!(mode, GameMode::Survival | GameMode::Creative) { + if self.mode == mode { return false; } - self.creative = creative; - if self.screen == Screen::Inventory && creative { + self.set_mode(mode); + if self.screen == Screen::Inventory && (self.creative || self.spectator()) { self.screen = Screen::Playing; } true diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 6e9fe2111..6f449c291 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -121,7 +121,8 @@ impl Game<'_> { p.position.z, ], alive: !p.health.dead_or_dying(), - creative: self.creative, + // EntitySelector.NO_CREATIVE_OR_SPECTATOR. + creative: self.creative || self.spectator(), sneaking: self.keys.contains(&winit::keyboard::KeyCode::ShiftLeft), look: view_vector(self.yaw, self.pitch), main_hand: self.held().map(|s| s.item.clone()), diff --git a/crates/horizon-client/src/game/game_mode.rs b/crates/horizon-client/src/game/game_mode.rs new file mode 100644 index 000000000..50d624796 --- /dev/null +++ b/crates/horizon-client/src/game/game_mode.rs @@ -0,0 +1,72 @@ +//! Game modes beyond survival and creative (`GameType`): adventure players +//! cannot build, spectators fly through blocks and only watch. +use super::{Game, Screen}; +use horizon_client::command::args::GameMode; + +impl Game<'_> { + pub(super) fn spectator(&self) -> bool { + self.mode == GameMode::Spectator + } + /// `Abilities.mayBuild`. + pub(super) fn may_build(&self) -> bool { + matches!(self.mode, GameMode::Survival | GameMode::Creative) + } + pub(super) fn mode_id(&self) -> i32 { + match self.mode { + GameMode::Survival => 0, + GameMode::Creative => 1, + GameMode::Adventure => 2, + GameMode::Spectator => 3, + } + } + pub(super) fn set_mode_id(&mut self, id: i32) { + let mode = match id { + 1 => GameMode::Creative, + 2 => GameMode::Adventure, + 3 => GameMode::Spectator, + _ => GameMode::Survival, + }; + self.set_mode(mode); + } + /// `ServerPlayer.setGameMode` and `GameType.updatePlayerAbilities`. + pub(super) fn set_mode(&mut self, mode: GameMode) { + let was_spectator = self.spectator(); + self.mode = mode; + self.creative = mode == GameMode::Creative; + if mode == GameMode::Spectator { + self.mining = None; + self.using = None; + self.left = false; + self.dismount_for_spectator(); + if let Some(p) = self.player.as_mut() { + p.flying = true; + p.spectator = true; + } + } else if let Some(p) = self.player.as_mut() { + p.spectator = false; + if !self.creative { + p.flying = false; + } else if was_spectator { + p.flying = true; + } + } + if mode == GameMode::Spectator && self.screen == Screen::Inventory { + self.screen = Screen::Playing; + } + } +} +impl Game<'_> { + /// `ServerPlayer.setGameMode(SPECTATOR)`: `stopRiding`. + fn dismount_for_spectator(&mut self) { + let Some(id) = self.riding else { + return; + }; + if let Some(i) = self.boats.iter().position(|b| b.id == id) { + self.dismount(i); + } else if let Some(i) = self.carts.iter().position(|c| c.id == id) { + self.dismount_cart(i); + } else { + self.riding = None; + } + } +} diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index fe9cd28d2..259758994 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -813,7 +813,7 @@ impl Game<'_> { true } /// `getDismountLocationForPassenger`: beside the cart (across its motion), else on top. - fn dismount_cart(&mut self, i: usize) { + pub(super) fn dismount_cart(&mut self, i: usize) { self.riding = None; let c = &self.carts[i]; let (bx, by, bz) = ( diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index a5cae32ec..3cc52f459 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -213,7 +213,7 @@ impl Game<'_> { self.container = None; self.carried = None; self.using = None; - self.creative = false; + self.set_mode_id(0); self.experience = Experience { level: 0, progress: 0.0, @@ -425,7 +425,7 @@ impl Game<'_> { r.dimension = dimension; r.game_mode = spawn.game_type; r.loaded_sent = false; - self.creative = spawn.game_type == 1; + self.set_mode_id(spawn.game_type); self.dim = if spawn.dimension == "minecraft:the_nether" { Dim::Nether } else { @@ -997,7 +997,7 @@ impl Game<'_> { } 3 => { let mode = p.param as i32; - self.creative = mode == 1; + self.set_mode_id(mode); if let Some(r) = self.remote.as_mut() { r.game_mode = mode; } diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index 26da78dfa..b978ce1f8 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -111,9 +111,11 @@ impl Game<'_> { p.position.z, ]; // LevelExtractor.getViewBlockingState: 8 points around the eye. + // ScreenEffectRenderer: none of it for a spectator (noPhysics). + let spectator = self.spectator(); let mut block_overlay = None; let w = f64::from(p.dimensions.width) * 0.8; - for i in 0..8 { + for i in (0..8).filter(|_| !spectator) { let t = [ p.position.x + (f64::from(i & 1) - 0.5) * w, eye[1] + (f64::from((i >> 1) & 1) - 0.5) * 0.1, @@ -135,7 +137,8 @@ impl Game<'_> { } let in_water = fog_type(&self.world, &self.res.facts, eye, self.forward()) == FogType::Water; - let water = in_water.then(|| (self.eye_brightness(eye), self.yaw, self.pitch)); + let water = + (in_water && !spectator).then(|| (self.eye_brightness(eye), self.yaw, self.pitch)); let camera_overlays = match &self.armor[0] { Some(stack) if &*stack.item == "minecraft:carved_pumpkin" => { vec!["misc/pumpkinblur".to_owned()] diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 5f87437c2..e9e64229e 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -142,6 +142,8 @@ pub struct PlayerMotion { pub may_fly: bool, /// Abilities.invulnerable (creative): only damage bypassing invulnerability hurts. pub invulnerable: bool, + /// Spectator mode: no physics, always flying (`Player.tick`). + pub spectator: bool, pub flying: bool, /// The jump key last tick (LocalPlayer.aiStep's wasJumping). pub was_jumping: bool, @@ -225,6 +227,7 @@ impl PlayerMotion { jump_trigger_time: 0, may_fly: false, invulnerable: false, + spectator: false, flying: false, was_jumping: false, glider: false, @@ -465,7 +468,10 @@ impl MotionWorld<'_, '_, '_> { // LocalPlayer.aiStep: double-tap jump toggles creative flight. let was_jumping = std::mem::replace(&mut self.player.was_jumping, jumping); let flying_before = self.player.flying; - if !self.player.may_fly { + if self.player.spectator { + self.player.flying = true; + self.player.ground.on_ground = false; + } else if !self.player.may_fly { self.player.flying = false; } else if !was_jumping && jumping { if self.player.jump_trigger_time == 0 { @@ -509,9 +515,12 @@ impl MotionWorld<'_, '_, '_> { direction, jumping: jumping && !self.player.flying, })?; + if self.player.spectator { + self.player.ground.on_ground = false; + } if self.player.flying { self.player.fall_distance = 0.0; - if self.player.ground.on_ground { + if self.player.ground.on_ground && !self.player.spectator { self.player.flying = false; } } @@ -733,7 +742,7 @@ impl EdgeBackoffWorld for MotionWorld<'_, '_, '_> { impl EntityMoveWorld for MotionWorld<'_, '_, '_> { type Error = Box; fn no_physics(&self) -> bool { - false + self.player.spectator } fn position(&self) -> Vec3 { self.player.position diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 215cee37f..bda61a014 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -1260,6 +1260,9 @@ pub struct Level { pub name: String, pub seed: i64, pub creative: bool, + /// The game mode id (0 survival, 1 creative, 2 adventure, 3 spectator); + /// `creative` wins when the two disagree. + pub game_mode: i32, pub time: i64, pub day_time: i64, /// The player's respawn point (bed, or the world spawn). @@ -1310,6 +1313,14 @@ impl Level { .or_else(|| GAME_RULES.iter().find(|(n, _)| *n == name).map(|(_, v)| *v)) .unwrap_or(0) } + /// The saved game mode id. + pub fn mode(&self) -> i32 { + match (self.creative, self.game_mode) { + (true, _) => 1, + (false, 1) => 0, + (false, m) => m.clamp(0, 3), + } + } pub fn read(dir: &Path) -> Option { let root = read_dat(&dir.join("level.dat"))?; let data = root.get("Data")?; @@ -1317,6 +1328,7 @@ impl Level { let mut level = Self { name: text(data.get("LevelName")).unwrap_or_else(|| "World".into()), creative: n(data.get("GameType")) == 1, + game_mode: n(data.get("GameType")) as i32, time: n(data.get("Time")), health: 20.0, food: 20, @@ -1436,6 +1448,7 @@ impl Level { self.on_ground = n("OnGround").unwrap_or(0) != 0; if let Some(mode) = n("playerGameType") { self.creative = mode == 1; + self.game_mode = mode as i32; } if let Some(d) = text(p.get("Dimension")) { self.dimension = d; @@ -1534,16 +1547,21 @@ impl Level { _ => {} } } - let creative = self.creative; + // GameType.updatePlayerAbilities. + let mode = self.mode(); + let (creative, spectator) = (mode == 1, mode == 3); let mut abilities = compound_or_empty(p.get("abilities").cloned()); for (k, v) in [ - ("invulnerable", creative), - ("mayfly", creative), + ("invulnerable", creative || spectator), + ("mayfly", creative || spectator), ("instabuild", creative), + ("mayBuild", mode < 2), ] { put(&mut abilities, k, Tag::Byte(i8::from(v))); } - if !creative { + if spectator { + put(&mut abilities, "flying", Tag::Byte(1)); + } else if !creative { put(&mut abilities, "flying", Tag::Byte(0)); } if let Tag::Compound(map) = &mut abilities { @@ -1593,7 +1611,7 @@ impl Level { .collect(), ), ), - ("playerGameType", Tag::Int(i32::from(self.creative))), + ("playerGameType", Tag::Int(self.mode())), ("Inventory", Tag::List(inventory)), ("EnderItems", encode_items(&self.ender_items)), ("abilities", abilities), @@ -1762,7 +1780,7 @@ impl Level { ("version_history", Tag::List(history)), ("version", Tag::Int(19133)), ("LevelName", string(&self.name)), - ("GameType", Tag::Int(i32::from(self.creative))), + ("GameType", Tag::Int(self.mode())), ("Time", Tag::Long(self.time)), ("LastPlayed", Tag::Long(now)), (