using Robust.Shared.Containers;
namespace Content.Server.Containers
{
public static class ContainerExt
{
///
/// Searches the entity, and the entities containers recursively for a prototypeId
///
/// The entity to search
/// The prototypeId to find
/// Optional entity manager
/// True if entity is, or contains a prototype Id
public static bool ContainsPrototypeRecursive(this EntityUid entityUid, string prototypeId, IEntityManager? entityManager = null)
{
IoCManager.Resolve(ref entityManager);
var metaQuery = entityManager.GetEntityQuery();
var managerQuery = entityManager.GetEntityQuery();
var stack = new Stack();
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;
}
}
}