From aae6f7e260547a85f785f27887ffdf520a18f5e0 Mon Sep 17 00:00:00 2001 From: Dawid Bepierszcz <41084667+daffyyyy@users.noreply.github.com> Date: Sat, 10 Feb 2024 12:23:56 +0100 Subject: [PATCH] 1.6b - Minor changes - Fixed tables creation --- Commands.cs | 87 +++++++++++----------- Config.cs | 4 +- Events.cs | 5 +- PlayerInfo.cs | 2 +- Utility.cs | 42 ++++++++--- WeaponAction.cs | 157 +++++++++++++++++++++++++-------------- WeaponPaints.cs | 6 +- WeaponSynchronization.cs | 2 +- lang/lv.json | 2 +- lang/pt-BR.json | 2 +- lang/pt-PT.json | 2 +- lang/tr.json | 2 +- lang/zh-cn.json | 2 +- website/style.css | 14 ++-- 14 files changed, 200 insertions(+), 129 deletions(-) diff --git a/Commands.cs b/Commands.cs index a4cafc52..dc407144 100644 --- a/Commands.cs +++ b/Commands.cs @@ -19,7 +19,7 @@ namespace WeaponPaints { UserId = player.UserId, Index = (int)player.Index, - SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(), + SteamId = player?.SteamID.ToString(), Name = player?.PlayerName, IpAddress = player?.IpAddress?.Split(":")[0] }; @@ -107,44 +107,39 @@ namespace WeaponPaints .ToDictionary(pair => pair.Key, pair => pair.Value); var giveItemMenu = new ChatMenu(Localizer["wp_knife_menu_title"]); - var handleGive = (CCSPlayerController? player, ChatMenuOption option) => + var handleGive = (CCSPlayerController player, ChatMenuOption option) => { - if (Utility.IsPlayerValid(player)) + if (!Utility.IsPlayerValid(player)) return; + + var knifeName = option.Text; + var knifeKey = knivesOnly.FirstOrDefault(x => x.Value == knifeName).Key; + if (!string.IsNullOrEmpty(knifeKey)) { - if (player == null) return; - var knifeName = option.Text; - var knifeKey = knivesOnly.FirstOrDefault(x => x.Value == knifeName).Key; - if (!string.IsNullOrEmpty(knifeKey)) + if (!string.IsNullOrEmpty(Localizer["wp_knife_menu_select"])) { - if (!string.IsNullOrEmpty(Localizer["wp_knife_menu_select"])) - { - player!.Print(Localizer["wp_knife_menu_select", knifeName]); - } - - if (!string.IsNullOrEmpty(Localizer["wp_knife_menu_kill"]) && Config.Additional.CommandKillEnabled) - { - player!.Print(Localizer["wp_knife_menu_kill"]); - } - - PlayerInfo playerInfo = new PlayerInfo - { - UserId = player.UserId, - Index = (int)player.Index, - SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(), - Name = player?.PlayerName, - IpAddress = player?.IpAddress?.Split(":")[0] - }; - - g_playersKnife[(int)player!.Index] = knifeKey; - - if (player!.PawnIsAlive && g_bCommandsAllowed) - { - RefreshWeapons(player); - } - - if (weaponSync != null) - Task.Run(async () => await weaponSync.SyncKnifeToDatabase(playerInfo, knifeKey)); + player!.Print(Localizer["wp_knife_menu_select", knifeName]); } + + if (!string.IsNullOrEmpty(Localizer["wp_knife_menu_kill"]) && Config.Additional.CommandKillEnabled) + { + player!.Print(Localizer["wp_knife_menu_kill"]); + } + + PlayerInfo playerInfo = new PlayerInfo + { + UserId = player.UserId, + Index = (int)player.Index, + SteamId = player.SteamID.ToString(), + Name = player.PlayerName, + IpAddress = player.IpAddress?.Split(":")[0] + }; + + g_playersKnife[(int)player!.Index] = knifeKey; + + if (g_bCommandsAllowed && (LifeState_t)player.LifeState == LifeState_t.LIFE_ALIVE) + RefreshKnife(player); + + _ = weaponSync?.SyncKnifeToDatabase(playerInfo, knifeKey) ?? Task.CompletedTask; } }; foreach (var knifePair in knivesOnly) @@ -195,15 +190,13 @@ namespace WeaponPaints var skinSubMenu = new ChatMenu(Localizer["wp_skin_menu_skin_title", selectedWeapon]); // Function to handle skin selection for the chosen weapon - var handleSkinSelection = (CCSPlayerController? p, ChatMenuOption opt) => + var handleSkinSelection = (CCSPlayerController p, ChatMenuOption opt) => { - if (p == null || !p.IsValid || p.Index <= 0) return; + if (!Utility.IsPlayerValid(p)) return; playerIndex = (int)p.Index; - if (p.AuthorizedSteamID == null) return; - - string steamId = p.AuthorizedSteamID.SteamId64.ToString(); + string steamId = p.SteamID.ToString(); var firstSkin = skinsList?.FirstOrDefault(skin => { if (skin != null && skin.TryGetValue("weapon_name", out var weaponName)) @@ -234,13 +227,17 @@ namespace WeaponPaints PlayerInfo playerInfo = new PlayerInfo { - UserId = player.UserId, - Index = (int)player.Index, - SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(), - Name = player?.PlayerName, - IpAddress = player?.IpAddress?.Split(":")[0] + UserId = p.UserId, + Index = (int)p.Index, + SteamId = p.SteamID.ToString(), + Name = p.PlayerName, + IpAddress = p.IpAddress?.Split(":")[0] }; + if (g_bCommandsAllowed && (LifeState_t)p.LifeState == LifeState_t.LIFE_ALIVE) + RefreshWeapons(p); + + if (!Config.GlobalShare) { if (weaponSync != null) diff --git a/Config.cs b/Config.cs index 2795400c..7f5105a4 100644 --- a/Config.cs +++ b/Config.cs @@ -40,6 +40,7 @@ namespace WeaponPaints [JsonPropertyName("GiveRandomSkin")] public bool GiveRandomSkin { get; set; } = false; + [JsonPropertyName("GiveKnifeAfterRemove")] public bool GiveKnifeAfterRemove { get; set; } = false; } @@ -78,5 +79,4 @@ namespace WeaponPaints [JsonPropertyName("Additional")] public Additional Additional { get; set; } = new Additional(); } - -} +} \ No newline at end of file diff --git a/Events.cs b/Events.cs index 511ec757..846aedf7 100644 --- a/Events.cs +++ b/Events.cs @@ -74,7 +74,8 @@ namespace WeaponPaints if (weapon.OwnerEntity.Value == null) return; if (weapon.OwnerEntity.Index <= 0) return; int weaponOwner = (int)weapon.OwnerEntity.Index; - var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex(weaponOwner)); + CBasePlayerPawn? pawn = Utilities.GetEntityFromIndex((int)weaponOwner); + //var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex(weaponOwner)); if (!pawn.IsValid) return; var playerIndex = (int)pawn.Controller.Index; @@ -110,7 +111,6 @@ namespace WeaponPaints if (player == null || !player.IsValid || !g_knifePickupCount.ContainsKey((int)player.Index) || player.IsBot || !g_playersKnife.ContainsKey((int)player.Index)) return HookResult.Continue; - if (g_knifePickupCount[(int)player.Index] >= 2) return HookResult.Continue; if (g_playersKnife.ContainsKey((int)player.Index) @@ -345,7 +345,6 @@ namespace WeaponPaints HookEntityOutput("weapon_knife", "OnPlayerPickup", OnPickup, HookMode.Pre); } - /* WORKAROUND FOR CLIENTS WITHOUT STEAMID ON AUTHORIZATION */ /*private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info) { diff --git a/PlayerInfo.cs b/PlayerInfo.cs index dba103b6..a191c5ca 100644 --- a/PlayerInfo.cs +++ b/PlayerInfo.cs @@ -8,4 +8,4 @@ public string? Name { get; set; } public string? IpAddress { get; set; } } -} +} \ No newline at end of file diff --git a/Utility.cs b/Utility.cs index 64afed4a..49a83449 100644 --- a/Utility.cs +++ b/Utility.cs @@ -29,22 +29,38 @@ namespace WeaponPaints return builder.ConnectionString; } - internal static async void CheckDatabaseTables() + internal static async Task CheckDatabaseTables() { + if (WeaponPaints._database is null) return; + try { - using var connection = new MySqlConnection(BuildDatabaseConnectionString()); - await connection.OpenAsync(); + await using var connection = await WeaponPaints._database.GetConnectionAsync(); - using var transaction = await connection.BeginTransactionAsync(); + await using var transaction = await connection.BeginTransactionAsync(); try { - string createTable1 = "CREATE TABLE IF NOT EXISTS `wp_player_skins` (`steamid` varchar(64) NOT NULL, `weapon_defindex` int(6) NOT NULL, `weapon_paint_id` int(6) NOT NULL, `weapon_wear` float NOT NULL DEFAULT 0.000001, `weapon_seed` int(16) NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci"; - string createTable2 = "CREATE TABLE IF NOT EXISTS `wp_player_knife` (`steamid` varchar(64) NOT NULL, `knife` varchar(64) NOT NULL, UNIQUE (`steamid`)) ENGINE = InnoDB"; + string[] createTableQueries = new[] + { + @"CREATE TABLE IF NOT EXISTS `wp_player_skins` ( + `steamid` varchar(64) NOT NULL, + `weapon_defindex` int(6) NOT NULL, + `weapon_paint_id` int(6) NOT NULL, + `weapon_wear` float NOT NULL DEFAULT 0.000001, + `weapon_seed` int(16) NOT NULL DEFAULT 0 + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci", + @"CREATE TABLE IF NOT EXISTS `wp_player_knife` ( + `steamid` varchar(64) NOT NULL, + `knife` varchar(64) NOT NULL, + UNIQUE (`steamid`) + ) ENGINE = InnoDB" + }; - await connection.ExecuteAsync(createTable1, transaction: transaction); - await connection.ExecuteAsync(createTable2, transaction: transaction); + foreach (var query in createTableQueries) + { + await connection.ExecuteAsync(query, transaction: transaction); + } await transaction.CommitAsync(); } @@ -56,14 +72,18 @@ namespace WeaponPaints } catch (Exception ex) { - throw new Exception("[WeaponPaints] Unknown mysql exception! " + ex.Message); + throw new Exception("[WeaponPaints] Unknown MySQL exception! " + ex.Message); } } internal static bool IsPlayerValid(CCSPlayerController? player) { - return (player != null && player.IsValid && !player.IsBot && !player.IsHLTV && player.AuthorizedSteamID != null); + if (player is null) return false; + + return (player is not null && player.IsValid && !player.IsBot && !player.IsHLTV + && WeaponPaints.weaponSync != null && player.Connected == PlayerConnectedState.PlayerConnected); } + internal static void LoadSkinsFromFile(string filePath) { try @@ -168,6 +188,7 @@ namespace WeaponPaints Console.WriteLine(" "); } + /*( internal static void TestDatabaseConnection() { try @@ -186,5 +207,6 @@ namespace WeaponPaints } CheckDatabaseTables(); } + */ } } \ No newline at end of file diff --git a/WeaponAction.cs b/WeaponAction.cs index d8cc0115..40937a1a 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -11,7 +11,7 @@ namespace WeaponPaints { internal static void ChangeWeaponAttributes(CBasePlayerWeapon? weapon, CCSPlayerController? player, bool isKnife = false) { - if (player == null || weapon == null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return; + if (player is null || weapon is null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return; int playerIndex = (int)player.Index; @@ -144,6 +144,7 @@ namespace WeaponPaints return false; } + /* internal void RefreshPlayerKnife(CCSPlayerController? player) { return; @@ -202,7 +203,9 @@ namespace WeaponPaints } } } + */ + /* internal void RefreshSkins(CCSPlayerController? player) { return; @@ -212,10 +215,11 @@ namespace WeaponPaints 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.PlayerPawn?.Value == null || !player.PawnIsAlive) + if (player == null || !player.IsValid || player.PlayerPawn?.Value == null || (LifeState_t)player.LifeState != LifeState_t.LIFE_ALIVE) return; if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) @@ -225,40 +229,101 @@ namespace WeaponPaints if (weapons == null || weapons.Count == 0) return; - var itemServices = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle); + // Dictionary to store weapons and their ammo - foreach (var weapon in weapons) + + if (weapons != null && weapons.Count > 0) { - if (weapon == null || !weapon.IsValid || weapon.Value == null || !weapon.Value.IsValid || weapon.Index <= 0 || !weapon.Value.DesignerName.Contains("weapon_")) - continue; + //Dictionary weaponsWithAmmo = new Dictionary(); + Dictionary> weaponsWithAmmo = new Dictionary>(); + bool bomb = false; + bool defuser = player.PawnHasDefuser; + bool healthshot = false; - try + // Iterate through each weapon + foreach (var weapon in weapons) { - string? weaponByDefindex = null; + if (weapon == null || !weapon.IsValid || weapon.Value == null || + !weapon.Value.IsValid || !weapon.Value.DesignerName.Contains("weapon_")) + continue; - if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) + try { - player.RemoveItemByDesignerName(weapon.Value.DesignerName, false); - GiveKnifeToPlayer(player); - } - else - { - if (weaponDefindex.TryGetValue(weapon.Value.AttributeManager.Item.ItemDefinitionIndex, out weaponByDefindex) && weaponByDefindex != null) + string? weaponByDefindex = null; + + CCSWeaponBaseVData? weaponData = weapon.Value.As().VData; + + if (weaponData != null) + { + if (weaponData.GearSlot == gear_slot_t.GEAR_SLOT_C4) + bomb = true; + + if (weaponData.Name.Equals("weapon_healtshot")) + healthshot = true; + + if (weaponData.GearSlot == gear_slot_t.GEAR_SLOT_GRENADES || weaponData.GearSlot == gear_slot_t.GEAR_SLOT_UTILITY || weaponData.GearSlot == gear_slot_t.GEAR_SLOT_BOOSTS) + { + int clip1 = weapon.Value.Clip1; + int reservedAmmo = weapon.Value.ReserveAmmo[0]; + + weaponsWithAmmo.Add(weapon.Value.DesignerName, new List<(int, int)>() { (clip1, reservedAmmo) }); + } + } + + if (!weapon.Value.DesignerName.Contains("knife") && weaponDefindex.TryGetValue(weapon.Value.AttributeManager.Item.ItemDefinitionIndex, out weaponByDefindex) && weaponByDefindex != null) { int clip1 = weapon.Value.Clip1; int reservedAmmo = weapon.Value.ReserveAmmo[0]; - player.RemoveItemByDesignerName(weapon.Value.DesignerName, false); - var newWeapon = new CBasePlayerWeapon(player.GiveNamedItem(weaponByDefindex)); + if (!weaponsWithAmmo.ContainsKey(weaponByDefindex)) + { + weaponsWithAmmo.Add(weaponByDefindex, new List<(int, int)>()); + } + weaponsWithAmmo[weaponByDefindex].Add((clip1, reservedAmmo)); + } + } + catch (Exception ex) + { + Logger.LogWarning(ex.Message); + continue; + } + } + + player.RemoveWeapons(); + AddTimer(0.2f, () => + { + if (bomb) + player.GiveNamedItem("weapon_c4"); + + if (defuser) + { + var itemServ = player.PlayerPawn?.Value?.ItemServices; + if (itemServ != null) + { + var items = new CCSPlayer_ItemServices(itemServ.Handle); + items.HasDefuser = true; + } + } + + if (healthshot) + player.GiveNamedItem("weapon_healtshot"); + + GiveKnifeToPlayer(player); + + foreach (var entry in weaponsWithAmmo) + { + foreach (var ammo in entry.Value) + { + var newWeapon = new CBasePlayerWeapon(player.GiveNamedItem(entry.Key)); Server.NextFrame(() => { try { if (newWeapon != null) { - newWeapon.Clip1 = clip1; - newWeapon.ReserveAmmo[0] = reservedAmmo; + newWeapon.Clip1 = ammo.Item1; + newWeapon.ReserveAmmo[0] = ammo.Item2; } } catch (Exception ex) @@ -267,62 +332,46 @@ namespace WeaponPaints } }); } - else - { - Logger.LogWarning("Unable to find weapon by defindex."); - } } - } - catch (Exception ex) - { - Logger.LogWarning("Refreshing weapons exception: " + ex.Message); - } + }); } } - internal void RemovePlayerKnife(CCSPlayerController? player, bool force = false) + internal void RefreshKnife(CCSPlayerController? player, bool force = false) { - if (player == null || !player.IsValid || player.PlayerPawn.Value == null || !player.PawnIsAlive) return; - if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) return; + if (player == null || !player.IsValid || player.PlayerPawn?.Value == null || (LifeState_t)player.LifeState != LifeState_t.LIFE_ALIVE) + 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); - foreach (var weapon in weapons) { - if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid) + if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid && weapon.Index > 0) { - if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) + try { - if (!force) - { - 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)); + CCSWeaponBaseVData? weaponData = weapon.Value.As().VData; - AddTimer(1.0f, () => - { - CEntityInstance? knife = Utilities.GetEntityFromIndex(weaponEntityIndex); - if (knife != null && knife.IsValid) - { - knife.Remove(); - } - }); - } - else + if (weapon.Value.DesignerName.Contains("knife") || weaponData?.GearSlot == gear_slot_t.GEAR_SLOT_KNIFE) { - weapon.Value.Remove(); + player.RemoveItemByDesignerName(weapon.Value.DesignerName, weapon.Value.Entity?.EntityInstance.IsValid ?? false); + AddTimer(0.2f, () => GiveKnifeToPlayer(player)); + break; } - - break; + } + catch (Exception ex) + { + Logger.LogWarning($"Cannot remove knife: {ex.Message}"); } } } } } + private static int GetRandomPaint(int defindex) { if (skinsList == null || skinsList.Count == 0) diff --git a/WeaponPaints.cs b/WeaponPaints.cs index 3ee5ce65..cd350cbb 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -86,6 +86,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig commandsCooldown = new Dictionary(); internal static Database? _database; + //private CounterStrikeSharp.API.Modules.Timers.Timer? g_hTimerCheckSkinsData = null; public static Dictionary weaponDefindex { get; } = new Dictionary { @@ -150,7 +151,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig "Nereziel & daffyy"; public override string ModuleDescription => "Skin and knife selector, standalone and web-based"; public override string ModuleName => "WeaponPaints"; - public override string ModuleVersion => "1.6a"; + public override string ModuleVersion => "1.6b"; public static WeaponPaintsConfig GetWeaponPaintsConfig() { @@ -207,6 +208,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig