using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Utils;
using System.Collections.Generic;
public class C4Timer : BasePlugin
{
public override string ModuleName => "C4Timer";
public override string ModuleVersion => "1.0";
private float _plantTime = 0f;
private ConVar? _c4TimerCvar;
public override void Load(bool hotReload)
{
RegisterEventHandler<EventBombPlanted>(OnBombPlanted);
RegisterEventHandler<EventBombDefused>(OnBombDefused);
RegisterEventHandler<EventBombExploded>(OnBombExploded);
RegisterEventHandler<EventRoundStart>(OnRoundStart);
RegisterListener<Listeners.OnTick>(OnTick);
}
private HookResult OnRoundStart(EventRoundStart _, GameEventInfo __)
{
_plantTime = 0f;
return HookResult.Continue;
}
private HookResult OnBombPlanted(EventBombPlanted @event, GameEventInfo _)
{
_c4TimerCvar = ConVar.Find("mp_c4timer");
_plantTime = Server.CurrentTime;
return HookResult.Continue;
}
private HookResult OnBombDefused(EventBombDefused _, GameEventInfo __)
{
_plantTime = 0f;
return HookResult.Continue;
}
private HookResult OnBombExploded(EventBombExploded _, GameEventInfo __)
{
_plantTime = 0f;
return HookResult.Continue;
}
private int GetC4Duration()
{
return _c4TimerCvar?.GetPrimitiveValue<int>() ?? 40;
}
private void OnTick()
{
if (_plantTime == 0f)
return;
float remain = GetC4Duration() - (Server.CurrentTime - _plantTime);
if (remain <= 0) return;
foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && !p.IsBot))
p.PrintToCenterAlert($"C4爆炸倒计时:{remain:F1}s");
}
}