diff --git a/Commands.cs b/Commands.cs index 9e8bc041..524a3ca6 100644 --- a/Commands.cs +++ b/Commands.cs @@ -151,10 +151,12 @@ namespace WeaponPaints 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; @@ -223,15 +225,16 @@ namespace WeaponPaints if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return; if (!Utility.IsPlayerValid(player)) return; string temp = ""; + if (!player!.EntityIndex.HasValue) return; int playerIndex = (int)player!.EntityIndex!.Value.Value; - if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds)) + 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)) + /*if (PlayerHasKnife(player)) RefreshPlayerKnife(player); /* AddTimer(1.0f, () => @@ -264,8 +267,7 @@ namespace WeaponPaints if (!Config.Additional.SkinEnabled) return; if (!Utility.IsPlayerValid(player)) return; - string temp = ""; - + string temp; if (!string.IsNullOrEmpty(Config.Messages.WebsiteMessageCommand)) { temp = $" {Config.Prefix} {Config.Messages.WebsiteMessageCommand}"; diff --git a/Events.cs b/Events.cs index f94afe5c..ebc145b6 100644 --- a/Events.cs +++ b/Events.cs @@ -17,15 +17,39 @@ namespace WeaponPaints private void RegisterEvents() { RegisterListener(OnEntitySpawned); - RegisterEventHandler(OnEventItemPurchasePost); - RegisterListener(OnClientAuthorized); + /*RegisterListener(OnClientAuthorized);*/ RegisterListener(OnClientDisconnect); RegisterListener(OnMapStart); + RegisterEventHandler(OnPlayerConnectFull); RegisterEventHandler(OnPlayerSpawn); RegisterEventHandler(OnRoundStart, HookMode.Pre); + RegisterEventHandler(OnEventItemPurchasePost); RegisterEventHandler(OnItemPickup); } + + private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info) + { + CCSPlayerController? player = @event.Userid; + + if (player == null || !player.IsValid || !player.EntityIndex.HasValue || player.IsHLTV) return HookResult.Continue; + + 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; + } + private void OnMapStart(string mapName) { if (!Config.Additional.KnifeEnabled) return; @@ -42,10 +66,10 @@ namespace WeaponPaints int playerIndex = playerSlot + 1; Task.Run(async () => { - if (Config.Additional.KnifeEnabled && weaponSync != null) - await weaponSync.GetKnifeFromDatabase(playerIndex); if (Config.Additional.SkinEnabled && weaponSync != null) await weaponSync.GetWeaponPaintsFromDatabase(playerIndex); + if (Config.Additional.KnifeEnabled && weaponSync != null) + await weaponSync.GetKnifeFromDatabase(playerIndex); }); } private void OnClientDisconnect(int playerSlot) diff --git a/Utility.cs b/Utility.cs index a59a6047..a847ca0c 100644 --- a/Utility.cs +++ b/Utility.cs @@ -15,7 +15,7 @@ namespace WeaponPaints internal static bool IsPlayerValid(CCSPlayerController? player) { - return (player != null && player.IsValid && !player.IsBot && !player.IsHLTV); + return (player != null && player.IsValid && !player.IsBot && !player.IsHLTV && player.SteamID.ToString() != "0"); } internal static string BuildDatabaseConnectionString() diff --git a/WeaponAction.cs b/WeaponAction.cs index 9e8d52a6..ffefd9ef 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -113,6 +113,7 @@ namespace WeaponPaints } } } + internal void RefreshPlayerKnife(CCSPlayerController? player) { if (player == null || !player.IsValid || !player.PawnIsAlive) return; @@ -134,17 +135,28 @@ namespace WeaponPaints if (!weapon.Value.EntityIndex.HasValue) return; int weaponEntityIndex = (int)weapon.Value.EntityIndex!.Value.Value; NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"); - AddTimer(0.35f, () => service.DropActivePlayerWeapon(weapon.Value)); + AddTimer(0.22f, () => + { + if (player.PlayerPawn.Value.WeaponServices.ActiveWeapon.Value.DesignerName.Contains("knife") + || + player.PlayerPawn.Value.WeaponServices.ActiveWeapon.Value.DesignerName.Contains("bayonet") + ) + { + if (player.PawnIsAlive) + { + NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"); + service.DropActivePlayerWeapon(weapon.Value); + GiveKnifeToPlayer(player); + } + } + }); - AddTimer(1.0f, () => + AddTimer(2.5f, () => { CEntityInstance? knife = Utilities.GetEntityFromIndex(weaponEntityIndex); - if (knife != null && knife.IsValid) + if (knife != null && knife.IsValid && knife.EntityIndex.HasValue) { knife.Remove(); - - if (player.PawnIsAlive) - GiveKnifeToPlayer(player); } }); @@ -165,14 +177,17 @@ namespace WeaponPaints } internal static bool PlayerHasKnife(CCSPlayerController? player) { - if (!WeaponPaints._config.Additional.KnifeEnabled) return false; + if (!_config.Additional.KnifeEnabled) return false; if (player == null || !player.IsValid) { return false; } - var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons; + if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) + return false; + + var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons; if (weapons == null || weapons.Count <= 0) return false; foreach (var weapon in weapons) { diff --git a/WeaponSynchronization.cs b/WeaponSynchronization.cs index 560d425f..d919d9c6 100644 --- a/WeaponSynchronization.cs +++ b/WeaponSynchronization.cs @@ -124,10 +124,9 @@ namespace WeaponPaints CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); if (!Utility.IsPlayerValid(player)) return; - var steamId = new SteamID(player.SteamID); - if (!WeaponPaints.gPlayerWeaponsInfo.ContainsKey(playerIndex)) + if (!WeaponPaints.gPlayerWeaponsInfo.TryGetValue(playerIndex, out _)) { WeaponPaints.gPlayerWeaponsInfo[playerIndex] = new Dictionary(); }