Skip to content

Commit 7b9df90

Browse files
authored
feat: improve getplayers & RemoveItemByDesignerName (#1044)
1 parent 31cedca commit 7b9df90

File tree

3 files changed

+81
-33
lines changed

3 files changed

+81
-33
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using CounterStrikeSharp.API.Modules.Entities;
2+
3+
namespace CounterStrikeSharp.API.Modules.Extensions;
4+
5+
public static class CBasePlayerWeaponExtensions
6+
{
7+
/// <summary>
8+
/// Gets the weapon's designer name (e.g., "weapon_ak47").
9+
/// </summary>
10+
/// <param name="weapon">The <see cref="CBasePlayerWeapon"/> instance.</param>
11+
/// <returns>The designer name of the weapon as a string, or <c>null</c> if it cannot be retrieved.</returns>
12+
public static string? GetWeaponName(this CBasePlayerWeapon weapon)
13+
{
14+
return weapon.GetVData<CCSWeaponBaseVData>()?.Name;
15+
}
16+
17+
/// <summary>
18+
/// Gets the econ owner of a weapon entity
19+
/// </summary>
20+
/// <param name="weapon">The weapon entity.</param>
21+
/// <returns>The <see cref="CCSPlayerController"/> instance for the player, or <c>null</c> if it doesn't exist.</returns>
22+
public static CCSPlayerController? GetEconOwner(this CBasePlayerWeapon weapon)
23+
{
24+
ulong originalXuid = weapon.OriginalOwnerXuidLow;
25+
SteamID? steamId = originalXuid > 0 ? new(originalXuid) : null;
26+
27+
CCSPlayerController? player = null;
28+
29+
if (steamId?.IsValid() == true)
30+
player = Utilities.GetPlayers().FirstOrDefault(p =>
31+
p.SteamID == steamId.SteamId64 || p.SteamID == originalXuid);
32+
33+
if (player == null)
34+
player = weapon.OwnerEntity.Get()?.As<CCSPlayerController>();
35+
36+
return player?.Connected == PlayerConnectedState.PlayerConnected ? player : null;
37+
}
38+
}

managed/CounterStrikeSharp.API/Server.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,17 @@ public static void PrintToChatAll(string message)
142142

143143
public static string GameDirectory => NativeAPI.GetGameDirectory();
144144

145-
public static int MaxPlayers => NativeAPI.GetMaxClients();
145+
private static int? _maxPlayers;
146+
147+
public static int MaxPlayers
148+
{
149+
get
150+
{
151+
_maxPlayers ??= NativeAPI.GetMaxClients();
152+
153+
return _maxPlayers.Value;
154+
}
155+
}
146156

147157
public static bool IsMapValid(string mapName) => NativeAPI.IsMapValid(mapName);
148158

managed/CounterStrikeSharp.API/Utilities.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
using CounterStrikeSharp.API.Modules.Memory;
1818
using CounterStrikeSharp.API.Modules.Utils;
19-
using System.Collections.Generic;
20-
using System.Linq;
2119
using System.Runtime.CompilerServices;
2220
using System.Runtime.InteropServices;
2321
using System.Text;
24-
using CounterStrikeSharp.API.Core.Logging;
2522
using CounterStrikeSharp.API.Modules.Commands.Targeting;
2623
using CounterStrikeSharp.API.Modules.Entities;
2724
using Microsoft.Extensions.Logging;
@@ -60,13 +57,13 @@ public static IEnumerable<T> FlagsToList<T>(this T flags) where T : Enum
6057

6158
public static CCSPlayerController? GetPlayerFromIndex(int index)
6259
{
63-
var player = GetEntityFromIndex<CCSPlayerController>(index);
64-
if (player == null || player.DesignerName != "cs_player_controller")
60+
var entityPtr = EntitySystem.GetEntityByIndex((uint)index);
61+
if (entityPtr is null || entityPtr == IntPtr.Zero)
6562
{
6663
return null;
6764
}
6865

69-
return player;
66+
return new CCSPlayerController(entityPtr.Value);
7067
}
7168

7269
public static CCSPlayerController? GetPlayerFromSlot(int slot)
@@ -81,47 +78,50 @@ public static IEnumerable<T> FlagsToList<T>(this T flags) where T : Enum
8178

8279
public static CCSPlayerController? GetPlayerFromSteamId(ulong steamId)
8380
{
84-
return Utilities.GetPlayers().FirstOrDefault(player => player.AuthorizedSteamID == (SteamID)steamId);
81+
return GetPlayers().FirstOrDefault(player => player.AuthorizedSteamID == (SteamID)steamId);
8582
}
8683

87-
public static TargetResult ProcessTargetString(string pattern, CCSPlayerController player)
84+
public static CCSPlayerController? GetPlayerFromSteamId64(ulong steamId)
8885
{
89-
return new Target(pattern).GetTarget(player);
86+
return GetPlayers().FirstOrDefault(player => player.SteamID == steamId);
9087
}
9188

92-
public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName)
89+
public static TargetResult ProcessTargetString(string pattern, CCSPlayerController player)
9390
{
94-
return RemoveItemByDesignerName(player, designerName, false);
91+
return new Target(pattern).GetTarget(player);
9592
}
9693

97-
public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName, bool shouldRemoveEntity)
94+
public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName)
9895
{
99-
CHandle<CBasePlayerWeapon>? item = null;
100-
if (player.PlayerPawn.Value == null || player.PlayerPawn.Value.WeaponServices == null) return false;
96+
var weapon = player.PlayerPawn.Value?.WeaponServices?.MyWeapons
97+
.Select(w => w.Value)
98+
.Where(w => w?.IsValid is true && w.DesignerName == designerName)
99+
.FirstOrDefault();
101100

102-
foreach (var weapon in player.PlayerPawn.Value.WeaponServices.MyWeapons)
103-
{
104-
if (weapon is not { IsValid: true, Value.IsValid: true })
105-
continue;
106-
if (weapon.Value.DesignerName != designerName)
107-
continue;
101+
if (weapon == null)
102+
return false;
108103

109-
item = weapon;
110-
}
104+
weapon.AddEntityIOEvent("Kill", weapon, delay: 0.1f);
105+
return true;
106+
}
111107

112-
if (item != null && item.Value != null)
113-
{
114-
player.PlayerPawn.Value.RemovePlayerItem(item.Value);
108+
public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName, bool _)
109+
{
110+
return RemoveItemByDesignerName(player, designerName);
111+
}
115112

116-
if (shouldRemoveEntity)
117-
{
118-
item.Value.Remove();
119-
}
113+
public static bool RemoveItemBySlot(this CCSPlayerController player, gear_slot_t slot)
114+
{
115+
var weapon = player.PlayerPawn.Value?.WeaponServices?.MyWeapons
116+
.Select(w => w.Value)
117+
.Where(w => w?.As<CCSWeaponBase>().VData?.GearSlot == slot)
118+
.FirstOrDefault();
120119

121-
return true;
122-
}
120+
if (weapon == null)
121+
return false;
123122

124-
return false;
123+
weapon.AddEntityIOEvent("Kill", weapon, delay: 0.1f);
124+
return true;
125125
}
126126

127127
public static IEnumerable<T> FindAllEntitiesByDesignerName<T>(string designerName) where T : CEntityInstance

0 commit comments

Comments
 (0)