Skip to content

Commit d1ceab7

Browse files
committed
Adapted the plugin to use the player index instead of the controller
1 parent 2f0586a commit d1ceab7

File tree

5 files changed

+41
-66
lines changed

5 files changed

+41
-66
lines changed

Modules/Core.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ public override void Load(bool hotReload)
4040

4141
if (hotReload)
4242
{
43-
Utilities.GetPlayers().ForEach(player =>
44-
{
45-
AddPlayerToList(player, player.AuthorizedSteamID!);
46-
});
43+
Utilities.GetPlayers().ForEach(AddPlayerToList);
4744
}
4845
}
4946

Modules/Handlers/Listeners.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static void RegisterListeners()
1616
{
1717
Plugin.RegisterListener<CounterStrikeSharp.API.Core.Listeners.OnMapStart>(OnMapStart);
1818
Plugin.RegisterListener<CounterStrikeSharp.API.Core.Listeners.OnClientAuthorized>(OnClientAuthorized);
19-
Plugin.RegisterListener<CounterStrikeSharp.API.Core.Listeners.OnClientConnected>(OnClientConnected);
2019
Plugin.RegisterListener<CounterStrikeSharp.API.Core.Listeners.OnClientDisconnect>(OnClientDisconnect);
2120

2221
Plugin.AddCommandListener("say", OnSay);
@@ -26,28 +25,13 @@ public static void RegisterListeners()
2625
private static void OnMapStart(string map_name)
2726
{
2827
Players.Clear();
29-
Utilities.GetPlayers().ForEach(player =>
30-
{
31-
AddPlayerToList(player, player.AuthorizedSteamID!);
32-
});
28+
Utilities.GetPlayers().ForEach(AddPlayerToList);
3329
}
3430

35-
private static void OnClientAuthorized(int playerSlot, [CastFrom(typeof(ulong))] SteamID steamId)
31+
private static void OnClientAuthorized(int playerSlot, SteamID steamID)
3632
{
37-
if (steamId.SteamId2 == string.Empty)
38-
{
39-
ServerCommand($"kickid {playerSlot} \"SteamID2 not found.\"");
40-
return;
41-
}
42-
43-
var player = Utilities.GetPlayerFromSlot(playerSlot);
44-
AddPlayerToList(player, steamId);
45-
}
46-
47-
private static void OnClientConnected(int playerSlot)
48-
{
49-
var player = Utilities.GetPlayerFromSlot(playerSlot);
50-
AddPlayerToList(player, player.AuthorizedSteamID!);
33+
CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot);
34+
AddPlayerToList(player);
5135
}
5236

5337
private static void OnClientDisconnect(int playerSlot)

Modules/Models/Player.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1+
using CounterStrikeSharp.API;
12
using CounterStrikeSharp.API.Core;
23
using CounterStrikeSharp.API.Modules.Entities;
34
using CounterStrikeSharp.API.Modules.Utils;
45
using RetakesAllocator.Modules.Weapons;
56

67
using static RetakesAllocator.Modules.Core;
8+
using static RetakesAllocator.Modules.Utils;
79

810
namespace RetakesAllocator.Modules.Models;
911

1012
public class Player
1113
{
12-
public CCSPlayerController player;
13-
14-
private SteamID _steamId;
14+
public int playerIndex;
15+
public CCSPlayerController player => Utilities.GetPlayerFromIndex(playerIndex);
1516

1617
public readonly Allocator WeaponsAllocator;
1718

18-
public Player(CCSPlayerController player, SteamID steamId)
19+
public Player(CCSPlayerController player)
1920
{
20-
this.player = player;
21-
_steamId = steamId;
22-
WeaponsAllocator = new Allocator(player);
21+
playerIndex = (int)player.Index;
22+
WeaponsAllocator = new Allocator(this);
2323
}
2424

2525
public static void SetupPlayers(List<Player> players)
@@ -69,7 +69,7 @@ private CsTeam GetTeam()
6969

7070
public string GetSteamId2()
7171
{
72-
return _steamId.SteamId2;
72+
return player.AuthorizedSteamID!.SteamId2;
7373
}
7474

7575
public string GetName()

Modules/Utils.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using CounterStrikeSharp.API;
22
using CounterStrikeSharp.API.Core;
33
using CounterStrikeSharp.API.Modules.Commands;
4-
using CounterStrikeSharp.API.Modules.Entities;
54
using static RetakesAllocator.Modules.Core;
65
using static RetakesAllocator.Modules.Database;
76
using Player = RetakesAllocator.Modules.Models.Player;
@@ -37,27 +36,19 @@ public static void ReplyToCommand(CommandInfo commandInfo, string msg)
3736
commandInfo.ReplyToCommand(msg);
3837
}
3938

40-
public static Player FindPlayer(CCSPlayerController player)
39+
public static Player FindPlayer(CCSPlayerController cCSPlayerController)
4140
{
42-
foreach(var playerObj in Players)
43-
{
44-
if (playerObj.player == player)
45-
{
46-
return playerObj;
47-
}
48-
}
49-
50-
return null!;
41+
return Players.Find(player => player.playerIndex == cCSPlayerController.Index)!;
5142
}
5243

5344
public static void ServerCommand(string command, params object[] args)
5445
{
5546
Server.ExecuteCommand(string.Format(command, args));
5647
}
5748

58-
public static void AddPlayerToList(CCSPlayerController player, SteamID steamId)
49+
public static void AddPlayerToList(CCSPlayerController player)
5950
{
60-
if (player == null || !player.IsValid || player.IsBot || steamId is null)
51+
if (player == null || !player.IsValid || player.IsBot)
6152
{
6253
return;
6354
}
@@ -67,7 +58,7 @@ public static void AddPlayerToList(CCSPlayerController player, SteamID steamId)
6758
return;
6859
}
6960

