Files
tbd-station-14/Content.Server/Commands/CommandUtils.cs
Acruid 8b5d66050a Console Unify API Changes (#3059)
* Remove unused IChatCommand.

* Lots of refactoring into a shared context.

* Removed ICommonSession from server concmd Execute.

* Added argStr parameter to concmd execute.

* The execute function of client concmds now returns void, use the new shell.RemoteExecuteCommand function to forward commands.

* Finally move shells and commands into shared.

* Console commands can now be registered directly without a class in a shared context.

* Engine API Changes.

* Repair rebase damage.

* Update Submodule.
2021-02-01 16:49:43 -08:00

58 lines
2.1 KiB
C#

#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using Robust.Server.Console;
using Robust.Server.Interfaces.Player;
using Robust.Shared.Console;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Network;
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, IPlayerSession performer, [NotNullWhen(true)] out IPlayerSession? 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, IPlayerSession performer, [NotNullWhen(true)] out IEntity? attachedEntity)
{
attachedEntity = null;
if (!TryGetSessionByUsernameOrId(shell, usernameOrId, performer, out var session)) return false;
if (session.AttachedEntity == null)
{
shell.WriteLine("User has no attached entity.");
return false;
}
attachedEntity = session.AttachedEntity;
return true;
}
}
}