Skip to content

Commit 9bef103

Browse files
committed
Create Custom Commands
1 parent bcc3ab0 commit 9bef103

8 files changed

+348
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.vs

CustomCommands/Commands.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"Title": "Discord",
4+
"Command": "discord",
5+
"Message": "{GREEN}Discord: https://discord.gg/H9KKjMeZsM",
6+
"CenterMessage": "",
7+
"PrintTo": 0,
8+
"Description": "Command for Discord"
9+
},
10+
{
11+
"Title": "Steam",
12+
"Command": "steam",
13+
"Message": "SteamGroup: https://steamcommunity.com/groups/OrizonSurf",
14+
"CenterMessage": "<div>Discord</div><br><div><font color='#00ff00'>https://steamcommunity.com/groups/OrizonSurf</font></div>",
15+
"PrintTo": 7,
16+
"Description": "Command for SteamGroup"
17+
}
18+
]

CustomCommands/CustomCommands.cs

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using CounterStrikeSharp.API;
2+
using CounterStrikeSharp.API.Core;
3+
using CounterStrikeSharp.API.Core.Attributes;
4+
using CounterStrikeSharp.API.Core.Attributes.Registration;
5+
using CustomCommands.Model;
6+
using System.Text.Json;
7+
8+
namespace CustomCommands
9+
{
10+
[MinimumApiVersion(86)]
11+
public class CustomCommands : BasePlugin, IPluginConfig<CustomCommandsConfig>
12+
{
13+
public override string ModuleName => "CustomCommands";
14+
15+
public override string ModuleVersion => "1.0.0";
16+
17+
public override string ModuleAuthor => "HerrMagic";
18+
19+
public override string ModuleDescription => "Create your own commands per config";
20+
21+
private List<CCSPlayerController> PlayerList = new();
22+
23+
public CustomCommandsConfig Config { get; set; } = new();
24+
25+
public void OnConfigParsed(CustomCommandsConfig config)
26+
{
27+
Config = config;
28+
}
29+
30+
public override void Load(bool hotReload)
31+
{
32+
if (!Config.IsPluginEnabled)
33+
{
34+
Console.WriteLine($"{Config.LogPrefix} {ModuleName} is disabled");
35+
return;
36+
}
37+
38+
Console.WriteLine(
39+
$"CustomCommands has been loaded, and the hot reload flag was {hotReload}, path is {ModulePath}");
40+
41+
42+
var json = System.IO.File.ReadAllText(Path.Combine(ModuleDirectory, "Commands.json"));
43+
var comms = JsonSerializer.Deserialize<List<Commands>>(json);
44+
45+
if (comms != null)
46+
{
47+
foreach (var com in comms)
48+
{
49+
AddCommand(com.Command, com.Description, (player, info) =>
50+
{
51+
if (player == null) return;
52+
53+
string message = ReplaceColorTags(com.Message);
54+
55+
switch (com.PrintTo)
56+
{
57+
case Sender.ClientChat:
58+
player.PrintToChat(Config.Prefix + message);
59+
60+
break;
61+
case Sender.AllChat:
62+
Server.PrintToChatAll(Config.Prefix + message);
63+
64+
break;
65+
case Sender.ClientCenter:
66+
player.PrintToCenterHtml(com.CenterMessage);
67+
68+
break;
69+
case Sender.AllCenter:
70+
foreach (var controller in PlayerList)
71+
controller.PrintToCenterHtml(com.CenterMessage);
72+
73+
break;
74+
case Sender.ClientChatClientCenter:
75+
player.PrintToChat(Config.Prefix + message);
76+
player.PrintToCenterHtml(com.CenterMessage);
77+
78+
break;
79+
case Sender.ClientChatAllCenter:
80+
player.PrintToChat(Config.Prefix + message);
81+
foreach (var controller in PlayerList)
82+
controller.PrintToCenterHtml(com.CenterMessage);
83+
84+
break;
85+
case Sender.AllChatClientCenter:
86+
Server.PrintToChatAll(Config.Prefix + message);
87+
player.PrintToCenterHtml(com.CenterMessage);
88+
89+
break;
90+
case Sender.AllChatAllCenter:
91+
Server.PrintToChatAll(Config.Prefix + message);
92+
foreach (var controller in PlayerList)
93+
controller.PrintToCenterHtml(com.CenterMessage);
94+
95+
break;
96+
default:
97+
break;
98+
}
99+
});
100+
}
101+
}
102+
else
103+
Console.WriteLine("No Config file found. Please create one");
104+
105+
if (hotReload)
106+
InitializeLists();
107+
}
108+
109+
private string ReplaceColorTags(string input)
110+
{
111+
string[] colorPatterns =
112+
{
113+
"{DEFAULT}", "{RED}", "{LIGHTPURPLE}", "{GREEN}", "{LIME}", "{LIGHTGREEN}", "{LIGHTRED}", "{GRAY}",
114+
"{LIGHTOLIVE}", "{OLIVE}", "{LIGHTBLUE}", "{BLUE}", "{PURPLE}", "{GRAYBLUE}"
115+
};
116+
string[] colorReplacements =
117+
{
118+
"\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x10", "\x0B", "\x0C", "\x0E",
119+
"\x0A"
120+
};
121+
122+
for (var i = 0; i < colorPatterns.Length; i++)
123+
input = input.Replace(colorPatterns[i], colorReplacements[i]);
124+
125+
return input;
126+
}
127+
128+
private void InitializeLists()
129+
{
130+
Utilities.GetPlayers().ForEach(controller =>
131+
{
132+
PlayerList.Add(controller);
133+
});
134+
}
135+
136+
[GameEventHandler(HookMode.Post)]
137+
public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo _)
138+
{
139+
if (!PlayerList.Contains(@event.Userid))
140+
PlayerList.Add(@event.Userid);
141+
142+
return HookResult.Continue;
143+
}
144+
}
145+
}

