Files
OldThink/Content.Server/Containers/ContainerExt.cs

28 lines
975 B
C#
Raw Normal View History

using Robust.Shared.Containers;
2021-12-03 11:13:58 +01:00
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Containers
{
public static class ContainerExt
{
public static int CountPrototypeOccurencesRecursive(this ContainerManagerComponent mgr, string prototypeId)
{
2021-12-08 17:17:12 +01:00
var entMan = IoCManager.Resolve<IEntityManager>();
int total = 0;
foreach (var container in mgr.GetAllContainers())
{
foreach (var entity in container.ContainedEntities)
{
2021-12-08 17:17:12 +01:00
if (entMan.GetComponent<MetaDataComponent>(entity).EntityPrototype?.ID == prototypeId) total++;
if(!entMan.TryGetComponent<ContainerManagerComponent?>(entity, out var component)) continue;
total += component.CountPrototypeOccurencesRecursive(prototypeId);
}
}
return total;
}
}
}