Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit f2b44aa

Browse files
authored
Bug Fix
1 parent baa61e3 commit f2b44aa

3 files changed

Lines changed: 32 additions & 43 deletions

File tree

Events.cs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public void RegisterEventsListeners()
1515
{
1616
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
1717
RegisterEventHandler<EventPlayerHurt>(OnPlayerHurt);
18-
RegisterEventHandler<EventRoundStart>(OnRoundStart);
1918
RegisterEventHandler<EventRoundPrestart>(OnRoundPrestart);
2019
RegisterEventHandler<EventRoundEnd>(OnRoundEnd);
2120
RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath);
@@ -38,43 +37,24 @@ public void RegisterEventsListeners()
3837
Logger.LogInformation("Registered Events and Listeners");
3938
}
4039

41-
public CounterStrikeSharp.API.Modules.Timers.Timer? SpawnTimer;
40+
public Timer? SpawnTimer { get; set; }
4241
public CounterStrikeSharp.API.Modules.Timers.Timer? renderTimer;
43-
public float CountdownTimer;
4442

45-
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
46-
{
47-
SpawnTimer?.Kill();
48-
CountdownTimer = Config.SpawnProtTime;
49-
50-
if (IsWarmup)
51-
return HookResult.Continue;
52-
53-
AddTimer(FreezeTime, () =>
54-
{
55-
SpawnTimer = AddTimer(0.1f, () =>
56-
{
57-
if (CountdownTimer <= 0) { SpawnTimer?.Kill(); return; }
58-
CountdownTimer -= 0.1f;
59-
}, TimerFlags.REPEAT);
60-
});
61-
return HookResult.Continue;
62-
}
6343
public HookResult OnRoundPrestart(EventRoundPrestart @event, GameEventInfo info)
6444
{
45+
SpawnTimer?.Kill();
6546
Utilities.GetPlayers().
66-
Where(player => player is not null && player.IsValid == true).ToList().ForEach(x => HandleCenterMessage(x));
47+
Where(player => player is not null && player.IsValid == true).ToList().ForEach(x => CenterMessage[x.Index] = false);
6748

6849
return HookResult.Continue;
6950
}
7051

7152
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
7253
{
54+
SpawnTimer?.Kill();
7355
Utilities.GetPlayers().
7456
Where(player => player is not null && player.IsValid == true).ToList().ForEach(x => CenterMessage[x.Index] = false);
7557

76-
SpawnTimer?.Kill();
77-
7858
return HookResult.Continue;
7959
}
8060

@@ -104,13 +84,21 @@ public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
10484
if (IsWarmup)
10585
return HookResult.Continue;
10686

107-
AddTimer(FreezeTime, () =>
87+
SpawnTimer?.Kill();
88+
protTimer[player.Index] = Config.SpawnProtTime;
89+
90+
SpawnTimer = AddTimer(0.1f, () =>
10891
{
109-
HandleSpawnProt(player);
92+
if (protTimer[player.Index] <= 0) { SpawnTimer?.Kill(); return; }
93+
protTimer[player.Index] -= 0.1f;
94+
}, TimerFlags.REPEAT);
11095

111-
if (Config.TransparentModel)
112-
HandlePlayerModel(player);
113-
});
96+
HandleSpawnProt(player);
97+
98+
if (Config.TransparentModel)
99+
HandlePlayerModel(player);
100+
101+
HandleCenterMessage(player);
114102

