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+ }
0 commit comments