-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathCCSPlayerController.cs
More file actions
355 lines (292 loc) · 11.9 KB
/
Copy pathCCSPlayerController.cs
File metadata and controls
355 lines (292 loc) · 11.9 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
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
350
351
352
353
354
355
using System;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.ValveConstants.Protobuf;
namespace CounterStrikeSharp.API.Core;
public partial class CCSPlayerController
{
public int? UserId => NativeAPI.GetUseridFromIndex((int)Index);
public CsTeam Team => (CsTeam)TeamNum;
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public IntPtr GiveNamedItem(string item)
{
return GiveNamedItem<CEntityInstance>(item)?.Handle ?? IntPtr.Zero;
}
public T? GiveNamedItem<T>(string item) where T : CEntityInstance
{
Guard.IsValidEntity(this);
return PlayerPawn.Value?.ItemServices?.As<CCSPlayer_ItemServices>().GiveNamedItem<T>(item);
}
public IntPtr GiveNamedItem(CsItem item)
{
string? itemString = EnumUtils.GetEnumMemberAttributeValue(item);
if (string.IsNullOrWhiteSpace(itemString))
{
return IntPtr.Zero;
}
return GiveNamedItem(itemString);
}
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void PrintToConsole(string message)
{
Guard.IsValidEntity(this);
NativeAPI.PrintToConsole((int)Index, $"{message}\n\0");
}
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void PrintToChat(string message)
{
Guard.IsValidEntity(this);
VirtualFunctions.ClientPrint(Handle, HudDestination.Chat, message, 0, 0, 0, 0);
}
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void PrintToCenter(string message)
{
Guard.IsValidEntity(this);
VirtualFunctions.ClientPrint(Handle, HudDestination.Center, message, 0, 0, 0, 0);
}
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void PrintToCenterAlert(string message)
{
Guard.IsValidEntity(this);
VirtualFunctions.ClientPrint(Handle, HudDestination.Alert, message, 0, 0, 0, 0);
}
public void PrintToCenterHtml(string message) => PrintToCenterHtml(message, 5);
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void PrintToCenterHtml(string message, int duration)
{
Guard.IsValidEntity(this);
var @event = new EventShowSurvivalRespawnStatus(true)
{
LocToken = message,
Duration = duration,
Userid = this
};
@event.FireEventToClient(this);
@event.Free();
}
/// <summary>
/// Drops the active player weapon on the ground.
/// </summary>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void DropActiveWeapon()
{
Guard.IsValidEntity(this);
var itemServices = PlayerPawn.Value?.ItemServices?.As<CCSPlayer_ItemServices>();
var activeWeapon = PlayerPawn.Value?.WeaponServices?.ActiveWeapon.Value;
if (activeWeapon == null || itemServices == null) return;
itemServices.DropActivePlayerWeapon(activeWeapon);
}
/// <summary>
/// Removes every weapon from the player.
/// </summary>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void RemoveWeapons()
{
Guard.IsValidEntity(this);
PlayerPawn.Value?.ItemServices?.As<CCSPlayer_ItemServices>().RemoveWeapons();
}
/// <summary>
/// Force player suicide
/// </summary>
/// <param name="explode"></param>
/// <param name="force"></param>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void CommitSuicide(bool explode, bool force)
{
Guard.IsValidEntity(this);
PlayerPawn.Value?.CommitSuicide(explode, force);
}
/// <summary>
/// Respawn player
/// </summary>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void Respawn()
{
Guard.IsValidEntity(this);
if (PlayerPawn.Value == null) return;
// The Call To Arms update appears to have invalidated the need for CCSPlayerPawn_Respawn.
SetPawn(PlayerPawn.Value);
VirtualFunction.CreateVoid<IntPtr>(Handle, GameData.GetOffset("CCSPlayerController_Respawn"))(Handle);
}
public bool IsBot => ((PlayerFlags)Flags).HasFlag(PlayerFlags.FL_FAKECLIENT);
/// <summary>
/// Forcibly switches the team of the player, the player will remain alive and keep their weapons.
/// </summary>
/// <param name="team">The team to switch to</param>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void SwitchTeam(CsTeam team)
{
Guard.IsValidEntity(this);
VirtualFunctions.SwitchTeam(Handle, (byte)team);
}
/// <summary>
/// Switches the team of the player, has the same effect as the "jointeam" console command.
/// <remarks>
/// This follows gamemode rules, so this will usually cause a player suicide/loss of weapons.
/// </remarks>
/// </summary>
/// <param name="team">The team to change to</param>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void ChangeTeam(CsTeam team)
{
Guard.IsValidEntity(this);
VirtualFunction.CreateVoid<IntPtr, CsTeam>(Handle, GameData.GetOffset("CCSPlayerController_ChangeTeam"))(Handle,
team);
}
/// <summary>
/// Get a ConVar value for given player
/// </summary>
/// <param name="conVar">Name of the convar to retrieve</param>
/// <returns>ConVar string value</returns>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public string GetConVarValue(string conVar)
{
Guard.IsValidEntity(this);
return NativeAPI.GetClientConvarValue(Slot, conVar);
}
public string GetConVarValue(ConVar? conVar)
{
if (conVar == null)
{
throw new Exception("Invalid convar passed to 'GetConVarValue'");
}
return GetConVarValue(conVar.Name);
}
/// <summary>
/// Sets a ConVar value on a fake client (bot).
/// </summary>
/// <param name="conVar">Console variable name</param>
/// <param name="value">String value to set</param>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
/// <exception cref="InvalidOperationException">Player is not a bot</exception>
public void SetFakeClientConVar(string conVar, string value)
{
Guard.IsValidEntity(this);
if (!IsBot) throw new InvalidOperationException("'SetFakeClientConVar' can only be called for fake clients (bots)");
NativeAPI.SetFakeClientConvarValue(Slot, conVar, value);
}
/// <summary>
/// <inheritdoc cref="SetFakeClientConVar(string,string)"/>
/// </summary>
/// <exception cref="ArgumentException"><paramref name="conVar"/> is <see langword="null"/></exception>
/// <inheritdoc cref="SetFakeClientConVar(string,string)" select="exception"/>
public void SetFakeClientConVar(ConVar conVar, string value)
{
if (conVar == null)
{
throw new ArgumentException("Invalid convar passed to 'SetFakeClientConVar'");
}
SetFakeClientConVar(conVar.Name, value);
}
/// <summary>
/// Gets the active pawns button state. Will work even if the player is dead or observing.
/// </summary>
public PlayerButtons Buttons => (PlayerButtons)Pawn.Value.MovementServices!.Buttons.ButtonStates[0];
/// <summary>
/// Issue the specified command to the specified client (mimics that client typing the command at the console).
/// Note: Only works for some commands, marked with the FCVAR_CLIENT_CAN_EXECUTE flag (not many).
/// </summary>
/// <param name="command"></param>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void ExecuteClientCommand(string command)
{
Guard.IsValidEntity(this);
NativeAPI.IssueClientCommand(Slot, command);
}
/// <summary>
/// Disconnects a player from the server with the specified reason.
/// </summary>
/// <param name="reason"></param>
public void Disconnect(NetworkDisconnectionReason reason)
{
NativeAPI.DisconnectClient(Slot, (int)reason);
}
/// <summary>
/// Issue the specified command directly from the server (mimics the server executing the command with the given player context).
/// <remarks>Works with server commands like `kill`, `explode`, `noclip`, etc. </remarks>
/// </summary>
/// <param name="command"></param>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void ExecuteClientCommandFromServer(string command)
{
Guard.IsValidEntity(this);
NativeAPI.IssueClientCommandFromServer(Slot, command);
}
/// <summary>
/// Overrides who a player can hear in voice chat.
/// </summary>
/// <param name="sender">Player talking in the voice chat</param>
/// <param name="override">Whether the talker should be heard</param>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public void SetListenOverride(CCSPlayerController sender, ListenOverride @override)
{
Guard.IsValidEntity(this);
NativeAPI.SetClientListening(Handle, sender.Handle, (Byte)@override);
}
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public ListenOverride GetListenOverride(CCSPlayerController sender)
{
Guard.IsValidEntity(this);
return NativeAPI.GetClientListening(Handle, sender.Handle);
}
public int Slot => (int)Index - 1;
/// <summary>
/// Returns the authorized SteamID of this user which has been validated with the SteamAPI.
/// </summary>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public SteamID? AuthorizedSteamID
{
get
{
Guard.IsValidEntity(this);
var authorizedSteamId = NativeAPI.GetPlayerAuthorizedSteamid(Slot);
if ((long)authorizedSteamId == -1) return null;
return (SteamID)authorizedSteamId;
}
}
/// <summary>
/// Returns the IP address (and possibly port) of this player.
/// <remarks>Returns 127.0.0.1 if the player is a bot.</remarks>
/// </summary>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public string? IpAddress
{
get
{
Guard.IsValidEntity(this);
var ipAddress = NativeAPI.GetPlayerIpAddress(Slot);
if (string.IsNullOrWhiteSpace(ipAddress)) return null;
return ipAddress;
}
}
/// <summary>
/// Determines how the player interacts with voice chat.
/// </summary>
/// <exception cref="InvalidOperationException">Entity is not valid</exception>
public VoiceFlags VoiceFlags
{
get
{
Guard.IsValidEntity(this);
return (VoiceFlags)NativeAPI.GetClientVoiceFlags(Handle);
}
set
{
Guard.IsValidEntity(this);
NativeAPI.SetClientVoiceFlags(Handle, (Byte)value);
}
}
[Obsolete(
"You are trying to call Teleport on a non-physical player. Maybe you mean Pawn? (See: https://docs.cssharp.dev/docs/guides/referencing-players.html#controllers--pawns)")]
public new void Teleport(Vector? position = null, QAngle? angles = null, Vector? velocity = null)
{
base.Teleport(position, angles, velocity);
}
public void ReplicateConVar(string conVar, string value)
{
NativeAPI.ReplicateConvar(Slot, conVar, value);
}
}