From 77b6fbad7f60442607c45598bc98607ed53d9b8b Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 22 Jun 2025 09:28:50 -0400 Subject: [PATCH] Fix the "unknown command" message for real, now Now it takes into account if the command exists on the Rust side, and the message should now *only* display if no one truly handles this command. --- src/bin/kawari-world.rs | 69 +++++++++++++++++++++------------------ src/world/chat_handler.rs | 20 +++++++++--- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 6c1bed9..f7970eb 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -504,12 +504,13 @@ async fn client_loop( let parts: Vec<&str> = chat_message.message.split(' ').collect(); let command_name = &parts[0][1..]; - let lua = lua.lock().unwrap(); - let state = lua.app_data_ref::().unwrap(); + { + let lua = lua.lock().unwrap(); + let state = lua.app_data_ref::().unwrap(); - // If a Lua command exists, try using that first - if let Some(command_script) = - state.command_scripts.get(command_name) + // If a Lua command exists, try using that first + if let Some(command_script) = + state.command_scripts.get(command_name) { handled = true; @@ -568,36 +569,40 @@ async fn client_loop( if let Err(err) = run_script() { tracing::warn!("Lua error in {file_name}: {:?}", err); - handled = false; - } - } else { - tracing::info!("Unknown command {command_name}"); - - let mut call_func = || { - lua.scope(|scope| { - let connection_data = scope - .create_userdata_ref_mut(&mut lua_player)?; - let func: Function = - lua.globals().get("onUnknownCommandError")?; - func.call::<()>((command_name, connection_data))?; - Ok(()) - }) - }; - - if let Err(err) = call_func() { - tracing::warn!("Lua error in Global.lua: {:?}", err); - handled = false; } } - } + } - // Fallback to Rust implemented commands - if !handled { - ChatHandler::handle_chat_message( - &mut connection, - chat_message, - ) - .await; + // Fallback to Rust implemented commands + if !handled { + handled = ChatHandler::handle_chat_message( + &mut connection, + chat_message, + ) + .await; + } + + // If it's truly not existent: + if !handled { + tracing::info!("Unknown command {command_name}"); + + let lua = lua.lock().unwrap(); + + let mut call_func = || { + lua.scope(|scope| { + let connection_data = scope + .create_userdata_ref_mut(&mut lua_player)?; + let func: Function = + lua.globals().get("onUnknownCommandError")?; + func.call::<()>((command_name, connection_data))?; + Ok(()) + }) + }; + + if let Err(err) = call_func() { + tracing::warn!("Lua error in Global.lua: {:?}", err); + } + } } } ClientZoneIpcData::GMCommand { command, arg0, arg1, .. } => { diff --git a/src/world/chat_handler.rs b/src/world/chat_handler.rs index 34a1ac2..ee13777 100644 --- a/src/world/chat_handler.rs +++ b/src/world/chat_handler.rs @@ -9,14 +9,18 @@ use super::ZoneConnection; pub struct ChatHandler {} impl ChatHandler { - pub async fn handle_chat_message(connection: &mut ZoneConnection, chat_message: &ChatMessage) { + /// Returns true if the command is handled, otherwise false. + pub async fn handle_chat_message( + connection: &mut ZoneConnection, + chat_message: &ChatMessage, + ) -> bool { if connection.player_data.gm_rank == GameMasterRank::NormalUser { tracing::info!("Rejecting debug command because the user is not GM!"); - return; + return true; } let parts: Vec<&str> = chat_message.message.split(' ').collect(); - match parts[0] { + return match parts[0] { "!spawnnpc" => { connection .handle @@ -25,6 +29,7 @@ impl ChatHandler { connection.player_data.actor_id, )) .await; + true } "!spawnmonster" => { connection @@ -34,6 +39,7 @@ impl ChatHandler { connection.player_data.actor_id, )) .await; + true } "!spawnclone" => { connection @@ -43,6 +49,7 @@ impl ChatHandler { connection.player_data.actor_id, )) .await; + true } "!equip" => { let (_, name) = chat_message.message.split_once(' ').unwrap(); @@ -69,10 +76,12 @@ impl ChatHandler { } connection.send_inventory(true).await; + true } "!reload" => { connection.reload_scripts(); connection.send_message("Scripts reloaded!").await; + true } "!finishevent" => { if let Some(event) = &connection.event { @@ -81,8 +90,9 @@ impl ChatHandler { .send_message("Current event forcefully finished.") .await; } + true } - _ => {} - } + _ => false, + }; } }