diff --git a/Commands.cs b/Commands.cs index 55f4dc9b..fc6d6474 100644 --- a/Commands.cs +++ b/Commands.cs @@ -1,12 +1,68 @@ using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Commands; -using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Menu; namespace WeaponPaints { public partial class WeaponPaints { + 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 == null || player.Index <= 0) return; + int playerIndex = (int)player!.Index; + if (playerIndex != 0 && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds)) + { + commandCooldown[playerIndex] = DateTime.UtcNow; + if (weaponSync != null) + Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerIndex)); + if (Config.Additional.KnifeEnabled) + { + if (weaponSync != null) + Task.Run(async () => await weaponSync.GetKnifeFromDatabase(playerIndex)); + + RefreshWeapons(player); + } + if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand)) + { + temp = $" {Config.Prefix} {Config.Messages.SuccessRefreshCommand}"; + player.PrintToChat(Utility.ReplaceTags(temp)); + } + return; + } + if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand)) + { + temp = $" {Config.Prefix} {Config.Messages.CooldownRefreshCommand}"; + player.PrintToChat(Utility.ReplaceTags(temp)); + } + } + + private void OnCommandWS(CCSPlayerController? player, CommandInfo command) + { + if (!Config.Additional.SkinEnabled) return; + if (!Utility.IsPlayerValid(player)) return; + + string temp; + if (!string.IsNullOrEmpty(Config.Messages.WebsiteMessageCommand)) + { + temp = $" {Config.Prefix} {Config.Messages.WebsiteMessageCommand}"; + player!.PrintToChat(Utility.ReplaceTags(temp)); + } + if (!string.IsNullOrEmpty(Config.Messages.SynchronizeMessageCommand)) + { + temp = $" {Config.Prefix} {Config.Messages.SynchronizeMessageCommand}"; + player!.PrintToChat(Utility.ReplaceTags(temp)); + } + if (!Config.Additional.KnifeEnabled) return; + if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand)) + { + temp = $" {Config.Prefix} {Config.Messages.KnifeMessageCommand}"; + player!.PrintToChat(Utility.ReplaceTags(temp)); + } + } + private void RegisterCommands() { AddCommand($"css_{Config.Additional.CommandSkin}", "Skins info", (player, info) => @@ -29,6 +85,7 @@ namespace WeaponPaints }); } } + private void SetupKnifeMenu() { if (!Config.Additional.KnifeEnabled || !g_bCommandsAllowed) return; @@ -64,13 +121,7 @@ namespace WeaponPaints if (player!.PawnIsAlive && g_bCommandsAllowed) { - //g_changedKnife.Add((int)player.Index); RefreshWeapons(player); - //RefreshPlayerKnife(player); - - /* - AddTimer(1.0f, () => GiveKnifeToPlayer(player)); - */ } if (weaponSync != null) Task.Run(() => weaponSync.SyncKnifeToDatabase((int)player.Index, knifeKey)); @@ -219,76 +270,7 @@ namespace WeaponPaints string temp = $"{Config.Prefix} {Config.Messages.CooldownRefreshCommand}"; player.PrintToChat(Utility.ReplaceTags(temp)); } - }); } - - 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 == null || player.Index <= 0) return; - int playerIndex = (int)player!.Index; - if (playerIndex != 0 && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds)) - { - commandCooldown[playerIndex] = DateTime.UtcNow; - if (weaponSync != null) - Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerIndex)); - if (Config.Additional.KnifeEnabled) - { - /*if (PlayerHasKnife(player)) - RefreshPlayerKnife(player); - /* - AddTimer(1.0f, () => - { - GiveKnifeToPlayer(player); - }); - */ - if (weaponSync != null) - Task.Run(async () => await weaponSync.GetKnifeFromDatabase(playerIndex)); - /* - RemoveKnifeFromPlayer(player); - AddTimer(0.2f, () => GiveKnifeToPlayer(player)); - */ - - RefreshWeapons(player); - } - if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand)) - { - temp = $" {Config.Prefix} {Config.Messages.SuccessRefreshCommand}"; - player.PrintToChat(Utility.ReplaceTags(temp)); - } - return; - } - if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand)) - { - temp = $" {Config.Prefix} {Config.Messages.CooldownRefreshCommand}"; - player.PrintToChat(Utility.ReplaceTags(temp)); - } - } - private void OnCommandWS(CCSPlayerController? player, CommandInfo command) - { - if (!Config.Additional.SkinEnabled) return; - if (!Utility.IsPlayerValid(player)) return; - - string temp; - if (!string.IsNullOrEmpty(Config.Messages.WebsiteMessageCommand)) - { - temp = $" {Config.Prefix} {Config.Messages.WebsiteMessageCommand}"; - player!.PrintToChat(Utility.ReplaceTags(temp)); - } - if (!string.IsNullOrEmpty(Config.Messages.SynchronizeMessageCommand)) - { - temp = $" {Config.Prefix} {Config.Messages.SynchronizeMessageCommand}"; - player!.PrintToChat(Utility.ReplaceTags(temp)); - } - if (!Config.Additional.KnifeEnabled) return; - if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand)) - { - temp = $" {Config.Prefix} {Config.Messages.KnifeMessageCommand}"; - player!.PrintToChat(Utility.ReplaceTags(temp)); - } - } } -} +} \ No newline at end of file diff --git a/Events.cs b/Events.cs index c10cd136..09e30a39 100644 --- a/Events.cs +++ b/Events.cs @@ -6,42 +6,98 @@ namespace WeaponPaints { public partial class WeaponPaints { - private void RegisterListeners() + private void OnClientAuthorized(int playerSlot, SteamID steamID) { - RegisterListener(OnEntitySpawned); - RegisterListener(OnClientAuthorized); - RegisterListener(OnClientDisconnect); - RegisterListener(OnMapStart); - - RegisterEventHandler(OnPlayerConnectFull); - RegisterEventHandler(OnPlayerSpawn); - RegisterEventHandler(OnRoundStart, HookMode.Pre); - RegisterEventHandler(OnRoundEnd); - RegisterEventHandler(OnEventItemPurchasePost); - RegisterEventHandler(OnItemPickup); - } + int playerIndex = playerSlot + 1; - /*private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info) - { - CCSPlayerController? player = @event.Userid; + CCSPlayerController? player = Utilities.GetPlayerFromIndex(playerIndex); - if (player == null || !player.IsValid || !player.EntityIndex.HasValue || player.IsHLTV) return HookResult.Continue; + if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return; - int playerIndex = (int)player.EntityIndex.Value.Value; if (Config.Additional.SkinEnabled && weaponSync != null) _ = weaponSync.GetWeaponPaintsFromDatabase(playerIndex); if (Config.Additional.KnifeEnabled && weaponSync != null) _ = weaponSync.GetKnifeFromDatabase(playerIndex); + } - Task.Run(async () => + private void OnClientDisconnect(int playerSlot) + { + CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot); + + if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return; + + if (Config.Additional.KnifeEnabled) + g_playersKnife.Remove((int)player.Index); + if (Config.Additional.SkinEnabled) + gPlayerWeaponsInfo.Remove((int)player.Index); + } + + private void OnEntitySpawned(CEntityInstance entity) + { + if (!Config.Additional.SkinEnabled) return; + var designerName = entity.DesignerName; + if (!weaponList.ContainsKey(designerName)) return; + bool isKnife = false; + var weapon = new CBasePlayerWeapon(entity.Handle); + + if (designerName.Contains("knife") || designerName.Contains("bayonet")) { - if (Config.Additional.SkinEnabled && weaponSync != null) - if (Config.Additional.KnifeEnabled && weaponSync != null) + isKnife = true; + } + Server.NextFrame(() => + { + try + { + if (!weapon.IsValid) return; + 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)); + if (!pawn.IsValid) return; + + var playerIndex = (int)pawn.Controller.Index; + var player = Utilities.GetPlayerFromIndex(playerIndex); + if (!Utility.IsPlayerValid(player)) return; + + ChangeWeaponAttributes(weapon, player, isKnife); + } + catch (Exception) { } }); + } + + private HookResult OnEventItemPurchasePost(EventItemPurchase @event, GameEventInfo info) + { + CCSPlayerController? player = @event.Userid; + + if (player == null || !player.IsValid) return HookResult.Continue; + + if (Config.Additional.SkinVisibilityFix) + AddTimer(0.2f, () => RefreshSkins(player)); return HookResult.Continue; } - */ + + 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.Index] >= 2) return HookResult.Continue; + + if (g_playersKnife.ContainsKey((int)player.Index) + && + g_playersKnife[(int)player.Index] != "weapon_knife") + { + g_knifePickupCount[(int)player.Index]++; + + RemovePlayerKnife(player, true); + AddTimer(0.3f, () => GiveKnifeToPlayer(player)); + + } + } + return HookResult.Continue; + } + private void OnMapStart(string mapName) { if (!Config.Additional.KnifeEnabled) return; @@ -70,26 +126,10 @@ namespace WeaponPaints _ = weaponSync.GetWeaponPaintsFromDatabase((int)player.Index); if (Config.Additional.KnifeEnabled && weaponSync != null) _ = weaponSync.GetKnifeFromDatabase((int)player.Index); - } }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE | CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT); } - private void OnClientAuthorized(int playerSlot, SteamID steamID) - { - int playerIndex = playerSlot + 1; - - CCSPlayerController? player = Utilities.GetPlayerFromIndex(playerIndex); - - if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return; - - if (Config.Additional.SkinEnabled && weaponSync != null) - _ = weaponSync.GetWeaponPaintsFromDatabase(playerIndex); - if (Config.Additional.KnifeEnabled && weaponSync != null) - _ = weaponSync.GetKnifeFromDatabase(playerIndex); - } - - /* WORKAROUND FOR CLIENTS WITHOUT STEAMID ON AUTHORIZATION */ private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info) { CCSPlayerController? player = @event.Userid; @@ -103,34 +143,10 @@ namespace WeaponPaints if (Config.Additional.KnifeEnabled && weaponSync != null) _ = weaponSync.GetKnifeFromDatabase((int)player.Index); - /* - AddTimer(2.0f, () => - { - if (!gPlayerWeaponsInfo.ContainsKey((int)player.Index)) - { - Console.WriteLine($"[WeaponPaints] Last try to retrieve player {player.PlayerName} skins"); - if (Config.Additional.SkinEnabled && weaponSync != null) - _ = weaponSync.GetWeaponPaintsFromDatabase((int)player.Index); - if (Config.Additional.KnifeEnabled && weaponSync != null) - _ = weaponSync.GetKnifeFromDatabase((int)player.Index); - } - }); - */ } return HookResult.Continue; } - private void OnClientDisconnect(int playerSlot) - { - CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot); - - if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return; - - if (Config.Additional.KnifeEnabled) - g_playersKnife.Remove((int)player.Index); - if (Config.Additional.SkinEnabled) - gPlayerWeaponsInfo.Remove((int)player.Index); - } private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) { @@ -155,6 +171,12 @@ namespace WeaponPaints return HookResult.Continue; } + private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) + { + g_bCommandsAllowed = false; + return HookResult.Continue; + } + private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) { NativeAPI.IssueServerCommand("mp_t_default_melee \"\""); @@ -166,110 +188,42 @@ namespace WeaponPaints return HookResult.Continue; } - private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) + private void RegisterListeners() { - g_bCommandsAllowed = false; - return HookResult.Continue; + RegisterListener(OnEntitySpawned); + RegisterListener(OnClientAuthorized); + RegisterListener(OnClientDisconnect); + RegisterListener(OnMapStart); + + RegisterEventHandler(OnPlayerConnectFull); + RegisterEventHandler(OnPlayerSpawn); + RegisterEventHandler(OnRoundStart, HookMode.Pre); + RegisterEventHandler(OnRoundEnd); + RegisterEventHandler(OnEventItemPurchasePost); + RegisterEventHandler(OnItemPickup); } - 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.Index] >= 2) return HookResult.Continue; - - if (g_playersKnife.ContainsKey((int)player.Index) - && - g_playersKnife[(int)player.Index] != "weapon_knife") - { - g_knifePickupCount[(int)player.Index]++; - - RemovePlayerKnife(player, true); - AddTimer(0.3f, () => GiveKnifeToPlayer(player)); - - //RefreshPlayerKnife(player); - /* - if (!PlayerHasKnife(player)) - GiveKnifeToPlayer(player); - - if (Config.Additional.SkinVisibilityFix) - { - AddTimer(0.25f, () => RefreshSkins(player)); - } - */ - } - } - return HookResult.Continue; - } - - private void OnEntitySpawned(CEntityInstance entity) - { - if (!Config.Additional.SkinEnabled) return; - var designerName = entity.DesignerName; - if (!weaponList.ContainsKey(designerName)) return; - bool isKnife = false; - var weapon = new CBasePlayerWeapon(entity.Handle); - - if (designerName.Contains("knife") || designerName.Contains("bayonet")) - { - isKnife = true; - } - Server.NextFrame(() => - { - try - { - - if (!weapon.IsValid) return; - if (weapon.OwnerEntity.Value == null) return; - /* - 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.Index)) - { - ChangeWeaponAttributes(weapon, ghostPlayer, isKnife); - g_changedKnife.Remove((int)ghostPlayer.Index); - break; - } - } - - } - */ - 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.Index; - var player = Utilities.GetPlayerFromIndex(playerIndex); - if (!Utility.IsPlayerValid(player)) return; - - // TODO: Remove knife crashes here, needs another solution - /*if (isKnife && g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife" && (weapon.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.AttributeManager.Item.ItemDefinitionIndex == 59)) - { - RemoveKnifeFromPlayer(player); - return; - }*/ - ChangeWeaponAttributes(weapon, player, isKnife); - } - catch (Exception) { } - }); - } - - private HookResult OnEventItemPurchasePost(EventItemPurchase @event, GameEventInfo info) + /* WORKAROUND FOR CLIENTS WITHOUT STEAMID ON AUTHORIZATION */ + /*private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info) { CCSPlayerController? player = @event.Userid; - if (player == null || !player.IsValid) return HookResult.Continue; + if (player == null || !player.IsValid || !player.EntityIndex.HasValue || player.IsHLTV) return HookResult.Continue; - if (Config.Additional.SkinVisibilityFix) - AddTimer(0.2f, () => RefreshSkins(player)); + int playerIndex = (int)player.EntityIndex.Value.Value; + if (Config.Additional.SkinEnabled && weaponSync != null) + _ = weaponSync.GetWeaponPaintsFromDatabase(playerIndex); + if (Config.Additional.KnifeEnabled && weaponSync != null) + _ = weaponSync.GetKnifeFromDatabase(playerIndex); + + Task.Run(async () => + { + if (Config.Additional.SkinEnabled && weaponSync != null) + if (Config.Additional.KnifeEnabled && weaponSync != null) + }); return HookResult.Continue; } + */ } -} +} \ No newline at end of file diff --git a/Utility.cs b/Utility.cs index 1419fd64..6e7876da 100644 --- a/Utility.cs +++ b/Utility.cs @@ -12,11 +12,6 @@ namespace WeaponPaints { internal static WeaponPaintsConfig? Config { get; set; } - internal static bool IsPlayerValid(CCSPlayerController? player) - { - return (player != null && player.IsValid && !player.IsBot && !player.IsHLTV && player.AuthorizedSteamID != null); - } - internal static string BuildDatabaseConnectionString() { if (Config == null) return string.Empty; @@ -32,25 +27,6 @@ namespace WeaponPaints return builder.ConnectionString; } - internal static void TestDatabaseConnection() - { - try - { - using var connection = new MySqlConnection(BuildDatabaseConnectionString()); - connection.Open(); - - if (connection.State != System.Data.ConnectionState.Open) - { - throw new Exception("[WeaponPaints] Unable connect to database!"); - } - } - catch (Exception ex) - { - throw new Exception("[WeaponPaints] Unknown mysql exception! " + ex.Message); - } - CheckDatabaseTables(); - } - internal static async void CheckDatabaseTables() { try @@ -81,6 +57,11 @@ namespace WeaponPaints 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); + } internal static void LoadSkinsFromFile(string filePath) { if (File.Exists(filePath)) @@ -95,6 +76,14 @@ namespace WeaponPaints } } + internal static void Log(string message) + { + Console.BackgroundColor = ConsoleColor.DarkGray; + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("[WeaponPaints] " + message); + Console.ResetColor(); + } + internal static string ReplaceTags(string message) { if (message.Contains('{')) @@ -118,13 +107,6 @@ namespace WeaponPaints return message; } - internal static void Log(string message) - { - Console.BackgroundColor = ConsoleColor.DarkGray; - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine("[WeaponPaints] " + message); - Console.ResetColor(); - } internal static void ShowAd(string moduleVersion) { Console.WriteLine(" "); @@ -139,5 +121,24 @@ namespace WeaponPaints Console.WriteLine(" >> GitHub: https://github.com/Nereziel/cs2-WeaponPaints"); Console.WriteLine(" "); } + + internal static void TestDatabaseConnection() + { + try + { + using var connection = new MySqlConnection(BuildDatabaseConnectionString()); + connection.Open(); + + if (connection.State != System.Data.ConnectionState.Open) + { + throw new Exception("[WeaponPaints] Unable connect to database!"); + } + } + catch (Exception ex) + { + throw new Exception("[WeaponPaints] Unknown mysql exception! " + ex.Message); + } + CheckDatabaseTables(); + } } } \ No newline at end of file diff --git a/WeaponAction.cs b/WeaponAction.cs index 00bc6e30..ed33bda6 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -52,6 +52,7 @@ namespace WeaponPaints skeleton.ModelState.MeshGroupMask = 2; } } + internal static void GiveKnifeToPlayer(CCSPlayerController? player) { if (!_config.Additional.KnifeEnabled || player == null || !player.IsValid) return; @@ -75,50 +76,32 @@ namespace WeaponPaints player.GiveNamedItem(defaultKnife); } } - internal void RemovePlayerKnife(CCSPlayerController? player, bool force = false) + + internal static bool PlayerHasKnife(CCSPlayerController? player) { - 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 (!_config.Additional.KnifeEnabled) return false; - var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; - if (weapons != null && weapons.Count > 0) + if (player == null || !player.IsValid || !player.PlayerPawn.IsValid) { - CCSPlayer_ItemServices service = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle); - //var dropWeapon = VirtualFunction.CreateVoid(service.Handle, GameData.GetOffset("CCSPlayer_ItemServices_DropActivePlayerWeapon")); + return false; + } - foreach (var weapon in weapons) + 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) return false; + 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) + if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) { - //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 ((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, () => - { - CEntityInstance? knife = Utilities.GetEntityFromIndex(weaponEntityIndex); - if (knife != null && knife.IsValid) - { - knife.Remove(); - } - }); - } - else - { - weapon.Value.Remove(); - } - - break; - } + return true; } } } + return false; } internal void RefreshPlayerKnife(CCSPlayerController? player) @@ -197,7 +180,6 @@ namespace WeaponPaints if (weapons != null && weapons.Count > 0) { 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) { @@ -214,13 +196,15 @@ namespace WeaponPaints } else { + if (!weaponDefindex.ContainsKey(weapon.Value.AttributeManager.Item.ItemDefinitionIndex)) continue; int clip1, reservedAmmo; clip1 = weapon.Value.Clip1; reservedAmmo = weapon.Value.ReserveAmmo[0]; weapon.Value.Remove(); - CBasePlayerWeapon newWeapon = new(player.GiveNamedItem(weapon.Value.DesignerName)); + string weaponByDefindex = weaponDefindex[weapon.Value.AttributeManager.Item.ItemDefinitionIndex]; + CBasePlayerWeapon newWeapon = new(player.GiveNamedItem(weaponByDefindex)); Server.NextFrame(() => { @@ -247,39 +231,50 @@ namespace WeaponPaints } } - internal static bool PlayerHasKnife(CCSPlayerController? player) + internal void RemovePlayerKnife(CCSPlayerController? player, bool force = false) { - if (!_config.Additional.KnifeEnabled) return 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.IsValid) + var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; + if (weapons != null && weapons.Count > 0) { - return false; - } + CCSPlayer_ItemServices service = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle); - 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) return false; - foreach (var weapon in weapons) - { - if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid) + foreach (var weapon in weapons) { - if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) + if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid) { - return true; + //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 ((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, () => + { + CEntityInstance? knife = Utilities.GetEntityFromIndex(weaponEntityIndex); + if (knife != null && knife.IsValid) + { + knife.Remove(); + } + }); + } + else + { + weapon.Value.Remove(); + } + + break; + } } } } - return false; } - - private static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node) - { - Func GetSkeletonInstance = VirtualFunction.Create(node.Handle, 8); - return new CSkeletonInstance(GetSkeletonInstance(node.Handle)); - } - private static int GetRandomPaint(int defindex) { Random rnd = new Random(); @@ -300,10 +295,15 @@ namespace WeaponPaints { return 0; } - } } return 0; } + + private static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node) + { + Func GetSkeletonInstance = VirtualFunction.Create(node.Handle, 8); + return new CSkeletonInstance(GetSkeletonInstance(node.Handle)); + } } -} +} \ No newline at end of file diff --git a/WeaponInfo.cs b/WeaponInfo.cs index 770ac38e..dc702bbf 100644 --- a/WeaponInfo.cs +++ b/WeaponInfo.cs @@ -6,4 +6,4 @@ public int Seed { get; set; } public float Wear { get; set; } } -} +} \ No newline at end of file diff --git a/WeaponPaints.cs b/WeaponPaints.cs index 555f5acd..5ceb2e39 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -5,38 +5,10 @@ using CounterStrikeSharp.API.Modules.Cvars; using Newtonsoft.Json.Linq; namespace WeaponPaints; + [MinimumApiVersion(90)] public partial class WeaponPaints : BasePlugin, IPluginConfig { - public override string ModuleName => "WeaponPaints"; - public override string ModuleDescription => "Skin and knife selector, standalone and web-based"; - public override string ModuleAuthor => "Nereziel & daffyy"; - public override string ModuleVersion => "1.3b"; - public WeaponPaintsConfig Config { get; set; } = new(); - internal static WeaponPaintsConfig _config = new WeaponPaintsConfig(); - - internal static WeaponSynchronization? weaponSync; - - private CounterStrikeSharp.API.Modules.Timers.Timer? g_hTimerCheckSkinsData = null; - - /* - private Dictionary> gPlayerWeaponPaints = new(); - private Dictionary> gPlayerWeaponSeed = new(); - private Dictionary> gPlayerWeaponWear = new(); - */ - private string DatabaseConnectionString = string.Empty; - - internal Uri GlobalShareApi = new Uri("https://weaponpaints.fun/api.php"); - internal int GlobalShareServerId = 0; - - private DateTime[] commandCooldown = new DateTime[Server.MaxPlayers]; - internal static Dictionary> gPlayerWeaponsInfo = new Dictionary>(); - internal static Dictionary g_knifePickupCount = new Dictionary(); - internal static Dictionary g_playersKnife = new(); - //internal static List g_changedKnife = new(); - internal bool g_bCommandsAllowed = true; - - internal static List skinsList = new List(); internal static readonly Dictionary weaponList = new() { {"weapon_deagle", "Desert Eagle"}, @@ -95,6 +67,87 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig g_knifePickupCount = new Dictionary(); + internal static Dictionary g_playersKnife = new(); + internal static Dictionary> gPlayerWeaponsInfo = new Dictionary>(); + internal static List skinsList = new List(); + internal static WeaponSynchronization? weaponSync; + //internal static List g_changedKnife = new(); + internal bool g_bCommandsAllowed = true; + + internal Uri GlobalShareApi = new Uri("https://weaponpaints.fun/api.php"); + internal int GlobalShareServerId = 0; + private DateTime[] commandCooldown = new DateTime[Server.MaxPlayers]; + private string DatabaseConnectionString = string.Empty; + private CounterStrikeSharp.API.Modules.Timers.Timer? g_hTimerCheckSkinsData = null; + public static Dictionary weaponDefindex { get; } = new Dictionary + { + { 1, "weapon_deagle" }, + { 2, "weapon_elite" }, + { 3, "weapon_fiveseven" }, + { 4, "weapon_glock" }, + { 7, "weapon_ak47" }, + { 8, "weapon_aug" }, + { 9, "weapon_awp" }, + { 10, "weapon_famas" }, + { 11, "weapon_g3sg1" }, + { 13, "weapon_galilar" }, + { 14, "weapon_m249" }, + { 16, "weapon_m4a1" }, + { 17, "weapon_mac10" }, + { 19, "weapon_p90" }, + { 23, "weapon_mp5sd" }, + { 24, "weapon_ump45" }, + { 25, "weapon_xm1014" }, + { 26, "weapon_bizon" }, + { 27, "weapon_mag7" }, + { 28, "weapon_negev" }, + { 29, "weapon_sawedoff" }, + { 30, "weapon_tec9" }, + { 32, "weapon_hkp2000" }, + { 33, "weapon_mp7" }, + { 34, "weapon_mp9" }, + { 35, "weapon_nova" }, + { 36, "weapon_p250" }, + { 38, "weapon_scar20" }, + { 39, "weapon_sg556" }, + { 40, "weapon_ssg08" }, + { 60, "weapon_m4a1_silencer" }, + { 61, "weapon_usp_silencer" }, + { 63, "weapon_cz75a" }, + { 64, "weapon_revolver" }, + { 500, "weapon_bayonet" }, + { 503, "weapon_knife_css" }, + { 505, "weapon_knife_flip" }, + { 506, "weapon_knife_gut" }, + { 507, "weapon_knife_karambit" }, + { 508, "weapon_knife_m9_bayonet" }, + { 509, "weapon_knife_tactical" }, + { 512, "weapon_knife_falchion" }, + { 514, "weapon_knife_survival_bowie" }, + { 515, "weapon_knife_butterfly" }, + { 516, "weapon_knife_push" }, + { 517, "weapon_knife_cord" }, + { 518, "weapon_knife_canis" }, + { 519, "weapon_knife_ursus" }, + { 520, "weapon_knife_gypsy_jackknife" }, + { 521, "weapon_knife_outdoor" }, + { 522, "weapon_knife_stiletto" }, + { 523, "weapon_knife_widowmaker" }, + { 525, "weapon_knife_skeleton" } + }; + + public WeaponPaintsConfig Config { get; set; } = new(); + public override string ModuleAuthor => "Nereziel & daffyy"; + public override string ModuleDescription => "Skin and knife selector, standalone and web-based"; + public override string ModuleName => "WeaponPaints"; + public override string ModuleVersion => "1.3c"; + public static WeaponPaintsConfig GetWeaponPaintsConfig() + { + return _config; + } + public override void Load(bool hotReload) { if (!Config.GlobalShare) @@ -148,19 +201,6 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig {g_playersKnife[playerIndex]}"); - } - catch (Exception e) - { - Utility.Log(e.Message); - return; - } - } - - internal async Task SyncKnifeToDatabase(int playerIndex, string knife) - { - if (!_config.Additional.KnifeEnabled) return; - try - { - CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); - if (player == null || !player.IsValid) return; - if (player.AuthorizedSteamID == null) return; - string steamId = player.AuthorizedSteamID.SteamId64.ToString(); - - using var connection = new MySqlConnection(_databaseConnectionString); - await connection.OpenAsync(); - string query = "INSERT INTO `wp_player_knife` (`steamid`, `knife`) VALUES(@steamid, @newKnife) ON DUPLICATE KEY UPDATE `knife` = @newKnife"; - await connection.ExecuteAsync(query, new { steamid = steamId, newKnife = knife }); - await connection.CloseAsync(); } catch (Exception e) { @@ -170,16 +142,11 @@ namespace WeaponPaints { WeaponInfo weaponInfo = new WeaponInfo { - Paint = weaponPaintId.Value, // Example paint value - Seed = weaponSeed.Value, // Example seed value - Wear = weaponWear.Value // Example wear value + Paint = weaponPaintId.Value, + Seed = weaponSeed.Value, + Wear = weaponWear.Value }; WeaponPaints.gPlayerWeaponsInfo[playerIndex][weaponDefIndex.Value] = weaponInfo; - /* - gPlayerWeaponPaints[playerIndex][weaponDefIndex.Value] = weaponPaintId.Value; - gPlayerWeaponWear[playerIndex][weaponDefIndex.Value] = weaponWear.Value; - gPlayerWeaponSeed[playerIndex][weaponDefIndex.Value] = weaponSeed.Value; - */ } } } @@ -231,6 +198,28 @@ namespace WeaponPaints } } + internal async Task SyncKnifeToDatabase(int playerIndex, string knife) + { + if (!_config.Additional.KnifeEnabled) return; + try + { + CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); + if (player == null || !player.IsValid) return; + if (player.AuthorizedSteamID == null) return; + string steamId = player.AuthorizedSteamID.SteamId64.ToString(); + + using var connection = new MySqlConnection(_databaseConnectionString); + await connection.OpenAsync(); + string query = "INSERT INTO `wp_player_knife` (`steamid`, `knife`) VALUES(@steamid, @newKnife) ON DUPLICATE KEY UPDATE `knife` = @newKnife"; + await connection.ExecuteAsync(query, new { steamid = steamId, newKnife = knife }); + await connection.CloseAsync(); + } + catch (Exception e) + { + Utility.Log(e.Message); + return; + } + } internal async Task SyncWeaponPaintsToDatabase(CCSPlayerController? player) { if (player == null || !Utility.IsPlayerValid(player)) return; @@ -273,4 +262,4 @@ namespace WeaponPaints await connection.CloseAsync(); } } -} +} \ No newline at end of file