CommandUtils.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Globalization;
  3. using Robust.Server.Player;
  4. using Robust.Shared.Console;
  5. using Robust.Shared.Network;
  6. using Robust.Shared.Player;
  7. using Robust.Shared.Toolshed.Commands.Generic;
  8. namespace Content.Server.Commands
  9. {
  10. /// <summary>
  11. /// Utilities for writing commands
  12. /// </summary>
  13. public static class CommandUtils
  14. {
  15. /// <summary>
  16. /// Gets the player session for the player with the indicated id,
  17. /// sending a failure to the performer if unable to.
  18. /// </summary>
  19. public static bool TryGetSessionByUsernameOrId(IConsoleShell shell,
  20. string usernameOrId, ICommonSession performer, [NotNullWhen(true)] out ICommonSession? session)
  21. {
  22. var plyMgr = IoCManager.Resolve<IPlayerManager>();
  23. if (plyMgr.TryGetSessionByUsername(usernameOrId, out session)) return true;
  24. if (Guid.TryParse(usernameOrId, out var targetGuid))
  25. {
  26. if (plyMgr.TryGetSessionById(new NetUserId(targetGuid), out session)) return true;
  27. shell.WriteLine("Unable to find user with that name/id.");
  28. return false;
  29. }
  30. shell.WriteLine("Unable to find user with that name/id.");
  31. return false;
  32. }
  33. /// <summary>
  34. /// Gets the attached entity for the player session with the indicated id,
  35. /// sending a failure to the performer if unable to.
  36. /// </summary>
  37. public static bool TryGetAttachedEntityByUsernameOrId(IConsoleShell shell,
  38. string usernameOrId, ICommonSession performer, out EntityUid attachedEntity)
  39. {
  40. attachedEntity = default;
  41. if (!TryGetSessionByUsernameOrId(shell, usernameOrId, performer, out var session)) return false;
  42. if (session.AttachedEntity == null)
  43. {
  44. shell.WriteLine("User has no attached entity.");
  45. return false;
  46. }
  47. attachedEntity = session.AttachedEntity.Value;
  48. return true;
  49. }
  50. }
  51. }