Merge pull request #23 from cubrr/tidy-command-parsing

Tidy command parsing
This commit is contained in:
Drew DeVault 2015-02-07 11:23:15 -07:00
commit d92e26b3f7
2 changed files with 19 additions and 45 deletions

View File

@ -29,51 +29,33 @@ namespace TrueCraft.Commands
} }
/// <summary> /// <summary>
/// Handle the specified command if it exists. We run the check twice to separate /// Tries to find the specified command by first performing a
/// actual command names from command aliases to prevent aliases from being prioritized /// case-insensitive search on the command names, then a
/// over other command names. /// case-sensitive search on the aliases.
/// </summary> /// </summary>
/// <param name="Client"></param> /// <param name="client">Client which called the command</param>
/// <param name="Command"></param> /// <param name="alias">Case-insensitive name or case-sensitive alias of the command</param>
/// <param name="Arguments"></param> /// <param name="arguments"></param>
public void HandleCommand(IRemoteClient Client, string Alias, string[] Arguments) public void HandleCommand(IRemoteClient client, string alias, string[] arguments)
{ {
ICommand Found = null; ICommand foundCommand = FindByName(alias) ?? FindByName(alias);
if ((Found = FindByName(Alias)) != null) if (foundCommand == null)
{ {
Found.Handle(Client, Alias, Arguments); client.SendMessage("Unable to locate the command \"" + alias + "\". It might be in a different server!");
return; return;
} }
else if ((Found = FindByAlias(Alias)) != null) foundCommand.Handle(client, alias, arguments);
{
Found.Handle(Client, Alias, Arguments);
return;
}
Client.SendMessage("Unable to locate the command \"" + Alias + "\". It might be in a different server!");
} }
public ICommand FindByName(string Name) public ICommand FindByName(string name)
{ {
foreach (ICommand C in Commands) return Commands.FirstOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
{
if (C.Name.ToLower() == Name.ToLower())
{
return C;
}
}
return null;
} }
public ICommand FindByAlias(string Alias) public ICommand FindByAlias(string alias)
{ {
foreach (ICommand C in Commands) // uncomment below if alias searching should be case-insensitive
{ return Commands.FirstOrDefault(c => c.Aliases.Contains(alias /*, StringComparer.OrdinalIgnoreCase*/));
if (C.Aliases.Contains(Alias))
{
return C;
}
}
return null;
} }
} }
} }

View File

@ -54,20 +54,12 @@ namespace TrueCraft
static void HandleChatMessageReceived(object sender, ChatMessageEventArgs e) static void HandleChatMessageReceived(object sender, ChatMessageEventArgs e)
{ {
// TODO: Make this more sophisticated
if (e.Message.StartsWith("/")) if (e.Message.StartsWith("/"))
{ {
e.PreventDefault = true; e.PreventDefault = true;
var Message = e.Message.Remove(0, 1); var messageArray = e.Message.TrimStart('/')
var Command = Message.Trim(); .Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
var Arguments = new string[0]; CommandManager.HandleCommand(e.Client, messageArray[0], messageArray);
if (Message.Split(' ').Length > 1)
{
Command = Message.Split(' ')[0];
Arguments = Message.Substring(Command.Length).Trim().Split(' ');
}
CommandManager.HandleCommand(e.Client, Command, Arguments);
return; return;
} }
} }