| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System.Numerics;
- using Content.Shared.EntityTable;
- using Robust.Shared.Containers;
- using Robust.Shared.Map;
- namespace Content.Shared.Containers;
- public sealed class ContainerFillSystem : EntitySystem
- {
- [Dependency] private readonly SharedContainerSystem _containerSystem = default!;
- [Dependency] private readonly EntityTableSystem _entityTable = default!;
- [Dependency] private readonly SharedTransformSystem _transform = default!;
- public override void Initialize()
- {
- base.Initialize();
- SubscribeLocalEvent<ContainerFillComponent, MapInitEvent>(OnMapInit);
- SubscribeLocalEvent<EntityTableContainerFillComponent, MapInitEvent>(OnTableMapInit);
- }
- private void OnMapInit(EntityUid uid, ContainerFillComponent component, MapInitEvent args)
- {
- if (!TryComp(uid, out ContainerManagerComponent? containerComp))
- 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))
- {
- Log.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 (!_containerSystem.Insert(ent, container, containerXform: xform))
- {
- Log.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} failed to insert an entity: {ToPrettyString(ent)}.");
- _transform.AttachToGridOrMap(ent);
- break;
- }
- }
- }
- }
- private void OnTableMapInit(Entity<EntityTableContainerFillComponent> ent, ref MapInitEvent args)
- {
- if (!TryComp(ent, out ContainerManagerComponent? containerComp))
- return;
- if (TerminatingOrDeleted(ent) || !Exists(ent))
- return;
- var xform = Transform(ent);
- var coords = new EntityCoordinates(ent, Vector2.Zero);
- foreach (var (containerId, table) in ent.Comp.Containers)
- {
- if (!_containerSystem.TryGetContainer(ent, containerId, out var container, containerComp))
- {
- Log.Error($"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} is missing a container ({containerId}).");
- continue;
- }
- var spawns = _entityTable.GetSpawns(table);
- foreach (var proto in spawns)
- {
- var spawn = Spawn(proto, coords);
- if (!_containerSystem.Insert(spawn, container, containerXform: xform))
- {
- Log.Error($"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} failed to insert an entity: {ToPrettyString(spawn)}.");
- _transform.AttachToGridOrMap(spawn);
- break;
- }
- }
- }
- }
- }
|