CustomCommands/CustomCommands.csproj

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<!--<OutputPath>.</OutputPath>-->
8+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
9+
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.86" />
14+
</ItemGroup>
15+
16+
</Project>
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using CounterStrikeSharp.API.Core;
2+
using CounterStrikeSharp.API.Modules.Utils;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Text.Json.Serialization;
8+
using System.Threading.Tasks;
9+
10+
namespace CustomCommands
11+
{
12+
public class CustomCommandsConfig : BasePluginConfig
13+
{
14+
[JsonPropertyName("IsPluginEnabled")]
15+
public bool IsPluginEnabled { get; set; } = true;
16+
17+
[JsonPropertyName("LogPrefix")]
18+
public string LogPrefix { get; set; } = "CSSharp";
19+
20+
[JsonPropertyName("Prefix")]
21+
public string Prefix { get; set; } = $"[{ChatColors.Yellow}Info{ChatColors.Default}] ";
22+
}
23+
}

CustomCommands/Model/Commands.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CustomCommands.Model
8+
{
9+
public class Commands
10+
{
11+
public string Title { get; set; }
12+
public string Command { get; set; }
13+
public string Message { get; set; }
14+
public string CenterMessage { get; set; }
15+
public Sender PrintTo { get; set; }
16+
public string Description { get; set; }
17+
}
18+
public enum Sender
19+
{
20+
ClientChat = 0,
21+
AllChat = 1,
22+
ClientCenter = 2,
23+
AllCenter = 3,
24+
ClientChatClientCenter = 4,
25+
ClientChatAllCenter = 5,
26+
AllChatClientCenter = 6,
27+
AllChatAllCenter = 7
28+
}
29+
}

