Files
tbd-station-14/Content.Server/Containers/ContainerExt.cs
wrexbe 5eaa7149e2 Replace count prototype (#10212)
* Replace count prototype

* Doc improvement
2022-08-01 12:32:42 -07:00

43 lines
1.8 KiB
C#

using Robust.Shared.Containers;
namespace Content.Server.Containers
{
public static class ContainerExt
{
/// <summary>
/// Searches the entity, and the entities containers recursively for a prototypeId
/// </summary>
/// <param name="entityUid">The entity to search</param>
/// <param name="prototypeId">The prototypeId to find</param>
/// <param name="entityManager">Optional entity manager</param>
/// <returns>True if entity is, or contains a prototype Id</returns>
public static bool ContainsPrototypeRecursive(this EntityUid entityUid, string prototypeId, IEntityManager? entityManager = null)
{
IoCManager.Resolve(ref entityManager);
var metaQuery = entityManager.GetEntityQuery<MetaDataComponent>();
var managerQuery = entityManager.GetEntityQuery<ContainerManagerComponent>();
var stack = new Stack<ContainerManagerComponent>();
if (metaQuery.GetComponent(entityUid).EntityPrototype?.ID == prototypeId)
return true;
if (!managerQuery.TryGetComponent(entityUid, out var currentManager))
return false;
do
{
foreach (var container in currentManager.Containers.Values)
{
foreach (var entity in container.ContainedEntities)
{
if (metaQuery.GetComponent(entity).EntityPrototype?.ID == prototypeId)
return true;
if (!managerQuery.TryGetComponent(entity, out var containerManager))
continue;
stack.Push(containerManager);
}
}
} while (stack.TryPop(out currentManager));
return false;
}
}
}