From ee2c115e5b6cd0e0e8126bd81166c0fcb23a7b9e Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Wed, 19 Jun 2024 02:30:41 +1200 Subject: [PATCH] Turn interaction related attempt events into structs (#29168) * Turn InteractionAttemptEvent into a struct event * readonly * GettingInteractedWithAttemptEvent * ConsciousAttemptEvent --- Content.Client/Interaction/DragDropSystem.cs | 2 +- .../ReplaySpectatorSystem.Blockers.cs | 7 +++- .../ActionBlocker/ActionBlockerSystem.cs | 6 ++-- .../Administration/SharedAdminFrozenSystem.cs | 8 ++++- Content.Shared/Bed/Sleep/SleepingSystem.cs | 2 +- Content.Shared/Cuffs/SharedCuffableSystem.cs | 8 ++++- Content.Shared/Ghost/SharedGhostSystem.cs | 8 ++++- .../Events/InteractionAttemptEvent.cs | 34 ++++++++----------- .../SharedInteractionSystem.Blocking.cs | 8 ++++- .../Systems/MobStateSystem.Subscribers.cs | 13 ++++++- .../Puppet/SharedVentriloquistPuppetSystem.cs | 10 ++++-- Content.Shared/Stunnable/SharedStunSystem.cs | 7 ++-- .../SubFloor/SharedSubFloorHideSystem.cs | 4 +-- 13 files changed, 80 insertions(+), 37 deletions(-) diff --git a/Content.Client/Interaction/DragDropSystem.cs b/Content.Client/Interaction/DragDropSystem.cs index 8baa4d15fe..d249766bbc 100644 --- a/Content.Client/Interaction/DragDropSystem.cs +++ b/Content.Client/Interaction/DragDropSystem.cs @@ -495,7 +495,7 @@ public sealed class DragDropSystem : SharedDragDropSystem // CanInteract() doesn't support checking a second "target" entity. // Doing so manually: var ev = new GettingInteractedWithAttemptEvent(user, dragged); - RaiseLocalEvent(dragged, ev, true); + RaiseLocalEvent(dragged, ref ev); if (ev.Cancelled) return false; diff --git a/Content.Client/Replay/Spectator/ReplaySpectatorSystem.Blockers.cs b/Content.Client/Replay/Spectator/ReplaySpectatorSystem.Blockers.cs index 2fa862f3df..99d85350b5 100644 --- a/Content.Client/Replay/Spectator/ReplaySpectatorSystem.Blockers.cs +++ b/Content.Client/Replay/Spectator/ReplaySpectatorSystem.Blockers.cs @@ -17,7 +17,7 @@ public sealed partial class ReplaySpectatorSystem SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); - SubscribeLocalEvent(OnAttempt); + SubscribeLocalEvent(OnInteractAttempt); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); @@ -27,6 +27,11 @@ public sealed partial class ReplaySpectatorSystem SubscribeLocalEvent(OnPullAttempt); } + private void OnInteractAttempt(Entity ent, ref InteractionAttemptEvent args) + { + args.Cancelled = true; + } + private void OnAttempt(EntityUid uid, ReplaySpectatorComponent component, CancellableEntityEventArgs args) { args.Cancel(); diff --git a/Content.Shared/ActionBlocker/ActionBlockerSystem.cs b/Content.Shared/ActionBlocker/ActionBlockerSystem.cs index f1c77fda43..005c7dc78e 100644 --- a/Content.Shared/ActionBlocker/ActionBlockerSystem.cs +++ b/Content.Shared/ActionBlocker/ActionBlockerSystem.cs @@ -70,7 +70,7 @@ namespace Content.Shared.ActionBlocker return false; var ev = new InteractionAttemptEvent(user, target); - RaiseLocalEvent(user, ev); + RaiseLocalEvent(user, ref ev); if (ev.Cancelled) return false; @@ -79,7 +79,7 @@ namespace Content.Shared.ActionBlocker return true; var targetEv = new GettingInteractedWithAttemptEvent(user, target); - RaiseLocalEvent(target.Value, targetEv); + RaiseLocalEvent(target.Value, ref targetEv); return !targetEv.Cancelled; } @@ -110,7 +110,7 @@ namespace Content.Shared.ActionBlocker public bool CanConsciouslyPerformAction(EntityUid user) { var ev = new ConsciousAttemptEvent(user); - RaiseLocalEvent(user, ev); + RaiseLocalEvent(user, ref ev); return !ev.Cancelled; } diff --git a/Content.Shared/Administration/SharedAdminFrozenSystem.cs b/Content.Shared/Administration/SharedAdminFrozenSystem.cs index 2fa22e0005..259df2bdf2 100644 --- a/Content.Shared/Administration/SharedAdminFrozenSystem.cs +++ b/Content.Shared/Administration/SharedAdminFrozenSystem.cs @@ -11,6 +11,7 @@ using Content.Shared.Throwing; namespace Content.Shared.Administration; +// TODO deduplicate with BlockMovementComponent public abstract class SharedAdminFrozenSystem : EntitySystem { [Dependency] private readonly ActionBlockerSystem _blocker = default!; @@ -23,7 +24,7 @@ public abstract class SharedAdminFrozenSystem : EntitySystem SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); - SubscribeLocalEvent(OnAttempt); + SubscribeLocalEvent(OnInteractAttempt); SubscribeLocalEvent(OnStartup); SubscribeLocalEvent(UpdateCanMove); SubscribeLocalEvent(OnUpdateCanMove); @@ -34,6 +35,11 @@ public abstract class SharedAdminFrozenSystem : EntitySystem SubscribeLocalEvent(OnSpeakAttempt); } + private void OnInteractAttempt(Entity ent, ref InteractionAttemptEvent args) + { + args.Cancelled = true; + } + private void OnSpeakAttempt(EntityUid uid, AdminFrozenComponent component, SpeakAttemptEvent args) { if (!component.Muted) diff --git a/Content.Shared/Bed/Sleep/SleepingSystem.cs b/Content.Shared/Bed/Sleep/SleepingSystem.cs index aac3e7bb18..6008e301cf 100644 --- a/Content.Shared/Bed/Sleep/SleepingSystem.cs +++ b/Content.Shared/Bed/Sleep/SleepingSystem.cs @@ -153,7 +153,7 @@ public sealed partial class SleepingSystem : EntitySystem private void OnConsciousAttempt(Entity ent, ref ConsciousAttemptEvent args) { - args.Cancel(); + args.Cancelled = true; } private void OnExamined(Entity ent, ref ExaminedEvent args) diff --git a/Content.Shared/Cuffs/SharedCuffableSystem.cs b/Content.Shared/Cuffs/SharedCuffableSystem.cs index 0e506f938e..be169deb0e 100644 --- a/Content.Shared/Cuffs/SharedCuffableSystem.cs +++ b/Content.Shared/Cuffs/SharedCuffableSystem.cs @@ -79,7 +79,7 @@ namespace Content.Shared.Cuffs SubscribeLocalEvent(CheckAct); SubscribeLocalEvent(CheckAct); SubscribeLocalEvent(CheckAct); - SubscribeLocalEvent(CheckAct); + SubscribeLocalEvent(CheckInteract); SubscribeLocalEvent(OnCuffAfterInteract); SubscribeLocalEvent(OnCuffMeleeHit); @@ -87,6 +87,12 @@ namespace Content.Shared.Cuffs SubscribeLocalEvent(OnCuffVirtualItemDeleted); } + private void CheckInteract(Entity ent, ref InteractionAttemptEvent args) + { + if (!ent.Comp.CanStillInteract) + args.Cancelled = true; + } + private void OnUncuffAttempt(ref UncuffAttemptEvent args) { if (args.Cancelled) diff --git a/Content.Shared/Ghost/SharedGhostSystem.cs b/Content.Shared/Ghost/SharedGhostSystem.cs index ad8b86f7dd..6e62bee131 100644 --- a/Content.Shared/Ghost/SharedGhostSystem.cs +++ b/Content.Shared/Ghost/SharedGhostSystem.cs @@ -19,12 +19,18 @@ namespace Content.Shared.Ghost { base.Initialize(); SubscribeLocalEvent(OnAttempt); - SubscribeLocalEvent(OnAttempt); + SubscribeLocalEvent(OnAttemptInteract); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); } + private void OnAttemptInteract(Entity ent, ref InteractionAttemptEvent args) + { + if (!ent.Comp.CanGhostInteract) + args.Cancelled = true; + } + private void OnAttempt(EntityUid uid, GhostComponent component, CancellableEntityEventArgs args) { if (!component.CanGhostInteract) diff --git a/Content.Shared/Interaction/Events/InteractionAttemptEvent.cs b/Content.Shared/Interaction/Events/InteractionAttemptEvent.cs index 0024811c36..a04c053635 100644 --- a/Content.Shared/Interaction/Events/InteractionAttemptEvent.cs +++ b/Content.Shared/Interaction/Events/InteractionAttemptEvent.cs @@ -3,39 +3,33 @@ /// /// Event raised directed at a user to see if they can perform a generic interaction. /// - public sealed class InteractionAttemptEvent : CancellableEntityEventArgs + [ByRefEvent] + public struct InteractionAttemptEvent(EntityUid uid, EntityUid? target) { - public InteractionAttemptEvent(EntityUid uid, EntityUid? target) - { - Uid = uid; - Target = target; - } - - public EntityUid Uid { get; } - public EntityUid? Target { get; } + public bool Cancelled; + public readonly EntityUid Uid = uid; + public readonly EntityUid? Target = target; } /// /// Raised to determine whether an entity is conscious to perform an action. /// - public sealed class ConsciousAttemptEvent(EntityUid Uid) : CancellableEntityEventArgs + [ByRefEvent] + public struct ConsciousAttemptEvent(EntityUid uid) { - public EntityUid Uid { get; } = Uid; + public bool Cancelled; + public readonly EntityUid Uid = uid; } /// /// Event raised directed at the target entity of an interaction to see if the user is allowed to perform some /// generic interaction. /// - public sealed class GettingInteractedWithAttemptEvent : CancellableEntityEventArgs + [ByRefEvent] + public struct GettingInteractedWithAttemptEvent(EntityUid uid, EntityUid? target) { - public GettingInteractedWithAttemptEvent(EntityUid uid, EntityUid? target) - { - Uid = uid; - Target = target; - } - - public EntityUid Uid { get; } - public EntityUid? Target { get; } + public bool Cancelled; + public readonly EntityUid Uid = uid; + public readonly EntityUid? Target = target; } } diff --git a/Content.Shared/Interaction/SharedInteractionSystem.Blocking.cs b/Content.Shared/Interaction/SharedInteractionSystem.Blocking.cs index 9a84789adf..a682bf9815 100644 --- a/Content.Shared/Interaction/SharedInteractionSystem.Blocking.cs +++ b/Content.Shared/Interaction/SharedInteractionSystem.Blocking.cs @@ -6,6 +6,7 @@ using Content.Shared.Movement.Events; namespace Content.Shared.Interaction; +// TODO deduplicate with AdminFrozenComponent /// /// Handles , which prevents various /// kinds of movement and interactions when attached to an entity. @@ -16,7 +17,7 @@ public partial class SharedInteractionSystem { SubscribeLocalEvent(OnMoveAttempt); SubscribeLocalEvent(CancelEvent); - SubscribeLocalEvent(CancelEvent); + SubscribeLocalEvent(CancelInteractEvent); SubscribeLocalEvent(CancelEvent); SubscribeLocalEvent(CancelEvent); SubscribeLocalEvent(CancelEvent); @@ -25,6 +26,11 @@ public partial class SharedInteractionSystem SubscribeLocalEvent(OnBlockingShutdown); } + private void CancelInteractEvent(Entity ent, ref InteractionAttemptEvent args) + { + args.Cancelled = true; + } + private void OnMoveAttempt(EntityUid uid, BlockMovementComponent component, UpdateCanMoveEvent args) { if (component.LifeStage > ComponentLifeStage.Running) diff --git a/Content.Shared/Mobs/Systems/MobStateSystem.Subscribers.cs b/Content.Shared/Mobs/Systems/MobStateSystem.Subscribers.cs index 08b351e61e..155cfede01 100644 --- a/Content.Shared/Mobs/Systems/MobStateSystem.Subscribers.cs +++ b/Content.Shared/Mobs/Systems/MobStateSystem.Subscribers.cs @@ -31,7 +31,7 @@ public partial class MobStateSystem SubscribeLocalEvent(CheckAct); SubscribeLocalEvent(CheckAct); SubscribeLocalEvent(CheckAct); - SubscribeLocalEvent(CheckAct); + SubscribeLocalEvent(CheckConcious); SubscribeLocalEvent(CheckAct); SubscribeLocalEvent(OnSpeakAttempt); SubscribeLocalEvent(OnEquipAttempt); @@ -48,6 +48,17 @@ public partial class MobStateSystem SubscribeLocalEvent(OnAttemptPacifiedAttack); } + private void CheckConcious(Entity ent, ref ConsciousAttemptEvent args) + { + switch (ent.Comp.CurrentState) + { + case MobState.Dead: + case MobState.Critical: + args.Cancelled = true; + break; + } + } + private void OnStateExitSubscribers(EntityUid target, MobStateComponent component, MobState state) { switch (state) diff --git a/Content.Shared/Puppet/SharedVentriloquistPuppetSystem.cs b/Content.Shared/Puppet/SharedVentriloquistPuppetSystem.cs index 430c2b1b17..e3fa21ed37 100644 --- a/Content.Shared/Puppet/SharedVentriloquistPuppetSystem.cs +++ b/Content.Shared/Puppet/SharedVentriloquistPuppetSystem.cs @@ -7,6 +7,7 @@ using Content.Shared.Movement.Events; namespace Content.Shared.Puppet; +// TODO deduplicate with BlockMovementComponent public abstract class SharedVentriloquistPuppetSystem : EntitySystem { [Dependency] private readonly ActionBlockerSystem _blocker = default!; @@ -15,7 +16,7 @@ public abstract class SharedVentriloquistPuppetSystem : EntitySystem { base.Initialize(); SubscribeLocalEvent(Cancel); - SubscribeLocalEvent(Cancel); + SubscribeLocalEvent(CancelInteract); SubscribeLocalEvent(Cancel); SubscribeLocalEvent(Cancel); SubscribeLocalEvent(Cancel); @@ -24,6 +25,11 @@ public abstract class SharedVentriloquistPuppetSystem : EntitySystem SubscribeLocalEvent(OnStartup); } + private void CancelInteract(Entity ent, ref InteractionAttemptEvent args) + { + args.Cancelled = true; + } + private void OnStartup(EntityUid uid, VentriloquistPuppetComponent component, ComponentStartup args) { _blocker.UpdateCanMove(uid); @@ -33,4 +39,4 @@ public abstract class SharedVentriloquistPuppetSystem : EntitySystem { args.Cancel(); } -} \ No newline at end of file +} diff --git a/Content.Shared/Stunnable/SharedStunSystem.cs b/Content.Shared/Stunnable/SharedStunSystem.cs index 9190427d32..092da8fe5a 100644 --- a/Content.Shared/Stunnable/SharedStunSystem.cs +++ b/Content.Shared/Stunnable/SharedStunSystem.cs @@ -54,7 +54,7 @@ public abstract class SharedStunSystem : EntitySystem // Attempt event subscriptions. SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnMoveAttempt); - SubscribeLocalEvent(OnAttempt); + SubscribeLocalEvent(OnAttemptInteract); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); SubscribeLocalEvent(OnAttempt); @@ -65,7 +65,10 @@ public abstract class SharedStunSystem : EntitySystem SubscribeLocalEvent(OnMobStateChanged); } - + private void OnAttemptInteract(Entity ent, ref InteractionAttemptEvent args) + { + args.Cancelled = true; + } private void OnMobStateChanged(EntityUid uid, MobStateComponent component, MobStateChangedEvent args) { diff --git a/Content.Shared/SubFloor/SharedSubFloorHideSystem.cs b/Content.Shared/SubFloor/SharedSubFloorHideSystem.cs index ba78ff651f..cebc84ecb9 100644 --- a/Content.Shared/SubFloor/SharedSubFloorHideSystem.cs +++ b/Content.Shared/SubFloor/SharedSubFloorHideSystem.cs @@ -45,11 +45,11 @@ namespace Content.Shared.SubFloor args.Cancelled = true; } - private void OnInteractionAttempt(EntityUid uid, SubFloorHideComponent component, GettingInteractedWithAttemptEvent args) + private void OnInteractionAttempt(EntityUid uid, SubFloorHideComponent component, ref GettingInteractedWithAttemptEvent args) { // No interactions with entities hidden under floor tiles. if (component.BlockInteractions && component.IsUnderCover) - args.Cancel(); + args.Cancelled = true; } private void OnSubFloorStarted(EntityUid uid, SubFloorHideComponent component, ComponentStartup _) -- 2.51.2