]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
wielding refactor/fixes (#32188)
authordeltanedas <39013340+deltanedas@users.noreply.github.com>
Sun, 5 Jan 2025 19:06:10 +0000 (19:06 +0000)
committerGitHub <noreply@github.com>
Sun, 5 Jan 2025 19:06:10 +0000 (20:06 +0100)
* refactor wieldable events

* fix inconsitency with wielding and use updated events

* wieldable cosmetic refactoring

* Update Content.Shared/Wieldable/Events.cs

Co-authored-by: Centronias <charlie.t.santos@gmail.com>
* real

Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: Centronias <charlie.t.santos@gmail.com>
Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Content.Shared/Wieldable/BeforeUnwieldEvent.cs [deleted file]
Content.Shared/Wieldable/BeforeWieldEvent.cs [deleted file]
Content.Shared/Wieldable/Events.cs [new file with mode: 0644]
Content.Shared/Wieldable/ItemUnwieldedEvent.cs [deleted file]
Content.Shared/Wieldable/ItemWieldedEvent.cs [deleted file]
Content.Shared/Wieldable/WieldableDoAfterEvent.cs [deleted file]
Content.Shared/Wieldable/WieldableSystem.cs

diff --git a/Content.Shared/Wieldable/BeforeUnwieldEvent.cs b/Content.Shared/Wieldable/BeforeUnwieldEvent.cs
deleted file mode 100644 (file)
index 4328ba5..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-namespace Content.Shared.Wieldable;
-
-public sealed class BeforeUnwieldEvent : CancellableEntityEventArgs
-{
-}
\ No newline at end of file
diff --git a/Content.Shared/Wieldable/BeforeWieldEvent.cs b/Content.Shared/Wieldable/BeforeWieldEvent.cs
deleted file mode 100644 (file)
index 744a009..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-namespace Content.Shared.Wieldable;
-
-public sealed class BeforeWieldEvent : CancellableEntityEventArgs
-{
-}
\ No newline at end of file
diff --git a/Content.Shared/Wieldable/Events.cs b/Content.Shared/Wieldable/Events.cs
new file mode 100644 (file)
index 0000000..fe7e084
--- /dev/null
@@ -0,0 +1,43 @@
+namespace Content.Shared.Wieldable;
+
+/// <summary>
+/// Raised directed on an item when it is wielded.
+/// </summary>
+[ByRefEvent]
+public readonly record struct ItemWieldedEvent(EntityUid User);
+
+/// <summary>
+/// Raised directed on an item that has been unwielded.
+/// Force is whether the item is being forced to be unwielded, or if the player chose to unwield it themselves.
+/// </summary>
+[ByRefEvent]
+public readonly record struct ItemUnwieldedEvent(EntityUid User, bool Force);
+
+/// <summary>
+/// Raised directed on an item before a user tries to wield it.
+/// If this event is cancelled wielding will not happen.
+/// </summary>
+[ByRefEvent]
+public record struct WieldAttemptEvent(EntityUid User, bool Cancelled = false)
+{
+    public void Cancel()
+    {
+        Cancelled = true;
+    }
+}
+
+/// <summary>
+/// Raised directed on an item before a user tries to stop wielding it willingly.
+/// If this event is cancelled unwielding will not happen.
+/// </summary>
+/// <remarks>
+/// This event is not raised if the user is forced to unwield the item.
+/// </remarks>
+[ByRefEvent]
+public record struct UnwieldAttemptEvent(EntityUid User, bool Cancelled = false)
+{
+    public void Cancel()
+    {
+        Cancelled = true;
+    }
+}
diff --git a/Content.Shared/Wieldable/ItemUnwieldedEvent.cs b/Content.Shared/Wieldable/ItemUnwieldedEvent.cs
deleted file mode 100644 (file)
index f980d45..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-namespace Content.Shared.Wieldable;
-
-#region Events
-
-/// <summary>
-///     Raised on the item that has been unwielded.
-/// </summary>
-public sealed class ItemUnwieldedEvent : EntityEventArgs
-{
-    public EntityUid? User;
-    /// <summary>
-    ///     Whether the item is being forced to be unwielded, or if the player chose to unwield it themselves.
-    /// </summary>
-    public bool Force;
-
-    public ItemUnwieldedEvent(EntityUid? user = null, bool force=false)
-    {
-        User = user;
-        Force = force;
-    }
-}
-
-#endregion
diff --git a/Content.Shared/Wieldable/ItemWieldedEvent.cs b/Content.Shared/Wieldable/ItemWieldedEvent.cs
deleted file mode 100644 (file)
index 15e2047..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace Content.Shared.Wieldable;
-
-/// <summary>
-/// Raised directed on an entity when it is wielded.
-/// </summary>
-[ByRefEvent]
-public readonly record struct ItemWieldedEvent;
diff --git a/Content.Shared/Wieldable/WieldableDoAfterEvent.cs b/Content.Shared/Wieldable/WieldableDoAfterEvent.cs
deleted file mode 100644 (file)
index 8431de9..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-using Content.Shared.DoAfter;
-using Robust.Shared.Serialization;
-
-namespace Content.Shared.Wieldable;
-
-[Serializable, NetSerializable]
-public sealed partial class WieldableDoAfterEvent : SimpleDoAfterEvent
-{
-}
index 0a193576bfdf959783e403c93c93b9b7e20ea670..200a50d28f73e0aebb176c1f4a3d40fc814ae104 100644 (file)
@@ -25,16 +25,16 @@ namespace Content.Shared.Wieldable;
 
 public sealed class WieldableSystem : EntitySystem
 {
-    [Dependency] private readonly SharedVirtualItemSystem _virtualItemSystem = default!;
-    [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
-    [Dependency] private readonly SharedItemSystem _itemSystem = default!;
-    [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
-    [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
-    [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
-    [Dependency] private readonly UseDelaySystem _delay = default!;
-    [Dependency] private readonly SharedGunSystem _gun = default!;
     [Dependency] private readonly IGameTiming _timing = default!;
     [Dependency] private readonly INetManager _netManager = default!;
+    [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
+    [Dependency] private readonly SharedAudioSystem _audio = default!;
+    [Dependency] private readonly SharedGunSystem _gun = default!;
+    [Dependency] private readonly SharedHandsSystem _hands = default!;
+    [Dependency] private readonly SharedItemSystem _item = default!;
+    [Dependency] private readonly SharedPopupSystem _popup = default!;
+    [Dependency] private readonly SharedVirtualItemSystem _virtualItem = default!;
+    [Dependency] private readonly UseDelaySystem _delay = default!;
 
     public override void Initialize()
     {
@@ -82,7 +82,7 @@ public sealed class WieldableSystem : EntitySystem
             {
                 component.LastPopup = time;
                 var message = Loc.GetString("wieldable-component-requires", ("item", uid));
-                _popupSystem.PopupClient(message, args.Used, args.User);
+                _popup.PopupClient(message, args.Used, args.User);
             }
         }
     }
@@ -99,8 +99,7 @@ public sealed class WieldableSystem : EntitySystem
 
     private void OnDeselectWieldable(EntityUid uid, WieldableComponent component, HandDeselectedEvent args)
     {
-        if (!component.Wielded ||
-            _handsSystem.EnumerateHands(args.User).Count() > 2)
+        if (_hands.EnumerateHands(args.User).Count() > 2)
             return;
 
         TryUnwield(uid, component, args.User);
@@ -138,7 +137,7 @@ public sealed class WieldableSystem : EntitySystem
         if (args.Hands == null || !args.CanAccess || !args.CanInteract)
             return;
 
-        if (!_handsSystem.IsHolding(args.User, uid, out _, args.Hands))
+        if (!_hands.IsHolding(args.User, uid, out _, args.Hands))
             return;
 
         // TODO VERB TOOLTIPS Make CanWield or some other function return string, set as verb tooltip and disable
@@ -170,28 +169,28 @@ public sealed class WieldableSystem : EntitySystem
     public bool CanWield(EntityUid uid, WieldableComponent component, EntityUid user, bool quiet = false)
     {
         // Do they have enough hands free?
-        if (!EntityManager.TryGetComponent<HandsComponent>(user, out var hands))
+        if (!TryComp<HandsComponent>(user, out var hands))
         {
             if (!quiet)
-                _popupSystem.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
+                _popup.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
             return false;
         }
 
         // Is it.. actually in one of their hands?
-        if (!_handsSystem.IsHolding(user, uid, out _, hands))
+        if (!_hands.IsHolding(user, uid, out _, hands))
         {
             if (!quiet)
-                _popupSystem.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
+                _popup.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
             return false;
         }
 
-        if (_handsSystem.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
+        if (_hands.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
         {
             if (!quiet)
             {
                 var message = Loc.GetString("wieldable-component-not-enough-free-hands",
                     ("number", component.FreeHandsRequired), ("item", uid));
-                _popupSystem.PopupClient(message, user, user);
+                _popup.PopupClient(message, user, user);
             }
             return false;
         }
@@ -209,22 +208,26 @@ public sealed class WieldableSystem : EntitySystem
         if (!CanWield(used, component, user))
             return false;
 
-        var ev = new BeforeWieldEvent();
-        RaiseLocalEvent(used, ev);
+        if (TryComp(used, out UseDelayComponent? useDelay)
+            && !_delay.TryResetDelay((used, useDelay), true))
+            return false;
+
+        var attemptEv = new WieldAttemptEvent(user);
+        RaiseLocalEvent(used, ref attemptEv);
 
-        if (ev.Cancelled)
+        if (attemptEv.Cancelled)
             return false;
 
         if (TryComp<ItemComponent>(used, out var item))
         {
             component.OldInhandPrefix = item.HeldPrefix;
-            _itemSystem.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
+            _item.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
         }
 
-        component.Wielded = true;
+        SetWielded((used, component), true);
 
         if (component.WieldSound != null)
-            _audioSystem.PlayPredicted(component.WieldSound, used, user);
+            _audio.PlayPredicted(component.WieldSound, used, user);
 
         //This section handles spawning the virtual item(s) to occupy the required additional hand(s).
         //Since the client can't currently predict entity spawning, only do this if this is running serverside.
@@ -234,7 +237,7 @@ public sealed class WieldableSystem : EntitySystem
             var virtuals = new List<EntityUid>();
             for (var i = 0; i < component.FreeHandsRequired; i++)
             {
-                if (_virtualItemSystem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
+                if (_virtualItem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
                 {
                     virtuals.Add(virtualItem.Value);
                     continue;
@@ -249,78 +252,79 @@ public sealed class WieldableSystem : EntitySystem
             }
         }
 
-        if (TryComp(used, out UseDelayComponent? useDelay)
-            && !_delay.TryResetDelay((used, useDelay), true))
-            return false;
-
         var selfMessage = Loc.GetString("wieldable-component-successful-wield", ("item", used));
         var othersMessage = Loc.GetString("wieldable-component-successful-wield-other", ("user", Identity.Entity(user, EntityManager)), ("item", used));
-        _popupSystem.PopupPredicted(selfMessage, othersMessage, user, user);
+        _popup.PopupPredicted(selfMessage, othersMessage, user, user);
 
-        var targEv = new ItemWieldedEvent();
-        RaiseLocalEvent(used, ref targEv);
+        var ev = new ItemWieldedEvent(user);
+        RaiseLocalEvent(used, ref ev);
 
-        Dirty(used, component);
         return true;
     }
 
     /// <summary>
-    ///     Attempts to unwield an item, with no DoAfter.
+    ///     Attempts to unwield an item, with no use delay.
     /// </summary>
     /// <returns>True if the attempt wasn't blocked.</returns>
-    public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user)
+    public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user, bool force = false)
     {
-        var ev = new BeforeUnwieldEvent();
-        RaiseLocalEvent(used, ev);
+        if (!component.Wielded)
+            return false; // already unwielded
 
-        if (ev.Cancelled)
-            return false;
+        if (!force)
+        {
+            var attemptEv = new UnwieldAttemptEvent(user);
+            RaiseLocalEvent(used, ref attemptEv);
+
+            if (attemptEv.Cancelled)
+                return false;
+        }
 
-        component.Wielded = false;
-        var targEv = new ItemUnwieldedEvent(user);
+        SetWielded((used, component), false);
 
-        RaiseLocalEvent(used, targEv);
+        var ev = new ItemUnwieldedEvent(user, force);
+        RaiseLocalEvent(used, ref ev);
         return true;
     }
 
+    /// <summary>
+    /// Sets wielded without doing any checks.
+    /// </summary>
+    private void SetWielded(Entity<WieldableComponent> ent, bool wielded)
+    {
+        ent.Comp.Wielded = wielded;
+        Dirty(ent);
+        _appearance.SetData(ent, WieldableVisuals.Wielded, wielded);
+    }
+
     private void OnItemUnwielded(EntityUid uid, WieldableComponent component, ItemUnwieldedEvent args)
     {
-        if (args.User == null)
-            return;
+        _item.SetHeldPrefix(uid, component.OldInhandPrefix);
 
-        if (TryComp<ItemComponent>(uid, out var item))
-        {
-            _itemSystem.SetHeldPrefix(uid, component.OldInhandPrefix, component: item);
-        }
+        var user = args.User;
+        _virtualItem.DeleteInHandsMatching(user, uid);
 
         if (!args.Force) // don't play sound/popup if this was a forced unwield
         {
             if (component.UnwieldSound != null)
-                _audioSystem.PlayPredicted(component.UnwieldSound, uid, args.User);
+                _audio.PlayPredicted(component.UnwieldSound, uid, user);
 
             var selfMessage = Loc.GetString("wieldable-component-failed-wield", ("item", uid));
-            var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User.Value, EntityManager)), ("item", uid));
-            _popupSystem.PopupPredicted(selfMessage, othersMessage, args.User.Value, args.User.Value);
+            var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User, EntityManager)), ("item", uid));
+            _popup.PopupPredicted(selfMessage, othersMessage, user, user);
         }
-
-        _appearance.SetData(uid, WieldableVisuals.Wielded, false);
-
-        Dirty(uid, component);
-        _virtualItemSystem.DeleteInHandsMatching(args.User.Value, uid);
     }
 
     private void OnItemLeaveHand(EntityUid uid, WieldableComponent component, GotUnequippedHandEvent args)
     {
-        if (!component.Wielded || uid != args.Unequipped)
-            return;
-
-        RaiseLocalEvent(uid, new ItemUnwieldedEvent(args.User, force: true), true);
+        if (uid == args.Unequipped)
+            TryUnwield(uid, component, args.User, force: true);
     }
 
     private void OnVirtualItemDeleted(EntityUid uid, WieldableComponent component, VirtualItemDeletedEvent args)
     {
-        if (args.BlockingEntity == uid && component.Wielded)
-            TryUnwield(args.BlockingEntity, component, args.User);
+        if (args.BlockingEntity == uid)
+            TryUnwield(uid, component, args.User, force: true);
     }
 
     private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args)