Files
crystall-punk-14/Content.Server/Commands/CommandUtils.cs

56 lines
2.1 KiB
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.Network;
using Robust.Shared.Player;
2025-01-02 17:00:13 +13:00
using Robust.Shared.Toolshed.Commands.Generic;
namespace Content.Server.Commands
{
/// <summary>
/// Utilities for writing commands
/// </summary>
public static class CommandUtils
{
/// <summary>
/// Gets the player session for the player with the indicated id,
/// sending a failure to the performer if unable to.
/// </summary>
public static bool TryGetSessionByUsernameOrId(IConsoleShell shell,
string usernameOrId, ICommonSession performer, [NotNullWhen(true)] out ICommonSession? session)
{
var plyMgr = IoCManager.Resolve<IPlayerManager>();
if (plyMgr.TryGetSessionByUsername(usernameOrId, out session)) return true;
if (Guid.TryParse(usernameOrId, out var targetGuid))
{
if (plyMgr.TryGetSessionById(new NetUserId(targetGuid), out session)) return true;
shell.WriteLine("Unable to find user with that name/id.");
return false;
}
shell.WriteLine("Unable to find user with that name/id.");
return false;
}
/// <summary>
/// Gets the attached entity for the player session with the indicated id,
/// sending a failure to the performer if unable to.
/// </summary>
public static bool TryGetAttachedEntityByUsernameOrId(IConsoleShell shell,
string usernameOrId, ICommonSession performer, out EntityUid attachedEntity)
{
2021-12-06 15:39:46 +11:00
attachedEntity = default;
if (!TryGetSessionByUsernameOrId(shell, usernameOrId, performer, out var session)) return false;
2021-12-06 15:34:46 +01:00
if (session.AttachedEntity == null)
{
shell.WriteLine("User has no attached entity.");
return false;
}
2021-12-06 15:34:46 +01:00
attachedEntity = session.AttachedEntity.Value;
return true;
}
}
}