diff --git a/Commands.cs b/Commands.cs index 6db1c65e..0a9a4059 100644 --- a/Commands.cs +++ b/Commands.cs @@ -16,7 +16,7 @@ private void RegisterCommands() }); AddCommand($"css_{Config.Additional.CommandRefresh}", "Skins refresh", (player, info) => { - if (!Utility.IsPlayerValid(player)) return; + if (!Utility.IsPlayerValid(player) || !g_bCommandsAllowed) return; OnCommandRefresh(player, info); }); if (Config.Additional.CommandKillEnabled) @@ -31,7 +31,7 @@ private void RegisterCommands() } private void SetupKnifeMenu() { - if (!Config.Additional.KnifeEnabled) return; + if (!Config.Additional.KnifeEnabled || !g_bCommandsAllowed) return; var knivesOnly = weaponList .Where(pair => pair.Key.StartsWith("weapon_knife") || pair.Key.StartsWith("weapon_bayonet")) @@ -62,13 +62,11 @@ private void SetupKnifeMenu() g_playersKnife[(int)player!.EntityIndex!.Value.Value] = knifeKey; - if (player!.PawnIsAlive) + if (player!.PawnIsAlive && g_bCommandsAllowed) { g_changedKnife.Add((int)player.EntityIndex!.Value.Value); - if (PlayerHasKnife(player)) - { - RefreshPlayerKnife(player); - } + RefreshWeapons(player); + //RefreshPlayerKnife(player); /* AddTimer(1.0f, () => GiveKnifeToPlayer(player)); @@ -85,7 +83,7 @@ private void SetupKnifeMenu() } AddCommand($"css_{Config.Additional.CommandKnife}", "Knife Menu", (player, info) => { - if (!Utility.IsPlayerValid(player)) return; + if (!Utility.IsPlayerValid(player) || !g_bCommandsAllowed) return; int playerIndex = (int)player!.EntityIndex!.Value.Value; if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds) && playerIndex > 0 && playerIndex < commandCooldown.Length) @@ -128,9 +126,11 @@ private void SetupSkinsMenu() // Function to handle skin selection for the chosen weapon var handleSkinSelection = (CCSPlayerController? p, ChatMenuOption opt) => { - if (p == null || !p.IsValid) return; + if (p == null || !p.IsValid || !p.EntityIndex.HasValue) return; - var steamId = new SteamID(player.SteamID); + playerIndex = (int)p.EntityIndex.Value.Value; + + var steamId = new SteamID(p.SteamID); var firstSkin = skinsList?.FirstOrDefault(skin => { if (skin != null && skin.TryGetValue("weapon_name", out var weaponName)) @@ -151,22 +151,23 @@ private void SetupSkinsMenu() string temp = $" {Config.Prefix} {Config.Messages.ChosenSkinMenu}".Replace("{SKIN}", selectedSkin); p.PrintToChat(Utility.ReplaceTags(temp)); - /* - if (!gPlayerWeaponsInfo[playerIndex].ContainsKey(weaponDefIndex)) + if (!gPlayerWeaponsInfo[playerIndex].ContainsKey(weaponDefIndex)) { gPlayerWeaponsInfo[playerIndex][weaponDefIndex] = new WeaponInfo(); } - */ gPlayerWeaponsInfo[playerIndex][weaponDefIndex].Paint = paintID; gPlayerWeaponsInfo[playerIndex][weaponDefIndex].Wear = 0.0f; gPlayerWeaponsInfo[playerIndex][weaponDefIndex].Seed = 0; - if (weaponSync == null) return; - Task.Run(async () => + if (!Config.GlobalShare) { - await weaponSync.SyncWeaponPaintsToDatabase(player); - }); + if (weaponSync == null) return; + Task.Run(async () => + { + await weaponSync.SyncWeaponPaintsToDatabase(p); + }); + } } }; @@ -222,7 +223,7 @@ private void SetupSkinsMenu() private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command) { - if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return; + if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled || !g_bCommandsAllowed) return; if (!Utility.IsPlayerValid(player)) return; string temp = ""; if (!player!.EntityIndex.HasValue) return; diff --git a/Events.cs b/Events.cs index bc2b5462..9336ef05 100644 --- a/Events.cs +++ b/Events.cs @@ -15,6 +15,7 @@ private void RegisterEvents() //RegisterEventHandler(OnPlayerConnectFull); RegisterEventHandler(OnPlayerSpawn); RegisterEventHandler(OnRoundStart, HookMode.Pre); + RegisterEventHandler(OnRoundEnd); RegisterEventHandler(OnEventItemPurchasePost); RegisterEventHandler(OnItemPickup); } @@ -66,7 +67,6 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID) if (Config.Additional.KnifeEnabled && weaponSync != null) _ = weaponSync.GetKnifeFromDatabase(playerIndex); - /* Task.Run(async () => { @@ -118,8 +118,17 @@ private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) NativeAPI.IssueServerCommand("mp_ct_default_melee \"\""); NativeAPI.IssueServerCommand("mp_equipment_reset_rounds 0"); + g_bCommandsAllowed = true; + return HookResult.Continue; } + + private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) + { + g_bCommandsAllowed = false; + return HookResult.Continue; + } + private HookResult OnItemPickup(EventItemPickup @event, GameEventInfo info) { if (@event.Defindex == 42 || @event.Defindex == 59) diff --git a/WeaponAction.cs b/WeaponAction.cs index 4b9acba6..192cb9d4 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -196,7 +196,7 @@ internal void RefreshWeapons(CCSPlayerController? player) var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; if (weapons != null && weapons.Count > 0) { - CCSPlayer_ItemServices service = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle); + 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) @@ -205,31 +205,38 @@ internal void RefreshWeapons(CCSPlayerController? player) { if (!weapon.Value.EntityIndex.HasValue || !weapon.Value.DesignerName.Contains("weapon_")) continue; //if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59) - if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) - { - weapon.Value.Remove(); - GiveKnifeToPlayer(player); - } - else + try { - int clip1, reservedAmmo; + if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet")) + { + weapon.Value.Remove(); + GiveKnifeToPlayer(player); + } + else + { + int clip1, reservedAmmo; - clip1 = weapon.Value.Clip1; - reservedAmmo = weapon.Value.ReserveAmmo[0]; + clip1 = weapon.Value.Clip1; + reservedAmmo = weapon.Value.ReserveAmmo[0]; - weapon.Value.Remove(); - CBasePlayerWeapon newWeapon = new CBasePlayerWeapon(player.GiveNamedItem(weapon.Value.DesignerName)); + weapon.Value.Remove(); + CBasePlayerWeapon newWeapon = new(player.GiveNamedItem(weapon.Value.DesignerName)); - Server.NextFrame(() => - { - newWeapon.Clip1 = clip1; - newWeapon.ReserveAmmo[0] = reservedAmmo; - }); - + Server.NextFrame(() => + { + newWeapon.Clip1 = clip1; + newWeapon.ReserveAmmo[0] = reservedAmmo; + }); + } + } catch(Exception ex) + { + Console.WriteLine("[WeaponPaints] Refreshing weapons exception"); + Console.WriteLine(ex.Message); } } } - RefreshSkins(player); + if (Config.Additional.SkinVisibilityFix) + RefreshSkins(player); } } @@ -273,7 +280,7 @@ private static int GetRandomPaint(int defindex) if (WeaponPaints.skinsList != null) { // Filter weapons by the provided defindex - var filteredWeapons = WeaponPaints.skinsList.FindAll(w => w["weapon_defindex"]?.ToString() == defindex.ToString()); + var filteredWeapons = skinsList.FindAll(w => w["weapon_defindex"]?.ToString() == defindex.ToString()); if (filteredWeapons.Count > 0) { diff --git a/WeaponPaints.cs b/WeaponPaints.cs index 34d3553e..0f4cd368 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -5,7 +5,7 @@ using Newtonsoft.Json.Linq; namespace WeaponPaints; -[MinimumApiVersion(71)] +[MinimumApiVersion(82)] public partial class WeaponPaints : BasePlugin, IPluginConfig { public override string ModuleName => "WeaponPaints"; @@ -32,6 +32,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig 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()