Map-Init guidebook entities. (#14823)
This commit is contained in:
47
Content.Shared/Containers/ContainerFillSystem.cs
Normal file
47
Content.Shared/Containers/ContainerFillSystem.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user