Plugins.sln

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.7.34018.315
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpawnProtection", "SpawnProtection\SpawnProtection.csproj", "{DBAE0443-7660-456C-B185-444366A42766}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WeaponBalance", "WeaponBalance\WeaponBalance.csproj", "{C4B486A3-D8FC-41AC-827E-905BAFF50A9D}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rank", "Rank\Rank.csproj", "{A590C358-C1B2-4F21-B9B0-FE56B6562F39}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NoLegs", "NoLegs\NoLegs.csproj", "{8BF8B3C3-FE3F-459E-B414-72469816539A}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomCommands", "CustomCommands\CustomCommands.csproj", "{CFD687D3-02AF-4F8B-B561-ED01C4B2C5D3}"
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Any CPU = Debug|Any CPU
19+
Release|Any CPU = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{DBAE0443-7660-456C-B185-444366A42766}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{DBAE0443-7660-456C-B185-444366A42766}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{DBAE0443-7660-456C-B185-444366A42766}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{DBAE0443-7660-456C-B185-444366A42766}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{C4B486A3-D8FC-41AC-827E-905BAFF50A9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{C4B486A3-D8FC-41AC-827E-905BAFF50A9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{C4B486A3-D8FC-41AC-827E-905BAFF50A9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{C4B486A3-D8FC-41AC-827E-905BAFF50A9D}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{A590C358-C1B2-4F21-B9B0-FE56B6562F39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{A590C358-C1B2-4F21-B9B0-FE56B6562F39}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{A590C358-C1B2-4F21-B9B0-FE56B6562F39}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{A590C358-C1B2-4F21-B9B0-FE56B6562F39}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{8BF8B3C3-FE3F-459E-B414-72469816539A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{8BF8B3C3-FE3F-459E-B414-72469816539A}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{8BF8B3C3-FE3F-459E-B414-72469816539A}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{8BF8B3C3-FE3F-459E-B414-72469816539A}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{CFD687D3-02AF-4F8B-B561-ED01C4B2C5D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{CFD687D3-02AF-4F8B-B561-ED01C4B2C5D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{CFD687D3-02AF-4F8B-B561-ED01C4B2C5D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{CFD687D3-02AF-4F8B-B561-ED01C4B2C5D3}.Release|Any CPU.Build.0 = Release|Any CPU
42+
EndGlobalSection
43+
GlobalSection(SolutionProperties) = preSolution
44+
HideSolutionNode = FALSE
45+
EndGlobalSection
46+
GlobalSection(ExtensibilityGlobals) = postSolution
47+
SolutionGuid = {EE21A19C-5459-44A8-B594-5ACEC2B2BA82}
48+
EndGlobalSection
49+
EndGlobal

README.md

+67-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
1-
# CSS-CreateCustomCommands
2-
CSS-CreateCustomCommands
1+
## Custom Commands Plugin
2+
3+
Create your custom commands in ./plugins/CustomCommands/Commands.json
4+
5+
#### Example
6+
7+
```json
8+
[
9+
{
10+
"Title": "Discord",
11+
"Command": "discord",
12+
"Message": "{GREEN}Discord: https://discord.gg/H9KKjMeZsM",
13+
"CenterMessage": "",
14+
"PrintTo": 0,
15+
"Description": "Command for Discord"
16+
},
17+
{
18+
"Title": "Steam",
19+
"Command": "steam",
20+
"Message": "SteamGroup: https://steamcommunity.com/groups/OrizonSurf",
21+
"CenterMessage": "<div>Discord</div><br>
22+
<div><font color='#00ff00'>https://steamcommunity.com/groups/OrizonSurf</font></div>",
23+
"PrintTo": 7,
24+
"Description": "Command for SteamGroup"
25+
}
26+
]
27+
```
28+
29+
**Title**: Just the title for your
30+
31+
**Command:** just type the name. !`<command>` and /`<command>` will work normally
32+
33+
**Message**: The message you want to send.
34+
35+
Colors:
36+
37+
* {DEFAULT}
38+
* {RED}
39+
* {LIGHTPURPLE}
40+
* {GREEN}
41+
* {LIME}
42+
* {LIGHTGREEN}
43+
* {LIGHTRED}
44+
* {GRAY}
45+
* {LIGHTOLIVE}
46+
* {OLIVE}
47+
* {LIGHTBLUE}
48+
* {BLUE}
49+
* {PURPLE}
50+
* {GRAYBLUE}
51+
52+
**CenterMessage**: The Center Message. HTML works as well.
53+
54+
**PrintTo**: Where the message should be shown
55+
56+
| Desc | Nr |
57+
| --------------------------- | -- |
58+
| Client Chat | 0 |
59+
| All Chat | 1 |
60+
| Client Center | 2 |
61+
| All Center | 3 |
62+
| Client Chat & Client Center | 4 |
63+
| Client Chat & All Center | 5 |
64+
| All Chat & Client Center | 6 |
65+
| All Chat & All Center | 7 |
66+
67+
**Description**: What the Description for the command should be

0 commit comments

Comments
 (0)