Skip to content

Commit 9f02b60

Browse files
committed
fixing argument problems
1 parent 139e34a commit 9f02b60

File tree

6 files changed

+110
-120
lines changed

6 files changed

+110
-120
lines changed

CustomCommands/CustomCommands.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CounterStrikeSharp.API.Core.Attributes;
33
using CustomCommands.Interfaces;
44
using Microsoft.Extensions.Logging;
5+
using System.Text.Json;
56

67
namespace CustomCommands;
78

@@ -61,7 +62,8 @@ public override void Load(bool hotReload)
6162
{
6263
PluginGlobals.CustomCommands = comms;
6364

64-
PluginGlobals.CustomCommands = RegisterCommands.CheckForDuplicateCommands(comms);
65+
RegisterCommands.CheckForDuplicateCommands();
66+
RegisterCommands.ConvertingCommandsForRegister();
6567

6668
// Add commands from the JSON file to the server
6769
foreach (var cmd in PluginGlobals.CustomCommands)

CustomCommands/Interfaces/IPluginUtilities.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ namespace CustomCommands.Interfaces;
55

66
public interface IPluginUtilities
77
{
8-
/// <summary>
9-
/// Splits the commands string into an array of strings.
10-
/// </summary>
11-
/// <param name="commands"></param>
12-
/// <returns></returns>
13-
string[] GettingCommandsFromString(string commands);
14-
158
/// <summary>
169
/// Adds the css_ prefix to each alias.
1710
/// This will help cs# tell this command belongs to the framework.

CustomCommands/Interfaces/IRegisterCommands.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ public interface IRegisterCommands
1313
/// <summary>
1414
/// Checks for duplicate commands in the provided list and removes them.
1515
/// </summary>
16-
/// <param name="comms">The list of commands to check for duplicates.</param>
17-
/// <returns>A new list of commands without any duplicates.</returns>
18-
List<Commands> CheckForDuplicateCommands(List<Commands> comms);
16+
void CheckForDuplicateCommands();
17+
18+
/// <summary>
19+
/// Converts custom commands stored in the global plugin state by processing each command string,
20+
/// splitting it by commas or semicolons, and adjusting the command structure.
21+
///
22+
/// The method clones each command, assigns a new unique ID, and processes its arguments.
23+
/// Each split command is then added back to the global list with updated formatting.
24+
/// </summary>
25+
void ConvertingCommandsForRegister();
1926
}

CustomCommands/Model/CommandsConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Commands : ICloneable
1515
public List<string> ClientCommands { get; set; } = new();
1616
public List<string> ClientCommandsFromServer { get; set; } = new();
1717
public Permission? Permission { get; set; } = new();
18+
public bool IsRegisterable { get; set; } = true;
1819

1920
public object Clone()
2021
{
@@ -32,7 +33,8 @@ public object Clone()
3233
ServerCommands = new List<string>(ServerCommands),
3334
ClientCommands = new List<string>(ClientCommands),
3435
ClientCommandsFromServer = new List<string>(ClientCommandsFromServer),
35-
Permission = Permission?.Clone() as Permission
36+
Permission = Permission?.Clone() as Permission,
37+
IsRegisterable = IsRegisterable
3638
};
3739
}
3840
}

