Skip to content

Commit

Permalink
1.4.7a
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
daffyyyy committed Jun 23, 2024
1 parent 8e4724f commit cdd7715
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 92 deletions.
8 changes: 5 additions & 3 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin

public static IStringLocalizer? _localizer;
public static readonly Dictionary<string, int> VoteAnswers = [];
private static readonly ConcurrentBag<int> GodPlayers = [];
private static readonly ConcurrentBag<int> SilentPlayers = [];
private static readonly HashSet<int> GodPlayers = [];
private static readonly HashSet<int> SilentPlayers = [];
//private static readonly ConcurrentBag<int> SilentPlayers = [];
private static readonly ConcurrentBag<string> 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;
Expand All @@ -37,7 +39,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public override string ModuleName => "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();

Expand Down
4 changes: 2 additions & 2 deletions CS2-SimpleAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.243" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.244" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Discord.Net.Webhook" Version="3.15.0" />
<PackageReference Include="Discord.Net.Webhook" Version="3.15.1" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="Newtonsoft.Json" Version="*" />
</ItemGroup>
Expand Down
15 changes: 11 additions & 4 deletions Commands/basebans.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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);
Expand All @@ -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.");
Expand Down Expand Up @@ -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}.");
}
Expand Down
2 changes: 1 addition & 1 deletion Commands/basechat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 5 additions & 7 deletions Commands/basecommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -410,15 +410,15 @@ 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()}\"");
}
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}\"");
Expand Down Expand Up @@ -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();

Expand Down
32 changes: 16 additions & 16 deletions Commands/basecomms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()))
{
Expand All @@ -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()))
{
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -321,7 +321,7 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command)
/*
TargetResult? targets = GetTarget(command);
if (targets == null) return;
List<CCSPlayerController> playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.SteamID.ToString().Length == 17 &&!player.IsHLTV).ToList();
List<CCSPlayerController> 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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -644,7 +644,7 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command)
/*
TargetResult? targets = GetTarget(command);
if (targets == null) return;
List<CCSPlayerController> playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.SteamID.ToString().Length == 17 &&!player.IsHLTV).ToList();
List<CCSPlayerController> 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)
{
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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()))
{
Expand All @@ -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);
Expand Down Expand Up @@ -974,7 +974,7 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command)
/*
TargetResult? targets = GetTarget(command);
if (targets == null) return;
List<CCSPlayerController> playersToTarget = targets!.Players.Where(player => player!= null && player.IsValid && player.SteamID.ToString().Length == 17 &&!player.IsHLTV).ToList();
List<CCSPlayerController> 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)
{
Expand All @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions Commands/funcommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit cdd7715

Please sign in to comment.