Merge pull request #463 from ludovic111/fix/mod-commands-in-chat

Mod commands: coloured and Tab-completed in the chat like vanilla's (#456)
This commit is contained in:
Ludovic Marie
2026-10-16 09:50:12 +02:00
committed by GitHub
6 changed files with 109 additions and 8 deletions
+38
View File
@@ -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: &registries,
};
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: &registries,
};
let mut screen = typed(&modded, "/coo");
screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false);
assert_eq!(screen.value(), "/coords");
}
+16
View File
@@ -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<Item = &'a str>) -> 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<usize> {
self.nodes[0]
@@ -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, &reg);
assert_eq!(parse.executable().unwrap().1, MOD_COMMAND, "{line}");
}
let parse = d.parse("w", &reg);
let names: Vec<String> = d
.client_suggestions(&parse, 1, &reg)
.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());
}
+7 -7
View File
@@ -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);
+23 -1
View File
@@ -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<PathBuf>,
/// When dev mods were last checked for a rebuild.
checked: Option<Instant>,
/// The chat's command tree while a running mod owns commands.
tree: Option<Dispatcher>,
}
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)
}
};
+3
View File
@@ -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