Small command changes.

This commit is contained in:
Lucca Faria Ferri 2023-02-23 16:12:59 -08:00
parent 1fff1a96c7
commit a00de9d18a
2 changed files with 11 additions and 14 deletions

View File

@ -12,7 +12,7 @@ namespace DiIiS_NA.GameServer.CommandManager
{
public class CommandGroup
{
private static readonly Logger Logger = LogManager.CreateLogger("CmdGrp");
private static readonly Logger Logger = LogManager.CreateLogger(nameof(CommandGroup));
private CommandGroupAttribute Attributes { get; set; }
@ -29,7 +29,7 @@ namespace DiIiS_NA.GameServer.CommandManager
{
foreach (var method in GetType().GetMethods())
{
object[] attributes = method.GetCustomAttributes(typeof(CommandAttribute), true);
var attributes = method.GetCustomAttributes(typeof(CommandAttribute), true);
if (attributes.Length == 0) continue;
var attribute = (CommandAttribute)attributes[0];
@ -46,7 +46,7 @@ namespace DiIiS_NA.GameServer.CommandManager
{
foreach (var method in GetType().GetMethods())
{
object[] attributes = method.GetCustomAttributes(typeof(DefaultCommand), true);
var attributes = method.GetCustomAttributes(typeof(DefaultCommand), true);
if (attributes.Length == 0) continue;
if (method.Name.ToLower() == "fallback") continue;
@ -111,10 +111,9 @@ namespace DiIiS_NA.GameServer.CommandManager
public string GetHelp(string command)
{
foreach (var pair in _commands.Where(pair => command == pair.Key.Name))
{
return pair.Key.Help;
}
var commandData = _commands.FirstOrDefault(pair => command == pair.Key.Name);
if (commandData.Key?.Help is {} help && !string.IsNullOrWhiteSpace(help))
return help;
return string.Empty;
}

View File

@ -14,10 +14,7 @@ namespace DiIiS_NA.GameServer.CommandManager
private static readonly Logger Logger = LogManager.CreateLogger(nameof(CommandManager));
private static readonly Dictionary<CommandGroupAttribute, CommandGroup> CommandGroups = new();
static CommandManager()
{
RegisterCommandGroups();
}
static CommandManager() => RegisterCommandGroups();
private static void RegisterCommandGroups()
{
@ -30,7 +27,8 @@ namespace DiIiS_NA.GameServer.CommandManager
if (groupAttribute.Name == null) continue;
if (groupAttribute.Name.Contains(" "))
{
Logger.Warn($"Command group name '{groupAttribute.Name}' contains spaces (which is $[red]$not$[/]$ allowed). Command group will be ignored.");
Logger.Warn($"Command group name '{groupAttribute.Name}' contains spaces (which is $[red]$not$[/]$ allowed). $[red]$Command group will be ignored.$[/]$");
continue;
}
if (CommandsConfig.Instance.DisabledGroupsData.Contains(groupAttribute.Name))
@ -147,10 +145,10 @@ namespace DiIiS_NA.GameServer.CommandManager
if (line[0] != CommandsConfig.Instance.CommandPrefix) // if line does not start with command-prefix
return false;
line = line.Substring(1); // advance to actual command.
line = line[1..]; // advance to actual command.
command = line.Split(' ')[0].ToLower(); // get command
parameters = String.Empty;
if (line.Contains(' ')) parameters = line.Substring(line.IndexOf(' ') + 1).Trim(); // get parameters if any.
if (line.Contains(' ')) parameters = line[(line.IndexOf(' ') + 1)..].Trim(); // get parameters if any.
return true;
}