-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Events.cs
349 lines (281 loc) · 9.75 KB
/
Events.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
namespace WeaponPaints
{
public partial class WeaponPaints
{
private bool _mvpPlayed;
[GameEventHandler]
public HookResult OnClientFullConnect(EventPlayerConnectFull @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
if (player is null || !player.IsValid || player.IsBot ||
WeaponSync == null || Database == null) return HookResult.Continue;
var playerInfo = new PlayerInfo
{
UserId = player.UserId,
Slot = player.Slot,
Index = (int)player.Index,
SteamId = player.SteamID.ToString(),
Name = player.PlayerName,
IpAddress = player.IpAddress?.Split(":")[0]
};
try
{
_ = Task.Run(async () => await WeaponSync.GetPlayerData(playerInfo));
/*
if (Config.Additional.SkinEnabled)
{
_ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));
}
if (Config.Additional.KnifeEnabled)
{
_ = Task.Run(async () => await weaponSync.GetKnifeFromDatabase(playerInfo));
}
if (Config.Additional.GloveEnabled)
{
_ = Task.Run(async () => await weaponSync.GetGloveFromDatabase(playerInfo));
}
if (Config.Additional.AgentEnabled)
{
_ = Task.Run(async () => await weaponSync.GetAgentFromDatabase(playerInfo));
}
if (Config.Additional.MusicEnabled)
{
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
}
*/
}
catch
{
}
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
if (player is null || !player.IsValid || player.IsBot) return HookResult.Continue;
var playerInfo = new PlayerInfo
{
UserId = player.UserId,
Slot = player.Slot,
Index = (int)player.Index,
SteamId = player.SteamID.ToString(),
Name = player.PlayerName,
IpAddress = player.IpAddress?.Split(":")[0]
};
Task.Run(async () =>
{
if (WeaponSync != null)
await WeaponSync.SyncStatTrakToDatabase(playerInfo);
if (Config.Additional.SkinEnabled)
{
GPlayerWeaponsInfo.TryRemove(player.Slot, out _);
}
});
if (Config.Additional.KnifeEnabled)
{
GPlayersKnife.TryRemove(player.Slot, out _);
}
if (Config.Additional.GloveEnabled)
{
GPlayersGlove.TryRemove(player.Slot, out _);
}
if (Config.Additional.AgentEnabled)
{
GPlayersAgent.TryRemove(player.Slot, out _);
}
if (Config.Additional.MusicEnabled)
{
GPlayersMusic.TryRemove(player.Slot, out _);
}
if (Config.Additional.PinsEnabled)
{
GPlayersPin.TryRemove(player.Slot, out _);
}
_temporaryPlayerWeaponWear.TryRemove(player.Slot, out _);
CommandsCooldown.Remove(player.Slot);
return HookResult.Continue;
}
private void OnMapStart(string mapName)
{
if (Config.Additional is { KnifeEnabled: false, SkinEnabled: false, GloveEnabled: false }) return;
if (Database != null)
WeaponSync = new WeaponSynchronization(Database, Config);
_fadeSeed = 0;
_nextItemId = MinimumCustomItemId;
}
private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
if (player is null || !player.IsValid || Config.Additional is { KnifeEnabled: false, GloveEnabled: false })
return HookResult.Continue;
CCSPlayerPawn? pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid)
return HookResult.Continue;
GivePlayerMusicKit(player);
GivePlayerAgent(player);
GivePlayerGloves(player);
GivePlayerPin(player);
return HookResult.Continue;
}
private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
_gBCommandsAllowed = false;
return HookResult.Continue;
}
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
_gBCommandsAllowed = true;
_mvpPlayed = false;
return HookResult.Continue;
}
private HookResult OnRoundMvp(EventRoundMvp @event, GameEventInfo info)
{
if (_mvpPlayed)
return HookResult.Continue;
var player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot)
return HookResult.Continue;
if (!(GPlayersMusic.TryGetValue(player.Slot, out var musicInfo)
&& musicInfo.TryGetValue(player.Team, out var musicId)
&& musicId != 0))
return HookResult.Continue;
@event.Musickitid = musicId;
@event.Nomusic = 0;
info.DontBroadcast = true;
var newEvent = new EventRoundMvp(true)
{
Userid = player,
Musickitid = musicId,
Nomusic = 0,
};
_mvpPlayed = true;
newEvent.FireEvent(false);
return HookResult.Continue;
}
private HookResult OnGiveNamedItemPost(DynamicHook hook)
{
try
{
var itemServices = hook.GetParam<CCSPlayer_ItemServices>(0);
var weapon = hook.GetReturn<CBasePlayerWeapon>();
if (!weapon.DesignerName.Contains("weapon"))
return HookResult.Continue;
var player = GetPlayerFromItemServices(itemServices);
if (player != null)
{
GivePlayerWeaponSkin(player, weapon);
}
}
catch { }
return HookResult.Continue;
}
private void OnEntityCreated(CEntityInstance entity)
{
var designerName = entity.DesignerName;
if (designerName.Contains("weapon"))
{
Server.NextFrame(() =>
{
var weapon = new CBasePlayerWeapon(entity.Handle);
if (!weapon.IsValid) return;
try
{
SteamID? steamid = null;
if (weapon.OriginalOwnerXuidLow > 0)
steamid = new SteamID(weapon.OriginalOwnerXuidLow);
CCSPlayerController? player;
if (steamid != null && steamid.IsValid())
{
player = Utilities.GetPlayers().FirstOrDefault(p => p.IsValid && p.SteamID == steamid.SteamId64);
if (player == null)
player = Utilities.GetPlayerFromSteamId(weapon.OriginalOwnerXuidLow);
}
else
{
CCSWeaponBaseGun gun = weapon.As<CCSWeaponBaseGun>();
player = Utilities.GetPlayerFromIndex((int)weapon.OwnerEntity.Index) ?? Utilities.GetPlayerFromIndex((int)gun.OwnerEntity.Value!.Index);
}
if (string.IsNullOrEmpty(player?.PlayerName)) return;
if (!Utility.IsPlayerValid(player)) return;
GivePlayerWeaponSkin(player, weapon);
}
catch (Exception)
{
}
});
}
}
private void OnTick()
{
if (!Config.Additional.ShowSkinImage) return;
foreach (var player in Utilities.GetPlayers().Where(p =>
p is { IsValid: true, PlayerPawn.IsValid: true, IsBot: false } and
{ Connected: PlayerConnectedState.PlayerConnected }
)
)
{
if (_playerWeaponImage.TryGetValue(player.Slot, out var value) && !string.IsNullOrEmpty(value))
{
player.PrintToCenterHtml("<img src='{PATH}'</img>".Replace("{PATH}", value));
}
}
}
[GameEventHandler]
public HookResult OnItemPickup(EventItemPickup @event, GameEventInfo _)
{
// if (!IsWindows) return HookResult.Continue;
var player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot) return HookResult.Continue;
if (!@event.Item.Contains("knife")) return HookResult.Continue;
var weaponDefIndex = (int)@event.Defindex;
if (!HasChangedKnife(player, out var _) || !HasChangedPaint(player, weaponDefIndex, out var _))
return HookResult.Continue;
if (player is { Connected: PlayerConnectedState.PlayerConnected, PawnIsAlive: true, PlayerPawn.IsValid: true })
{
GiveOnItemPickup(player);
}
return HookResult.Continue;
}
private HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Attacker;
CCSPlayerController? victim = @event.Userid;
if (player is null || !player.IsValid)
return HookResult.Continue;
if (victim == null || !victim.IsValid || victim == player)
return HookResult.Continue;
CBasePlayerWeapon? weapon = player.PlayerPawn.Value?.WeaponServices?.ActiveWeapon.Value;
if (weapon == null) return HookResult.Continue;
int weaponDefIndex = weapon.AttributeManager.Item.ItemDefinitionIndex;
if (!HasChangedPaint(player, weaponDefIndex, out var weaponInfo) || weaponInfo == null)
return HookResult.Continue;
if (!weaponInfo.StatTrak) return HookResult.Continue;
weaponInfo.StatTrakCount += 1;
CAttributeListSetOrAddAttributeValueByName.Invoke(weapon.AttributeManager.Item.NetworkedDynamicAttributes.Handle, "kill eater", ViewAsFloat((uint)weaponInfo.StatTrakCount));
CAttributeListSetOrAddAttributeValueByName.Invoke(weapon.AttributeManager.Item.NetworkedDynamicAttributes.Handle, "kill eater score type", 0);
CAttributeListSetOrAddAttributeValueByName.Invoke(weapon.AttributeManager.Item.AttributeList.Handle, "kill eater", ViewAsFloat((uint)weaponInfo.StatTrakCount));
CAttributeListSetOrAddAttributeValueByName.Invoke(weapon.AttributeManager.Item.AttributeList.Handle, "kill eater score type", 0);
return HookResult.Continue;
}
private void RegisterListeners()
{
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
RegisterEventHandler<EventRoundStart>(OnRoundStart);
RegisterEventHandler<EventRoundEnd>(OnRoundEnd);
RegisterEventHandler<EventRoundMvp>(OnRoundMvp);
RegisterListener<Listeners.OnEntityCreated>(OnEntityCreated);
RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath);
if (Config.Additional.ShowSkinImage)
RegisterListener<Listeners.OnTick>(OnTick);
VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post);
}
}
}