AttachBodyPartCommand.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Content.Server.Administration;
  2. using Content.Server.Body.Systems;
  3. using Content.Shared.Administration;
  4. using Content.Shared.Body.Components;
  5. using Content.Shared.Body.Part;
  6. using Robust.Shared.Console;
  7. namespace Content.Server.Body.Commands
  8. {
  9. [AdminCommand(AdminFlags.Fun)]
  10. public sealed class AttachBodyPartCommand : IConsoleCommand
  11. {
  12. [Dependency] private readonly IEntityManager _entManager = default!;
  13. public string Command => "attachbodypart";
  14. public string Description => "Attaches a body part to you or someone else.";
  15. public string Help => $"{Command} <partEntityUid> / {Command} <entityUid> <partEntityUid>";
  16. public void Execute(IConsoleShell shell, string argStr, string[] args)
  17. {
  18. var player = shell.Player;
  19. EntityUid bodyId;
  20. EntityUid? partUid;
  21. switch (args.Length)
  22. {
  23. case 1:
  24. if (player == null)
  25. {
  26. shell.WriteLine($"You need to specify an entity to attach the part to if you aren't a player.\n{Help}");
  27. return;
  28. }
  29. if (player.AttachedEntity == null)
  30. {
  31. shell.WriteLine($"You need to specify an entity to attach the part to if you aren't attached to an entity.\n{Help}");
  32. return;
  33. }
  34. if (!NetEntity.TryParse(args[0], out var partNet) || !_entManager.TryGetEntity(partNet, out partUid))
  35. {
  36. shell.WriteLine($"{args[0]} is not a valid entity uid.");
  37. return;
  38. }
  39. bodyId = player.AttachedEntity.Value;
  40. break;
  41. case 2:
  42. if (!NetEntity.TryParse(args[0], out var entityNet) || !_entManager.TryGetEntity(entityNet, out var entityUid))
  43. {
  44. shell.WriteLine($"{args[0]} is not a valid entity uid.");
  45. return;
  46. }
  47. if (!NetEntity.TryParse(args[1], out partNet) || !_entManager.TryGetEntity(partNet, out partUid))
  48. {
  49. shell.WriteLine($"{args[1]} is not a valid entity uid.");
  50. return;
  51. }
  52. if (!_entManager.EntityExists(entityUid))
  53. {
  54. shell.WriteLine($"{entityUid} is not a valid entity.");
  55. return;
  56. }
  57. bodyId = entityUid.Value;
  58. break;
  59. default:
  60. shell.WriteLine(Help);
  61. return;
  62. }
  63. if (!_entManager.TryGetComponent(bodyId, out BodyComponent? body))
  64. {
  65. shell.WriteLine($"Entity {_entManager.GetComponent<MetaDataComponent>(bodyId).EntityName} with uid {bodyId} does not have a {nameof(BodyComponent)}.");
  66. return;
  67. }
  68. if (!_entManager.EntityExists(partUid))
  69. {
  70. shell.WriteLine($"{partUid} is not a valid entity.");
  71. return;
  72. }
  73. if (!_entManager.TryGetComponent(partUid, out BodyPartComponent? part))
  74. {
  75. shell.WriteLine($"Entity {_entManager.GetComponent<MetaDataComponent>(partUid.Value).EntityName} with uid {args[0]} does not have a {nameof(BodyPartComponent)}.");
  76. return;
  77. }
  78. var bodySystem = _entManager.System<BodySystem>();
  79. if (bodySystem.BodyHasChild(bodyId, partUid.Value, body, part))
  80. {
  81. shell.WriteLine($"Body part {_entManager.GetComponent<MetaDataComponent>(partUid.Value).EntityName} with uid {partUid} is already attached to entity {_entManager.GetComponent<MetaDataComponent>(bodyId).EntityName} with uid {bodyId}");
  82. return;
  83. }
  84. var slotId = $"AttachBodyPartVerb-{partUid}";
  85. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  86. if (body.RootContainer.ContainedEntity != null)
  87. {
  88. bodySystem.AttachPartToRoot(bodyId, partUid.Value, body, part);
  89. }
  90. else
  91. {
  92. var (rootPartId, rootPart) = bodySystem.GetRootPartOrNull(bodyId, body)!.Value;
  93. if (!bodySystem.TryCreatePartSlotAndAttach(rootPartId, slotId, partUid.Value, part.PartType, rootPart, part))
  94. {
  95. shell.WriteError($"Could not create slot {slotId} on entity {_entManager.ToPrettyString(bodyId)}");
  96. return;
  97. }
  98. }
  99. shell.WriteLine($"Attached part {_entManager.ToPrettyString(partUid.Value)} to {_entManager.ToPrettyString(bodyId)}");
  100. }
  101. }
  102. }