]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add events for custom action target validation (#27230)
authorDrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Mon, 22 Apr 2024 08:39:50 +0000 (01:39 -0700)
committerGitHub <noreply@github.com>
Mon, 22 Apr 2024 08:39:50 +0000 (01:39 -0700)
Content.Client/UserInterface/Systems/Actions/ActionUIController.cs
Content.Server/Actions/ActionOnInteractSystem.cs
Content.Shared/Actions/Events/ValidateActionEntityTargetEvent.cs [new file with mode: 0644]
Content.Shared/Actions/Events/ValidateActionWorldTargetEvent.cs [new file with mode: 0644]
Content.Shared/Actions/SharedActionsSystem.cs

index 09663ba82cd1478ecf58ab2a9b6df471b90ff298..c79f0f80f96dfe39caa1b7af583eaf561c3f38a0 100644 (file)
@@ -200,7 +200,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
 
         var coords = args.Coordinates;
 
-        if (!_actionsSystem.ValidateWorldTarget(user, coords, action))
+        if (!_actionsSystem.ValidateWorldTarget(user, coords, (actionId, action)))
         {
             // Invalid target.
             if (action.DeselectOnMiss)
@@ -235,7 +235,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
 
         var entity = args.EntityUid;
 
-        if (!_actionsSystem.ValidateEntityTarget(user, entity, action))
+        if (!_actionsSystem.ValidateEntityTarget(user, entity, (actionId, action)))
         {
             if (action.DeselectOnMiss)
                 StopTargeting();
index c9a5f4b5d09f0cb2771a059c1d8cf3e32e7c28c5..eb35d411962c2a8946e1a1def5121e9376dc3d99 100644 (file)
@@ -64,7 +64,7 @@ public sealed class ActionOnInteractSystem : EntitySystem
             var entOptions = GetValidActions<EntityTargetActionComponent>(component.ActionEntities, args.CanReach);
             for (var i = entOptions.Count - 1; i >= 0; i--)
             {
-                var action = entOptions[i].Comp;
+                var action = entOptions[i];
                 if (!_actions.ValidateEntityTarget(args.User, args.Target.Value, action))
                     entOptions.RemoveAt(i);
             }
@@ -88,7 +88,7 @@ public sealed class ActionOnInteractSystem : EntitySystem
         var options = GetValidActions<WorldTargetActionComponent>(component.ActionEntities, args.CanReach);
         for (var i = options.Count - 1; i >= 0; i--)
         {
-            var action = options[i].Comp;
+            var action = options[i];
             if (!_actions.ValidateWorldTarget(args.User, args.ClickLocation, action))
                 options.RemoveAt(i);
         }
diff --git a/Content.Shared/Actions/Events/ValidateActionEntityTargetEvent.cs b/Content.Shared/Actions/Events/ValidateActionEntityTargetEvent.cs
new file mode 100644 (file)
index 0000000..9f22e79
--- /dev/null
@@ -0,0 +1,4 @@
+namespace Content.Shared.Actions.Events;
+
+[ByRefEvent]
+public record struct ValidateActionEntityTargetEvent(EntityUid User, EntityUid Target, bool Cancelled = false);
diff --git a/Content.Shared/Actions/Events/ValidateActionWorldTargetEvent.cs b/Content.Shared/Actions/Events/ValidateActionWorldTargetEvent.cs
new file mode 100644 (file)
index 0000000..43e398a
--- /dev/null
@@ -0,0 +1,6 @@
+using Robust.Shared.Map;
+
+namespace Content.Shared.Actions.Events;
+
+[ByRefEvent]
+public record struct ValidateActionWorldTargetEvent(EntityUid User, EntityCoordinates Target, bool Cancelled = false);
index 9f3fb964100ba612c749cf582bc37fe551e912c2..e1b76f517e46ecf17ee0d479af76d07584c17142 100644 (file)
@@ -8,14 +8,13 @@ using Content.Shared.Hands;
 using Content.Shared.Interaction;
 using Content.Shared.Inventory.Events;
 using Content.Shared.Mind;
-using Content.Shared.Mobs.Components;
+using Content.Shared.Rejuvenate;
 using Robust.Shared.Audio.Systems;
 using Robust.Shared.Containers;
 using Robust.Shared.GameStates;
 using Robust.Shared.Map;
 using Robust.Shared.Timing;
 using Robust.Shared.Utility;
-using Content.Shared.Rejuvenate;
 
 namespace Content.Shared.Actions;
 
@@ -389,7 +388,7 @@ public abstract class SharedActionsSystem : EntitySystem
                 var targetWorldPos = _transformSystem.GetWorldPosition(entityTarget);
                 _rotateToFaceSystem.TryFaceCoordinates(user, targetWorldPos);
 
-                if (!ValidateEntityTarget(user, entityTarget, entityAction))
+                if (!ValidateEntityTarget(user, entityTarget, (actionEnt, entityAction)))
                     return;
 
                 _adminLogger.Add(LogType.Action,
@@ -413,7 +412,7 @@ public abstract class SharedActionsSystem : EntitySystem
                 var entityCoordinatesTarget = GetCoordinates(netCoordinatesTarget);
                 _rotateToFaceSystem.TryFaceCoordinates(user, entityCoordinatesTarget.ToMapPos(EntityManager, _transformSystem));
 
-                if (!ValidateWorldTarget(user, entityCoordinatesTarget, worldAction))
+                if (!ValidateWorldTarget(user, entityCoordinatesTarget, (actionEnt, worldAction)))
                     return;
 
                 _adminLogger.Add(LogType.Action,
@@ -445,7 +444,17 @@ public abstract class SharedActionsSystem : EntitySystem
         PerformAction(user, component, actionEnt, action, performEvent, curTime);
     }
 
-    public bool ValidateEntityTarget(EntityUid user, EntityUid target, EntityTargetActionComponent action)
+    public bool ValidateEntityTarget(EntityUid user, EntityUid target, Entity<EntityTargetActionComponent> actionEnt)
+    {
+        if (!ValidateEntityTargetBase(user, target, actionEnt))
+            return false;
+
+        var ev = new ValidateActionEntityTargetEvent(user, target);
+        RaiseLocalEvent(actionEnt, ref ev);
+        return !ev.Cancelled;
+    }
+
+    private bool ValidateEntityTargetBase(EntityUid user, EntityUid target, EntityTargetActionComponent action)
     {
         if (!target.IsValid() || Deleted(target))
             return false;
@@ -484,7 +493,17 @@ public abstract class SharedActionsSystem : EntitySystem
         return _interactionSystem.CanAccessViaStorage(user, target);
     }
 
-    public bool ValidateWorldTarget(EntityUid user, EntityCoordinates coords, WorldTargetActionComponent action)
+    public bool ValidateWorldTarget(EntityUid user, EntityCoordinates coords, Entity<WorldTargetActionComponent> action)
+    {
+        if (!ValidateWorldTargetBase(user, coords, action))
+            return false;
+
+        var ev = new ValidateActionWorldTargetEvent(user, coords);
+        RaiseLocalEvent(action, ref ev);
+        return !ev.Cancelled;
+    }
+
+    private bool ValidateWorldTargetBase(EntityUid user, EntityCoordinates coords, WorldTargetActionComponent action)
     {
         if (action.CheckCanInteract && !_actionBlockerSystem.CanInteract(user, null))
             return false;