1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-06-30 11:47:45 +00:00

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.
This commit is contained in:
Joshua Goins 2025-06-22 09:28:50 -04:00
parent fbe6862c7b
commit 77b6fbad7f
2 changed files with 52 additions and 37 deletions

View file

@ -504,12 +504,13 @@ async fn client_loop(
let parts: Vec<&str> = chat_message.message.split(' ').collect(); let parts: Vec<&str> = chat_message.message.split(' ').collect();
let command_name = &parts[0][1..]; let command_name = &parts[0][1..];
let lua = lua.lock().unwrap(); {
let state = lua.app_data_ref::<ExtraLuaState>().unwrap(); let lua = lua.lock().unwrap();
let state = lua.app_data_ref::<ExtraLuaState>().unwrap();
// If a Lua command exists, try using that first // If a Lua command exists, try using that first
if let Some(command_script) = if let Some(command_script) =
state.command_scripts.get(command_name) state.command_scripts.get(command_name)
{ {
handled = true; handled = true;
@ -568,36 +569,40 @@ async fn client_loop(
if let Err(err) = run_script() { if let Err(err) = run_script() {
tracing::warn!("Lua error in {file_name}: {:?}", err); 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 // Fallback to Rust implemented commands
if !handled { if !handled {
ChatHandler::handle_chat_message( handled = ChatHandler::handle_chat_message(
&mut connection, &mut connection,
chat_message, chat_message,
) )
.await; .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, .. } => { ClientZoneIpcData::GMCommand { command, arg0, arg1, .. } => {

View file

@ -9,14 +9,18 @@ use super::ZoneConnection;
pub struct ChatHandler {} pub struct ChatHandler {}
impl 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 { if connection.player_data.gm_rank == GameMasterRank::NormalUser {
tracing::info!("Rejecting debug command because the user is not GM!"); tracing::info!("Rejecting debug command because the user is not GM!");
return; return true;
} }
let parts: Vec<&str> = chat_message.message.split(' ').collect(); let parts: Vec<&str> = chat_message.message.split(' ').collect();
match parts[0] { return match parts[0] {
"!spawnnpc" => { "!spawnnpc" => {
connection connection
.handle .handle
@ -25,6 +29,7 @@ impl ChatHandler {
connection.player_data.actor_id, connection.player_data.actor_id,
)) ))
.await; .await;
true
} }
"!spawnmonster" => { "!spawnmonster" => {
connection connection
@ -34,6 +39,7 @@ impl ChatHandler {
connection.player_data.actor_id, connection.player_data.actor_id,
)) ))
.await; .await;
true
} }
"!spawnclone" => { "!spawnclone" => {
connection connection
@ -43,6 +49,7 @@ impl ChatHandler {
connection.player_data.actor_id, connection.player_data.actor_id,
)) ))
.await; .await;
true
} }
"!equip" => { "!equip" => {
let (_, name) = chat_message.message.split_once(' ').unwrap(); let (_, name) = chat_message.message.split_once(' ').unwrap();
@ -69,10 +76,12 @@ impl ChatHandler {
} }
connection.send_inventory(true).await; connection.send_inventory(true).await;
true
} }
"!reload" => { "!reload" => {
connection.reload_scripts(); connection.reload_scripts();
connection.send_message("Scripts reloaded!").await; connection.send_message("Scripts reloaded!").await;
true
} }
"!finishevent" => { "!finishevent" => {
if let Some(event) = &connection.event { if let Some(event) = &connection.event {
@ -81,8 +90,9 @@ impl ChatHandler {
.send_message("Current event forcefully finished.") .send_message("Current event forcefully finished.")
.await; .await;
} }
true
} }
_ => {} _ => false,
} };
} }
} }