From e028bef5b0983661fe4167386f62618eb9d740ae Mon Sep 17 00:00:00 2001 From: Dawid Bepierszcz <41084667+daffyyyy@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:56:13 +0100 Subject: [PATCH] 1.2.8d - Minor changes - Added the ability to change the map to a workshop map via the `css_map ws:id/name` command - Fixed the `css_wsmap` command and retained it for backward compatibility - Changed gags from userid to steamid --- CS2-SimpleAdmin.cs | 99 +++++++++++++-------- Events.cs | 208 +++++---------------------------------------- MuteManager.cs | 6 +- 3 files changed, 88 insertions(+), 225 deletions(-) diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index fa2e012..e14d8f7 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -22,7 +22,7 @@ namespace CS2_SimpleAdmin; public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { public static IStringLocalizer? _localizer; - public static ConcurrentBag gaggedPlayers = new ConcurrentBag(); + public static ConcurrentBag gaggedPlayers = new ConcurrentBag(); //public static ConcurrentBag mutedPlayers = new ConcurrentBag(); public static Dictionary voteAnswers = new Dictionary(); public static HashSet godPlayers = new HashSet(); @@ -36,7 +36,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig "CS2-SimpleAdmin"; public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)"; public override string ModuleAuthor => "daffyy"; - public override string ModuleVersion => "1.2.8c"; + public override string ModuleVersion => "1.2.8d"; public CS2_SimpleAdminConfig Config { get; set; } = new(); @@ -508,10 +508,10 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command) }); if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_mute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_mute {player!.SteamID.ToString()}"); - if (player != null && player.UserId != null && !gaggedPlayers.Contains((ushort)player.UserId)) - gaggedPlayers.Add((ushort)player.UserId); + if (player != null && player.SteamID.ToString() != "" && !gaggedPlayers.Contains(player.SteamID.ToString())) + gaggedPlayers.Add(player.SteamID.ToString()); if (time > 0 && time <= 30) { @@ -520,11 +520,11 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command) if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return; if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}"); - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (ushort)player.UserId) + if (gaggedPlayers.TryTake(out string? removedItem) && removedItem != player.SteamID.ToString()) { gaggedPlayers.Add(removedItem); } @@ -631,7 +631,7 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command) } if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_mute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_mute {player!.SteamID.ToString()}"); if (time > 0 && time <= 30) { @@ -640,11 +640,11 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command) if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return; if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}"); - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (ushort)player.UserId) + if (gaggedPlayers.TryTake(out string? removedItem) && removedItem != player.SteamID.ToString()) { gaggedPlayers.Add(removedItem); } @@ -654,8 +654,8 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command) }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); } - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) - gaggedPlayers.Add((ushort)player.UserId); + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) + gaggedPlayers.Add(player.SteamID.ToString()); } } _ = _muteManager.AddMuteBySteamid(steamid, adminInfo, reason, time, 0); @@ -686,16 +686,16 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) CCSPlayerController? player = matches.FirstOrDefault(); if (player != null && player.IsValid) { - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (ushort)player.UserId) + if (gaggedPlayers.TryTake(out string? removedItem) && removedItem != player.SteamID.ToString()) { gaggedPlayers.Add(removedItem); } } if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}"); found = true; } @@ -709,16 +709,16 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) CCSPlayerController? player = matches.FirstOrDefault(); if (player != null && player.IsValid) { - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (ushort)player.UserId) + if (gaggedPlayers.TryTake(out string? removedItem) && removedItem != player.SteamID.ToString()) { gaggedPlayers.Add(removedItem); } } if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}"); pattern = player!.AuthorizedSteamID!.SteamId64.ToString(); @@ -746,9 +746,9 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) { playersToTarget.ForEach(player => { - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (ushort)player.UserId) + if (gaggedPlayers.TryTake(out string? removedItem) && removedItem != player.SteamID.ToString()) { gaggedPlayers.Add(removedItem); } @@ -758,7 +758,7 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) _ = _muteManager.UnmutePlayer(player.AuthorizedSteamID.SteamId64.ToString(), 0); // Unmute by type 0 (gag) if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}"); }); command.ReplyToCommand($"Ungaged player with pattern {pattern}."); @@ -1697,12 +1697,32 @@ public void OnVoteCommand(CCSPlayerController? caller, CommandInfo command) [CommandHelper(minArgs: 1, usage: "", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] public void OnMapCommand(CCSPlayerController? caller, CommandInfo command) { - string map = command.GetArg(1); + string _command = string.Empty; + string? map = command.GetCommandString.Split(" ")[1]; - if (!Server.IsMapValid(map)) + if (map.StartsWith("ws:")) { - command.ReplyToCommand($"Map {map} not found."); - return; + if (long.TryParse(map.Replace("ws:", ""), out long mapId)) + { + _command = $"host_workshop_map {mapId}"; + } + else + { + _command = $"ds_workshop_changelevel {map.Replace("ws:", "")}"; + } + + AddTimer(2.0f, () => + { + Server.ExecuteCommand(_command); + }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); + } + else + { + if (!Server.IsMapValid(map)) + { + command.ReplyToCommand($"Map {map} not found."); + return; + } } if (caller == null || caller != null && caller.UserId != null && !silentPlayers.Contains((ushort)caller.UserId)) @@ -1712,10 +1732,13 @@ public void OnMapCommand(CCSPlayerController? caller, CommandInfo command) Server.PrintToChatAll(sb.ToString()); } - AddTimer(3.0f, () => + if (!map.StartsWith("ws:")) { - Server.ExecuteCommand($"changelevel {map}"); - }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); + AddTimer(2.0f, () => + { + Server.ExecuteCommand($"changelevel {map}"); + }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); + } } [ConsoleCommand("css_changewsmap", "Change workshop map.")] @@ -1725,10 +1748,18 @@ public void OnMapCommand(CCSPlayerController? caller, CommandInfo command) [RequiresPermissions("@css/changemap")] public void OnWorkshopMapCommand(CCSPlayerController? caller, CommandInfo command) { - string? _command = null; - var map = command.GetArg(1); + string _command = string.Empty; + string? map = command.GetArg(1); + + if (long.TryParse(map, out long mapId)) + { + _command = $"host_workshop_map {mapId}"; + } + else + { + _command = $"ds_workshop_changelevel {map}"; + } - _command = int.TryParse(map, out var mapId) ? $"host_workshop_map {mapId}" : $"ds_workshop_changelevel {map}"; if (caller == null || caller != null && caller.UserId != null && !silentPlayers.Contains((ushort)caller.UserId)) { StringBuilder sb = new(_localizer!["sa_prefix"]); @@ -1736,7 +1767,7 @@ public void OnWorkshopMapCommand(CCSPlayerController? caller, CommandInfo comman Server.PrintToChatAll(sb.ToString()); } - AddTimer(3.0f, () => + AddTimer(2.0f, () => { Server.ExecuteCommand(_command); }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); diff --git a/Events.cs b/Events.cs index 5433402..1c36c99 100644 --- a/Events.cs +++ b/Events.cs @@ -3,7 +3,6 @@ using CounterStrikeSharp.API.Modules.Admin; using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Cvars; -using CounterStrikeSharp.API.Modules.Entities; using Dapper; using MySqlConnector; using System.Text; @@ -64,7 +63,7 @@ private HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info) { if (player == null || !player.IsValid || info.GetArg(1).Length == 0) return HookResult.Continue; - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { return HookResult.Handled; } @@ -76,7 +75,7 @@ private HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo inf { if (player == null || !player.IsValid || info.GetArg(1).Length == 0) return HookResult.Continue; - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { return HookResult.Handled; } @@ -133,6 +132,8 @@ private void OnClientPutInServer(int playerSlot) if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return; + Console.WriteLine("test"); + string? ipAddress = !string.IsNullOrEmpty(player.IpAddress) ? player.IpAddress.Split(":")[0] : null; if ( @@ -150,7 +151,7 @@ private void OnClientPutInServer(int playerSlot) IpAddress = ipAddress }; - Task.Run(async () => + _ = Task.Run(async () => { BanManager _banManager = new(dbConnectionString, Config); bool isBanned = await _banManager.IsPlayerBanned(playerInfo); @@ -188,11 +189,11 @@ private void OnClientPutInServer(int playerSlot) if (muteType == "GAG") { // Chat mute - if (player.UserId != null && !gaggedPlayers.Any(index => index == (ushort)player.UserId)) - gaggedPlayers.Add((ushort)player.UserId); + if (player.SteamID.ToString() != "" && !gaggedPlayers.Any(steamid => steamid.Equals(player.SteamID.ToString()))) + gaggedPlayers.Add(player.SteamID.ToString()); if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_mute {player.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_mute {player!.SteamID.ToString()}"); if (durationInSeconds != 0 && duration.Minutes >= 0 && duration.Minutes <= 30) { @@ -200,16 +201,16 @@ private void OnClientPutInServer(int playerSlot) { if (player == null || !player.IsValid || player.SteamID.ToString() == "") return; - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (ushort)player.UserId) + if (gaggedPlayers.TryTake(out string? removedItem) && removedItem != player.SteamID.ToString()) { gaggedPlayers.Add(removedItem); } } if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}"); MuteManager _muteManager = new(dbConnectionString); _ = _muteManager.UnmutePlayer(player!.SteamID.ToString(), 0); @@ -230,7 +231,7 @@ private void OnClientPutInServer(int playerSlot) } */ } - else + else if (muteType == "MUTE") { // Voice mute player.VoiceFlags = VoiceFlags.Muted; @@ -334,187 +335,15 @@ private void OnClientPutInServer(int playerSlot) */ } - private void OnClientAuthorized(int playerSlot, SteamID steamID) - { - return; -#pragma warning disable CS0162 - CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot); -#pragma warning restore CS0162 - - if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) - return; - - if ( - player.IpAddress != null && bannedPlayers.Contains(player.IpAddress) || - player.AuthorizedSteamID != null && bannedPlayers.Contains(player.AuthorizedSteamID.SteamId64.ToString()) - ) - Helper.KickPlayer((ushort)player.UserId!, "Banned"); - - PlayerInfo playerInfo = new PlayerInfo - { - UserId = player.UserId, - Index = (ushort)player.Index, - SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(), - Name = player?.PlayerName, - IpAddress = player?.IpAddress?.Split(":")[0] - }; - - Task.Run(async () => - { - BanManager _banManager = new(dbConnectionString, Config); - bool isBanned = await _banManager.IsPlayerBanned(playerInfo); - - MuteManager _muteManager = new(dbConnectionString); - List activeMutes = await _muteManager.IsPlayerMuted(playerInfo.SteamId!); - - AdminSQLManager _adminManager = new(dbConnectionString); - List<(List, int)> activeFlags = await _adminManager.GetAdminFlags(playerInfo.SteamId!); - - Server.NextFrame(() => - { - if (player == null || !player.IsValid) return; - if (isBanned) - { - if (player.IpAddress != null && !bannedPlayers.Contains(player.IpAddress)) - bannedPlayers.Add(player.IpAddress); - if (player.AuthorizedSteamID != null && !bannedPlayers.Contains(player.AuthorizedSteamID.SteamId64.ToString())) - bannedPlayers.Add(player.AuthorizedSteamID.SteamId64.ToString()); - - Helper.KickPlayer((ushort)player.UserId!, "Banned"); - return; - } - - //Helper.GivePlayerFlags(player, activeFlags); - - if (activeMutes.Count > 0) - { - foreach (var mute in activeMutes) - { - string muteType = mute.type; - TimeSpan duration = mute.ends - mute.created; - int durationInSeconds = (int)duration.TotalSeconds; - - if (muteType == "GAG") - { - // Chat mute - if (player.UserId != null && !gaggedPlayers.Any(index => index == (ushort)player.UserId)) - gaggedPlayers.Add((ushort)player.UserId); - - if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_mute {player.UserId}"); - - if (durationInSeconds != 0 && duration.Minutes >= 0 && duration.Minutes <= 30) - { - AddTimer(durationInSeconds, () => - { - if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return; - - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) - { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (int)player.UserId) - { - gaggedPlayers.Add(removedItem); - } - } - - if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player!.UserId}"); - - MuteManager _muteManager = new(dbConnectionString); - _ = _muteManager.UnmutePlayer(player!.AuthorizedSteamID.SteamId64.ToString(), 0); - }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); - } - - /* - CCSPlayerController currentPlayer = player; - - if (mute.duration == 0 || durationInSeconds >= 1800) continue; - - await Task.Delay(TimeSpan.FromSeconds(durationInSeconds)); - - if (currentPlayer != null && currentPlayer.IsValid) - { - NativeAPI.IssueServerCommand($"css_tag_unmute {currentPlayer.Index.ToString()}"); - await UnmutePlayer(currentPlayer.AuthorizedSteamID.SteamId64.ToString(), 0); - } - */ - } - else - { - // Voice mute - player.VoiceFlags = VoiceFlags.Muted; - - if (durationInSeconds != 0 && duration.Minutes >= 0 && duration.Minutes <= 30) - { - AddTimer(durationInSeconds, () => - { - if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return; - - /* - if (mutedPlayers.Contains((ushort)player.UserId)) - { - if (mutedPlayers.TryTake(out int removedItem) && removedItem != (ushort)player.UserId) - { - mutedPlayers.Add(removedItem); - } - } - */ - - player.VoiceFlags = VoiceFlags.Normal; - - //MuteManager _muteManager = new(dbConnectionString); - //_ = _muteManager.UnmutePlayer(player.AuthorizedSteamID.SteamId64.ToString(), 1); - }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); - } - } - } - } - - /* - - if (_adminManager._adminCache != null && _adminManager._adminCache.Count > 0) - { - foreach (var flags in activeFlags) - { - if (flags == null) continue; - string flagsValue = flags.flags.ToString(); - - if (!string.IsNullOrEmpty(flagsValue)) - { - string[] _flags = flagsValue.Split(","); - - AddTimer(10, () => - { - if (player == null) return; - foreach (var _flag in _flags) - { - if (_flag.StartsWith("@")) - { - AdminManager.AddPlayerPermissions(player, _flag); - } - if (_flag.StartsWith("3")) - { - AdminManager.AddPlayerToGroup(player, _flag); - } - } - }); - } - } - } - */ - }); - }); - } - private void OnClientDisconnect(int playerSlot) { CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot); if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return; - if (player != null && player.UserId != null && gaggedPlayers.Contains((ushort)player.UserId)) + if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString())) { - if (gaggedPlayers.TryTake(out ushort removedItem) && removedItem != (ushort)player.UserId) + if (gaggedPlayers.TryTake(out string? removedItem) && removedItem != player.SteamID.ToString()) { gaggedPlayers.Add(removedItem); } @@ -550,11 +379,13 @@ private void OnClientDisconnect(int playerSlot) } if (TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_unmute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}"); } private void OnMapStart(string mapName) { + gaggedPlayers.Clear(); + AdminSQLManager _adminManager = new(dbConnectionString); AddTimer(60.0f, bannedPlayers.Clear, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); @@ -573,7 +404,7 @@ private void OnMapStart(string mapName) TagsDetected = true; } - AddTimer(1.0f, () => + AddTimer(2.0f, () => { using (var connection = new MySqlConnection(dbConnectionString)) { @@ -591,10 +422,11 @@ private void OnMapStart(string mapName) ServerId = serverId; connection.Close(); + + _ = _adminManager.GiveAllFlags(); } }); - _ = _adminManager.GiveAllFlags(); } private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info) diff --git a/MuteManager.cs b/MuteManager.cs index 6c3a1fe..a2344c1 100644 --- a/MuteManager.cs +++ b/MuteManager.cs @@ -174,11 +174,11 @@ public async Task CheckMute(PlayerInfo player) if (muteType == "GAG") { - if (!CS2_SimpleAdmin.gaggedPlayers.Any(index => index == player.UserId)) - CS2_SimpleAdmin.gaggedPlayers.Add((ushort)player.UserId); + if (!CS2_SimpleAdmin.gaggedPlayers.Any(steamid => steamid == player.SteamId!.ToString())) + CS2_SimpleAdmin.gaggedPlayers.Add(player.SteamId!.ToString()); if (CS2_SimpleAdmin.TagsDetected) - NativeAPI.IssueServerCommand($"css_tag_mute {player!.UserId}"); + NativeAPI.IssueServerCommand($"css_tag_mute {player!.SteamId!.ToString()}"); /* CCSPlayerController currentPlayer = player;