Files
RobustToolbox/Robust.Server/Console/Commands/AddComponentCommand.cs
T
DrSmugleafandGitHub cefcad775b Make addcomp and rmcomp give better feedback and case insensitive (#1570)
* Make addcomp and rmcomp case insensitive

* Fix up names

* Make addcomp and rmcomp give better feedback

* Make addcomp and rmcomp less fail happy
2021-02-18 20:01:14 -08:00

62 lines
2.0 KiB
C#

using JetBrains.Annotations;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Robust.Server.Console.Commands
{
[UsedImplicitly]
internal sealed class AddComponentCommand : IConsoleCommand
{
public string Command => "addcomp";
public string Description => "Adds a component to an entity";
public string Help => $"{Command} <uid> <componentName>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 2)
{
shell.WriteLine($"Invalid amount of arguments.\n{Help}");
return;
}
if (!EntityUid.TryParse(args[0], out var uid))
{
shell.WriteLine($"{uid} is not a valid entity uid.");
return;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!entityManager.TryGetEntity(uid, out var entity))
{
shell.WriteLine($"No entity found with id {uid}.");
return;
}
var componentName = args[1];
var compManager = IoCManager.Resolve<IComponentManager>();
var compFactory = IoCManager.Resolve<IComponentFactory>();
if (!compFactory.TryGetRegistration(componentName, out var registration, true))
{
shell.WriteLine($"No component found with name {componentName}.");
return;
}
if (entity.HasComponent(registration.Type))
{
shell.WriteLine($"Entity {entity.Name} already has a {componentName} component.");
}
var component = (Component) compFactory.GetComponent(registration.Type);
component.Owner = entity;
compManager.AddComponent(entity, component);
shell.WriteLine($"Added {componentName} component to entity {entity.Name}.");
}
}
}