70-
var playerObj = new Player(player, steamId);
61+
var playerObj = new Player(player);
7162

7263
Players.Add(playerObj);
7364

Modules/Weapons/Allocator.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CounterStrikeSharp.API.Core;
22
using CounterStrikeSharp.API.Modules.Utils;
33
using CounterStrikeSharp.API.Modules.Entities.Constants;
4+
using RetakesAllocator.Modules.Models;
45

56
namespace RetakesAllocator.Modules.Weapons;
67

@@ -55,7 +56,9 @@ public enum WeaponType
5556
new("weapon_deagle", "Desert Eagle")
5657
};
5758

58-
private readonly CCSPlayerController _player;
59+
private readonly Player _player;
60+
61+
private CCSPlayerController cCSPlayerController => _player.player;
5962

6063
public int PrimaryWeaponT = 0;
6164
public int PrimaryWeaponCt = 0;
@@ -64,7 +67,7 @@ public enum WeaponType
6467
public GiveAwp GiveAwp = GiveAwp.Never;
6568
public bool ShouldGiveAwp = false;
6669

67-
public Allocator(CCSPlayerController player)
70+
public Allocator(Player player)
6871
{
6972
_player = player;
7073
}
@@ -98,17 +101,17 @@ public bool SetupGiveAwp()
98101

99102
public void Allocate()
100103
{
101-
if (_player == null || !_player.IsValid)
104+
if (_player == null || cCSPlayerController == null || !_player.player.IsValid)
102105
{
103106
return;
104107
}
105108

106-
if (!_player.PawnIsAlive)
109+
if (!cCSPlayerController.PawnIsAlive)
107110
{
108111
return;
109112
}
110113

111-
if ((CsTeam)_player.TeamNum < CsTeam.Terrorist || (CsTeam)_player.TeamNum > CsTeam.CounterTerrorist)
114+
if ((CsTeam)cCSPlayerController.TeamNum < CsTeam.Terrorist || (CsTeam)cCSPlayerController.TeamNum > CsTeam.CounterTerrorist)
112115
{
113116
return;
114117
}
@@ -120,7 +123,7 @@ public void Allocate()
120123
}
121124
else
122125
{
123-
if ((CsTeam)_player.TeamNum == CsTeam.Terrorist)
126+
if ((CsTeam)cCSPlayerController.TeamNum == CsTeam.Terrorist)
124127
{
125128
primary = PrimaryT[PrimaryWeaponT].Item;
126129
}
@@ -132,14 +135,14 @@ public void Allocate()
132135

133136
string secondary = Pistols[SecondaryWeapon].Item;
134137

135-
_player.GiveNamedItem(primary);
136-
_player.GiveNamedItem(secondary);
137-
_player.GiveNamedItem(CsItem.Knife);
138+
cCSPlayerController.GiveNamedItem(primary);
139+
cCSPlayerController.GiveNamedItem(secondary);
140+
cCSPlayerController.GiveNamedItem(CsItem.Knife);
138141

139142
CsItem grenade = SelectGrenade();
140-
_player.GiveNamedItem(grenade);
143+
cCSPlayerController.GiveNamedItem(grenade);
141144

142-
if (_player.TeamNum == (byte)CsTeam.CounterTerrorist)
145+
if (cCSPlayerController.TeamNum == (byte)CsTeam.CounterTerrorist)
143146
{
144147
GiveCtEquipment();
145148
}
@@ -166,7 +169,7 @@ private CsItem SelectGrenade()
166169
grenade = CsItem.SmokeGrenade;
167170
break;
168171
case 3:
169-
grenade = (CsTeam)_player.TeamNum == CsTeam.Terrorist ? CsItem.Molotov : CsItem.Incendiary;
172+
grenade = (CsTeam)cCSPlayerController.TeamNum == CsTeam.Terrorist ? CsItem.Molotov : CsItem.Incendiary;
170173
break;
171174
}
172175

@@ -175,16 +178,16 @@ private CsItem SelectGrenade()
175178

176179
private void GiveCtEquipment()
177180
{
178-
_player.GiveNamedItem(CsItem.KevlarHelmet);
181+
cCSPlayerController.GiveNamedItem(CsItem.KevlarHelmet);
179182

180183
if (
181-
(CsTeam)_player.TeamNum == CsTeam.CounterTerrorist
182-
&& _player.PlayerPawn.IsValid
183-
&& _player.PlayerPawn.Value != null
184-
&& _player.PlayerPawn.Value.IsValid
185-
&& _player.PlayerPawn.Value.ItemServices != null
184+
(CsTeam)cCSPlayerController.TeamNum == CsTeam.CounterTerrorist
185+
&& cCSPlayerController.PlayerPawn.IsValid
186+
&& cCSPlayerController.PlayerPawn.Value != null
187+
&& cCSPlayerController.PlayerPawn.Value.IsValid
188+
&& cCSPlayerController.PlayerPawn.Value.ItemServices != null
186189
) {
187-
var itemServices = new CCSPlayer_ItemServices(_player.PlayerPawn.Value.ItemServices.Handle)
190+
var itemServices = new CCSPlayer_ItemServices(cCSPlayerController.PlayerPawn.Value.ItemServices.Handle)
188191
{
189192
HasDefuser = true
190193
};
@@ -193,6 +196,6 @@ private void GiveCtEquipment()
193196

194197
private void GiveArmor()
195198
{
196-
_player.GiveNamedItem(CsItem.KevlarHelmet);
199+
cCSPlayerController.GiveNamedItem(CsItem.KevlarHelmet);
197200
}
198201
}

0 commit comments

Comments
 (0)