Turn interaction related attempt events into structs (#29168)

* Turn InteractionAttemptEvent into a struct event

* readonly

* GettingInteractedWithAttemptEvent

* ConsciousAttemptEvent
This commit is contained in:
Leon Friedrich
2024-06-19 02:30:41 +12:00
committed by GitHub
parent db30b5e040
commit ee2c115e5b
13 changed files with 80 additions and 37 deletions

View File

@@ -495,7 +495,7 @@ public sealed class DragDropSystem : SharedDragDropSystem
// CanInteract() doesn't support checking a second "target" entity. // CanInteract() doesn't support checking a second "target" entity.
// Doing so manually: // Doing so manually:
var ev = new GettingInteractedWithAttemptEvent(user, dragged); var ev = new GettingInteractedWithAttemptEvent(user, dragged);
RaiseLocalEvent(dragged, ev, true); RaiseLocalEvent(dragged, ref ev);
if (ev.Cancelled) if (ev.Cancelled)
return false; return false;

View File

@@ -17,7 +17,7 @@ public sealed partial class ReplaySpectatorSystem
SubscribeLocalEvent<ReplaySpectatorComponent, UseAttemptEvent>(OnAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, UseAttemptEvent>(OnAttempt);
SubscribeLocalEvent<ReplaySpectatorComponent, PickupAttemptEvent>(OnAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, PickupAttemptEvent>(OnAttempt);
SubscribeLocalEvent<ReplaySpectatorComponent, ThrowAttemptEvent>(OnAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, ThrowAttemptEvent>(OnAttempt);
SubscribeLocalEvent<ReplaySpectatorComponent, InteractionAttemptEvent>(OnAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, InteractionAttemptEvent>(OnInteractAttempt);
SubscribeLocalEvent<ReplaySpectatorComponent, AttackAttemptEvent>(OnAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, AttackAttemptEvent>(OnAttempt);
SubscribeLocalEvent<ReplaySpectatorComponent, DropAttemptEvent>(OnAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, DropAttemptEvent>(OnAttempt);
SubscribeLocalEvent<ReplaySpectatorComponent, IsEquippingAttemptEvent>(OnAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, IsEquippingAttemptEvent>(OnAttempt);
@@ -27,6 +27,11 @@ public sealed partial class ReplaySpectatorSystem
SubscribeLocalEvent<ReplaySpectatorComponent, PullAttemptEvent>(OnPullAttempt); SubscribeLocalEvent<ReplaySpectatorComponent, PullAttemptEvent>(OnPullAttempt);
} }
private void OnInteractAttempt(Entity<ReplaySpectatorComponent> ent, ref InteractionAttemptEvent args)
{
args.Cancelled = true;
}
private void OnAttempt(EntityUid uid, ReplaySpectatorComponent component, CancellableEntityEventArgs args) private void OnAttempt(EntityUid uid, ReplaySpectatorComponent component, CancellableEntityEventArgs args)
{ {
args.Cancel(); args.Cancel();

View File

@@ -70,7 +70,7 @@ namespace Content.Shared.ActionBlocker
return false; return false;
var ev = new InteractionAttemptEvent(user, target); var ev = new InteractionAttemptEvent(user, target);
RaiseLocalEvent(user, ev); RaiseLocalEvent(user, ref ev);
if (ev.Cancelled) if (ev.Cancelled)
return false; return false;
@@ -79,7 +79,7 @@ namespace Content.Shared.ActionBlocker
return true; return true;
var targetEv = new GettingInteractedWithAttemptEvent(user, target); var targetEv = new GettingInteractedWithAttemptEvent(user, target);
RaiseLocalEvent(target.Value, targetEv); RaiseLocalEvent(target.Value, ref targetEv);
return !targetEv.Cancelled; return !targetEv.Cancelled;
} }
@@ -110,7 +110,7 @@ namespace Content.Shared.ActionBlocker
public bool CanConsciouslyPerformAction(EntityUid user) public bool CanConsciouslyPerformAction(EntityUid user)
{ {
var ev = new ConsciousAttemptEvent(user); var ev = new ConsciousAttemptEvent(user);
RaiseLocalEvent(user, ev); RaiseLocalEvent(user, ref ev);
return !ev.Cancelled; return !ev.Cancelled;
} }

View File

@@ -11,6 +11,7 @@ using Content.Shared.Throwing;
namespace Content.Shared.Administration; namespace Content.Shared.Administration;
// TODO deduplicate with BlockMovementComponent
public abstract class SharedAdminFrozenSystem : EntitySystem public abstract class SharedAdminFrozenSystem : EntitySystem
{ {
[Dependency] private readonly ActionBlockerSystem _blocker = default!; [Dependency] private readonly ActionBlockerSystem _blocker = default!;
@@ -23,7 +24,7 @@ public abstract class SharedAdminFrozenSystem : EntitySystem
SubscribeLocalEvent<AdminFrozenComponent, UseAttemptEvent>(OnAttempt); SubscribeLocalEvent<AdminFrozenComponent, UseAttemptEvent>(OnAttempt);
SubscribeLocalEvent<AdminFrozenComponent, PickupAttemptEvent>(OnAttempt); SubscribeLocalEvent<AdminFrozenComponent, PickupAttemptEvent>(OnAttempt);
SubscribeLocalEvent<AdminFrozenComponent, ThrowAttemptEvent>(OnAttempt); SubscribeLocalEvent<AdminFrozenComponent, ThrowAttemptEvent>(OnAttempt);
SubscribeLocalEvent<AdminFrozenComponent, InteractionAttemptEvent>(OnAttempt); SubscribeLocalEvent<AdminFrozenComponent, InteractionAttemptEvent>(OnInteractAttempt);
SubscribeLocalEvent<AdminFrozenComponent, ComponentStartup>(OnStartup); SubscribeLocalEvent<AdminFrozenComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<AdminFrozenComponent, ComponentShutdown>(UpdateCanMove); SubscribeLocalEvent<AdminFrozenComponent, ComponentShutdown>(UpdateCanMove);
SubscribeLocalEvent<AdminFrozenComponent, UpdateCanMoveEvent>(OnUpdateCanMove); SubscribeLocalEvent<AdminFrozenComponent, UpdateCanMoveEvent>(OnUpdateCanMove);
@@ -34,6 +35,11 @@ public abstract class SharedAdminFrozenSystem : EntitySystem
SubscribeLocalEvent<AdminFrozenComponent, SpeakAttemptEvent>(OnSpeakAttempt); SubscribeLocalEvent<AdminFrozenComponent, SpeakAttemptEvent>(OnSpeakAttempt);
} }
private void OnInteractAttempt(Entity<AdminFrozenComponent> ent, ref InteractionAttemptEvent args)
{
args.Cancelled = true;
}
private void OnSpeakAttempt(EntityUid uid, AdminFrozenComponent component, SpeakAttemptEvent args) private void OnSpeakAttempt(EntityUid uid, AdminFrozenComponent component, SpeakAttemptEvent args)
{ {
if (!component.Muted) if (!component.Muted)

View File

@@ -153,7 +153,7 @@ public sealed partial class SleepingSystem : EntitySystem
private void OnConsciousAttempt(Entity<SleepingComponent> ent, ref ConsciousAttemptEvent args) private void OnConsciousAttempt(Entity<SleepingComponent> ent, ref ConsciousAttemptEvent args)
{ {
args.Cancel(); args.Cancelled = true;
} }
private void OnExamined(Entity<SleepingComponent> ent, ref ExaminedEvent args) private void OnExamined(Entity<SleepingComponent> ent, ref ExaminedEvent args)

View File

@@ -79,7 +79,7 @@ namespace Content.Shared.Cuffs
SubscribeLocalEvent<CuffableComponent, PickupAttemptEvent>(CheckAct); SubscribeLocalEvent<CuffableComponent, PickupAttemptEvent>(CheckAct);
SubscribeLocalEvent<CuffableComponent, AttackAttemptEvent>(CheckAct); SubscribeLocalEvent<CuffableComponent, AttackAttemptEvent>(CheckAct);
SubscribeLocalEvent<CuffableComponent, UseAttemptEvent>(CheckAct); SubscribeLocalEvent<CuffableComponent, UseAttemptEvent>(CheckAct);
SubscribeLocalEvent<CuffableComponent, InteractionAttemptEvent>(CheckAct); SubscribeLocalEvent<CuffableComponent, InteractionAttemptEvent>(CheckInteract);
SubscribeLocalEvent<HandcuffComponent, AfterInteractEvent>(OnCuffAfterInteract); SubscribeLocalEvent<HandcuffComponent, AfterInteractEvent>(OnCuffAfterInteract);
SubscribeLocalEvent<HandcuffComponent, MeleeHitEvent>(OnCuffMeleeHit); SubscribeLocalEvent<HandcuffComponent, MeleeHitEvent>(OnCuffMeleeHit);
@@ -87,6 +87,12 @@ namespace Content.Shared.Cuffs
SubscribeLocalEvent<HandcuffComponent, VirtualItemDeletedEvent>(OnCuffVirtualItemDeleted); SubscribeLocalEvent<HandcuffComponent, VirtualItemDeletedEvent>(OnCuffVirtualItemDeleted);
} }
private void CheckInteract(Entity<CuffableComponent> ent, ref InteractionAttemptEvent args)
{
if (!ent.Comp.CanStillInteract)
args.Cancelled = true;
}
private void OnUncuffAttempt(ref UncuffAttemptEvent args) private void OnUncuffAttempt(ref UncuffAttemptEvent args)
{ {
if (args.Cancelled) if (args.Cancelled)

View File

@@ -19,12 +19,18 @@ namespace Content.Shared.Ghost
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<GhostComponent, UseAttemptEvent>(OnAttempt); SubscribeLocalEvent<GhostComponent, UseAttemptEvent>(OnAttempt);
SubscribeLocalEvent<GhostComponent, InteractionAttemptEvent>(OnAttempt); SubscribeLocalEvent<GhostComponent, InteractionAttemptEvent>(OnAttemptInteract);
SubscribeLocalEvent<GhostComponent, EmoteAttemptEvent>(OnAttempt); SubscribeLocalEvent<GhostComponent, EmoteAttemptEvent>(OnAttempt);
SubscribeLocalEvent<GhostComponent, DropAttemptEvent>(OnAttempt); SubscribeLocalEvent<GhostComponent, DropAttemptEvent>(OnAttempt);
SubscribeLocalEvent<GhostComponent, PickupAttemptEvent>(OnAttempt); SubscribeLocalEvent<GhostComponent, PickupAttemptEvent>(OnAttempt);
} }
private void OnAttemptInteract(Entity<GhostComponent> ent, ref InteractionAttemptEvent args)
{
if (!ent.Comp.CanGhostInteract)
args.Cancelled = true;
}
private void OnAttempt(EntityUid uid, GhostComponent component, CancellableEntityEventArgs args) private void OnAttempt(EntityUid uid, GhostComponent component, CancellableEntityEventArgs args)
{ {
if (!component.CanGhostInteract) if (!component.CanGhostInteract)

View File

@@ -3,39 +3,33 @@
/// <summary> /// <summary>
/// Event raised directed at a user to see if they can perform a generic interaction. /// Event raised directed at a user to see if they can perform a generic interaction.
/// </summary> /// </summary>
public sealed class InteractionAttemptEvent : CancellableEntityEventArgs [ByRefEvent]
public struct InteractionAttemptEvent(EntityUid uid, EntityUid? target)
{ {
public InteractionAttemptEvent(EntityUid uid, EntityUid? target) public bool Cancelled;
{ public readonly EntityUid Uid = uid;
Uid = uid; public readonly EntityUid? Target = target;
Target = target;
}
public EntityUid Uid { get; }
public EntityUid? Target { get; }
} }
/// <summary> /// <summary>
/// Raised to determine whether an entity is conscious to perform an action. /// Raised to determine whether an entity is conscious to perform an action.
/// </summary> /// </summary>
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;
} }
/// <summary> /// <summary>
/// Event raised directed at the target entity of an interaction to see if the user is allowed to perform some /// Event raised directed at the target entity of an interaction to see if the user is allowed to perform some
/// generic interaction. /// generic interaction.
/// </summary> /// </summary>
public sealed class GettingInteractedWithAttemptEvent : CancellableEntityEventArgs [ByRefEvent]
public struct GettingInteractedWithAttemptEvent(EntityUid uid, EntityUid? target)
{ {
public GettingInteractedWithAttemptEvent(EntityUid uid, EntityUid? target) public bool Cancelled;
{ public readonly EntityUid Uid = uid;
Uid = uid; public readonly EntityUid? Target = target;
Target = target;
}
public EntityUid Uid { get; }
public EntityUid? Target { get; }
} }
} }

View File

@@ -6,6 +6,7 @@ using Content.Shared.Movement.Events;
namespace Content.Shared.Interaction; namespace Content.Shared.Interaction;
// TODO deduplicate with AdminFrozenComponent
/// <summary> /// <summary>
/// Handles <see cref="BlockMovementComponent"/>, which prevents various /// Handles <see cref="BlockMovementComponent"/>, which prevents various
/// kinds of movement and interactions when attached to an entity. /// kinds of movement and interactions when attached to an entity.
@@ -16,7 +17,7 @@ public partial class SharedInteractionSystem
{ {
SubscribeLocalEvent<BlockMovementComponent, UpdateCanMoveEvent>(OnMoveAttempt); SubscribeLocalEvent<BlockMovementComponent, UpdateCanMoveEvent>(OnMoveAttempt);
SubscribeLocalEvent<BlockMovementComponent, UseAttemptEvent>(CancelEvent); SubscribeLocalEvent<BlockMovementComponent, UseAttemptEvent>(CancelEvent);
SubscribeLocalEvent<BlockMovementComponent, InteractionAttemptEvent>(CancelEvent); SubscribeLocalEvent<BlockMovementComponent, InteractionAttemptEvent>(CancelInteractEvent);
SubscribeLocalEvent<BlockMovementComponent, DropAttemptEvent>(CancelEvent); SubscribeLocalEvent<BlockMovementComponent, DropAttemptEvent>(CancelEvent);
SubscribeLocalEvent<BlockMovementComponent, PickupAttemptEvent>(CancelEvent); SubscribeLocalEvent<BlockMovementComponent, PickupAttemptEvent>(CancelEvent);
SubscribeLocalEvent<BlockMovementComponent, ChangeDirectionAttemptEvent>(CancelEvent); SubscribeLocalEvent<BlockMovementComponent, ChangeDirectionAttemptEvent>(CancelEvent);
@@ -25,6 +26,11 @@ public partial class SharedInteractionSystem
SubscribeLocalEvent<BlockMovementComponent, ComponentShutdown>(OnBlockingShutdown); SubscribeLocalEvent<BlockMovementComponent, ComponentShutdown>(OnBlockingShutdown);
} }
private void CancelInteractEvent(Entity<BlockMovementComponent> ent, ref InteractionAttemptEvent args)
{
args.Cancelled = true;
}
private void OnMoveAttempt(EntityUid uid, BlockMovementComponent component, UpdateCanMoveEvent args) private void OnMoveAttempt(EntityUid uid, BlockMovementComponent component, UpdateCanMoveEvent args)
{ {
if (component.LifeStage > ComponentLifeStage.Running) if (component.LifeStage > ComponentLifeStage.Running)

View File

@@ -31,7 +31,7 @@ public partial class MobStateSystem
SubscribeLocalEvent<MobStateComponent, ChangeDirectionAttemptEvent>(CheckAct); SubscribeLocalEvent<MobStateComponent, ChangeDirectionAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, UseAttemptEvent>(CheckAct); SubscribeLocalEvent<MobStateComponent, UseAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, AttackAttemptEvent>(CheckAct); SubscribeLocalEvent<MobStateComponent, AttackAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, ConsciousAttemptEvent>(CheckAct); SubscribeLocalEvent<MobStateComponent, ConsciousAttemptEvent>(CheckConcious);
SubscribeLocalEvent<MobStateComponent, ThrowAttemptEvent>(CheckAct); SubscribeLocalEvent<MobStateComponent, ThrowAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, SpeakAttemptEvent>(OnSpeakAttempt); SubscribeLocalEvent<MobStateComponent, SpeakAttemptEvent>(OnSpeakAttempt);
SubscribeLocalEvent<MobStateComponent, IsEquippingAttemptEvent>(OnEquipAttempt); SubscribeLocalEvent<MobStateComponent, IsEquippingAttemptEvent>(OnEquipAttempt);
@@ -48,6 +48,17 @@ public partial class MobStateSystem
SubscribeLocalEvent<MobStateComponent, AttemptPacifiedAttackEvent>(OnAttemptPacifiedAttack); SubscribeLocalEvent<MobStateComponent, AttemptPacifiedAttackEvent>(OnAttemptPacifiedAttack);
} }
private void CheckConcious(Entity<MobStateComponent> 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) private void OnStateExitSubscribers(EntityUid target, MobStateComponent component, MobState state)
{ {
switch (state) switch (state)

View File

@@ -7,6 +7,7 @@ using Content.Shared.Movement.Events;
namespace Content.Shared.Puppet; namespace Content.Shared.Puppet;
// TODO deduplicate with BlockMovementComponent
public abstract class SharedVentriloquistPuppetSystem : EntitySystem public abstract class SharedVentriloquistPuppetSystem : EntitySystem
{ {
[Dependency] private readonly ActionBlockerSystem _blocker = default!; [Dependency] private readonly ActionBlockerSystem _blocker = default!;
@@ -15,7 +16,7 @@ public abstract class SharedVentriloquistPuppetSystem : EntitySystem
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<VentriloquistPuppetComponent, UseAttemptEvent>(Cancel); SubscribeLocalEvent<VentriloquistPuppetComponent, UseAttemptEvent>(Cancel);
SubscribeLocalEvent<VentriloquistPuppetComponent, InteractionAttemptEvent>(Cancel); SubscribeLocalEvent<VentriloquistPuppetComponent, InteractionAttemptEvent>(CancelInteract);
SubscribeLocalEvent<VentriloquistPuppetComponent, DropAttemptEvent>(Cancel); SubscribeLocalEvent<VentriloquistPuppetComponent, DropAttemptEvent>(Cancel);
SubscribeLocalEvent<VentriloquistPuppetComponent, PickupAttemptEvent>(Cancel); SubscribeLocalEvent<VentriloquistPuppetComponent, PickupAttemptEvent>(Cancel);
SubscribeLocalEvent<VentriloquistPuppetComponent, UpdateCanMoveEvent>(Cancel); SubscribeLocalEvent<VentriloquistPuppetComponent, UpdateCanMoveEvent>(Cancel);
@@ -24,6 +25,11 @@ public abstract class SharedVentriloquistPuppetSystem : EntitySystem
SubscribeLocalEvent<VentriloquistPuppetComponent, ComponentStartup>(OnStartup); SubscribeLocalEvent<VentriloquistPuppetComponent, ComponentStartup>(OnStartup);
} }
private void CancelInteract(Entity<VentriloquistPuppetComponent> ent, ref InteractionAttemptEvent args)
{
args.Cancelled = true;
}
private void OnStartup(EntityUid uid, VentriloquistPuppetComponent component, ComponentStartup args) private void OnStartup(EntityUid uid, VentriloquistPuppetComponent component, ComponentStartup args)
{ {
_blocker.UpdateCanMove(uid); _blocker.UpdateCanMove(uid);
@@ -33,4 +39,4 @@ public abstract class SharedVentriloquistPuppetSystem : EntitySystem
{ {
args.Cancel(); args.Cancel();
} }
} }

View File

@@ -54,7 +54,7 @@ public abstract class SharedStunSystem : EntitySystem
// Attempt event subscriptions. // Attempt event subscriptions.
SubscribeLocalEvent<StunnedComponent, ChangeDirectionAttemptEvent>(OnAttempt); SubscribeLocalEvent<StunnedComponent, ChangeDirectionAttemptEvent>(OnAttempt);
SubscribeLocalEvent<StunnedComponent, UpdateCanMoveEvent>(OnMoveAttempt); SubscribeLocalEvent<StunnedComponent, UpdateCanMoveEvent>(OnMoveAttempt);
SubscribeLocalEvent<StunnedComponent, InteractionAttemptEvent>(OnAttempt); SubscribeLocalEvent<StunnedComponent, InteractionAttemptEvent>(OnAttemptInteract);
SubscribeLocalEvent<StunnedComponent, UseAttemptEvent>(OnAttempt); SubscribeLocalEvent<StunnedComponent, UseAttemptEvent>(OnAttempt);
SubscribeLocalEvent<StunnedComponent, ThrowAttemptEvent>(OnAttempt); SubscribeLocalEvent<StunnedComponent, ThrowAttemptEvent>(OnAttempt);
SubscribeLocalEvent<StunnedComponent, DropAttemptEvent>(OnAttempt); SubscribeLocalEvent<StunnedComponent, DropAttemptEvent>(OnAttempt);
@@ -65,7 +65,10 @@ public abstract class SharedStunSystem : EntitySystem
SubscribeLocalEvent<MobStateComponent, MobStateChangedEvent>(OnMobStateChanged); SubscribeLocalEvent<MobStateComponent, MobStateChangedEvent>(OnMobStateChanged);
} }
private void OnAttemptInteract(Entity<StunnedComponent> ent, ref InteractionAttemptEvent args)
{
args.Cancelled = true;
}
private void OnMobStateChanged(EntityUid uid, MobStateComponent component, MobStateChangedEvent args) private void OnMobStateChanged(EntityUid uid, MobStateComponent component, MobStateChangedEvent args)
{ {

View File

@@ -45,11 +45,11 @@ namespace Content.Shared.SubFloor
args.Cancelled = true; 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. // No interactions with entities hidden under floor tiles.
if (component.BlockInteractions && component.IsUnderCover) if (component.BlockInteractions && component.IsUnderCover)
args.Cancel(); args.Cancelled = true;
} }
private void OnSubFloorStarted(EntityUid uid, SubFloorHideComponent component, ComponentStartup _) private void OnSubFloorStarted(EntityUid uid, SubFloorHideComponent component, ComponentStartup _)