1
0

DeleteComponent.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Content.Shared.Administration;
  2. using Robust.Shared.Console;
  3. namespace Content.Server.Administration.Commands
  4. {
  5. [AdminCommand(AdminFlags.Spawn)]
  6. public sealed class DeleteComponent : IConsoleCommand
  7. {
  8. public string Command => "deletecomponent";
  9. public string Description => "Deletes all instances of the specified component.";
  10. public string Help => $"Usage: {Command} <name>";
  11. public void Execute(IConsoleShell shell, string argStr, string[] args)
  12. {
  13. switch (args.Length)
  14. {
  15. case 0:
  16. shell.WriteLine($"Not enough arguments.\n{Help}");
  17. break;
  18. default:
  19. var name = string.Join(" ", args);
  20. var componentFactory = IoCManager.Resolve<IComponentFactory>();
  21. var entityManager = IoCManager.Resolve<IEntityManager>();
  22. if (!componentFactory.TryGetRegistration(name, out var registration))
  23. {
  24. shell.WriteLine($"No component exists with name {name}.");
  25. break;
  26. }
  27. var componentType = registration.Type;
  28. var components = entityManager.GetAllComponents(componentType, true);
  29. var i = 0;
  30. foreach (var (uid, component) in components)
  31. {
  32. entityManager.RemoveComponent(uid, component);
  33. i++;
  34. }
  35. shell.WriteLine($"Removed {i} components with name {name}.");
  36. break;
  37. }
  38. }
  39. }
  40. }