From 3cfb7b80533b648558bed6d637f3c46c2323be29 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 04:49:52 +0200 Subject: [PATCH] Mods' commands in the chat's command tree (#456) A mod's commands ran but the chat box parsed against vanilla's tree only, so `/coords` showed red as unknown and never completed. The game now keeps the chat's tree as vanilla's plus a literal per running mod's command (every permission level, optional greedy `args`), rebuilt when mods start, reload or stop. Fixes #456 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/chat_tests.rs | 38 ++++++++++++++++++++++ crates/horizon-client/src/command/mod.rs | 16 +++++++++ crates/horizon-client/src/command/tests.rs | 22 +++++++++++++ crates/horizon-client/src/game/chat.rs | 14 ++++---- crates/horizon-client/src/game/mods.rs | 24 +++++++++++++- docs/mods.md | 3 ++ 6 files changed, 109 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/chat_tests.rs b/crates/horizon-client/src/chat_tests.rs index 73e56af27..3cbac071b 100644 --- a/crates/horizon-client/src/chat_tests.rs +++ b/crates/horizon-client/src/chat_tests.rs @@ -192,3 +192,41 @@ fn history_arrows_recall_messages_and_restore_the_draft() { assert_eq!(screen.value(), expected); } } + +#[test] +fn mod_commands_are_coloured_and_complete_like_vanilla_ones() { + let font = font(); + let m = Measure(&font); + // Allow Commands off: a mod's command needs no level. + let registries = Registries::default(); + let vanilla = crate::command::vanilla::game_dispatcher(); + let modded = vanilla.with_mod_commands(["where", "coords"]); + let red = |screen: &ChatScreen| { + screen + .format(screen.value(), 0) + .spans + .iter() + .any(|s| s.style.color == Some(text::RED)) + }; + let typed = |dispatcher: &crate::command::Dispatcher, line: &str| { + let c = Commands { + dispatcher, + registries: ®istries, + }; + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + screen.type_text(&m, &c, line); + screen + }; + // Unknown without the mod's tree (#456). + assert!(red(&typed(vanilla, "/coords"))); + for line in ["/coords", "/where", "/where me and you"] { + assert!(!red(&typed(&modded, line)), "{line}"); + } + let c = Commands { + dispatcher: &modded, + registries: ®istries, + }; + let mut screen = typed(&modded, "/coo"); + screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false); + assert_eq!(screen.value(), "/coords"); +} diff --git a/crates/horizon-client/src/command/mod.rs b/crates/horizon-client/src/command/mod.rs index 3ff96d29b..52e76100a 100644 --- a/crates/horizon-client/src/command/mod.rs +++ b/crates/horizon-client/src/command/mod.rs @@ -23,6 +23,9 @@ mod vanilla_order; use crate::text::Text; pub use args::{Arg, ArgType, Registries}; +/// The command id of a mod's command (`Dispatcher::with_mod_commands`). +pub const MOD_COMMAND: &str = "mod"; + /// A command error (CommandSyntaxException): the message and, for parse /// errors, the input and cursor it occurred at. #[derive(Clone, Debug, PartialEq)] @@ -770,6 +773,19 @@ impl Dispatcher { self.nodes[node].permission = level; node } + /// This tree plus the commands mods own (`docs/mods.md`), so the chat + /// colours and completes them like the others. Every player may use one, + /// and the text after its name goes to the mod as it was typed. + pub fn with_mod_commands<'a>(&self, names: impl IntoIterator) -> Dispatcher { + let mut d = self.clone(); + for name in names { + let command = d.literal(0, name); + d.executes(command, MOD_COMMAND); + let args = d.argument(command, "args", ArgType::Greedy); + d.executes(args, MOD_COMMAND); + } + d + } /// A root literal by name. pub fn find(&self, name: &str) -> Option { self.nodes[0] diff --git a/crates/horizon-client/src/command/tests.rs b/crates/horizon-client/src/command/tests.rs index 1bd23be82..2bdea1aba 100644 --- a/crates/horizon-client/src/command/tests.rs +++ b/crates/horizon-client/src/command/tests.rs @@ -570,3 +570,25 @@ fn place_template_tree() { assert!(run("place template minecraft:x ~ ~ ~ sideways").is_err()); assert!(run("place template minecraft:x ~ ~ ~ none none 2").is_err()); } +#[test] +fn mod_commands_join_the_tree_for_every_player() { + let reg = Registries { + permission: 0, + ..reg() + }; + let d = vanilla::game_dispatcher().with_mod_commands(["where"]); + for line in ["where", "where north 12"] { + let parse = d.parse(line, ®); + assert_eq!(parse.executable().unwrap().1, MOD_COMMAND, "{line}"); + } + let parse = d.parse("w", ®); + let names: Vec = d + .client_suggestions(&parse, 1, ®) + .1 + .into_iter() + .map(|s| s.text) + .collect(); + assert!(names.contains(&"where".to_owned()), "{names:?}"); + // The game's own tree is left as it was. + assert!(vanilla::game_dispatcher().find("where").is_none()); +} diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index bd07107b0..16c5295c9 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -3,7 +3,7 @@ use super::{Dim, Game, Screen}; use horizon_client::{ chat::{Chat, ChatAction, ChatScreen, Commands, SYSTEM_INDICATOR}, - command::{Registries, vanilla}, + command::Registries, gamerules::GameRules, hud::{HudBatch, rgba}, screen_draw::Measure, @@ -391,7 +391,7 @@ impl Game<'_> { let b = HudBatch::new(size); let m = Measure(&self.res.atlas.font); let c = Commands { - dispatcher: vanilla::game_dispatcher(), + dispatcher: self.mods.dispatcher(), registries: &self.ui.registries, }; let mut screen = ChatScreen::new( @@ -443,7 +443,7 @@ impl Game<'_> { if let Some(shortcut) = self.edit_shortcut(event) { let m = Measure(&self.res.atlas.font); let c = Commands { - dispatcher: vanilla::game_dispatcher(), + dispatcher: self.mods.dispatcher(), registries: &self.ui.registries, }; if let Some(screen) = self.ui.chat_screen.as_mut() { @@ -489,7 +489,7 @@ impl Game<'_> { }; let m = Measure(&self.res.atlas.font); let c = Commands { - dispatcher: vanilla::game_dispatcher(), + dispatcher: self.mods.dispatcher(), registries: &self.ui.registries, }; match screen.key(&m, &c, &self.ui.chat.recent, key, shift, ctrl) { @@ -515,7 +515,7 @@ impl Game<'_> { pub(super) fn chat_type(&mut self, text: &str) { let m = Measure(&self.res.atlas.font); let c = Commands { - dispatcher: vanilla::game_dispatcher(), + dispatcher: self.mods.dispatcher(), registries: &self.ui.registries, }; if let Some(screen) = self.ui.chat_screen.as_mut() { @@ -566,7 +566,7 @@ impl Game<'_> { }); let m = Measure(&self.res.atlas.font); let c = Commands { - dispatcher: vanilla::game_dispatcher(), + dispatcher: self.mods.dispatcher(), registries: &self.ui.registries, }; if let Some(screen) = self.ui.chat_screen.as_mut() { @@ -613,7 +613,7 @@ impl Game<'_> { }; let m = Measure(&self.res.atlas.font); let c = Commands { - dispatcher: vanilla::game_dispatcher(), + dispatcher: self.mods.dispatcher(), registries: &self.ui.registries, }; screen.resize(&m, &c, batch.width as i32, batch.height as i32); diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 20981ac45..56cb1d0d8 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -4,6 +4,7 @@ //! every hook here returns after one check. use super::{Game, Screen}; use horizon_client::{ + command::{Dispatcher, vanilla}, extras::Corner, extras_hud::{self, Corners}, hud::HudBatch, @@ -23,13 +24,23 @@ pub(super) struct ModsState { pub(super) dev: Vec, /// When dev mods were last checked for a rebuild. checked: Option, + /// The chat's command tree while a running mod owns commands. + tree: Option, +} +impl ModsState { + /// The chat's command tree: vanilla's, plus the running mods' commands. + pub(super) fn dispatcher(&self) -> &Dispatcher { + self.tree + .as_ref() + .unwrap_or_else(|| vanilla::game_dispatcher()) + } } /// How often dev mods are checked for a rebuild. const RELOAD_CHECK: Duration = Duration::from_secs(1); /// A vanilla command name: mods may not take it. fn vanilla_command(name: &str) -> bool { - let d = horizon_client::command::vanilla::game_dispatcher(); + let d = vanilla::game_dispatcher(); d.nodes[0] .children .iter() @@ -55,6 +66,7 @@ impl Game<'_> { let mods = Mods::load(&paths, on, vanilla_command); self.mods.mods = mods; self.list_mods(); + self.sync_mod_commands(); self.show_mod_output(); } /// The Mods page's rows, from what loaded. @@ -97,6 +109,14 @@ impl Game<'_> { }) .collect(); } + /// The chat's tree after the running mods changed. + fn sync_mod_commands(&mut self) { + let mut names = self.mods.mods.commands().peekable(); + self.mods.tree = names + .peek() + .is_some() + .then(|| vanilla::game_dispatcher().with_mod_commands(names)); + } /// After the options changed: restart the mods if the list of the ones /// turned on did. pub(super) fn sync_mods(&mut self) { @@ -160,6 +180,7 @@ impl Game<'_> { self.mods.checked = Some(Instant::now()); if self.mods.mods.reload_changed(vanilla_command) { self.list_mods(); + self.sync_mod_commands(); self.system_message(Text::literal("Mods reloaded").with_color(text::GRAY)); } } @@ -201,6 +222,7 @@ impl Game<'_> { Output::Feedback { text, error: true } => Text::literal(text).with_color(text::RED), Output::Stopped { name, reason } => { self.list_mods(); + self.sync_mod_commands(); Text::literal(format!("Mod {name} stopped: {reason}")).with_color(text::RED) } }; diff --git a/docs/mods.md b/docs/mods.md index f0687b693..8a52bdf5a 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -93,6 +93,9 @@ folder, loads no runtime and plays exactly like vanilla 26.3. Commands: vanilla's command names win. A mod asking for `/give` does not get it (logged), and two mods cannot share a name (the first one loaded keeps it). +The chat box colours and Tab-completes a mod's commands like vanilla's, for +every player whatever their permission level, and takes any text after the +name (the mod reads it as `args`; API 0.1 has no way to declare argument types). ### Planned interfaces