From cdd771511bbfbb451ae29aa0bc7c1787821cb790 Mon Sep 17 00:00:00 2001 From: Dawid Bepierszcz <41084667+daffyyyy@users.noreply.github.com> Date: Sun, 23 Jun 2024 12:59:14 +0200 Subject: [PATCH] 1.4.7a - Fixed crash when using command on invalid player - Using banid command to reduce number of queries to database (only if unlock commands enabled in css config) - Minor changes --- CS2-SimpleAdmin.cs | 8 +++-- CS2-SimpleAdmin.csproj | 4 +-- Commands/basebans.cs | 15 ++++++--- Commands/basechat.cs | 2 +- Commands/basecommands.cs | 12 +++---- Commands/basecomms.cs | 32 +++++++++--------- Commands/funcommands.cs | 18 +++++++---- Commands/playercommands.cs | 42 ++++++++++++------------ Events.cs | 66 ++++++++++++++++++++------------------ VERSION | 2 +- 10 files changed, 109 insertions(+), 92 deletions(-) diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index de87c99..0254e9f 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -18,12 +18,14 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig VoteAnswers = []; - private static readonly ConcurrentBag GodPlayers = []; - private static readonly ConcurrentBag SilentPlayers = []; + private static readonly HashSet GodPlayers = []; + private static readonly HashSet SilentPlayers = []; + //private static readonly ConcurrentBag SilentPlayers = []; private static readonly ConcurrentBag BannedPlayers = []; private static bool _tagsDetected; public static bool VoteInProgress = false; public static int? ServerId = null; + public static bool UnlockedCommands = CoreConfig.UnlockConCommands; public static DiscordWebhookClient? DiscordWebhookClientLog; public static DiscordWebhookClient? DiscordWebhookClientPenalty; @@ -37,7 +39,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)"); public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)"; public override string ModuleAuthor => "daffyy & Dliix66"; - public override string ModuleVersion => "1.4.6b"; + public override string ModuleVersion => "1.4.7a"; public CS2_SimpleAdminConfig Config { get; set; } = new(); diff --git a/CS2-SimpleAdmin.csproj b/CS2-SimpleAdmin.csproj index 28a7898..8205265 100644 --- a/CS2-SimpleAdmin.csproj +++ b/CS2-SimpleAdmin.csproj @@ -10,9 +10,9 @@ - + - + diff --git a/Commands/basebans.cs b/Commands/basebans.cs index f4a235a..1c0b273 100644 --- a/Commands/basebans.cs +++ b/Commands/basebans.cs @@ -1,8 +1,10 @@ +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Core.Translations; using CounterStrikeSharp.API.Modules.Admin; using CounterStrikeSharp.API.Modules.Commands; +using CounterStrikeSharp.API.Modules.Entities; using System.Text; namespace CS2_SimpleAdmin @@ -22,7 +24,7 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command) var targets = GetTarget(command); if (targets == null) return; - var playersToTarget = targets.Players.Where(player => player.IsValid && player.SteamID.ToString().Length == 17 && !player.IsHLTV).ToList(); + var playersToTarget = targets.Players.Where(player => player.IsValid && player.Connected == PlayerConnectedState.PlayerConnected && !player.IsHLTV).ToList(); if (playersToTarget.Count > 1 && Config.DisableDangerousCommands || playersToTarget.Count == 0) { @@ -91,7 +93,7 @@ internal void Ban(CCSPlayerController? caller, CCSPlayerController? player, int if (time == 0) { - if (!player.IsBot && !player.IsHLTV) + if (player is { IsBot: false }) using (new WithTemporaryCulture(player.GetLanguage())) { player.PrintToCenter(_localizer!["sa_player_ban_message_perm", reason, caller == null ? "Console" : caller.PlayerName]); @@ -112,7 +114,7 @@ internal void Ban(CCSPlayerController? caller, CCSPlayerController? player, int } else { - if (!player.IsBot && !player.IsHLTV) + if (!player.IsBot) using (new WithTemporaryCulture(player.GetLanguage())) { player.PrintToCenter(_localizer!["sa_player_ban_message_time", reason, time, caller == null ? "Console" : caller.PlayerName]); @@ -131,6 +133,9 @@ internal void Ban(CCSPlayerController? caller, CCSPlayerController? player, int } } + if (UnlockedCommands) + Server.ExecuteCommand($"banid 2 {new SteamID(player.SteamID).SteamId3}"); + if (command != null) { Helper.LogCommand(caller, command); @@ -147,11 +152,11 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command) if (_database == null) return; var callerName = caller == null ? "Console" : caller.PlayerName; + if (command.ArgCount < 2) return; if (string.IsNullOrEmpty(command.GetArg(1))) return; - if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) { command.ReplyToCommand($"Invalid SteamID64."); @@ -248,6 +253,8 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command) Helper.LogCommand(caller, command); Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); //Helper.SendDiscordPenaltyMessage(caller, player, reason, time, Helper.PenaltyType.Ban, _discordWebhookClientPenalty, _localizer); + if (UnlockedCommands) + Server.ExecuteCommand($"banid 2 {steamId.SteamId3}"); command.ReplyToCommand($"Banned player with steamid {steamid}."); } diff --git a/Commands/basechat.cs b/Commands/basechat.cs index e93e6dc..a5ae25e 100644 --- a/Commands/basechat.cs +++ b/Commands/basechat.cs @@ -69,7 +69,7 @@ public void OnAdminPrivateSayCommand(CCSPlayerController? caller, CommandInfo co var targets = GetTarget(command); if (targets == null) return; - var playersToTarget = targets.Players.Where(player => player.IsValid && player.SteamID.ToString().Length == 17 && !player.IsHLTV).ToList(); + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); //Helper.LogCommand(caller, command); diff --git a/Commands/basecommands.cs b/Commands/basecommands.cs index e592a46..5c30bfa 100644 --- a/Commands/basecommands.cs +++ b/Commands/basecommands.cs @@ -345,7 +345,7 @@ public void OnHideCommand(CCSPlayerController? caller, CommandInfo command) if (SilentPlayers.Contains(caller.Slot)) { - RemoveFromConcurrentBag(SilentPlayers, caller.Slot); + SilentPlayers.Remove(caller.Slot); caller.PrintToChat($"You aren't hidden now!"); caller.ChangeTeam(CsTeam.Spectator); } @@ -377,7 +377,7 @@ public void OnWhoCommand(CCSPlayerController? caller, CommandInfo command) Helper.LogCommand(caller, command); //Helper.SendDiscordLogMessage(caller, command, _discordWebhookClientLog, _localizer); - var playersToTarget = targets.Players.Where(player => player.IsValid && player.SteamID.ToString().Length == 17 && !player.IsHLTV).ToList(); + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); Database.Database database = new(_dbConnectionString); BanManager banManager = new(database, Config); @@ -410,7 +410,7 @@ await Server.NextFrameAsync(() => printMethod($"• UserID: \"{playerInfo.UserId}\""); if (playerInfo.SteamId != null) printMethod($"• SteamID64: \"{playerInfo.SteamId}\""); - if (player.SteamID.ToString().Length == 17) + if (player.Connected == PlayerConnectedState.PlayerConnected) { printMethod($"• SteamID2: \"{player.SteamID}\""); printMethod($"• Community link: \"{new SteamID(player.SteamID).ToCommunityUrl()}\""); @@ -418,7 +418,7 @@ await Server.NextFrameAsync(() => if (playerInfo.IpAddress != null && AdminManager.PlayerHasPermissions(caller, "@css/showip")) printMethod($"• IP Address: \"{playerInfo.IpAddress}\""); printMethod($"• Ping: \"{player.Ping}\""); - if (player.SteamID.ToString().Length == 17) + if (player.Connected == PlayerConnectedState.PlayerConnected) { printMethod($"• Total Bans: \"{totalBans}\""); printMethod($"• Total Mutes: \"{totalMutes}\""); @@ -508,9 +508,7 @@ public void OnKickCommand(CCSPlayerController? caller, CommandInfo command) var targets = GetTarget(command); - if (targets == null) - return; - + if (targets == null) return; var playersToTarget = targets.Players .Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); diff --git a/Commands/basecomms.cs b/Commands/basecomms.cs index 7f68aaf..bebe553 100644 --- a/Commands/basecomms.cs +++ b/Commands/basecomms.cs @@ -22,7 +22,7 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command) var targets = GetTarget(command); if (targets == null) return; - var playersToTarget = targets.Players.Where(player => player.IsValid && player.SteamID.ToString().Length == 17 && !player.IsHLTV).ToList(); + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); if (playersToTarget.Count > 1 && Config.DisableDangerousCommands || playersToTarget.Count == 0) { @@ -77,7 +77,7 @@ internal static void Gag(CCSPlayerController? caller, CCSPlayerController? playe PlayerPenaltyManager.AddPenalty(player!.Slot, PenaltyType.Gag, DateTime.Now.AddMinutes(time), time); if (time == 0) { - if (!player.IsBot && !player.IsHLTV) + if (!player.IsBot) { using (new WithTemporaryCulture(player.GetLanguage())) { @@ -100,7 +100,7 @@ internal static void Gag(CCSPlayerController? caller, CCSPlayerController? playe } else { - if (!player.IsBot && !player.IsHLTV) + if (!player.IsBot) { using (new WithTemporaryCulture(player.GetLanguage())) { @@ -179,7 +179,7 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command) if (time == 0) { - if (!player.IsBot && !player.IsHLTV) + if (!player.IsBot) using (new WithTemporaryCulture(player.GetLanguage())) { player.PrintToCenter(_localizer!["sa_player_gag_message_perm", reason, caller == null ? "Console" : caller.PlayerName]); @@ -265,7 +265,7 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) var pattern = command.GetArg(1); MuteManager muteManager = new(_database); - + if (Helper.ValidateSteamId(pattern, out var steamId) && steamId != null) { var matches = Helper.GetPlayerFromSteamid64(steamId.SteamId64.ToString()); @@ -321,7 +321,7 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) /* TargetResult? targets = GetTarget(command); if (targets == null) return; - List playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.SteamID.ToString().Length == 17 &&!player.IsHLTV).ToList(); + List playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.Connected == PlayerConnectedState.PlayerConnected &&!player.IsHLTV).ToList(); if (playersToTarget.Count > 1 && Config.DisableDangerousCommands || playersToTarget.Count == 0) { @@ -361,7 +361,7 @@ public void OnMuteCommand(CCSPlayerController? caller, CommandInfo command) var targets = GetTarget(command); if (targets == null) return; - var playersToTarget = targets.Players.Where(player => player.IsValid && player.SteamID.ToString().Length == 17 && !player.IsHLTV).ToList(); + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); if (playersToTarget.Count > 1 && Config.DisableDangerousCommands || playersToTarget.Count == 0) { @@ -416,7 +416,7 @@ internal void Mute(CCSPlayerController? caller, CCSPlayerController? player, int if (time == 0) { - if (!player.IsBot && !player.IsHLTV) + if (!player.IsBot) using (new WithTemporaryCulture(player.GetLanguage())) { player.PrintToCenter(_localizer!["sa_player_mute_message_perm", reason, caller == null ? "Console" : caller.PlayerName]); @@ -474,7 +474,7 @@ public void OnAddMuteCommand(CCSPlayerController? caller, CommandInfo command) if (command.ArgCount < 2) return; - + if (string.IsNullOrEmpty(command.GetArg(1))) return; if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) @@ -644,7 +644,7 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command) /* TargetResult? targets = GetTarget(command); if (targets == null) return; - List playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.SteamID.ToString().Length == 17 &&!player.IsHLTV).ToList(); + List playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.Connected == PlayerConnectedState.PlayerConnected &&!player.IsHLTV).ToList(); if (playersToTarget.Count > 1 && Config.DisableDangerousCommands || playersToTarget.Count == 0) { @@ -655,7 +655,7 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command) { playersToTarget.ForEach(player => { - if (player.SteamID.ToString().Length == 17) + if (player.Connected == PlayerConnectedState.PlayerConnected) Task.Run(async () => { await _muteManager.UnmutePlayer(player.SteamID.ToString(), 1); // Unmute by type 1 (mute) @@ -679,7 +679,7 @@ public void OnSilenceCommand(CCSPlayerController? caller, CommandInfo command) var targets = GetTarget(command); if (targets == null) return; - var playersToTarget = targets.Players.Where(player => player.IsValid && player.SteamID.ToString().Length == 17 && !player.IsHLTV).ToList(); + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); if (playersToTarget.Count > 1 && Config.DisableDangerousCommands || playersToTarget.Count == 0) { @@ -759,7 +759,7 @@ internal void Silence(CCSPlayerController? caller, CCSPlayerController? player, } else { - if (!player.IsBot && !player.IsHLTV) + if (!player.IsBot) { using (new WithTemporaryCulture(player.GetLanguage())) { @@ -782,7 +782,7 @@ internal void Silence(CCSPlayerController? caller, CCSPlayerController? player, } if (command == null) return; - + Helper.SendDiscordPenaltyMessage(caller, player, reason, time, Helper.PenaltyType.Mute, DiscordWebhookClientPenalty, _localizer); Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); Helper.LogCommand(caller, command); @@ -974,7 +974,7 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command) /* TargetResult? targets = GetTarget(command); if (targets == null) return; - List playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.SteamID.ToString().Length == 17 &&!player.IsHLTV).ToList(); + List playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.Connected == PlayerConnectedState.PlayerConnected &&!player.IsHLTV).ToList(); if (playersToTarget.Count > 1 && Config.DisableDangerousCommands || playersToTarget.Count == 0) { @@ -985,7 +985,7 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command) { playersToTarget.ForEach(player => { - if (player.SteamID.ToString().Length == 17) + if (player.Connected == PlayerConnectedState.PlayerConnected) Task.Run(async () => { await _muteManager.UnmutePlayer(player.SteamID.ToString(), 2); }); // Unmute by type 2 (silence) if (TagsDetected) diff --git a/Commands/funcommands.cs b/Commands/funcommands.cs index f5a7c13..bb69de2 100644 --- a/Commands/funcommands.cs +++ b/Commands/funcommands.cs @@ -17,9 +17,11 @@ public void OnNoclipCommand(CCSPlayerController? caller, CommandInfo command) var callerName = caller == null ? "Console" : caller.PlayerName; var targets = GetTarget(command); - var playersToTarget = targets!.Players.Where(player => - player.IsValid && player.SteamID.ToString().Length == 17 && - player is { PawnIsAlive: true, IsHLTV: false }).ToList(); + if (targets == null) return; + var playersToTarget = targets.Players.Where(player => + player is not null && + player.IsValid && + player is { PawnIsAlive: true, IsHLTV: false, Connected: PlayerConnectedState.PlayerConnected }).ToList(); Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); @@ -60,13 +62,14 @@ public void OnFreezeCommand(CCSPlayerController? caller, CommandInfo command) int.TryParse(command.GetArg(2), out var time); var targets = GetTarget(command); - var playersToTarget = targets!.Players.Where(player => player is { IsValid: true, PawnIsAlive: true, IsHLTV: false }).ToList(); + if (targets == null) return; + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, PawnIsAlive: true, IsHLTV: false }).ToList(); Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (!player.IsBot && player.Connected == PlayerConnectedState.PlayerConnected) return; if (caller!.CanTarget(player)) @@ -107,11 +110,12 @@ public void OnUnfreezeCommand(CCSPlayerController? caller, CommandInfo command) var callerName = caller == null ? "Console" : caller.PlayerName; var targets = GetTarget(command); - var playersToTarget = targets!.Players.Where(player => player is { IsValid: true, PawnIsAlive: true, IsHLTV: false }).ToList(); + if (targets == null) return; + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, PawnIsAlive: true, IsHLTV: false }).ToList(); playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (!player.IsBot && player.Connected == PlayerConnectedState.PlayerConnected) return; Unfreeze(caller, player, callerName, command); diff --git a/Commands/playercommands.cs b/Commands/playercommands.cs index c58c417..6375bcb 100644 --- a/Commands/playercommands.cs +++ b/Commands/playercommands.cs @@ -31,7 +31,7 @@ public void OnSlayCommand(CCSPlayerController? caller, CommandInfo command) public void Slay(CCSPlayerController? caller, CCSPlayerController? player, string? callerName = null, CommandInfo? command = null) { - if (player != null && !player.IsBot && player.SteamID.ToString().Length != 17) + if (player == null || !player.IsValid || player.Connected != PlayerConnectedState.PlayerConnected) return; callerName ??= caller == null ? "Console" : caller.PlayerName; @@ -94,7 +94,7 @@ public void OnGiveCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; GiveWeapon(caller, player, weaponName, callerName, command); @@ -161,10 +161,10 @@ public void StripWeapons(CCSPlayerController? caller, CCSPlayerController? playe { callerName ??= caller == null ? "Console" : caller.PlayerName; - if (player != null && !player.IsBot && player.SteamID.ToString().Length != 17) + if (player == null || !player.IsValid || !player.PawnIsAlive || player.Connected != PlayerConnectedState.PlayerConnected) return; - player?.RemoveWeapons(); + player.RemoveWeapons(); if (command != null) { @@ -207,7 +207,7 @@ public void OnHpCommand(CCSPlayerController? caller, CommandInfo command) public void SetHp(CCSPlayerController? caller, CCSPlayerController? player, int health, CommandInfo? command = null) { - if (player != null && !player.IsBot && player.SteamID.ToString().Length != 17) + if (player != null && !player.IsBot && player.Connected == PlayerConnectedState.PlayerConnected) return; var callerName = caller == null ? "Console" : caller.PlayerName; @@ -247,7 +247,7 @@ public void OnSpeedCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; if (caller!.CanTarget(player)) @@ -298,7 +298,7 @@ public void OnGravityCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; if (caller!.CanTarget(player)) @@ -347,7 +347,7 @@ public void OnMoneyCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; if (caller!.CanTarget(player)) @@ -394,7 +394,7 @@ public void OnGodCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; if (caller!.CanTarget(player)) @@ -421,7 +421,7 @@ public void God(CCSPlayerController? caller, CCSPlayerController? player, string } else { - RemoveFromConcurrentBag(GodPlayers, player.Slot); + GodPlayers.Remove(player.Slot); } if (caller != null && SilentPlayers.Contains(caller.Slot)) return; @@ -455,7 +455,7 @@ public void OnSlapCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; if (caller!.CanTarget(player)) @@ -538,21 +538,21 @@ public void OnTeamCommand(CCSPlayerController? caller, CommandInfo command) public void ChangeTeam(CCSPlayerController? caller, CCSPlayerController? player, string teamName, CsTeam teamNum, bool kill, string? callerName = null, CommandInfo? command = null) { - if (player != null && !player.IsBot && player.SteamID.ToString().Length != 17) + if (player == null || !player.IsValid || player.Connected != PlayerConnectedState.PlayerConnected) return; callerName ??= caller == null ? "Console" : caller.PlayerName; if (!teamName.Equals("swap")) { - if (player != null && player.PawnIsAlive && teamNum != CsTeam.Spectator && !kill && Config.TeamSwitchType == 1) + if (player.PawnIsAlive && teamNum != CsTeam.Spectator && !kill && Config.TeamSwitchType == 1) player.SwitchTeam(teamNum); else player?.ChangeTeam(teamNum); } else { - if (player != null && player.TeamNum != (byte)CsTeam.Spectator) + if (player.TeamNum != (byte)CsTeam.Spectator) { var _teamNum = (CsTeam)player.TeamNum == CsTeam.Terrorist ? CsTeam.CounterTerrorist : CsTeam.Terrorist; teamName = _teamNum == CsTeam.Terrorist ? "TT" : "CT"; @@ -597,14 +597,15 @@ public void OnRenameCommand(CCSPlayerController? caller, CommandInfo command) return; var targets = GetTarget(command); - var playersToTarget = targets!.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); + if (targets == null) return; + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); Helper.LogCommand(caller, command); Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; if (!caller!.CanTarget(player)) return; @@ -633,11 +634,12 @@ public void OnRespawnCommand(CCSPlayerController? caller, CommandInfo command) var callerName = caller == null ? "Console" : caller.PlayerName; var targets = GetTarget(command); - var playersToTarget = targets!.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); + if (targets == null) return; + var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList(); playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17) + if (player.Connected != PlayerConnectedState.PlayerConnected) return; if (caller!.CanTarget(player)) @@ -697,7 +699,7 @@ public void OnGotoCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17 || !player.PawnIsAlive) + if (player.Connected != PlayerConnectedState.PlayerConnected || !player.PawnIsAlive) return; if (!caller.CanTarget(player)) return; @@ -742,7 +744,7 @@ public void OnBringCommand(CCSPlayerController? caller, CommandInfo command) playersToTarget.ForEach(player => { - if (!player.IsBot && player.SteamID.ToString().Length != 17 || !player.PawnIsAlive) + if (player.Connected != PlayerConnectedState.PlayerConnected || !player.PawnIsAlive) return; if (!caller.CanTarget(player)) return; diff --git a/Events.cs b/Events.cs index 9e72b62..bcb34f7 100644 --- a/Events.cs +++ b/Events.cs @@ -24,7 +24,7 @@ private void OnGameServerSteamAPIActivated() { AddTimer(8.5f, () => { - if (ServerId != null) return; + if (ServerId != null || _database == null) return; var ipAddress = ConVar.Find("ip")?.StringValue; @@ -106,7 +106,7 @@ public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo Logger.LogCritical("[OnClientDisconnect] Before"); #endif - if (player == null || !player.IsValid || string.IsNullOrEmpty(player.IpAddress) || player.IsBot) + if (player == null || !player.IsValid || player.IsBot) { return HookResult.Continue; } @@ -119,19 +119,10 @@ public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo PlayerPenaltyManager.RemoveAllPenalties(player.Slot); if (_tagsDetected) - { Server.ExecuteCommand($"css_tag_unmute {player.SteamID}"); - } - - if (SilentPlayers.Contains(player.Slot)) - { - RemoveFromConcurrentBag(SilentPlayers, player.Slot); - } - if (GodPlayers.Contains(player.Slot)) - { - RemoveFromConcurrentBag(GodPlayers, player.Slot); - } + SilentPlayers.Remove(player.Slot); + GodPlayers.Remove(player.Slot); var authorizedSteamId = player.AuthorizedSteamID; if (authorizedSteamId == null || !PermissionManager.AdminCache.TryGetValue(authorizedSteamId, @@ -196,18 +187,27 @@ public HookResult OnPlayerFullConnect(EventPlayerConnectFull @event, GameEventIn if (isBanned) { // Add player's IP and SteamID to bannedPlayers list if not already present - if (Config.BanType > 0 && playerInfo.IpAddress != null && !BannedPlayers.Contains(playerInfo.IpAddress)) + if (Config.BanType > 0 && playerInfo.IpAddress != null && + !BannedPlayers.Contains(playerInfo.IpAddress)) + { BannedPlayers.Add(playerInfo.IpAddress); + } if (playerInfo.SteamId != null && !BannedPlayers.Contains(playerInfo.SteamId)) + { BannedPlayers.Add(playerInfo.SteamId); + } // Kick the player if banned await Server.NextFrameAsync(() => { var victim = Utilities.GetPlayerFromUserid(playerInfo.UserId); + if (victim?.UserId != null) { + if (UnlockedCommands) + Server.ExecuteCommand($"banid 2 {playerInfo.UserId}"); + Helper.KickPlayer(victim.UserId.Value, "Banned"); } }); @@ -281,7 +281,7 @@ public HookResult OnRoundEnd(EventRoundStart @event, GameEventInfo info) public HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info) { - if (player is null || !player.IsValid || player.IsBot || player.IsHLTV) + if (player is null || !player.IsValid || player.IsBot) return HookResult.Continue; if (info.GetArg(1).StartsWith($"/") @@ -299,7 +299,7 @@ public HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info) public HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo info) { - if (player is null || !player.IsValid || player.IsBot || player.IsHLTV) + if (player is null || !player.IsValid || player.IsBot) return HookResult.Continue; if (info.GetArg(1).StartsWith($"/") @@ -464,31 +464,35 @@ await connection.ExecuteAsync( await Server.NextFrameAsync(() => { - try + if (onlinePlayers.Count > 0) { - foreach (var player in players.Where(player => PlayerPenaltyManager.IsSlotInPenalties(player.Slot))) + try { - if (!PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Mute) && !PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence)) + foreach (var player in players.Where(player => PlayerPenaltyManager.IsSlotInPenalties(player.Slot))) + { + if (!PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Mute) && !PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence)) + player.VoiceFlags = VoiceFlags.Normal; + + if (!PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag) && !PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence)) + { + if (_tagsDetected) + Server.ExecuteCommand($"css_tag_unmute {player.SteamID}"); + } + + if (PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence) || + PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Mute) || + PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag)) continue; player.VoiceFlags = VoiceFlags.Normal; - if (!PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag) && !PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence)) - { if (_tagsDetected) Server.ExecuteCommand($"css_tag_unmute {player.SteamID}"); } - if (PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence) || - PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Mute) || - PlayerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag)) continue; - player.VoiceFlags = VoiceFlags.Normal; - - if (_tagsDetected) - Server.ExecuteCommand($"css_tag_unmute {player.SteamID}"); + PlayerPenaltyManager.RemoveExpiredPenalties(); } - - PlayerPenaltyManager.RemoveExpiredPenalties(); + catch { } } - catch { } + }); }); }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); diff --git a/VERSION b/VERSION index e88f0eb..d66eab6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.4.6b \ No newline at end of file +1.4.7a \ No newline at end of file