using System.Linq;
using Content.Shared.Materials;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
namespace Content.Server.Materials
{
///
/// Component to store data such as "this object is made out of steel".
/// This is not a storage system for say smelteries.
///
[RegisterComponent]
public sealed class MaterialComponent : Component
{
[ViewVariables]
[DataField("materials", customTypeSerializer:typeof(PrototypeIdDictionarySerializer))]
// ReSharper disable once CollectionNeverUpdated.Local
public readonly Dictionary _materials = new();
public List MaterialIds => _materials.Keys.ToList();
///
/// Returns all materials which make up this entity.
/// This property has an IoC resolve and is generally slow, so be sure to cache the results if needed.
///
[ViewVariables]
public IEnumerable Materials
{
get
{
var prototypeManager = IoCManager.Resolve();
foreach (var id in MaterialIds)
{
if(prototypeManager.TryIndex(id, out var material))
yield return material;
else
Logger.Error($"Material prototype {id} does not exist! Entity: {Owner}");
}
}
}
}
}