CustomCommands/Services/PluginUtilities.cs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,7 @@ public PluginUtilities(IPluginGlobals PluginGlobals, IReplaceTagsFunctions Repla
2222
this.ReplaceTagsFunctions = ReplaceTagsFunctions;
2323
this.Logger = Logger;
2424
}
25-
26-
public string[] GettingCommandsFromString(string commands)
27-
{
28-
string[] splitCommands = SplitStringByCommaOrSemicolon(commands);
29-
List<string> commandsList = new List<string>();
30-
// Removes arguments from the command when spaces are present
31-
for (int i = 0; i < splitCommands.Length; i++)
32-
{
33-
if (splitCommands[i].Contains(' '))
34-
{
35-
Logger.LogInformation($"Contains space!");
36-
if (splitCommands[i].IndexOf(' ') == 0)
37-
{
38-
commandsList.Add(splitCommands[i]);
39-
continue;
40-
}
41-
Logger.LogInformation($"Is multiple args");
42-
string sub = splitCommands[i].Substring(0, splitCommands[i].IndexOf(' '));
43-
Logger.LogInformation($"Sub: {sub}");
44-
if (commandsList.Contains(sub))
45-
{
46-
Logger.LogInformation("In IF");
47-
continue;
48-
}
49-
50-
if (!contains)
51-
commandsList.Add(sub);
52-
}
53-
else
54-
{
55-
if (!commandsList.Contains(splitCommands[i]))
56-
commandsList.Add(splitCommands[i]);
57-
}
58-
}
59-
60-
61-
foreach (var command in commandsList)
62-
{
63-
Logger.LogInformation($"Command: {command}");
64-
}
65-
66-
if (PluginGlobals.Config.RegisterCommandsAsCSSFramework)
67-
return AddCSSTagsToAliases(commandsList);
68-
69-
70-
return commandsList.ToArray();
71-
}
72-
25+
7326
public string[] AddCSSTagsToAliases(List<string> input)
7427
{
7528
for (int i = 0; i < input.Count; i++)

CustomCommands/Services/RegisterCommands.cs

Lines changed: 93 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,83 +26,79 @@ public RegisterCommands(ILogger<CustomCommands> Logger, IMessageManager MessageM
2626
this.CooldownManager = CooldownManager;
2727
}
2828

29-
/// <summary>
30-
/// Adds custom commands to the plugin.
31-
/// </summary>
32-
/// <param name="cmd">The command to add.</param>
3329
public void AddCommands(Commands cmd)
3430
{
35-
CustomCommands plugin = (PluginContext.Plugin as CustomCommands)!;
36-
37-
string[] aliases = PluginUtilities.GettingCommandsFromString(cmd.Command);
31+
if (!cmd.IsRegisterable)
32+
return;
33+
34+
var pluginContext = (PluginContext.Plugin as CustomCommands)!;
3835

39-
for (int i = 0; i < aliases.Length; i++)
36+
pluginContext.AddCommand(cmd.Command, cmd.Description,
37+
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
38+
(player, info) =>
4039
{
41-
string alias = aliases[i];
42-
// System.Console.WriteLine($"Pre Command: {alias}.");
43-
plugin.AddCommand(alias, cmd.Description,
44-
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
45-
(player, info) =>
46-
{
47-
if (player == null)
48-
return;
49-
System.Console.WriteLine($"Post Command: {alias}.");
40+
if (player == null)
41+
return;
5042

51-
if (info.ArgCount < alias.Split(' ').Length)
43+
var command = cmd;
44+
45+
// Check if the command has arguments and if it does, check if the command exists and is the right one
46+
if (info.ArgCount > 1)
47+
{
48+
var findcommand = PluginGlobals.CustomCommands.Find(x => x.Command == command.Command && x.Argument == info.ArgString);
49+
// Check if the command is the right one with the right arguments
50+
if (findcommand is null)
5251
{
53-
player.PrintToChat($"This command requires at least {alias.Split(' ').Length-1} arguments.");
52+
player.PrintToChat("This command requires no Arguments or you added to many");
5453
return;
5554
}
56-
57-
var command = cmd;
58-
59-
// Check if the command has arguments and if it does, check if the command really exists in the list
60-
if (info.ArgCount > 1)
55+
56+
if (!findcommand.Argument!.Equals(info.ArgString))
6157
{
62-
var findcommand = PluginGlobals.CustomCommands.Find(x => x.Command.Contains(alias + $" {info.ArgString}"));
63-
// Check if the command is equal to the found command
64-
if (findcommand! != command)
65-
return;
66-
67-
command = findcommand;
58+
player.PrintToChat("Wrong Arguments");
59+
return;
6860
}
6961

70-
// Check if the player has the permission to use the command
71-
if (command!.Permission.PermissionList.Count > 0 && command.Permission != null)
72-
if (!PluginUtilities.RequiresPermissions(player, command.Permission))
73-
return;
62+
command = findcommand;
63+
}
64+
65+
// This will exit the command if the command has arguments but the client didn't provide any
66+
if (info.ArgCount <= 1 && !string.IsNullOrEmpty(command.Argument!))
67+
{
68+
player.PrintToChat("This command requires Arguments");
69+
return;
70+
}
7471

75-
// Check if the command is on cooldown
76-
if(CooldownManager.IsCommandOnCooldown(player, command)) return;
72+
// Check if the player has the permission to use the command
73+
if (command!.Permission?.PermissionList.Count > 0 && command.Permission != null)
74+
if (!PluginUtilities.RequiresPermissions(player, command.Permission))
75+
return;
76+
77+
// Check if the command is on cooldown
78+
if(CooldownManager.IsCommandOnCooldown(player, command)) return;
7779

78-
// Set the cooldown for the command if it has a cooldown set
79-
CooldownManager.SetCooldown(player, command);
80+
// Set the cooldown for the command if it has a cooldown set
81+
CooldownManager.SetCooldown(player, command);
8082

81-
// Sending the message to the player
82-
MessageManager.SendMessage(player, command);
83+
// Sending the message to the player
84+
MessageManager.SendMessage(player, command);
8385

84-
// Execute the server commands
85-
PluginUtilities.ExecuteServerCommands(command, player);
86+
// Execute the server commands
87+
PluginUtilities.ExecuteServerCommands(command, player);
8688

87-
// Execute the client commands
88-
PluginUtilities.ExecuteClientCommands(command, player);
89+
// Execute the client commands
90+
PluginUtilities.ExecuteClientCommands(command, player);
8991

90-
// Execute the client commands from the server
91-
PluginUtilities.ExecuteClientCommandsFromServer(command, player);
92-
});
93-
}
92+
// Execute the client commands from the server
93+
PluginUtilities.ExecuteClientCommandsFromServer(command, player);
94+
});
9495
}
9596

