AddHandCommand.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Server.Body.Systems;
  4. using Content.Shared.Administration;
  5. using Content.Shared.Body.Components;
  6. using Content.Shared.Body.Part;
  7. using Robust.Shared.Console;
  8. using Robust.Shared.Prototypes;
  9. using Robust.Shared.Random;
  10. namespace Content.Server.Body.Commands
  11. {
  12. [AdminCommand(AdminFlags.Fun)]
  13. sealed class AddHandCommand : IConsoleCommand
  14. {
  15. [Dependency] private readonly IEntityManager _entManager = default!;
  16. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  17. [Dependency] private readonly IRobustRandom _random = default!;
  18. [ValidatePrototypeId<EntityPrototype>]
  19. public const string DefaultHandPrototype = "LeftHandHuman";
  20. public string Command => "addhand";
  21. public string Description => "Adds a hand to your entity.";
  22. public string Help => $"Usage: {Command} <entityUid> <handPrototypeId> / {Command} <entityUid> / {Command} <handPrototypeId> / {Command}";
  23. public void Execute(IConsoleShell shell, string argStr, string[] args)
  24. {
  25. var player = shell.Player;
  26. EntityUid entity;
  27. EntityUid hand;
  28. switch (args.Length)
  29. {
  30. case 0:
  31. if (player == null)
  32. {
  33. shell.WriteLine("Only a player can run this command without arguments.");
  34. return;
  35. }
  36. if (player.AttachedEntity == null)
  37. {
  38. shell.WriteLine("You don't have an entity to add a hand to.");
  39. return;
  40. }
  41. entity = player.AttachedEntity.Value;
  42. hand = _entManager.SpawnEntity(DefaultHandPrototype, _entManager.GetComponent<TransformComponent>(entity).Coordinates);
  43. break;
  44. case 1:
  45. {
  46. if (NetEntity.TryParse(args[0], out var uidNet) && _entManager.TryGetEntity(uidNet, out var uid))
  47. {
  48. if (!_entManager.EntityExists(uid))
  49. {
  50. shell.WriteLine($"No entity found with uid {uid}");
  51. return;
  52. }
  53. entity = uid.Value;
  54. hand = _entManager.SpawnEntity(DefaultHandPrototype, _entManager.GetComponent<TransformComponent>(entity).Coordinates);
  55. }
  56. else
  57. {
  58. if (player == null)
  59. {
  60. shell.WriteLine("You must specify an entity to add a hand to when using this command from the server terminal.");
  61. return;
  62. }
  63. if (player.AttachedEntity == null)
  64. {
  65. shell.WriteLine("You don't have an entity to add a hand to.");
  66. return;
  67. }
  68. entity = player.AttachedEntity.Value;
  69. hand = _entManager.SpawnEntity(args[0], _entManager.GetComponent<TransformComponent>(entity).Coordinates);
  70. }
  71. break;
  72. }
  73. case 2:
  74. {
  75. if (!NetEntity.TryParse(args[0], out var netEnt) || !_entManager.TryGetEntity(netEnt, out var uid))
  76. {
  77. shell.WriteLine($"{args[0]} is not a valid entity uid.");
  78. return;
  79. }
  80. if (!_entManager.EntityExists(uid))
  81. {
  82. shell.WriteLine($"No entity exists with uid {uid}.");
  83. return;
  84. }
  85. entity = uid.Value;
  86. if (!_protoManager.HasIndex<EntityPrototype>(args[1]))
  87. {
  88. shell.WriteLine($"No hand entity exists with id {args[1]}.");
  89. return;
  90. }
  91. hand = _entManager.SpawnEntity(args[1], _entManager.GetComponent<TransformComponent>(entity).Coordinates);
  92. break;
  93. }
  94. default:
  95. shell.WriteLine(Help);
  96. return;
  97. }
  98. if (!_entManager.TryGetComponent(entity, out BodyComponent? body) || body.RootContainer.ContainedEntity == null)
  99. {
  100. var text = $"You have no body{(_random.Prob(0.2f) ? " and you must scream." : ".")}";
  101. shell.WriteLine(text);
  102. return;
  103. }
  104. if (!_entManager.TryGetComponent(hand, out BodyPartComponent? part))
  105. {
  106. shell.WriteLine($"Hand entity {hand} does not have a {nameof(BodyPartComponent)} component.");
  107. return;
  108. }
  109. var bodySystem = _entManager.System<BodySystem>();
  110. var attachAt = bodySystem.GetBodyChildrenOfType(entity, BodyPartType.Arm, body).FirstOrDefault();
  111. if (attachAt == default)
  112. attachAt = bodySystem.GetBodyChildren(entity, body).First();
  113. // Shitmed Change Start
  114. var slotId = $"{part.Symmetry.ToString().ToLower()} {part.GetHashCode().ToString()}";
  115. part.SlotId = part.GetHashCode().ToString();
  116. // Shitmed Change End
  117. if (!bodySystem.TryCreatePartSlotAndAttach(attachAt.Id, slotId, hand, BodyPartType.Hand, attachAt.Component, part))
  118. {
  119. shell.WriteError($"Couldn't create a slot with id {slotId} on entity {_entManager.ToPrettyString(entity)}");
  120. return;
  121. }
  122. shell.WriteLine($"Added hand to entity {_entManager.GetComponent<MetaDataComponent>(entity).EntityName}");
  123. }
  124. }
  125. }