Skip to content

Commit d1c2a1a

Browse files
authored
1.0.8
1 parent e0521b7 commit d1c2a1a

File tree

1 file changed

+137
-35
lines changed

1 file changed

+137
-35
lines changed

CnD_Sound.cs

Lines changed: 137 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
using System.Drawing;
88
using Microsoft.Extensions.Localization;
99
using CounterStrikeSharp.API.Modules.Commands;
10+
using Newtonsoft.Json;
1011

1112
namespace CnD_Sound;
1213

1314
public class CnDSoundConfig : BasePluginConfig
1415
{
1516
[JsonPropertyName("InGameSoundDisableCommands")] public string InGameSoundDisableCommands { get; set; } = "!stopsound,!stopsounds";
17+
[JsonPropertyName("RemovePlayerCookieOlderThanXDays")] public int RemovePlayerCookieOlderThanXDays { get; set; } = 7;
1618
[JsonPropertyName("InGameSoundConnect")] public string InGameSoundConnect { get; set; } = "sounds/buttons/blip1.vsnd_c";
1719
[JsonPropertyName("InGameSoundDisconnect")] public string InGameSoundDisconnect { get; set; } = "sounds/player/taunt_clap_01.vsnd_c";
1820

1921

20-
2122
[JsonPropertyName("SendLogToText")] public bool SendLogToText { get; set; } = false;
2223
[JsonPropertyName("LogFileFormat")] public string LogFileFormat { get; set; } = ".txt";
2324
[JsonPropertyName("LogFileDateFormat")] public string LogFileDateFormat { get; set; } = "MM-dd-yyyy";
@@ -44,7 +45,7 @@ public class CnDSoundConfig : BasePluginConfig
4445
public class CnDSound : BasePlugin, IPluginConfig<CnDSoundConfig>
4546
{
4647
public override string ModuleName => "Connect Disconnect Sound";
47-
public override string ModuleVersion => "1.0.7";
48+
public override string ModuleVersion => "1.0.8";
4849
public override string ModuleAuthor => "Gold KingZ";
4950
public override string ModuleDescription => "Connect , Disconnect , Country , City , Message , Sound , Logs , Discord";
5051
internal static IStringLocalizer? Stringlocalizer;
@@ -86,7 +87,8 @@ public override void Load(bool hotReload)
8687
private HookResult OnPlayerSayPublic(CCSPlayerController? player, CommandInfo info)
8788
{
8889
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);
9092
var message = info.GetArg(1);
9193
if (string.IsNullOrWhiteSpace(message)) return HookResult.Continue;
9294
string trimmedMessage1 = message.TrimStart();
@@ -96,25 +98,28 @@ private HookResult OnPlayerSayPublic(CCSPlayerController? player, CommandInfo in
9698

9799
if (disableCommands.Any(cmd => cmd.Equals(trimmedMessage, StringComparison.OrdinalIgnoreCase)))
98100
{
99-
if (player.UserId.HasValue)
101+
DateTime personDate = DateTime.Now;
102+
103+
104+
playerValue = !playerValue;
105+
106+
if (playerValue)
100107
{
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"]);
110112
}
113+
114+
SaveToJsonFile((int)playerid, playerValue, personDate);
111115
}
112116
return HookResult.Continue;
113117
}
114118
private HookResult OnPlayerSayTeam(CCSPlayerController? player, CommandInfo info)
115119
{
116120
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);
118123
var message = info.GetArg(1);
119124
if (string.IsNullOrWhiteSpace(message)) return HookResult.Continue;
120125
string trimmedMessage1 = message.TrimStart();
@@ -124,18 +129,20 @@ private HookResult OnPlayerSayTeam(CCSPlayerController? player, CommandInfo info
124129

125130
if (disableCommands.Any(cmd => cmd.Equals(trimmedMessage, StringComparison.OrdinalIgnoreCase)))
126131
{
127-
if (player.UserId.HasValue)
132+
DateTime personDate = DateTime.Now;
133+
134+
135+
playerValue = !playerValue;
136+
137+
if (playerValue)
128138
{
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"]);
138143
}
144+
145+
SaveToJsonFile((int)playerid, playerValue, personDate);
139146
}
140147
return HookResult.Continue;
141148
}
@@ -235,21 +242,22 @@ private void OnClientPutInServer(int playerSlot)
235242
}
236243
}
237244

238-
239245
if (!string.IsNullOrEmpty(Config.InGameSoundConnect))
240246
{
241247
foreach(var players in GetPlayerControllers().FindAll(x => x.Connected == PlayerConnectedState.PlayerConnected && !x.IsBot))
242248
{
243249
if (players.IsValid)
244250
{
251+
var playerid = players.SteamID;
252+
bool playerValue = RetrieveBoolValueById((int)playerid);
245253
if (!string.IsNullOrEmpty(Config.InGameSoundDisableCommands))
246254
{
247-
if (player.UserId.HasValue)
255+
if (playerValue)
256+
{
257+
//skip sounds
258+
}else
248259
{
249-
if(OnDisabled.ContainsKey(player.UserId.Value) == false)
250-
{
251-
players.ExecuteClientCommand("play " + Config.InGameSoundConnect);
252-
}
260+
players.ExecuteClientCommand("play " + Config.InGameSoundConnect);
253261
}
254262
}else
255263
{
@@ -370,14 +378,16 @@ private HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInf
370378
{
371379
if (players.IsValid)
372380
{
381+
var playerid = players.SteamID;
382+
bool playerValue = RetrieveBoolValueById((int)playerid);
373383
if (!string.IsNullOrEmpty(Config.InGameSoundDisableCommands))
374384
{
375-
if (player.UserId.HasValue)
385+
if (playerValue)
376386
{
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);
381391
}
382392
}else
383393
{
@@ -640,6 +650,98 @@ static void DeleteOldFiles(string folderPath, string searchPattern, TimeSpan max
640650
Console.WriteLine($"Error: {ex.Message}");
641651
}
642652
}
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+
}
643745
public override void Unload(bool hotReload)
644746
{
645747
OnDisabled.Clear();

0 commit comments

Comments
 (0)