Files
tbd-station-14/Content.Shared/Trigger/Systems/TriggerOnContainerInteractionSystem.cs
Artxmisery 2b31fa98c9 Add container-related triggers (#39647)
* added triggers for when an entity is inserted or removed from a container, for both the entity and the container

* removed unnecessary comments

* consolidated into one shared system for all four triggers

* consolidated into one shared system for all four triggers

* named it right this time

* made container owner user of got triggers and added guard statements

* container id

* Update Content.Shared/Trigger/Components/Triggers/TriggerOnRemovedFromContainerComponent.cs

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2025-08-15 01:53:48 +02:00

71 lines
2.8 KiB
C#

using Content.Shared.Trigger.Components.Triggers;
using Robust.Shared.Containers;
using Robust.Shared.Timing;
namespace Content.Shared.Trigger.Systems;
/// <summary>
/// System for creating triggers when entities are inserted into or removed from containers.
/// </summary>
public sealed class TriggerOnContainerInteractionSystem : EntitySystem
{
[Dependency] private readonly TriggerSystem _trigger = default!;
[Dependency] private readonly IGameTiming _timing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TriggerOnInsertedIntoContainerComponent, EntInsertedIntoContainerMessage>(OnInsertedIntoContainer);
SubscribeLocalEvent<TriggerOnRemovedFromContainerComponent, EntRemovedFromContainerMessage>(OnRemovedFromContainer);
SubscribeLocalEvent<TriggerOnGotInsertedIntoContainerComponent, EntGotInsertedIntoContainerMessage>(OnGotInsertedIntoContainer);
SubscribeLocalEvent<TriggerOnGotRemovedFromContainerComponent, EntGotRemovedFromContainerMessage>(OnGotRemovedFromContainer);
}
// Used by containers to trigger when entities are inserted into or removed from them
private void OnInsertedIntoContainer(Entity<TriggerOnInsertedIntoContainerComponent> ent, ref EntInsertedIntoContainerMessage args)
{
if (_timing.ApplyingState)
return;
if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
return;
_trigger.Trigger(ent.Owner, args.Entity, ent.Comp.KeyOut);
}
private void OnRemovedFromContainer(Entity<TriggerOnRemovedFromContainerComponent> ent, ref EntRemovedFromContainerMessage args)
{
if (_timing.ApplyingState)
return;
if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
return;
_trigger.Trigger(ent.Owner, args.Entity, ent.Comp.KeyOut);
}
// Used by entities to trigger when they are inserted into or removed from a container
private void OnGotInsertedIntoContainer(Entity<TriggerOnGotInsertedIntoContainerComponent> ent, ref EntGotInsertedIntoContainerMessage args)
{
if (_timing.ApplyingState)
return;
if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
return;
_trigger.Trigger(ent.Owner, args.Container.Owner, ent.Comp.KeyOut);
}
private void OnGotRemovedFromContainer(Entity<TriggerOnGotRemovedFromContainerComponent> ent, ref EntGotRemovedFromContainerMessage args)
{
if (_timing.ApplyingState)
return;
if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
return;
_trigger.Trigger(ent.Owner, args.Container.Owner, ent.Comp.KeyOut);
}
}