-
Notifications
You must be signed in to change notification settings - Fork 1
/
KnifeCooldownManager.cs
85 lines (68 loc) · 2.56 KB
/
KnifeCooldownManager.cs
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
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Utils;
namespace ChaseMod;
internal class KnifeCooldownManager
{
private readonly ChaseMod _plugin;
private readonly Dictionary<CBasePlayerController, DateTime> _invulnerablePlayers = new();
public KnifeCooldownManager(ChaseMod chaseMod)
{
_plugin = chaseMod;
}
public void EnableHooks()
{
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(CBaseEntity_TakeDamageOldFuncHook, HookMode.Pre);
}
public void DisableHooks()
{
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(CBaseEntity_TakeDamageOldFuncHook, HookMode.Pre);
}
private HookResult CBaseEntity_TakeDamageOldFuncHook(DynamicHook hook)
{
var entity = hook.GetParam<CEntityInstance>(0);
var info = hook.GetParam<CTakeDamageInfo>(1);
if (!entity.IsValid || !info.Attacker.IsValid)
{
return HookResult.Continue;
}
if (entity.DesignerName != "player" || info.Attacker.Value!.DesignerName != "player")
{
return HookResult.Continue;
}
var attacker = info.Attacker.Value!.As<CCSPlayerPawn>();
var pawn = entity.As<CCSPlayerPawn>();
var controller = pawn.OriginalController.Value!;
if (attacker.WeaponServices == null || !attacker.WeaponServices.ActiveWeapon.IsValid)
{
return HookResult.Continue;
}
var weapon = attacker.WeaponServices.ActiveWeapon.Value!;
if (weapon.DesignerName != "weapon_knife")
{
return HookResult.Continue;
}
// if attacked player is counter-terrorist, ignore damage from knife
if (controller.TeamNum == (byte)CsTeam.CounterTerrorist)
{
return HookResult.Handled;
}
// if attacked player is not terrorist, handle normally?
if (controller.TeamNum != (byte)CsTeam.Terrorist)
{
return HookResult.Continue;
}
if (_invulnerablePlayers.TryGetValue(controller, out DateTime expiry))
{
if (expiry > DateTime.Now)
{
return HookResult.Handled;
}
}
info.Damage = _plugin.Config.KnifeDamage;
info.DamageFlags |= TakeDamageFlags_t.DFLAG_SUPPRESS_PHYSICS_FORCE;
_invulnerablePlayers[controller] = DateTime.Now.AddSeconds(_plugin.Config.KnifeCooldown);
return HookResult.Continue;
}
}