7
7
using System . Drawing ;
8
8
using Microsoft . Extensions . Localization ;
9
9
using CounterStrikeSharp . API . Modules . Commands ;
10
+ using Newtonsoft . Json ;
10
11
11
12
namespace CnD_Sound ;
12
13
13
14
public class CnDSoundConfig : BasePluginConfig
14
15
{
15
16
[ JsonPropertyName ( "InGameSoundDisableCommands" ) ] public string InGameSoundDisableCommands { get ; set ; } = "!stopsound,!stopsounds" ;
17
+ [ JsonPropertyName ( "RemovePlayerCookieOlderThanXDays" ) ] public int RemovePlayerCookieOlderThanXDays { get ; set ; } = 7 ;
16
18
[ JsonPropertyName ( "InGameSoundConnect" ) ] public string InGameSoundConnect { get ; set ; } = "sounds/buttons/blip1.vsnd_c" ;
17
19
[ JsonPropertyName ( "InGameSoundDisconnect" ) ] public string InGameSoundDisconnect { get ; set ; } = "sounds/player/taunt_clap_01.vsnd_c" ;
18
20
19
21
20
-
21
22
[ JsonPropertyName ( "SendLogToText" ) ] public bool SendLogToText { get ; set ; } = false ;
22
23
[ JsonPropertyName ( "LogFileFormat" ) ] public string LogFileFormat { get ; set ; } = ".txt" ;
23
24
[ JsonPropertyName ( "LogFileDateFormat" ) ] public string LogFileDateFormat { get ; set ; } = "MM-dd-yyyy" ;
@@ -44,7 +45,7 @@ public class CnDSoundConfig : BasePluginConfig
44
45
public class CnDSound : BasePlugin , IPluginConfig < CnDSoundConfig >
45
46
{
46
47
public override string ModuleName => "Connect Disconnect Sound" ;
47
- public override string ModuleVersion => "1.0.7 " ;
48
+ public override string ModuleVersion => "1.0.8 " ;
48
49
public override string ModuleAuthor => "Gold KingZ" ;
49
50
public override string ModuleDescription => "Connect , Disconnect , Country , City , Message , Sound , Logs , Discord" ;
50
51
internal static IStringLocalizer ? Stringlocalizer ;
@@ -86,7 +87,8 @@ public override void Load(bool hotReload)
86
87
private HookResult OnPlayerSayPublic ( CCSPlayerController ? player , CommandInfo info )
87
88
{
88
89
if ( string . IsNullOrEmpty ( Config . InGameSoundDisableCommands ) || player == null || ! player . IsValid || player . IsBot || player . IsHLTV ) return HookResult . Continue ;
89
-
90
+ var playerid = player . SteamID ;
91
+ bool playerValue = RetrieveBoolValueById ( ( int ) playerid ) ;
90
92
var message = info . GetArg ( 1 ) ;
91
93
if ( string . IsNullOrWhiteSpace ( message ) ) return HookResult . Continue ;
92
94
string trimmedMessage1 = message . TrimStart ( ) ;
@@ -96,25 +98,28 @@ private HookResult OnPlayerSayPublic(CCSPlayerController? player, CommandInfo in
96
98
97
99
if ( disableCommands . Any ( cmd => cmd . Equals ( trimmedMessage , StringComparison . OrdinalIgnoreCase ) ) )
98
100
{
99
- if ( player . UserId . HasValue )
101
+ DateTime personDate = DateTime . Now ;
102
+
103
+
104
+ playerValue = ! playerValue ;
105
+
106
+ if ( playerValue )
100
107
{
101
- if ( OnDisabled . ContainsKey ( player . UserId . Value ) )
102
- {
103
- OnDisabled . Remove ( player . UserId . Value ) ;
104
- player . PrintToChat ( Localizer [ "InGame_Command_Enabled" ] ) ;
105
- } else
106
- {
107
- OnDisabled . Add ( player . UserId . Value , true ) ;
108
- player . PrintToChat ( Localizer [ "InGame_Command_Disabled" ] ) ;
109
- }
108
+ player . PrintToChat ( Localizer [ "InGame_Command_Disabled" ] ) ;
109
+ } else
110
+ {
111
+ player . PrintToChat ( Localizer [ "InGame_Command_Enabled" ] ) ;
110
112
}
113
+
114
+ SaveToJsonFile ( ( int ) playerid , playerValue , personDate ) ;
111
115
}
112
116
return HookResult . Continue ;
113
117
}
114
118
private HookResult OnPlayerSayTeam ( CCSPlayerController ? player , CommandInfo info )
115
119
{
116
120
if ( string . IsNullOrEmpty ( Config . InGameSoundDisableCommands ) || player == null || ! player . IsValid || player . IsBot || player . IsHLTV ) return HookResult . Continue ;
117
-
121
+ var playerid = player . SteamID ;
122
+ bool playerValue = RetrieveBoolValueById ( ( int ) playerid ) ;
118
123
var message = info . GetArg ( 1 ) ;
119
124
if ( string . IsNullOrWhiteSpace ( message ) ) return HookResult . Continue ;
120
125
string trimmedMessage1 = message . TrimStart ( ) ;
@@ -124,18 +129,20 @@ private HookResult OnPlayerSayTeam(CCSPlayerController? player, CommandInfo info
124
129
125
130
if ( disableCommands . Any ( cmd => cmd . Equals ( trimmedMessage , StringComparison . OrdinalIgnoreCase ) ) )
126
131
{
127
- if ( player . UserId . HasValue )
132
+ DateTime personDate = DateTime . Now ;
133
+
134
+
135
+ playerValue = ! playerValue ;
136
+
137
+ if ( playerValue )
128
138
{
129
- if ( OnDisabled . ContainsKey ( player . UserId . Value ) )
130
- {
131
- OnDisabled . Remove ( player . UserId . Value ) ;
132
- player . PrintToChat ( Localizer [ "InGame_Command_Enabled" ] ) ;
133
- } else
134
- {
135
- OnDisabled . Add ( player . UserId . Value , true ) ;
136
- player . PrintToChat ( Localizer [ "InGame_Command_Disabled" ] ) ;
137
- }
139
+ player . PrintToChat ( Localizer [ "InGame_Command_Disabled" ] ) ;
140
+ } else
141
+ {
142
+ player . PrintToChat ( Localizer [ "InGame_Command_Enabled" ] ) ;
138
143
}
144
+
145
+ SaveToJsonFile ( ( int ) playerid , playerValue , personDate ) ;
139
146
}
140
147
return HookResult . Continue ;
141
148
}
@@ -235,21 +242,22 @@ private void OnClientPutInServer(int playerSlot)
235
242
}
236
243
}
237
244
238
-
239
245
if ( ! string . IsNullOrEmpty ( Config . InGameSoundConnect ) )
240
246
{
241
247
foreach ( var players in GetPlayerControllers ( ) . FindAll ( x => x . Connected == PlayerConnectedState . PlayerConnected && ! x . IsBot ) )
242
248
{
243
249
if ( players . IsValid )
244
250
{
251
+ var playerid = players . SteamID ;
252
+ bool playerValue = RetrieveBoolValueById ( ( int ) playerid ) ;
245
253
if ( ! string . IsNullOrEmpty ( Config . InGameSoundDisableCommands ) )
246
254
{
247
- if ( player . UserId . HasValue )
255
+ if ( playerValue )
256
+ {
257
+ //skip sounds
258
+ } else
248
259
{
249
- if ( OnDisabled . ContainsKey ( player . UserId . Value ) == false )
250
- {
251
- players . ExecuteClientCommand ( "play " + Config . InGameSoundConnect ) ;
252
- }
260
+ players . ExecuteClientCommand ( "play " + Config . InGameSoundConnect ) ;
253
261
}
254
262
} else
255
263
{
@@ -370,14 +378,16 @@ private HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInf
370
378
{
371
379
if ( players . IsValid )
372
380
{
381
+ var playerid = players . SteamID ;
382
+ bool playerValue = RetrieveBoolValueById ( ( int ) playerid ) ;
373
383
if ( ! string . IsNullOrEmpty ( Config . InGameSoundDisableCommands ) )
374
384
{
375
- if ( player . UserId . HasValue )
385
+ if ( playerValue )
376
386
{
377
- if ( OnDisabled . ContainsKey ( player . UserId . Value ) == false )
378
- {
379
- players . ExecuteClientCommand ( "play " + Config . InGameSoundDisconnect ) ;
380
- }
387
+ //skip sounds
388
+ } else
389
+ {
390
+ players . ExecuteClientCommand ( "play " + Config . InGameSoundDisconnect ) ;
381
391
}
382
392
} else
383
393
{
@@ -640,6 +650,98 @@ static void DeleteOldFiles(string folderPath, string searchPattern, TimeSpan max
640
650
Console . WriteLine ( $ "Error: { ex . Message } ") ;
641
651
}
642
652
}
653
+ private void SaveToJsonFile ( int id , bool boolValue , DateTime date )
654
+ {
655
+ string Fpath = Path . Combine ( ModuleDirectory , "../../plugins/CnD_Sound/Cookies/" ) ;
656
+ string Fpathc = Path . Combine ( ModuleDirectory , "../../plugins/CnD_Sound/Cookies/CnD_Sound_Cookies.json" ) ;
657
+ try
658
+ {
659
+ if ( ! Directory . Exists ( Fpath ) )
660
+ {
661
+ Directory . CreateDirectory ( Fpath ) ;
662
+ }
663
+
664
+ if ( ! File . Exists ( Fpathc ) )
665
+ {
666
+ File . WriteAllText ( Fpathc , "[]" ) ;
667
+ }
668
+
669
+ List < PersonData > allPersonsData ;
670
+ string jsonData = File . ReadAllText ( Fpathc ) ;
671
+ allPersonsData = JsonConvert . DeserializeObject < List < PersonData > > ( jsonData ) ?? new List < PersonData > ( ) ;
672
+
673
+ PersonData existingPerson = allPersonsData . Find ( p => p . Id == id ) ! ;
674
+
675
+ if ( existingPerson != null )
676
+ {
677
+ existingPerson . BoolValue = boolValue ;
678
+ existingPerson . Date = date ;
679
+ }
680
+ else
681
+ {
682
+ PersonData newPerson = new PersonData { Id = id , BoolValue = boolValue , Date = date } ;
683
+ allPersonsData . Add ( newPerson ) ;
684
+ }
685
+ allPersonsData . RemoveAll ( p => ( DateTime . Now - p . Date ) . TotalDays > Config . RemovePlayerCookieOlderThanXDays ) ;
686
+
687
+ string updatedJsonData = JsonConvert . SerializeObject ( allPersonsData , Formatting . Indented ) ;
688
+ try
689
+ {
690
+ File . WriteAllText ( Fpathc , updatedJsonData ) ;
691
+ } catch
692
+ {
693
+ }
694
+ } catch
695
+ {
696
+ }
697
+ }
698
+
699
+ private bool RetrieveBoolValueById ( int targetId )
700
+ {
701
+ string Fpath = Path . Combine ( ModuleDirectory , "../../plugins/CnD_Sound/Cookies/" ) ;
702
+ string Fpathc = Path . Combine ( ModuleDirectory , "../../plugins/CnD_Sound/Cookies/CnD_Sound_Cookies.json" ) ;
703
+ try
704
+ {
705
+ if ( File . Exists ( Fpathc ) )
706
+ {
707
+ string jsonData = File . ReadAllText ( Fpathc ) ;
708
+ List < PersonData > allPersonsData = JsonConvert . DeserializeObject < List < PersonData > > ( jsonData ) ?? new List < PersonData > ( ) ;
709
+
710
+ PersonData targetPerson = allPersonsData . Find ( p => p . Id == targetId ) ! ;
711
+
712
+ if ( targetPerson != null )
713
+ {
714
+ if ( DateTime . Now - targetPerson . Date <= TimeSpan . FromDays ( Config . RemovePlayerCookieOlderThanXDays ) )
715
+ {
716
+ return targetPerson . BoolValue ;
717
+ }
718
+ else
719
+ {
720
+ allPersonsData . Remove ( targetPerson ) ;
721
+ string updatedJsonData = JsonConvert . SerializeObject ( allPersonsData , Formatting . Indented ) ;
722
+ try
723
+ {
724
+ File . WriteAllText ( Fpathc , updatedJsonData ) ;
725
+ } catch
726
+ {
727
+ }
728
+ }
729
+ }
730
+ }
731
+ return false ;
732
+ } catch
733
+ {
734
+ return false ;
735
+ }
736
+ }
737
+
738
+
739
+ private class PersonData
740
+ {
741
+ public int Id { get ; set ; }
742
+ public bool BoolValue { get ; set ; }
743
+ public DateTime Date { get ; set ; }
744
+ }
643
745
public override void Unload ( bool hotReload )
644
746
{
645
747
OnDisabled . Clear ( ) ;
0 commit comments