diff --git a/Commands/CommandCollection.cs b/Commands/CommandCollection.cs index d5f9360..d3e6642 100644 --- a/Commands/CommandCollection.cs +++ b/Commands/CommandCollection.cs @@ -1,10 +1,12 @@ using Microsoft.Extensions.DependencyInjection; using GangsAPI.Extensions; +using GangsAPI.Services; +using GangsAPI.Services.Commands; namespace Commands; public static class CommandCollection { public static void RegisterCommands(this IServiceCollection provider) { - provider.AddPluginBehavior(); + provider.AddPluginBehavior(); } } \ No newline at end of file diff --git a/Commands/CommandManager.cs b/Commands/CommandManager.cs new file mode 100644 index 0000000..86cb970 --- /dev/null +++ b/Commands/CommandManager.cs @@ -0,0 +1,35 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +using GangsAPI; +using GangsAPI.Data; +using GangsAPI.Data.Command; +using GangsAPI.Services; +using GangsAPI.Services.Commands; +using Mock; + +namespace Commands; + +public class CommandManager(IGangManager gangMgr) + : MockCommandManager, IPluginBehavior { + private BasePlugin plugin = null!; + + public void Start(BasePlugin? basePlugin, bool hotReload) { + ArgumentNullException.ThrowIfNull(basePlugin, nameof(basePlugin)); + plugin = basePlugin; + + RegisterCommand(new GangCommand(gangMgr)); + } + + public override bool RegisterCommand(ICommand command) { + base.RegisterCommand(command); + plugin.AddCommand(command.Name, command.Description ?? string.Empty, + (player, info) => { + var wrapper = player == null ? null : new PlayerWrapper(player); + var args = info.GetCommandString.Split(" "); + Server.NextFrameAsync(async () => { + await ProcessCommand(wrapper, args); + }); + }); + return true; + } +} \ No newline at end of file diff --git a/Commands/GangCommand.cs b/Commands/GangCommand.cs index 1f408ab..ce1c63e 100644 --- a/Commands/GangCommand.cs +++ b/Commands/GangCommand.cs @@ -1,26 +1,26 @@ using Commands.gang; using GangsAPI.Data; using GangsAPI.Data.Command; +using GangsAPI.Services; using GangsAPI.Services.Commands; using Mock; namespace Commands; -public class GangCommand : ICommand { +public class GangCommand(IGangManager gangMgr) : ICommand { public string Name => "css_gang"; public string? Description => "Master command for gangs"; - private Dictionary sub = new() { - // ["create"] = new CreateGangCommand(), + private readonly Dictionary sub = new() { // ["delete"] = new DeleteGangCommand(), // ["invite"] = new InviteGangCommand(), // ["kick"] = new KickGangCommand(), - // ["leave"] = new LeaveGangCommand(), + // ["leave"] = new LeaveGangCommand(),ggG // ["list"] = new ListGangCommand(), // ["promote"] = new PromoteGangCommand(), // ["demote"] = new DemoteGangCommand(), // ["info"] = new InfoGangCommand() - ["help"] = new HelpCommand() + ["create"] = new CreateCommand(gangMgr), ["help"] = new HelpCommand() }; public async Task Execute(PlayerWrapper? executor, diff --git a/Commands/gang/CreateCommand.cs b/Commands/gang/CreateCommand.cs index d1c0b8b..d525be1 100644 --- a/Commands/gang/CreateCommand.cs +++ b/Commands/gang/CreateCommand.cs @@ -17,19 +17,19 @@ public async Task Execute(PlayerWrapper? executor, if (executor == null) { return CommandResult.PLAYER_ONLY; } if (info.ArgCount < 2) { - info.ReplyToCommandSync("Please provide a name for the gang"); + info.ReplySync("Please provide a name for the gang"); return CommandResult.FAILURE; } var name = string.Join(' ', info.ArgString.Split(" ").Skip(1)); if (await gang.GetGang(executor.Steam) != null) { - info.ReplyToCommandSync("You are already in a gang"); + info.ReplySync("You are already in a gang"); return CommandResult.FAILURE; } if ((await gang.GetGangs()).Any(g => g.Name == name)) { - info.ReplyToCommandSync($"Gang '{name}' already exists"); + info.ReplySync($"Gang '{name}' already exists"); return CommandResult.FAILURE; } diff --git a/Commands/gang/HelpCommand.cs b/Commands/gang/HelpCommand.cs index def179e..e31792d 100644 --- a/Commands/gang/HelpCommand.cs +++ b/Commands/gang/HelpCommand.cs @@ -11,7 +11,7 @@ public class HelpCommand : ICommand { public Task Execute(PlayerWrapper? executor, CommandInfoWrapper info) { - info.ReplyToCommandSync("create [name] - Creates a new gang"); + info.ReplySync("create [name] - Creates a new gang"); return Task.FromResult(CommandResult.SUCCESS); } } \ No newline at end of file diff --git a/Core/GangServiceCollection.cs b/Core/GangServiceCollection.cs index 2165bf5..588896f 100644 --- a/Core/GangServiceCollection.cs +++ b/Core/GangServiceCollection.cs @@ -1,12 +1,22 @@ using Commands; using CounterStrikeSharp.API.Core; using GangsAPI; +using GangsAPI.Extensions; +using GangsAPI.Services; using Microsoft.Extensions.DependencyInjection; +using Mock; namespace GangsImpl; public class GangServiceCollection : IPluginServiceCollection { public void ConfigureServices(IServiceCollection serviceCollection) { + serviceCollection.AddPluginBehavior(); + serviceCollection.AddPluginBehavior(); + serviceCollection.AddPluginBehavior(); + serviceCollection + .AddPluginBehavior(); + serviceCollection + .AddPluginBehavior(); serviceCollection.RegisterCommands(); } } \ No newline at end of file diff --git a/GangsAPI/Data/Command/CommandInfoWrapper.cs b/GangsAPI/Data/Command/CommandInfoWrapper.cs index 43f3f8f..c777f90 100644 --- a/GangsAPI/Data/Command/CommandInfoWrapper.cs +++ b/GangsAPI/Data/Command/CommandInfoWrapper.cs @@ -30,7 +30,7 @@ public string ArgString public string GetCommandString => string.Join(' ', args.Skip(offset)); - public void ReplyToCommandSync(string message) { + public void ReplySync(string message) { if (CallingPlayer == null) { Console.WriteLine(message); return; diff --git a/GangsAPI/Services/IPlayerStatManager.cs b/GangsAPI/Services/IPlayerStatManager.cs index 6fbbbe8..7f408d4 100644 --- a/GangsAPI/Services/IPlayerStatManager.cs +++ b/GangsAPI/Services/IPlayerStatManager.cs @@ -3,7 +3,7 @@ namespace GangsAPI.Services; -public interface IPlayerStatManager : ICacher { +public interface IPlayerStatManager : IPluginBehavior, ICacher { Task?> GetForPlayer(ulong key, string statId); Task PushToPlayer(ulong key, IPlayerStat value) { diff --git a/GangsImpl/Mock/MockCommandManager.cs b/GangsImpl/Mock/MockCommandManager.cs index a02cdeb..d01ce12 100644 --- a/GangsImpl/Mock/MockCommandManager.cs +++ b/GangsImpl/Mock/MockCommandManager.cs @@ -7,7 +7,7 @@ namespace Mock; public class MockCommandManager : ICommandManager { private readonly Dictionary commands = new(); - public bool RegisterCommand(ICommand command) { + public virtual bool RegisterCommand(ICommand command) { return commands.TryAdd(command.Name, command); } diff --git a/GangsTest/Commands/GangCommandTestses.cs b/GangsTest/Commands/GangCommandTests.cs similarity index 84% rename from GangsTest/Commands/GangCommandTestses.cs rename to GangsTest/Commands/GangCommandTests.cs index ddacc80..8c2f577 100644 --- a/GangsTest/Commands/GangCommandTestses.cs +++ b/GangsTest/Commands/GangCommandTests.cs @@ -1,11 +1,12 @@ using Commands; using GangsAPI.Data.Command; +using GangsAPI.Services; using GangsAPI.Services.Commands; namespace GangsTest.Commands; -public class GangCommandTestses(ICommandManager commands) - : CommandTests(commands, new GangCommand()) { +public class GangCommandTests(ICommandManager commands, IGangManager gangMgr) + : CommandTests(commands, new GangCommand(gangMgr)) { [Fact] public async Task Gang_TestBase() { Assert.Equal("css_gang", Command.Name);