diff --git a/Commands.cs b/Commands.cs index 4d28c92..a1169de 100644 --- a/Commands.cs +++ b/Commands.cs @@ -35,18 +35,19 @@ private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command) commandsCooldown[(int)player.UserId] = DateTime.UtcNow.AddSeconds(Config.CmdRefreshCooldownSeconds); if (weaponSync != null) + { Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo)); - if (Config.Additional.GloveEnabled && weaponSync != null) - Task.Run(async () => await weaponSync.GetGloveFromDatabase(playerInfo)); + if (Config.Additional.GloveEnabled) + Task.Run(async () => await weaponSync.GetGloveFromDatabase(playerInfo)); - if (Config.Additional.KnifeEnabled) - { - if (weaponSync != null) + if (Config.Additional.KnifeEnabled) Task.Run(async () => await weaponSync.GetKnifeFromDatabase(playerInfo)); RefreshWeapons(player); + RefreshGloves(player); } + if (!string.IsNullOrEmpty(Localizer["wp_command_refresh_done"])) { player!.Print(Localizer["wp_command_refresh_done"]); @@ -266,13 +267,6 @@ private void SetupSkinsMenu() if (g_bCommandsAllowed && (LifeState_t)p.LifeState == LifeState_t.LIFE_ALIVE) AddTimer(0.15f, () => RefreshWeapons(p), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); - - - if (!Config.GlobalShare) - { - if (weaponSync != null) - Task.Run(async () => await weaponSync.SyncWeaponPaintsToDatabase(playerInfo)); - } } }; @@ -366,9 +360,20 @@ private void SetupGlovesMenu() }; if (paint != 0) - g_playersGlove[playerIndex] = ((ushort)weaponDefindex, paint); + { + g_playersGlove[playerIndex] = (ushort)weaponDefindex; + + if (!gPlayerWeaponsInfo[(int)playerIndex].ContainsKey(weaponDefindex)) + { + WeaponInfo weaponInfo = new(); + weaponInfo.Paint = paint; + gPlayerWeaponsInfo[(int)playerIndex][weaponDefindex] = weaponInfo; + } + } else + { g_playersGlove.TryRemove(playerIndex, out _); + } if (!string.IsNullOrEmpty(Localizer["wp_glove_menu_select"])) { @@ -382,7 +387,7 @@ private void SetupGlovesMenu() if (weaponSync != null) { - Task.Run(async () => await weaponSync.SyncGloveToDatabase(playerInfo, (ushort)weaponDefindex, paint)); + Task.Run(async () => await weaponSync.SyncGloveToDatabase(playerInfo, (ushort)weaponDefindex)); } } }; diff --git a/Events.cs b/Events.cs index cccad03..debf73a 100644 --- a/Events.cs +++ b/Events.cs @@ -46,6 +46,17 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo if (player is null || !player.IsValid || !player.UserId.HasValue || player.IsBot || player.IsHLTV || player.SteamID.ToString().Length != 17) return HookResult.Continue; + PlayerInfo playerInfo = new PlayerInfo + { + UserId = player.UserId, + Index = (int)player.Index, + SteamId = player.SteamID.ToString(), + Name = player.PlayerName, + IpAddress = player.IpAddress?.Split(":")[0] + }; + + if (weaponSync != null) + Task.Run(async () => await weaponSync.SyncWeaponPaintsToDatabase(playerInfo)); if (Config.Additional.SkinEnabled) gPlayerWeaponsInfo.TryRemove((int)player.Index, out _); diff --git a/Utility.cs b/Utility.cs index fcf0927..3847d4c 100644 --- a/Utility.cs +++ b/Utility.cs @@ -58,7 +58,6 @@ internal static async Task CheckDatabaseTables() @"CREATE TABLE IF NOT EXISTS `wp_player_gloves` ( `steamid` varchar(64) NOT NULL, `weapon_defindex` int(11) NOT NULL, - `paint` int(11) NOT NULL, UNIQUE (`steamid`) ) ENGINE=InnoDB" }; diff --git a/VERSION b/VERSION index e5251fb..fb55740 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.8b \ No newline at end of file +1.8c \ No newline at end of file diff --git a/WeaponAction.cs b/WeaponAction.cs index 885fafc..1f5c84d 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -311,7 +311,7 @@ internal void RefreshWeapons(CCSPlayerController? player) }); } } - }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); + }, TimerFlags.STOP_ON_MAPCHANGE); } } @@ -351,6 +351,8 @@ internal void RefreshKnife(CCSPlayerController? player) private static void RefreshGloves(CCSPlayerController player) { + if (!Utility.IsPlayerValid(player) || (LifeState_t)player.LifeState != LifeState_t.LIFE_ALIVE) return; + CCSPlayerPawn? pawn = player.PlayerPawn.Value; if (pawn == null || !pawn.IsValid || pawn.LifeState != (byte)LifeState_t.LIFE_ALIVE) return; @@ -369,20 +371,22 @@ private static void RefreshGloves(CCSPlayerController player) if (!player.IsValid) return; - if (g_playersGlove.TryGetValue(player.Index, out var gloveInfo) && gloveInfo.Paint != 0) + if (g_playersGlove.TryGetValue(player.Index, out var gloveInfo) && gloveInfo != 0) { CCSPlayerPawn? pawn = player.PlayerPawn.Value; if (pawn == null || !pawn.IsValid || pawn.LifeState != (byte)LifeState_t.LIFE_ALIVE) return; + WeaponInfo weaponInfo = gPlayerWeaponsInfo[(int)player.Index][gloveInfo]; + CEconItemView item = pawn.EconGloves; - item.ItemDefinitionIndex = gloveInfo.Definition; + item.ItemDefinitionIndex = gloveInfo; item.ItemIDLow = 16384 & 0xFFFFFFFF; item.ItemIDHigh = 16384; - CAttributeList_SetOrAddAttributeValueByName.Invoke(item.NetworkedDynamicAttributes.Handle, "set item texture prefab", gloveInfo.Paint); - CAttributeList_SetOrAddAttributeValueByName.Invoke(item.NetworkedDynamicAttributes.Handle, "set item texture seed", 0); - CAttributeList_SetOrAddAttributeValueByName.Invoke(item.NetworkedDynamicAttributes.Handle, "set item texture wear", 0.00f); + CAttributeList_SetOrAddAttributeValueByName.Invoke(item.NetworkedDynamicAttributes.Handle, "set item texture prefab", weaponInfo.Paint); + CAttributeList_SetOrAddAttributeValueByName.Invoke(item.NetworkedDynamicAttributes.Handle, "set item texture seed", weaponInfo.Seed); + CAttributeList_SetOrAddAttributeValueByName.Invoke(item.NetworkedDynamicAttributes.Handle, "set item texture wear", weaponInfo.Wear); item.Initialized = true; diff --git a/WeaponInfo.cs b/WeaponInfo.cs index dc702bb..c1a97d5 100644 --- a/WeaponInfo.cs +++ b/WeaponInfo.cs @@ -3,7 +3,7 @@ public class WeaponInfo { public int Paint { get; set; } - public int Seed { get; set; } - public float Wear { get; set; } + public int Seed { get; set; } = 0; + public float Wear { get; set; } = 0f; } } \ No newline at end of file diff --git a/WeaponPaints.cs b/WeaponPaints.cs index 818c8e7..01b871e 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -79,7 +79,7 @@ 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_playersGlove = new ConcurrentDictionary(); internal static ConcurrentDictionary> gPlayerWeaponsInfo = new ConcurrentDictionary>(); internal static List skinsList = new List(); internal static List glovesList = new List(); @@ -158,7 +158,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig "Nereziel & daffyy"; public override string ModuleDescription => "Skin, gloves and knife selector, standalone and web-based"; public override string ModuleName => "WeaponPaints"; - public override string ModuleVersion => "1.8b"; + public override string ModuleVersion => "1.8c"; public static WeaponPaintsConfig GetWeaponPaintsConfig() { diff --git a/WeaponSynchronization.cs b/WeaponSynchronization.cs index 2e17320..13048b7 100644 --- a/WeaponSynchronization.cs +++ b/WeaponSynchronization.cs @@ -88,16 +88,16 @@ internal async Task GetGloveFromDatabase(PlayerInfo player) await using var connection = await _database.GetConnectionAsync(); // Construct the SQL query with specific columns for better performance - string query = "SELECT `weapon_defindex`, `paint` FROM `wp_player_gloves` WHERE `steamid` = @steamid"; + string query = "SELECT `weapon_defindex` FROM `wp_player_gloves` WHERE `steamid` = @steamid"; // Execute the query and retrieve glove data - var gloveData = await connection.QueryFirstOrDefaultAsync<(ushort Definition, int Paint)>(query, new { steamid = player.SteamId }); + ushort? gloveData = await connection.QueryFirstOrDefaultAsync(query, new { steamid = player.SteamId }); // Check if glove data is retrieved successfully - if (gloveData != default) + if (gloveData != null) { // Update g_playersGlove dictionary with glove data - WeaponPaints.g_playersGlove[(uint)player.Index] = gloveData; + WeaponPaints.g_playersGlove[(uint)player.Index] = gloveData.Value; } } catch (Exception e) @@ -212,15 +212,15 @@ internal async Task SyncKnifeToDatabase(PlayerInfo player, string knife) } } - internal async Task SyncGloveToDatabase(PlayerInfo player, ushort defindex, int paint) + internal async Task SyncGloveToDatabase(PlayerInfo player, ushort defindex) { if (!_config.Additional.GloveEnabled) return; try { await using var connection = await _database.GetConnectionAsync(); - string query = "INSERT INTO `wp_player_gloves` (`steamid`, `weapon_defindex`, `paint`) VALUES(@steamid, @weapon_defindex, @paint) ON DUPLICATE KEY UPDATE `weapon_defindex` = @weapon_defindex, `paint` = @paint"; - await connection.ExecuteAsync(query, new { steamid = player.SteamId, weapon_defindex = defindex, paint }); + string query = "INSERT INTO `wp_player_gloves` (`steamid`, `weapon_defindex`) VALUES(@steamid, @weapon_defindex, @paint) ON DUPLICATE KEY UPDATE `weapon_defindex` = @weapon_defindex"; + await connection.ExecuteAsync(query, new { steamid = player.SteamId, weapon_defindex = defindex }); } catch (Exception e) {