Skip to content

Commit

Permalink
Push changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 6, 2024
1 parent 9b10630 commit 227bd54
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
9 changes: 5 additions & 4 deletions Commands/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions Commands/GangCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public async Task<CommandResult> 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;
}

Expand Down
14 changes: 9 additions & 5 deletions GangsAPI/Data/Command/CommandInfoWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Commands;

namespace GangsAPI.Data.Command;

Expand All @@ -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(
Expand Down Expand Up @@ -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);
});
}
}
41 changes: 17 additions & 24 deletions GangsImpl/Mock/MockCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,45 @@ public class MockCommandManager : ICommandManager {
private readonly Dictionary<string, ICommand> 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<CommandResult> 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<CommandResult> 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<CommandResult> 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);
}
}

0 comments on commit 227bd54

Please sign in to comment.