diff --git a/Commands.cs b/Commands.cs index b3ad9f0b..d37914d0 100644 --- a/Commands.cs +++ b/Commands.cs @@ -471,60 +471,62 @@ private void SetupAgentsMenu() if (!Utility.IsPlayerValid(player) || player is null) return; string selectedPaintName = option.Text; + var selectedAgent = agentsList.FirstOrDefault(g => + g.ContainsKey("agent_name") && + g["agent_name"] != null && g["agent_name"]!.ToString() == selectedPaintName && + g["team"] != null && (int)(g["team"]!) == player.TeamNum); - var selectedAgent = agentsList.FirstOrDefault(g => g.ContainsKey("agent_name") && g["agent_name"]?.ToString() == selectedPaintName); - if (selectedAgent != null) + if (selectedAgent == null) return; + + if ( + selectedAgent != null && + selectedAgent.ContainsKey("model") + ) { - if ( - selectedAgent != null && - selectedAgent.ContainsKey("model") - ) + PlayerInfo playerInfo = new PlayerInfo { - PlayerInfo 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] - }; + UserId = player.UserId, + Slot = player.Slot, + Index = (int)player.Index, + SteamId = player.SteamID.ToString(), + Name = player.PlayerName, + IpAddress = player.IpAddress?.Split(":")[0] + }; - if (Config.Additional.ShowSkinImage) - { - string image = selectedAgent["image"]?.ToString() ?? ""; - PlayerWeaponImage[player.Slot] = image; - AddTimer(2.0f, () => PlayerWeaponImage.Remove(player.Slot), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); - } + if (Config.Additional.ShowSkinImage) + { + string image = selectedAgent["image"]?.ToString() ?? ""; + PlayerWeaponImage[player.Slot] = image; + AddTimer(2.0f, () => PlayerWeaponImage.Remove(player.Slot), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); + } - if (!string.IsNullOrEmpty(Localizer["wp_agent_menu_select"])) - { - player!.Print(Localizer["wp_agent_menu_select", selectedPaintName]); - } + if (!string.IsNullOrEmpty(Localizer["wp_agent_menu_select"])) + { + player!.Print(Localizer["wp_agent_menu_select", selectedPaintName]); + } - if (player.TeamNum == 3) - { - g_playersAgent.AddOrUpdate(player.Slot, - key => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), null), - (key, oldValue) => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), oldValue.T)); - } - else - { - g_playersAgent.AddOrUpdate(player.Slot, - key => (null, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString()), - (key, oldValue) => (oldValue.CT, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString()) - ); - } + if (player.TeamNum == 3) + { + g_playersAgent.AddOrUpdate(player.Slot, + key => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), null), + (key, oldValue) => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), oldValue.T)); + } + else + { + g_playersAgent.AddOrUpdate(player.Slot, + key => (null, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString()), + (key, oldValue) => (oldValue.CT, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString()) + ); + } - if (weaponSync != null) + if (weaponSync != null) + { + _ = Task.Run(async () => { - _ = Task.Run(async () => - { - await weaponSync.SyncAgentToDatabase(playerInfo); - }); - } - }; - } + await weaponSync.SyncAgentToDatabase(playerInfo); + }); + } + }; }; // Command to open the weapon selection menu for players diff --git a/Events.cs b/Events.cs index d270853f..e7477f28 100644 --- a/Events.cs +++ b/Events.cs @@ -160,9 +160,9 @@ private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon return; } - if (!gPlayerWeaponsInfo[player.Slot].ContainsKey(weaponDefIndex) || gPlayerWeaponsInfo[player.Slot][weaponDefIndex].Paint == 0) return; + if (!gPlayerWeaponsInfo[player.Slot].TryGetValue(weaponDefIndex, out WeaponInfo? value) || value.Paint == 0) return; - WeaponInfo weaponInfo = gPlayerWeaponsInfo[player.Slot][weaponDefIndex]; + WeaponInfo weaponInfo = value; //Log($"Apply on {weapon.DesignerName}({weapon.AttributeManager.Item.ItemDefinitionIndex}) paint {gPlayerWeaponPaints[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} seed {gPlayerWeaponSeed[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} wear {gPlayerWeaponWear[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]}"); weapon.AttributeManager.Item.ItemID = 16384; weapon.AttributeManager.Item.ItemIDLow = 16384 & 0xFFFFFFFF; @@ -257,7 +257,7 @@ public HookResult OnGiveNamedItemPost(DynamicHook hook) } */ - public void OnEntityCreated(CEntityInstance entity) + public void OnEntitySpawned(CEntityInstance entity) { var designerName = entity.DesignerName; @@ -326,7 +326,7 @@ private void RegisterListeners() RegisterEventHandler(OnPlayerSpawn); RegisterEventHandler(OnRoundStart, HookMode.Pre); RegisterEventHandler(OnRoundEnd); - RegisterListener(OnEntityCreated); + RegisterListener(OnEntitySpawned); RegisterListener(OnTick); //VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post); } diff --git a/VERSION b/VERSION index c3b75c88..2fcdc9e8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.3a \ No newline at end of file +2.3b \ No newline at end of file diff --git a/WeaponAction.cs b/WeaponAction.cs index 3b968b92..d6560cf5 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -99,12 +99,13 @@ internal void RefreshWeapons(CCSPlayerController? player) int clip1 = weapon.Value.Clip1; int reservedAmmo = weapon.Value.ReserveAmmo[0]; - if (!weaponsWithAmmo.ContainsKey(weaponByDefindex)) + if (!weaponsWithAmmo.TryGetValue(weaponByDefindex, out List<(int, int)>? value)) { - weaponsWithAmmo.Add(weaponByDefindex, new List<(int, int)>()); + value = new List<(int, int)>(); + weaponsWithAmmo.Add(weaponByDefindex, value); } - weaponsWithAmmo[weaponByDefindex].Add((clip1, reservedAmmo)); + value.Add((clip1, reservedAmmo)); if (gun == null || gun.VData == null) return; diff --git a/WeaponPaints.cs b/WeaponPaints.cs index 19bd3949..8da76f0f 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -10,7 +10,7 @@ namespace WeaponPaints; -[MinimumApiVersion(191)] +[MinimumApiVersion(195)] public partial class WeaponPaints : BasePlugin, IPluginConfig { internal static WeaponPaints Instance { get; private set; } = new(); @@ -75,23 +75,23 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig g_knifePickupCount = new Dictionary(); - internal static ConcurrentDictionary g_playersKnife = new ConcurrentDictionary(); - internal static ConcurrentDictionary g_playersGlove = new ConcurrentDictionary(); - internal static ConcurrentDictionary g_playersMusic = new ConcurrentDictionary(); - internal static ConcurrentDictionary g_playersAgent = new ConcurrentDictionary(); - internal static ConcurrentDictionary> gPlayerWeaponsInfo = new ConcurrentDictionary>(); - internal static List skinsList = new List(); - internal static List glovesList = new List(); - internal static List agentsList = new List(); - internal static List musicList = new List(); + internal static Dictionary g_knifePickupCount = new(); + internal static ConcurrentDictionary g_playersKnife = new(); + internal static ConcurrentDictionary g_playersGlove = new(); + internal static ConcurrentDictionary g_playersMusic = new(); + internal static ConcurrentDictionary g_playersAgent = new(); + internal static ConcurrentDictionary> gPlayerWeaponsInfo = new(); + internal static List skinsList = new(); + internal static List glovesList = new(); + internal static List agentsList = new(); + internal static List musicList = new(); internal static WeaponSynchronization? weaponSync; public static bool g_bCommandsAllowed = true; internal Dictionary PlayerWeaponImage = new(); - internal static Dictionary commandsCooldown = new Dictionary(); + internal static Dictionary commandsCooldown = new(); internal static Database? _database; internal static MemoryFunctionVoid CAttributeList_SetOrAddAttributeValueByName = new(GameData.GetSignature("CAttributeList_SetOrAddAttributeValueByName")); @@ -160,7 +160,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig "Nereziel & daffyy"; public override string ModuleDescription => "Skin, gloves, agents and knife selector, standalone and web-based"; public override string ModuleName => "WeaponPaints"; - public override string ModuleVersion => "2.3a"; + public override string ModuleVersion => "2.3b"; public static WeaponPaintsConfig GetWeaponPaintsConfig() { @@ -177,7 +177,10 @@ public override void Load(bool hotReload) foreach (var player in Utilities.GetPlayers()) { - if (weaponSync == null || player is null || !player.IsValid || player.SteamID.ToString().Length != 17 || player.IsBot || + if (weaponSync == null) + break; + + if (player is null || !player.IsValid || player.SteamID.ToString().Length != 17 || string.IsNullOrEmpty(player.IpAddress) || player.IsBot || player.IsHLTV || player.Connected != PlayerConnectedState.PlayerConnected) continue;