48 lines
1.7 KiB
C#
48 lines
1.7 KiB
C#
using Robust.Shared.Containers;
|
|
using Robust.Shared.Map;
|
|
|
|
namespace Content.Shared.Containers;
|
|
|
|
public sealed class ContainerFillSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<ContainerFillComponent, MapInitEvent>(OnMapInit);
|
|
}
|
|
|
|
private void OnMapInit(EntityUid uid, ContainerFillComponent component, MapInitEvent args)
|
|
{
|
|
if (!TryComp(uid, out ContainerManagerComponent? containerComp))
|
|
{
|
|
Logger.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} has no {nameof(ContainerManagerComponent)}.");
|
|
return;
|
|
}
|
|
|
|
var xform = Transform(uid);
|
|
var coords = new EntityCoordinates(uid, Vector2.Zero);
|
|
|
|
foreach (var (contaienrId, prototypes) in component.Containers)
|
|
{
|
|
if (!_containerSystem.TryGetContainer(uid, contaienrId, out var container, containerComp))
|
|
{
|
|
Logger.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} is missing a container ({contaienrId}).");
|
|
continue;
|
|
}
|
|
|
|
foreach (var proto in prototypes)
|
|
{
|
|
var ent = Spawn(proto, coords);
|
|
if (!container.Insert(ent, EntityManager, null, xform))
|
|
{
|
|
Logger.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} failed to insert an entity: {ToPrettyString(ent)}.");
|
|
Transform(ent).AttachToGridOrMap();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|