diff --git a/WeaponPaints.cs b/WeaponPaints.cs index d8ed921b..aeb2b656 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -19,7 +19,9 @@ public class WeaponPaints : BasePlugin, IPluginConfig MySqlDb? MySql = null; public DateTime[] commandCooldown = new DateTime[Server.MaxPlayers]; - private Dictionary> g_playersSkins = new Dictionary>(); + private Dictionary> gPlayerWeaponPaints = new Dictionary>(); + private Dictionary> gPlayerWeaponSeed = new Dictionary>(); + private Dictionary> gPlayerWeaponWear = new Dictionary>(); private static Dictionary knifeTypes = new Dictionary() { { "m9", "weapon_knife_m9_bayonet" }, @@ -92,28 +94,27 @@ public class WeaponPaints : BasePlugin, IPluginConfig } Server.NextFrame(() => { - if (!weapon.IsValid || !weapon.OwnerEntity.IsValid) return; - var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex((int)weapon.OwnerEntity.Value.EntityIndex!.Value.Value)); - if (!pawn.IsValid || !pawn.Controller.Value.IsValid) return; + if (!weapon.IsValid) return; + if (weapon.OwnerEntity.Value == null) return; + if (!weapon.OwnerEntity.Value.EntityIndex.HasValue) return; + int weaponOwner = (int)weapon.OwnerEntity.Value.EntityIndex.Value.Value; + var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex(weaponOwner)); + if (!pawn.IsValid) return; var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value; - CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); - if (player == null || !player.IsValid) return; + var player = Utilities.GetPlayerFromIndex(playerIndex); + if (player == null || !player.IsValid || player.IsBot) return; var steamId = new SteamID(player.SteamID); - if (g_playersSkins.TryGetValue(steamId.SteamId64, out var weaponIDs)) + if (!gPlayerWeaponPaints.ContainsKey(steamId.SteamId64)) return; + if (!gPlayerWeaponPaints[steamId.SteamId64].ContainsKey(weapon.AttributeManager.Item.ItemDefinitionIndex)) return; + weapon.AttributeManager.Item.ItemIDLow = unchecked((uint)-1); + weapon.AttributeManager.Item.ItemIDHigh = unchecked((uint)-1); + weapon.FallbackPaintKit = gPlayerWeaponPaints[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]; + weapon.FallbackSeed = gPlayerWeaponSeed[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]; + weapon.FallbackWear = gPlayerWeaponWear[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]; + if (!isKnife && weapon.CBodyComponent != null && weapon.CBodyComponent.SceneNode != null) { - if (weaponIDs.TryGetValue(weapon.AttributeManager.Item.ItemDefinitionIndex, out var weaponPaint)) - { - weapon.AttributeManager.Item.ItemIDLow = unchecked((uint)-1); - weapon.AttributeManager.Item.ItemIDHigh = unchecked((uint)-1); - weapon.FallbackPaintKit = weaponPaint; - weapon.FallbackSeed = 0; - weapon.FallbackWear = 0.0001f; - if (!isKnife && weapon.CBodyComponent != null && weapon.CBodyComponent.SceneNode != null) - { - var skeleton = GetSkeletonInstance(weapon.CBodyComponent.SceneNode); - skeleton.ModelState.MeshGroupMask = 2; - } - } + var skeleton = GetSkeletonInstance(weapon.CBodyComponent.SceneNode); + skeleton.ModelState.MeshGroupMask = 2; } }); } @@ -143,12 +144,12 @@ public class WeaponPaints : BasePlugin, IPluginConfig Func GetSkeletonInstance = VirtualFunction.Create(node.Handle, 8); return new CSkeletonInstance(GetSkeletonInstance(node.Handle)); } - private async Task GetWeaponPaintsFromDatabase(int playerSlot) + private async Task GetWeaponPaintsFromDatabase(int playerIndex) { try { - CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot); - if (player == null || !player.IsValid || player.IsBot) return; + CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); + if (player == null || !player.IsValid) return; var steamId = new SteamID(player.SteamID); MySqlQueryCondition conditions = new MySqlQueryCondition() @@ -158,15 +159,27 @@ public class WeaponPaints : BasePlugin, IPluginConfig result.ToList().ForEach(pair => { - int weaponId = result.Get(pair.Key, "weapon_defindex"); - int weaponPaint = result.Get(pair.Key, "weapon_paint_id"); + int WeaponDefIndex = result.Get(pair.Key, "weapon_defindex"); + int PaintId = result.Get(pair.Key, "weapon_paint_id"); + float Wear = result.Get(pair.Key, "weapon_wear"); + int Seed = result.Get(pair.Key, "weapon_seed"); - if (!g_playersSkins.ContainsKey(steamId.SteamId64)) + if (!gPlayerWeaponPaints.ContainsKey(steamId.SteamId64)) { - g_playersSkins[steamId.SteamId64] = new Dictionary(); + gPlayerWeaponPaints[steamId.SteamId64] = new Dictionary(); + } + if (!gPlayerWeaponWear.ContainsKey(steamId.SteamId64)) + { + gPlayerWeaponWear[steamId.SteamId64] = new Dictionary(); + } + if (!gPlayerWeaponSeed.ContainsKey(steamId.SteamId64)) + { + gPlayerWeaponSeed[steamId.SteamId64] = new Dictionary(); } - g_playersSkins[steamId.SteamId64][weaponId] = weaponPaint; + gPlayerWeaponPaints[steamId.SteamId64][WeaponDefIndex] = PaintId; + gPlayerWeaponWear[steamId.SteamId64][WeaponDefIndex] = Wear; + gPlayerWeaponSeed[steamId.SteamId64][WeaponDefIndex] = Seed; }); } catch (Exception) diff --git a/website/database.sql b/website/database.sql index 26283357..261e24e1 100644 --- a/website/database.sql +++ b/website/database.sql @@ -30,9 +30,21 @@ SET time_zone = "+00:00"; CREATE TABLE `wp_player_skins` ( `steamid` varchar(64) NOT NULL, `weapon_defindex` int(6) NOT NULL, - `weapon_paint_id` int(6) NOT NULL + `weapon_paint_id` int(6) NOT NULL, + `weapon_wear` float NOT NULL DEFAULT 0.0001, + `weapon_seed` int(16) NOT NULL DEFAULT 0 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; +-- +-- Table structure for table `wp_player_knife` +-- + +CREATE TABLE `wp_player_knife` ( + `steamid` VARCHAR NOT NULL, + `knife` VARCHAR NOT NULL, + UNIQUE (`steamid`) +) ENGINE = InnoDB; + -- -- Table structure for table `wp_weapons` --