Repository navigation
Expand file tree
/
Copy pathC4Timer.cs
More file actions
239 lines (178 loc) · 7.63 KB
/
Copy pathC4Timer.cs
File metadata and controls
239 lines (178 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
using System.Text.Json.Serialization;
using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;
namespace C4Timer;
public class C4TimerConfig : BasePluginConfig
{
[JsonPropertyName("EnableTimer")]
public bool EnableTimer { get; set; } = true;
[JsonPropertyName("EnableProgressBar")]
public bool EnableProgressBar { get; set; } = true;
[JsonPropertyName("TimerStarting")]
public int TimerStarting { get; set; } = 45;
[JsonPropertyName("LeftSideTimer")]
public string LeftSideTimer { get; set; } = "-[ ";
[JsonPropertyName("RightSideTimer")]
public string RightSideTimer { get; set; } = " ]-";
[JsonPropertyName("EnableColorMessage")]
public bool EnableColorMessage { get; set; } = true;
[JsonPropertyName("SidesTimerColor")]
public string SidesTimerColor { get; set; } = "45:white";
[JsonPropertyName("TimeColor")]
public string TimeColor { get; set; } = "20:yellow, 10:red, 5:darkred";
[JsonPropertyName("ProgressBarColor")]
public string ProgressBarColor { get; set; } = "20:yellow, 10:red, 5:darkred";
}
public class C4Timer : BasePlugin, IPluginConfig<C4TimerConfig>
{
public override string ModuleName => "C4 Timer";
public override string ModuleVersion => "1.6";
public override string ModuleAuthor => "belom0r";
Dictionary<int, string> TimeColor = new Dictionary<int, string>();
Dictionary<int, string> ProgressBarColor = new Dictionary<int, string>();
Dictionary<int, string> SidesTimerColor = new Dictionary<int, string>();
private bool PlantedC4 = false;
private int TimerLength = 0;
private int TimerСountdown = 0;
private string messageCountdown = "";
private Timer? CountdownToExplosion;
public required C4TimerConfig Config { get; set; }
public void OnConfigParsed(C4TimerConfig config) { Config = config; }
public override void Load(bool hotReload)
{
RegisterEventHandler<EventBombPlanted>(BombPlantedPost); //bPlantedC4 = true
RegisterEventHandler<EventRoundPrestart>((_, _) => { PlantedC4 = false; return HookResult.Continue; });
RegisterEventHandler<EventBombExploded>((_, _) => { PlantedC4 = false; return HookResult.Continue; });
RegisterEventHandler<EventBombDefused>((_, _) => { PlantedC4 = false; return HookResult.Continue; });
if (Config.EnableColorMessage)
{
RegisterListener<Listeners.OnTick>(OnTick);
}
ColorMsg(Config.TimeColor, TimeColor);
ColorMsg(Config.ProgressBarColor, ProgressBarColor);
ColorMsg(Config.SidesTimerColor, SidesTimerColor);
}
private HookResult BombPlantedPost(EventBombPlanted @event, GameEventInfo info)
{
var planted = GetPlantedC4();
if (planted == null)
return HookResult.Continue;
PlantedC4 = true;
TimerLength = TimerСountdown = (int)(planted.TimerLength + 1.0f);
Config.TimerStarting = Math.Clamp(Config.TimerStarting, 0, TimerLength);
CountdownToExplosion = new Timer(1.0f, CountdownToExplosionC4, TimerFlags.REPEAT);
Timers.Add(CountdownToExplosion);
return HookResult.Continue;
}
public void OnTick()
{
if (string.IsNullOrEmpty(messageCountdown))
return;
foreach (var Player in GetPlayers())
{
Player.PrintToCenterHtml(messageCountdown);
}
}
public void CountdownToExplosionC4()
{
TimerСountdown--;
if ((int)TimerСountdown == 0)
{
messageCountdown = WrapWithColor("C4 bomb exploded !!!", "darkred");
}
else if (TimerСountdown < 0 || !PlantedC4)
{
CountdownToExplosion!.Kill();
Timers.Remove(CountdownToExplosion);
CountdownToExplosion = null;
TimerLength = 0;
TimerСountdown = 0;
messageCountdown = "";
return;
}
else messageCountdown = GenerateCountdownMessage();
if (!Config.EnableColorMessage)
VirtualFunctions.ClientPrintAll(HudDestination.Center, messageCountdown, 0, 0, 0, 0);
}
private string GenerateCountdownMessage()
{
if (TimerСountdown > Config.TimerStarting)
return "";
string timerStyle = GenerateTimerStyle();
string progressBarStyle = GenerateProgressBarStyle();
return Config.EnableColorMessage
? $"{timerStyle}{progressBarStyle}"
: ConnectStrings(timerStyle, progressBarStyle);
}
private string GenerateTimerStyle()
{
if (!Config.EnableTimer)
return "";
string leftSide = WrapWithColor(Config.LeftSideTimer, SidesTimerColor[TimerСountdown]);
string time = WrapWithColor(TimerСountdown.ToString(), TimeColor[TimerСountdown]);
string rightSide = WrapWithColor(Config.RightSideTimer, SidesTimerColor[TimerСountdown]);
return $"{leftSide}{time}{rightSide}";
}
private string GenerateProgressBarStyle()
{
if (!Config.EnableProgressBar)
return "";
int total = Math.Min(Config.TimerStarting, TimerLength);
char[] progressBar = new char[total];
for (int i = 0; i < total; i++)
progressBar[i] = i >= TimerСountdown ? '-' : '|';
string progressBar_txt = WrapWithColor(new string(progressBar), ProgressBarColor[TimerСountdown]);
return Config.EnableTimer && Config.EnableColorMessage ? "<br>" + progressBar_txt : progressBar_txt;
}
private string WrapWithColor(string text, string color)
{
return Config.EnableColorMessage ? $"<font color='{color}'>{text}</font>" : text;
}
private CPlantedC4? GetPlantedC4()
{
var PlantedC4 = Utilities.FindAllEntitiesByDesignerName<CPlantedC4>("planted_c4");
if (PlantedC4 == null || !PlantedC4.Any())
return null;
return PlantedC4.FirstOrDefault();
}
public string ConnectStrings(string str1, string str2)
{
if (string.IsNullOrEmpty(str2))
return str1;
return $"{str1}{Environment.NewLine}{str2}";
}
public void ColorMsg(string msg, Dictionary<int, string> colorDictionary)
{
colorDictionary.Clear();
for (int i = 0; i <= Config.TimerStarting; i++)
colorDictionary[i] = "white";
if (!Config.EnableColorMessage || string.IsNullOrEmpty(msg))
return;
foreach (var color in msg.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
try
{
var elements = color.Split(':', StringSplitOptions.RemoveEmptyEntries);
if (elements.Length != 2) continue;
int index = int.Parse(elements[0]);
string colorValue = elements[1];
for (int i = index; i >= 0; i--)
colorDictionary[i] = colorValue;
}
catch
{
Logger.LogError($"Invalid color format: {color}");
}
}
}
public List<CCSPlayerController> GetPlayers()
{
return Utilities.GetPlayers().Where(player =>
player != null && player.IsValid && player.Connected == PlayerConnectedState.PlayerConnected).ToList();
}
}