115103
return HookResult.Continue;
116104
}
@@ -156,7 +144,7 @@ private void HandleProtectedPlayer(CCSPlayerController player, EventPlayerHurt @
156144
Utilities.SetStateChanged(player.PlayerPawn.Value, "CCSPlayerPawn", "m_ArmorValue");
157145

158146
if (Config.SpawnProtCenterMsg && player.IsAlive())
159-
player.PrintToCenter($"{Localizer["player_isprotected", (int)CountdownTimer]}");
147+
player.PrintToCenter($"{Localizer["player_isprotected", (int)protTimer[player.Index]]}");
160148

161149
CCSPlayerController? Attacker = @event.Attacker;
162150

Plugin.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ public sealed partial class SpawnProt : BasePlugin, IPluginConfig<PluginConfig>
88
public override string ModuleName => "SpawnProt";
99
public override string ModuleAuthor => "audio_brutalci";
1010
public override string ModuleDescription => "Simple spawn protection for CS2";
11-
public override string ModuleVersion => "0.0.3";
11+
public override string ModuleVersion => "0.0.4";
1212

1313
public static SpawnProtectionState[] playerHasSpawnProt = new SpawnProtectionState[64];
1414
public static readonly bool[] CenterMessage = new bool[64];
15+
public static readonly float[] protTimer = new float[64];
1516
public static int FreezeTime;
1617
CCSGameRules? gameRules;
1718

@@ -20,7 +21,7 @@ public void OnConfigParsed(PluginConfig config)
2021
{
2122
if (config.Version < Config.Version)
2223
{
23-
base.Logger.LogWarning("The plugin configuration is outdated. Please consider updating the configuration file. [Expected: {0} | Current: {1}]", this.Config.Version, config.Version);
24+
base.Logger.LogWarning("Plugin configuration is outdated! Please consider updating. [Expected: {0} | Current: {1}]", this.Config.Version, config.Version);
2425
}
2526

2627
this.Config = config;
@@ -37,13 +38,13 @@ public override void Load(bool hotReload)
3738
}
3839
public void OnTick(CCSPlayerController player)
3940
{
40-
float progressPercentage = CountdownTimer / Config.SpawnProtTime;
41+
float progressPercentage = protTimer[player.Index] / Config.SpawnProtTime;
4142
string color = GetColorBasedOnProgress(progressPercentage);
4243
string progressBar = GenerateProgressBar(progressPercentage);
4344

4445
player.PrintToCenterHtml(
4546
$"<font class='fontSize-m' color='orange'>{Localizer["center_isprotected"]}</font><br>" +
46-
$"[<font class='fontSize-m' color='{color}'>{(int)CountdownTimer}</font><font color='white'>] SECONDS LEFT!</font><br>" +
47+
$"[<font class='fontSize-m' color='{color}'>{(int)protTimer[player.Index]}</font><font color='white'>] SECONDS LEFT!</font><br>" +
4748
$"<font class='fontSize-l' color='{color}'>{progressBar}</font>"
4849
);
4950
}
@@ -55,7 +56,7 @@ public void HandleSpawnProt(CCSPlayerController player)
5556
AddTimer(Config.SpawnProtTime, () =>
5657
{
5758
playerHasSpawnProt[player.Index] = SpawnProtectionState.None;
58-
AddTimer(1.0f, () => { player.PrintToCenterAlert($" {Localizer["player_isnotprotected"]} "); });
59+
AddTimer(0.8f, () => { player.PrintToCenterAlert($" {Localizer["player_isnotprotected"]} "); });
5960
});
6061
}
6162

@@ -71,7 +72,7 @@ public void HandlePlayerModel(CCSPlayerController player)
7172
public void HandleCenterMessage(CCSPlayerController player)
7273
{
7374
CenterMessage[player.Index] = true;
74-
AddTimer(Config.SpawnProtTime + 1.0f, () => { CenterMessage[player.Index] = false; });
75+
AddTimer(Config.SpawnProtTime, () => { CenterMessage[player.Index] = false; });
7576
}
7677
}
7778
}

Utils.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ private void ResetPlayerColor(CCSPlayerController? player)
8282

8383
Color defaultColor = Color.FromArgb(255, 255, 255, 255);
8484
Server.NextFrame(() =>
85-
{
86-
if (player.PlayerPawn is not null && player.PlayerPawn.Value is not null && player.IsAlive())
87-
{
88-
player.PlayerPawn.Value.Render = defaultColor;
89-
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");
90-
}
91-
});
85+
{
86+
if (player.PlayerPawn is not null && player.PlayerPawn.Value is not null && player.IsAlive())
87+
{
88+
player.PlayerPawn.Value.Render = defaultColor;
89+
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseModelEntity", "m_clrRender");
90+
}
91+
});
9292
}
9393
void GetGameRules() => gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules!;
9494

0 commit comments

Comments
 (0)