So on my game server we have server-side commands. I was wondering if it was possible to turn this
public string Command
{
get { return "g"; }
}
into something like this
public string Command
{
get { return "g", "guild", "group"; }
}
Here's the command interfaces code
internal interface ICommand
{
string Command { get; }
int RequiredRank { get; }
void Execute(Player player, string[] args);
}
Here's the command handler's code
part 1:
ProcessCmd(x[0].Trim('/'), x.Skip(1).ToArray());
part 2:
private void ProcessCmd(string cmd, string[] args)
{
if (cmds == null)
{
cmds = new Dictionary<string, ICommand>();
var t = typeof (ICommand);
foreach (var i in t.Assembly.GetTypes())
if (t.IsAssignableFrom(i) && i != t)
{
var instance = (ICommand) Activator.CreateInstance(i);
cmds.Add(instance.Command, instance);
}
}
ICommand command;
if (!cmds.TryGetValue(cmd, out command))
{
psr.SendPacket(new TextPacket
{
BubbleTime = 0,
Stars = -1,
Name = "*Error*",
Text = "Unknown Command!"
});
return;
}
try
{
ExecCmd(command, args);
}
catch (Exception e)
{
Console.Out.WriteLine(e);
psr.SendPacket(new TextPacket
{
BubbleTime = 0,
Stars = -1,
Name = "*Error*",
Text = "Error when executing the command!"
});
}
}
You want this:
public string[] Commands
{
get { return new string[] {"g", "guild", "group"}; }
}
Start with this. It will produce errors you should fix and by advancing in the fixing you will refactor a nice portion of code, like the ICommand
interface.