96-
/// <summary>
97-
/// Checks for duplicate commands in the provided list and removes them.
98-
/// </summary>
99-
/// <param name="comms">The list of commands to check for duplicates.</param>
100-
/// <returns>A new list of commands without any duplicates.</returns>
101-
public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
97+
public void CheckForDuplicateCommands()
10298
{
103-
List<Commands> duplicateCommands = new();
104-
List<Commands> commands = new();
105-
List<string> commandNames = new();
99+
var comms = PluginGlobals.CustomCommands;
100+
var duplicateCommands = new List<Commands>();
101+
var commandNames = new List<string>();
106102

107103
foreach (var cmd in comms)
108104
{
@@ -120,7 +116,7 @@ public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
120116
}
121117

122118
if (duplicateCommands.Count == 0)
123-
return comms;
119+
return;
124120

125121
// Log the duplicate commands
126122
Logger.LogError($"------------------------------------------------------------------------");
@@ -136,10 +132,47 @@ public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
136132
continue;
137133
}
138134

139-
commands.Add(comms[i]);
135+
comms.Add(comms[i]);
140136
}
141137
Logger.LogError($"------------------------------------------------------------------------");
138+
}
139+
140+
public void ConvertingCommandsForRegister()
141+
{
142+
var newCmds = new List<Commands>();
143+
144+
foreach (var cmd in PluginGlobals.CustomCommands)
145+
{
146+
var splitCommands = PluginUtilities.SplitStringByCommaOrSemicolon(cmd.Command);
147+
splitCommands = PluginUtilities.AddCSSTagsToAliases(splitCommands.ToList());
148+
149+
foreach (var split in splitCommands)
150+
{
151+
var args = split.Split(' ');
152+
153+
if(args.Length == 1)
154+
{
155+
var newCmd = cmd.Clone() as Commands;
156+
newCmd!.ID = Guid.NewGuid();
157+
newCmd.Command = split.Trim();
158+
newCmds.Add(newCmd);
159+
}
160+
else if (args.Length > 1)
161+
{
162+
var newCmd = cmd.Clone() as Commands;
163+
164+
if (newCmds.Any(p => p.Command.Contains(args[0])))
165+
newCmd!.IsRegisterable = false;
166+
167+
newCmd!.ID = Guid.NewGuid();
168+
newCmd.Command = args[0].Trim();
169+
args[0] = "";
170+
newCmd.Argument = string.Join(" ", args).Trim();
171+
newCmds.Add(newCmd);
172+
}
173+
}
174+
}
142175

143-
return commands;
176+
PluginGlobals.CustomCommands = newCmds;
144177
}
145178
}

0 commit comments

Comments
 (0)