From 6fd4467ee0a124dde070df68ec4d7bf9688ff233 Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Fri, 27 Nov 2020 11:28:38 +0100 Subject: [PATCH] Add command to delete all instances of a component (#2580) * Add command to delete all instances of a component * Fix help string * Consistent naming * Use TryGet for registrations * Remove newline Co-authored-by: Metal Gear Sloth --- .../Commands/DeleteComponent.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Content.Server/Administration/Commands/DeleteComponent.cs diff --git a/Content.Server/Administration/Commands/DeleteComponent.cs b/Content.Server/Administration/Commands/DeleteComponent.cs new file mode 100644 index 0000000000..2fbeb0ce91 --- /dev/null +++ b/Content.Server/Administration/Commands/DeleteComponent.cs @@ -0,0 +1,53 @@ +#nullable enable +using Content.Shared.Administration; +using Robust.Server.Interfaces.Console; +using Robust.Server.Interfaces.Player; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.IoC; + +namespace Content.Server.Administration.Commands +{ + [AdminCommand(AdminFlags.Admin)] + public class DeleteComponent : IClientCommand + { + public string Command => "deletecomponent"; + public string Description => "Deletes all instances of the specified component."; + public string Help => $"Usage: {Command} "; + + public void Execute(IConsoleShell shell, IPlayerSession? player, string[] args) + { + switch (args.Length) + { + case 0: + shell.SendText(player, $"Not enough arguments.\n{Help}"); + break; + default: + var name = string.Join(" ", args); + var componentFactory = IoCManager.Resolve(); + var entityManager = IoCManager.Resolve(); + + if (!componentFactory.TryGetRegistration(name, out var registration)) + { + shell.SendText(player, $"No component exists with name {name}."); + break; + } + + var componentType = registration.Type; + var components = entityManager.ComponentManager.GetAllComponents(componentType); + + var i = 0; + + foreach (var component in components) + { + var uid = component.Owner.Uid; + entityManager.ComponentManager.RemoveComponent(uid, component); + i++; + } + + shell.SendText(player, $"Removed {i} components with name {name}."); + + break; + } + } + } +}