Files
tbd-station-14/Content.Server/Commands/FindEntitiesWithComponents.cs
Acruid 8b5d66050a Console Unify API Changes (#3059)
* Remove unused IChatCommand.

* Lots of refactoring into a shared context.

* Removed ICommonSession from server concmd Execute.

* Added argStr parameter to concmd execute.

* The execute function of client concmds now returns void, use the new shell.RemoteExecuteCommand function to forward commands.

* Finally move shells and commands into shared.

* Console commands can now be registered directly without a class in a shared context.

* Engine API Changes.

* Repair rebase damage.

* Update Submodule.
2021-02-01 16:49:43 -08:00

74 lines
2.3 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Server.Interfaces.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Commands
{
[AdminCommand(AdminFlags.Mapping)]
public class FindEntitiesWithComponents : IConsoleCommand
{
public string Command => "findentitieswithcomponents";
public string Description => "Finds entities with all of the specified components.";
public string Help => $"{Command} <componentName1> <componentName2>...";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length == 0)
{
shell.WriteLine($"Invalid amount of arguments: {args.Length}.\n{Help}");
return;
}
var components = new List<Type>();
var componentFactory = IoCManager.Resolve<IComponentFactory>();
var invalidArgs = new List<string>();
foreach (var arg in args)
{
if (!componentFactory.TryGetRegistration(arg, out var registration))
{
invalidArgs.Add(arg);
continue;
}
components.Add(registration.Type);
}
if (invalidArgs.Count > 0)
{
shell.WriteLine($"No component found for component names: {string.Join(", ", invalidArgs)}");
return;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
var query = new MultipleTypeEntityQuery(components);
var entityIds = new HashSet<string>();
foreach (var entity in entityManager.GetEntities(query))
{
if (entity.Prototype == null)
{
continue;
}
entityIds.Add(entity.Prototype.ID);
}
if (entityIds.Count == 0)
{
shell.WriteLine($"No entities found with components {string.Join(", ", args)}.");
return;
}
shell.WriteLine($"{entityIds.Count} entities found:\n{string.Join("\n", entityIds)}");
}
}
}