From 227bd545d946a9df49c9c00b857ce07bbb52c43b Mon Sep 17 00:00:00 2001 From: MSWS Date: Thu, 5 Sep 2024 23:04:33 -0700 Subject: [PATCH] Push changes --- Commands/CommandManager.cs | 9 +++-- Commands/GangCommand.cs | 4 +- GangsAPI/Data/Command/CommandInfoWrapper.cs | 14 ++++--- GangsImpl/Mock/MockCommandManager.cs | 41 +++++++++------------ 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Commands/CommandManager.cs b/Commands/CommandManager.cs index c93b300..fb291d3 100644 --- a/Commands/CommandManager.cs +++ b/Commands/CommandManager.cs @@ -2,6 +2,7 @@ using CounterStrikeSharp.API.Core; using GangsAPI; using GangsAPI.Data; +using GangsAPI.Data.Command; using GangsAPI.Services; using GangsAPI.Services.Commands; using Mock; @@ -22,12 +23,12 @@ public override bool RegisterCommand(ICommand command) { var result = base.RegisterCommand(command); if (result == false) return false; foreach (var alias in command.Aliases) { - plugin?.AddCommand(command.Name, command.Description ?? string.Empty, + plugin?.AddCommand(alias, command.Description ?? string.Empty, (player, info) => { - var wrapper = player == null ? null : new PlayerWrapper(player); - var args = info.GetCommandString.Split(" "); + var wrapper = player == null ? null : new PlayerWrapper(player); + var wrappedInfo = new CommandInfoWrapper(info); Server.NextFrameAsync(async () => { - await ProcessCommand(wrapper, info); + await ProcessCommand(wrapper, wrappedInfo); }); }); } diff --git a/Commands/GangCommand.cs b/Commands/GangCommand.cs index 2431195..ac38c13 100644 --- a/Commands/GangCommand.cs +++ b/Commands/GangCommand.cs @@ -29,13 +29,13 @@ public async Task Execute(PlayerWrapper? executor, if (info.ArgCount == 0) throw new InvalidOperationException( "Attempted to execute GangCommand with no arguments"); - if (info[0] != Name) + if (!Aliases.Contains(info[0])) throw new InvalidOperationException( $"Attempted to execute GangCommand with invalid name: {info[0]}"); if (executor?.Player != null) { info.ReplySync( - $" {ChatColors.DarkRed}GANGS {ChatColors.White}> {ChatColors.Grey}SoonTM!"); + $" {ChatColors.Red}GANGS {ChatColors.DarkRed}> {ChatColors.Grey}SoonTM!"); return CommandResult.SUCCESS; } diff --git a/GangsAPI/Data/Command/CommandInfoWrapper.cs b/GangsAPI/Data/Command/CommandInfoWrapper.cs index b7c08f8..4964628 100644 --- a/GangsAPI/Data/Command/CommandInfoWrapper.cs +++ b/GangsAPI/Data/Command/CommandInfoWrapper.cs @@ -1,4 +1,5 @@ -using CounterStrikeSharp.API.Modules.Commands; +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Modules.Commands; namespace GangsAPI.Data.Command; @@ -15,6 +16,7 @@ public CommandInfoWrapper(CommandInfo info, int offset = 0) : this( offset, new string[info.ArgCount]) { CallingContext = info.CallingContext; for (var i = 0; i < info.ArgCount; i++) args[i] = info.GetArg(i); + if (offset == 0 && info.ArgCount > 0) args[0] = info.GetArg(0).ToLower(); } public CommandInfoWrapper(CommandInfoWrapper info, int offset) : this( @@ -42,9 +44,11 @@ public void ReplySync(string message) { return; } - if (CallingContext == CommandCallingContext.Console) - CallingPlayer.PrintToConsole(message); - else - CallingPlayer.PrintToChat(message); + Server.NextFrame(() => { + if (CallingContext == CommandCallingContext.Console) + CallingPlayer.PrintToConsole(message); + else + CallingPlayer.PrintToChat(message); + }); } } \ No newline at end of file diff --git a/GangsImpl/Mock/MockCommandManager.cs b/GangsImpl/Mock/MockCommandManager.cs index d2cfb1c..99e2bd0 100644 --- a/GangsImpl/Mock/MockCommandManager.cs +++ b/GangsImpl/Mock/MockCommandManager.cs @@ -9,52 +9,45 @@ public class MockCommandManager : ICommandManager { private readonly Dictionary commands = new(); public virtual bool RegisterCommand(ICommand command) { - return commands.TryAdd(command.Name, command); + return command.Aliases.All(alias => commands.TryAdd(alias, command)); } public bool UnregisterCommand(ICommand command) { - return commands.Remove(command.Name); + return command.Aliases.All(alias => commands.Remove(alias)); } public async Task ProcessCommand(PlayerWrapper? executor, CommandInfo sourceInfo) { var info = new CommandInfoWrapper(sourceInfo); - if (info.ArgCount == 0) return CommandResult.FAILURE; - if (!commands.TryGetValue(info[0], out var command)) + return await ProcessCommand(executor, info); + } + + public async Task ProcessCommand(PlayerWrapper? executor, + CommandInfoWrapper sourceInfo) { + if (sourceInfo.ArgCount == 0) return CommandResult.FAILURE; + var result = CommandResult.FAILURE; + + if (!commands.TryGetValue(sourceInfo[0], out var command)) { + sourceInfo.ReplySync("Unknown command: " + sourceInfo[0]); return CommandResult.UNKNOWN_COMMAND; + } if (!command.CanExecute(executor)) return CommandResult.NO_PERMISSION; - var result = CommandResult.FAILURE; await Task.Run(async () => { - result = await command.Execute(executor, info); + result = await command.Execute(executor, sourceInfo); }); if (result == CommandResult.PLAYER_ONLY) - info.ReplySync("This command can only be executed by a player"); + sourceInfo.ReplySync("This command can only be executed by a player"); return result; } public async Task ProcessCommand(PlayerWrapper? executor, params string[] args) { - if (args.Length == 0) return CommandResult.FAILURE; - if (!commands.TryGetValue(args[0], out var command)) - return CommandResult.UNKNOWN_COMMAND; - - if (!command.CanExecute(executor)) return CommandResult.NO_PERMISSION; - - var result = CommandResult.FAILURE; - var info = new CommandInfoWrapper(executor, args: args); - - await Task.Run(async () => { - result = await command.Execute(executor, info); - }); - - if (result == CommandResult.PLAYER_ONLY) - info.ReplySync("This command can only be executed by a player"); - - return result; + var info = new CommandInfoWrapper(executor, 0, args); + return await ProcessCommand(executor, info); } } \ No newline at end of file