forked from itsAudioo/SpawnProtection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.cs
More file actions
151 lines (117 loc) · 4.13 KB
/
Events.cs
File metadata and controls
151 lines (117 loc) · 4.13 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
namespace SpawnProtection
{
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Utils;
public sealed partial class SpawnProt
{
public void RegisterEventsListeners()
{
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn, HookMode.Pre);
RegisterEventHandler<EventRoundPrestart>(OnRoundPrestart);
RegisterEventHandler<EventRoundEnd>(OnRoundEnd);
RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath, HookMode.Pre);
RegisterEventHandler<EventWeaponFire>(OnWeaponFire);
RegisterEventHandler<EventPlayerActivate>(OnPlayerActivate);
RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect, HookMode.Pre);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre);
if (Config.CenterHtmlMessage)
{
RegisterListener<Listeners.OnTick>(() =>
{
if (Config.DisableDuringWarmup && (IsWarmup || gameRules is null))
return;
protectedPlayers.ToList().ForEach(OnTick);
});
}
RegisterListener<Listeners.OnMapStart>(name =>
{
protectedPlayers.Clear();
AddTimer(1.0f, GetGameRules);
AddTimer(1.0f, () =>
{
_freezeTime = ConVar.Find("mp_freezetime")!.GetPrimitiveValue<int>() - 1;
});
});
}
public HookResult OnRoundPrestart(EventRoundPrestart @event, GameEventInfo info)
{
protectedPlayers.Clear();
foreach (var state in _playerStates.Values)
{
state.ShowCenterMessage = false;
state.SpawnTimer?.Kill();
}
return HookResult.Continue;
}
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
protectedPlayers.Clear();
foreach (var state in _playerStates.Values)
{
state.ShowCenterMessage = false;
state.SpawnTimer?.Kill();
}
return HookResult.Continue;
}
public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
var player = @event.Userid;
if (player is null || player.IsBot() || !_playerStates.TryGetValue(player.Index, out var state))
return HookResult.Continue;
protectedPlayers.Remove(player);
StopSpawnProtection(player, state, true);
return HookResult.Continue;
}
public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
var player = @event.Userid;
if (player is null || player.IsBot() || !player.IzGud() ||
(Config.CTProtOnly && player.Team == CsTeam.Terrorist) ||
IsWarmup)
{
return HookResult.Continue;
}
StartSpawnProtection(player);
return HookResult.Continue;
}
public static HookResult OnTakeDamage(DynamicHook hook)
{
CEntityInstance entity = hook.GetParam<CEntityInstance>(0);
var playerPawn = hook.GetParam<CCSPlayerPawn>(0);
var player = playerPawn?.Controller.Value?.As<CCSPlayerController>();
if (player is null || player.IsBot() || !player.IsProtected())
return HookResult.Continue;
hook.SetReturn(false);
return HookResult.Handled;
}
public HookResult OnWeaponFire(EventWeaponFire @event, GameEventInfo info)
{
if (!Config.StopProtectionOnWeaponFire)
return HookResult.Continue;
CCSPlayerController? player = @event.Userid;
if (player is null || player.IsBot() || !player.IsAlive() || !player.IsProtected())
return HookResult.Continue;
StopSpawnProtection(player, _playerStates[player.Index]);
return HookResult.Continue;
}
public HookResult OnPlayerActivate(EventPlayerActivate @event, GameEventInfo _)
{
CCSPlayerController? player = @event.Userid;
if (player is not null && player.IzGud() && !player.IsBot())
playerCache.Add(player);
return HookResult.Continue;
}
public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo _)
{
CCSPlayerController? player = @event.Userid;
if (player is null || player.IsBot())
return HookResult.Continue;
protectedPlayers.Remove(player);
playerCache.Remove(player);
return HookResult.Continue;
}
}
}