diff --git a/TrueCraft/Commands/CommandManager.cs b/TrueCraft/Commands/CommandManager.cs index de7b4fb..0f9d1cd 100644 --- a/TrueCraft/Commands/CommandManager.cs +++ b/TrueCraft/Commands/CommandManager.cs @@ -29,51 +29,33 @@ namespace TrueCraft.Commands } /// - /// Handle the specified command if it exists. We run the check twice to separate - /// actual command names from command aliases to prevent aliases from being prioritized - /// over other command names. + /// Tries to find the specified command by first performing a + /// case-insensitive search on the command names, then a + /// case-sensitive search on the aliases. /// - /// - /// - /// - public void HandleCommand(IRemoteClient Client, string Alias, string[] Arguments) + /// Client which called the command + /// Case-insensitive name or case-sensitive alias of the command + /// + public void HandleCommand(IRemoteClient client, string alias, string[] arguments) { - ICommand Found = null; - if ((Found = FindByName(Alias)) != null) + ICommand foundCommand = FindByName(alias) ?? FindByName(alias); + if (foundCommand == null) { - Found.Handle(Client, Alias, Arguments); + client.SendMessage("Unable to locate the command \"" + alias + "\". It might be in a different server!"); return; } - else if ((Found = FindByAlias(Alias)) != null) - { - Found.Handle(Client, Alias, Arguments); - return; - } - Client.SendMessage("Unable to locate the command \"" + Alias + "\". It might be in a different server!"); + foundCommand.Handle(client, alias, arguments); } - public ICommand FindByName(string Name) + public ICommand FindByName(string name) { - foreach (ICommand C in Commands) - { - if (C.Name.ToLower() == Name.ToLower()) - { - return C; - } - } - return null; + return Commands.FirstOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); } - public ICommand FindByAlias(string Alias) + public ICommand FindByAlias(string alias) { - foreach (ICommand C in Commands) - { - if (C.Aliases.Contains(Alias)) - { - return C; - } - } - return null; + // uncomment below if alias searching should be case-insensitive + return Commands.FirstOrDefault(c => c.Aliases.Contains(alias /*, StringComparer.OrdinalIgnoreCase*/)); } } } \ No newline at end of file diff --git a/TrueCraft/Program.cs b/TrueCraft/Program.cs index 4eb248f..02bf963 100644 --- a/TrueCraft/Program.cs +++ b/TrueCraft/Program.cs @@ -54,20 +54,12 @@ namespace TrueCraft static void HandleChatMessageReceived(object sender, ChatMessageEventArgs e) { - // TODO: Make this more sophisticated if (e.Message.StartsWith("/")) { e.PreventDefault = true; - var Message = e.Message.Remove(0, 1); - var Command = Message.Trim(); - var Arguments = new string[0]; - if (Message.Split(' ').Length > 1) - { - Command = Message.Split(' ')[0]; - Arguments = Message.Substring(Command.Length).Trim().Split(' '); - } - - CommandManager.HandleCommand(e.Client, Command, Arguments); + var messageArray = e.Message.TrimStart('/') + .Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); + CommandManager.HandleCommand(e.Client, messageArray[0], messageArray); return; } }