From be51e4d1e9a527140e7fe6603663371d9c9e1ec3 Mon Sep 17 00:00:00 2001 From: daffyyyy Date: Thu, 30 Nov 2023 02:20:16 +0100 Subject: [PATCH] Improved player index The latest version of cssharp adds player.Index instead of entityIndex and does not allow to compile --- Commands.cs | 22 ++++++++--------- Events.cs | 33 +++++++++++++------------ Utility.cs | 2 +- WeaponAction.cs | 52 ++++++++++++++++++++-------------------- WeaponPaints.cs | 2 +- WeaponPaints.csproj | 9 +------ WeaponSynchronization.cs | 4 ++-- 7 files changed, 60 insertions(+), 64 deletions(-) diff --git a/Commands.cs b/Commands.cs index 0a9a4059..dce10b93 100644 --- a/Commands.cs +++ b/Commands.cs @@ -23,7 +23,7 @@ private void RegisterCommands() { AddCommand($"css_{Config.Additional.CommandKill}", "kill yourself", (player, info) => { - if (!Utility.IsPlayerValid(player) || !player!.PlayerPawn.IsValid) return; + if (player == null || !Utility.IsPlayerValid(player) || player.PlayerPawn.Value == null || !player!.PlayerPawn.IsValid) return; player.PlayerPawn.Value.CommitSuicide(true, false); }); @@ -60,11 +60,11 @@ private void SetupKnifeMenu() player!.PrintToChat(Utility.ReplaceTags(temp)); } - g_playersKnife[(int)player!.EntityIndex!.Value.Value] = knifeKey; + g_playersKnife[(int)player!.Index] = knifeKey; if (player!.PawnIsAlive && g_bCommandsAllowed) { - g_changedKnife.Add((int)player.EntityIndex!.Value.Value); + //g_changedKnife.Add((int)player.Index); RefreshWeapons(player); //RefreshPlayerKnife(player); @@ -73,7 +73,7 @@ private void SetupKnifeMenu() */ } if (weaponSync != null) - Task.Run(() => weaponSync.SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knifeKey)); + Task.Run(() => weaponSync.SyncKnifeToDatabase((int)player.Index, knifeKey)); } } }; @@ -84,7 +84,7 @@ private void SetupKnifeMenu() AddCommand($"css_{Config.Additional.CommandKnife}", "Knife Menu", (player, info) => { if (!Utility.IsPlayerValid(player) || !g_bCommandsAllowed) return; - int playerIndex = (int)player!.EntityIndex!.Value.Value; + int playerIndex = (int)player!.Index; if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds) && playerIndex > 0 && playerIndex < commandCooldown.Length) { @@ -110,7 +110,7 @@ private void SetupSkinsMenu() { if (!Utility.IsPlayerValid(player)) return; - int playerIndex = (int)player!.EntityIndex!.Value.Value; + int playerIndex = (int)player!.Index; string selectedWeapon = option.Text; if (classNamesByWeapon.TryGetValue(selectedWeapon, out string? selectedWeaponClassname)) { @@ -126,9 +126,9 @@ private void SetupSkinsMenu() // Function to handle skin selection for the chosen weapon var handleSkinSelection = (CCSPlayerController? p, ChatMenuOption opt) => { - if (p == null || !p.IsValid || !p.EntityIndex.HasValue) return; + if (p == null || !p.IsValid || p.Index <= 0) return; - playerIndex = (int)p.EntityIndex.Value.Value; + playerIndex = (int)p.Index; var steamId = new SteamID(p.SteamID); var firstSkin = skinsList?.FirstOrDefault(skin => @@ -204,7 +204,7 @@ private void SetupSkinsMenu() AddCommand($"css_{Config.Additional.CommandSkinSelection}", "Skins selection menu", (player, info) => { if (!Utility.IsPlayerValid(player)) return; - int playerIndex = (int)player!.EntityIndex!.Value.Value; + int playerIndex = (int)player!.Index; if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds) && playerIndex > 0 && playerIndex < commandCooldown.Length) { @@ -226,8 +226,8 @@ private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command) if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled || !g_bCommandsAllowed) return; if (!Utility.IsPlayerValid(player)) return; string temp = ""; - if (!player!.EntityIndex.HasValue) return; - int playerIndex = (int)player!.EntityIndex!.Value.Value; + if (player == null || player.Index <= 0) return; + int playerIndex = (int)player!.Index; if (playerIndex != 0 && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds)) { commandCooldown[playerIndex] = DateTime.UtcNow; diff --git a/Events.cs b/Events.cs index 9336ef05..49d73aa8 100644 --- a/Events.cs +++ b/Events.cs @@ -85,9 +85,9 @@ private void OnClientDisconnect(int playerSlot) if (player == null || !player.IsValid || player.IsHLTV) return; if (Config.Additional.KnifeEnabled) - g_playersKnife.Remove((int)player.EntityIndex!.Value.Value); + g_playersKnife.Remove((int)player.Index); if (Config.Additional.SkinEnabled) - gPlayerWeaponsInfo.Remove((int)player.EntityIndex!.Value.Value); + gPlayerWeaponsInfo.Remove((int)player.Index); } private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) @@ -100,7 +100,7 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) if (Config.Additional.KnifeEnabled) { - g_knifePickupCount[(int)player.EntityIndex!.Value.Value] = 0; + g_knifePickupCount[(int)player.Index] = 0; if (!PlayerHasKnife(player)) GiveKnifeToPlayer(player); } @@ -134,13 +134,13 @@ private HookResult OnItemPickup(EventItemPickup @event, GameEventInfo info) if (@event.Defindex == 42 || @event.Defindex == 59) { CCSPlayerController? player = @event.Userid; - if (!Utility.IsPlayerValid(player) || !player.PawnIsAlive || g_knifePickupCount[(int)player.EntityIndex!.Value.Value] >= 2) return HookResult.Continue; + if (!Utility.IsPlayerValid(player) || !player.PawnIsAlive || g_knifePickupCount[(int)player.Index] >= 2) return HookResult.Continue; - if (g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value) + if (g_playersKnife.ContainsKey((int)player.Index) && - g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife") + g_playersKnife[(int)player.Index] != "weapon_knife") { - g_knifePickupCount[(int)player.EntityIndex!.Value.Value]++; + g_knifePickupCount[(int)player.Index]++; RemovePlayerKnife(player, true); AddTimer(0.3f, ()=> GiveKnifeToPlayer(player)); @@ -176,29 +176,32 @@ private void OnEntitySpawned(CEntityInstance entity) { try { + if (!weapon.IsValid) return; if (weapon.OwnerEntity.Value == null) return; - if (!weapon.OwnerEntity.Value.EntityIndex.HasValue) + /* + if (weapon.OwnerEntity.Index > 0) { for (int i = 1; i <= Server.MaxPlayers; i++) { CCSPlayerController? ghostPlayer = Utilities.GetPlayerFromIndex(i); if (!Utility.IsPlayerValid(ghostPlayer)) continue; - if (g_changedKnife.Contains((int)ghostPlayer.EntityIndex!.Value.Value)) + if (g_changedKnife.Contains((int)ghostPlayer.Index)) { ChangeWeaponAttributes(weapon, ghostPlayer, isKnife); - g_changedKnife.Remove((int)ghostPlayer.EntityIndex!.Value.Value); + g_changedKnife.Remove((int)ghostPlayer.Index); break; } } - return; + } - - if (!weapon.OwnerEntity.Value.EntityIndex.HasValue) return; - int weaponOwner = (int)weapon.OwnerEntity.Value.EntityIndex.Value.Value; + */ + if (weapon.OwnerEntity.Index <= 0) return; + int weaponOwner = (int)weapon.OwnerEntity.Index; var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex(weaponOwner)); if (!pawn.IsValid) return; - var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value; + + var playerIndex = (int)pawn.Controller.Index; var player = Utilities.GetPlayerFromIndex(playerIndex); if (!Utility.IsPlayerValid(player)) return; diff --git a/Utility.cs b/Utility.cs index 3a824140..07572b66 100644 --- a/Utility.cs +++ b/Utility.cs @@ -19,7 +19,7 @@ internal static bool IsPlayerValid(CCSPlayerController? player) internal static string BuildDatabaseConnectionString() { - if (Config == null) return String.Empty; + if (Config == null) return string.Empty; var builder = new MySqlConnectionStringBuilder { Server = Config.DatabaseHost, diff --git a/WeaponAction.cs b/WeaponAction.cs index 192cb9d4..4070e78e 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -9,9 +9,9 @@ public partial class WeaponPaints { internal static void ChangeWeaponAttributes(CBasePlayerWeapon? weapon, CCSPlayerController? player, bool isKnife = false) { - if (weapon == null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return; + if (player == null || weapon == null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return; - int playerIndex = (int)player!.EntityIndex!.Value.Value; + int playerIndex = (int)player.Index; if (!gPlayerWeaponsInfo.ContainsKey(playerIndex)) return; @@ -55,7 +55,7 @@ internal static void ChangeWeaponAttributes(CBasePlayerWeapon? weapon, CCSPlayer internal static void GiveKnifeToPlayer(CCSPlayerController? player) { if (!_config.Additional.KnifeEnabled || player == null || !player.IsValid) return; - if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife)) + if (g_playersKnife.TryGetValue((int)player.Index, out var knife)) { player.GiveNamedItem(knife); } @@ -77,7 +77,7 @@ internal static void GiveKnifeToPlayer(CCSPlayerController? player) } internal void RemovePlayerKnife(CCSPlayerController? player, bool force = false) { - if (player == null || !player.IsValid || !player.PawnIsAlive) return; + if (player == null || !player.IsValid || player.PlayerPawn.Value == null || !player.PawnIsAlive) return; if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) return; var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; @@ -88,16 +88,16 @@ internal void RemovePlayerKnife(CCSPlayerController? player, bool force = false) foreach (var weapon in weapons) { - if (weapon != null && weapon.IsValid && weapon.Value.IsValid) + if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid) { //if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59) if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) { if (!force) { - if (!weapon.Value.EntityIndex.HasValue) return; - int weaponEntityIndex = (int)weapon.Value.EntityIndex!.Value.Value; - NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"); + if ((int)weapon.Index <= 0) return; + int weaponEntityIndex = (int)weapon.Index; + NativeAPI.IssueClientCommand((int)player.Index - 1, "slot3"); AddTimer(0.35f, () => service.DropActivePlayerWeapon(weapon.Value)); AddTimer(1.0f, () => @@ -123,35 +123,35 @@ internal void RemovePlayerKnife(CCSPlayerController? player, bool force = false) internal void RefreshPlayerKnife(CCSPlayerController? player) { - if (player == null || !player.IsValid || !player.PawnIsAlive) return; + if (player == null || !player.IsValid || player.PlayerPawn.Value == null || !player.PawnIsAlive) return; if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) return; var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; if (weapons != null && weapons.Count > 0) { - CCSPlayer_ItemServices service = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle); + CCSPlayer_ItemServices service = new(player.PlayerPawn.Value.ItemServices.Handle); //var dropWeapon = VirtualFunction.CreateVoid(service.Handle, GameData.GetOffset("CCSPlayer_ItemServices_DropActivePlayerWeapon")); foreach (var weapon in weapons) { - if (weapon != null && weapon.IsValid && weapon.Value.IsValid) + if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid) { //if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59) if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) { - if (!weapon.Value.EntityIndex.HasValue) return; - int weaponEntityIndex = (int)weapon.Value.EntityIndex!.Value.Value; - NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"); + if (weapon.Index <= 0) return; + int weaponEntityIndex = (int)weapon.Index; + NativeAPI.IssueClientCommand((int)player.Index - 1, "slot3"); AddTimer(0.22f, () => { - if (player.PlayerPawn.Value.WeaponServices.ActiveWeapon.Value.DesignerName.Contains("knife") + if (player.PlayerPawn.Value.WeaponServices.ActiveWeapon.Value!.DesignerName.Contains("knife") || - player.PlayerPawn.Value.WeaponServices.ActiveWeapon.Value.DesignerName.Contains("bayonet") + player.PlayerPawn.Value.WeaponServices.ActiveWeapon.Value!.DesignerName.Contains("bayonet") ) { if (player.PawnIsAlive) { - NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"); + NativeAPI.IssueClientCommand((int)player.Index - 1, "slot3"); service.DropActivePlayerWeapon(weapon.Value); GiveKnifeToPlayer(player); } @@ -164,7 +164,7 @@ internal void RefreshPlayerKnife(CCSPlayerController? player) { CEntityInstance? knife = Utilities.GetEntityFromIndex(weaponEntityIndex); - if (knife != null && knife.IsValid && knife.Handle != -1 && knife.EntityIndex.HasValue) + if (knife != null && knife.IsValid && knife.Handle != -1 && knife.Index > 0) { knife.Remove(); } @@ -183,14 +183,14 @@ internal void RefreshSkins(CCSPlayerController? player) { if (!Utility.IsPlayerValid(player) || !player!.PawnIsAlive) return; - AddTimer(0.18f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3")); - AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2")); - AddTimer(0.38f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot1")); + AddTimer(0.18f, () => NativeAPI.IssueClientCommand((int)player.Index - 1, "slot3")); + AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.Index - 1, "slot2")); + AddTimer(0.38f, () => NativeAPI.IssueClientCommand((int)player.Index - 1, "slot1")); } internal void RefreshWeapons(CCSPlayerController? player) { - if (player == null || !player.IsValid || !player.PawnIsAlive) return; + if (player == null || !player.IsValid || player.PlayerPawn.Value == null || !player.PawnIsAlive) return; if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) return; var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; @@ -201,9 +201,9 @@ internal void RefreshWeapons(CCSPlayerController? player) foreach (var weapon in weapons) { - if (weapon != null && weapon.IsValid && weapon.Value.IsValid) + if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid) { - if (!weapon.Value.EntityIndex.HasValue || !weapon.Value.DesignerName.Contains("weapon_")) continue; + if (weapon.Index <= 0 || !weapon.Value.DesignerName.Contains("weapon_")) continue; //if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59) try { @@ -249,14 +249,14 @@ internal static bool PlayerHasKnife(CCSPlayerController? player) return false; } - if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) + if (player.PlayerPawn.Value == null || player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) return false; var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; if (weapons == null || weapons.Count <= 0) return false; foreach (var weapon in weapons) { - if (weapon != null && weapon.IsValid && weapon.Value.IsValid) + if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid) { if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) { diff --git a/WeaponPaints.cs b/WeaponPaints.cs index 0f4cd368..de83e1d1 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -31,7 +31,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig> gPlayerWeaponsInfo = new Dictionary>(); internal static Dictionary g_knifePickupCount = new Dictionary(); internal static Dictionary g_playersKnife = new(); - internal static List g_changedKnife = new(); + //internal static List g_changedKnife = new(); internal bool g_bCommandsAllowed = true; internal static List skinsList = new List(); diff --git a/WeaponPaints.csproj b/WeaponPaints.csproj index ee25d9f5..f65ee2fa 100644 --- a/WeaponPaints.csproj +++ b/WeaponPaints.csproj @@ -8,16 +8,9 @@ - + - - - - deps\CounterStrikeSharp.API.dll - - - diff --git a/WeaponSynchronization.cs b/WeaponSynchronization.cs index d919d9c6..e45ffd07 100644 --- a/WeaponSynchronization.cs +++ b/WeaponSynchronization.cs @@ -229,9 +229,9 @@ internal async Task GetWeaponPaintsFromDatabase(int playerIndex) internal async Task SyncWeaponPaintsToDatabase(CCSPlayerController? player) { - if (!Utility.IsPlayerValid(player)) return; + if (player == null || !Utility.IsPlayerValid(player)) return; - int playerIndex = (int)player!.EntityIndex!.Value.Value; + int playerIndex = (int)player.Index; string steamId = new SteamID(player.SteamID).SteamId64.ToString(); using var connection = new MySqlConnection(_databaseConnectionString);