using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Translations;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using System.Text;
using CounterStrikeSharp.API.Modules.Entities;
namespace CS2_SimpleAdmin;
public partial class CS2_SimpleAdmin
{
///
/// Sends a chat message only to admins that have chat permission.
/// The message is encoded properly to handle UTF-8 characters.
///
/// The admin player sending the message, or null for console.
/// The command input containing the message.
[CommandHelper(1, "")]
[RequiresPermissions("@css/chat")]
public void OnAdminToAdminSayCommand(CCSPlayerController? caller, CommandInfo command)
{
Helper.LogCommand(caller, command);
var utf8BytesString = Encoding.UTF8.GetBytes(command.GetCommandString[command.GetCommandString.IndexOf(' ')..]);
var utf8String = Encoding.UTF8.GetString(utf8BytesString);
foreach (var player in Helper.GetValidPlayers()
.Where(p => AdminManager.PlayerHasPermissions(new SteamID(p.SteamID), "@css/chat")))
{
if (_localizer == null) continue;
// zio: players get ">[STAFF][RANK] name: message", like @ in team chat.
player.PrintToChat(caller == null
? _localizer["sa_adminchat_template_admin", _localizer["sa_console"], utf8String.Trim()]
: StaffChatMessage(caller, utf8String.Trim()));
}
}
// zio: staff chat line, ">[STAFF][RANK] name: message" ("[RANK]" left out without a rank group).
private static string StaffChatMessage(CCSPlayerController sender, string message)
{
var rank = Helper.GetStaffRankTag(sender);
return rank == null
? _localizer!["sa_staffchat_template_norank", sender.PlayerName, message]
: _localizer!["sa_staffchat_template", rank, sender.PlayerName, message];
}
///
/// Sends a custom chat message to all players with color tags processed.
///
/// The admin or console sending the message.
/// The command input containing the message.
[CommandHelper(1, "")]
[RequiresPermissions("@css/chat")]
public void OnAdminCustomSayCommand(CCSPlayerController? caller, CommandInfo command)
{
if (command.GetCommandString[command.GetCommandString.IndexOf(' ')..].Length == 0) return;
var utf8BytesString = Encoding.UTF8.GetBytes(command.GetCommandString[command.GetCommandString.IndexOf(' ')..]);
var utf8String = Encoding.UTF8.GetString(utf8BytesString);
Helper.LogCommand(caller, command);
foreach (var player in Helper.GetValidPlayers())
{
player.PrintToChat(utf8String.ReplaceColorTags());
}
}
///
/// Sends a chat message to all players with localization prefix and color tags handled.
///
/// The admin or console sending the message.
/// The command input containing the message.
[CommandHelper(1, "")]
[RequiresPermissions("@css/chat")]
public void OnAdminSayCommand(CCSPlayerController? caller, CommandInfo command)
{
if (command.GetCommandString[command.GetCommandString.IndexOf(' ')..].Length == 0) return;
var utf8BytesString = Encoding.UTF8.GetBytes(command.GetCommandString[command.GetCommandString.IndexOf(' ')..]);
var utf8String = Encoding.UTF8.GetString(utf8BytesString);
Helper.LogCommand(caller, command);
// zio: from a player (including @ in all chat), ">[RANK] name: message" with no [SA] prefix.
// Console (and the web panel over RCON) keeps the plain "ADMIN:" message.
if (caller != null && _localizer != null)
{
var rank = Helper.GetStaffRankTag(caller);
var line = rank == null
? _localizer["sa_staffsay_template_norank", caller.PlayerName, utf8String.Trim()]
: _localizer["sa_staffsay_template", rank, caller.PlayerName, utf8String.Trim()];
foreach (var player in Helper.GetValidPlayers())
player.PrintToChat(line);
return;
}
foreach (var player in Helper.GetValidPlayers())
{
player.SendLocalizedMessage(_localizer,
"sa_adminsay_prefix",
utf8String.Trim().ReplaceColorTags()); // zio: Trim, the message starts with the space after the command
}
}
///
/// Sends a private chat message from the caller to the specified target player(s).
///
/// The admin or console sending the private message.
/// The command input containing target and message.
[CommandHelper(2, "<#userid or name> ")]
[RequiresPermissions("@css/chat")]
public void OnAdminPrivateSayCommand(CCSPlayerController? caller, CommandInfo command)
{
var callerName = caller == null ? _localizer?["sa_console"] ?? "Console" : caller.PlayerName;
var targets = GetTarget(command);
if (targets == null) return;
var playersToTarget = targets.Players.Where(player => player is { IsValid: true, IsHLTV: false }).ToList();
//Helper.LogCommand(caller, command);
var range = command.GetArg(0).Length + command.GetArg(1).Length + 2;
var message = command.GetCommandString[range..];
var utf8BytesString = Encoding.UTF8.GetBytes(message);
var utf8String = Encoding.UTF8.GetString(utf8BytesString);
// zio: from a player, "(PM) [RANK] name: message".
var rank = caller == null ? null : Helper.GetStaffRankTag(caller);
var line = caller == null || _localizer == null
? $"({callerName}) {utf8String.Trim()}".ReplaceColorTags()
: rank == null
? _localizer["sa_psay_template_norank", callerName, utf8String.Trim()]
: _localizer["sa_psay_template", rank, callerName, utf8String.Trim()];
playersToTarget.ForEach(player =>
{
player.PrintToChat(line);
});
command.ReplyToCommand($" Private message sent!");
}
///
/// Broadcasts a center-screen message to all players.
///
/// The admin or console sending the message.
/// The command input containing the message.
[CommandHelper(1, "")]
[RequiresPermissions("@css/chat")]
public void OnAdminCenterSayCommand(CCSPlayerController? caller, CommandInfo command)
{
var utf8BytesString = Encoding.UTF8.GetBytes(command.GetCommandString[command.GetCommandString.IndexOf(' ')..]);
var utf8String = Encoding.UTF8.GetString(utf8BytesString);
Helper.LogCommand(caller, command);
Helper.PrintToCenterAll(utf8String.ReplaceColorTags());
}
///
/// Sends a HUD alert message to all players.
///
/// The admin or console sending the message.
/// The command input containing the message.
[CommandHelper(1, "")]
[RequiresPermissions("@css/chat")]
public void OnAdminHudSayCommand(CCSPlayerController? caller, CommandInfo command)
{
var utf8BytesString = Encoding.UTF8.GetBytes(command.GetCommandString[command.GetCommandString.IndexOf(' ')..]);
var utf8String = Encoding.UTF8.GetString(utf8BytesString);
Helper.LogCommand(caller, command);
VirtualFunctions.ClientPrintAll(
HudDestination.Alert,
utf8String.ReplaceColorTags(),
0, 0, 0, 0, 0);
}
}