From c1ef48cee9121da87dd1c88ff68ae9e7355c64d1 Mon Sep 17 00:00:00 2001 From: Menshin Date: Sun, 23 Apr 2023 21:38:52 +0200 Subject: [PATCH] Raise hand (un)equipped events on container insert/remove (#15664) --- Content.Client/Hands/Systems/HandsSystem.cs | 15 +++++----- Content.Server/Hands/Systems/HandsSystem.cs | 6 ++-- Content.Shared/Actions/SharedActionsSystem.cs | 7 +++-- .../EntitySystems/SharedHandsSystem.Drop.cs | 25 ++++++++++++---- .../EntitySystems/SharedHandsSystem.Pickup.cs | 30 ++++++++++++------- .../Hands/EntitySystems/SharedHandsSystem.cs | 2 ++ .../Inventory/InventorySystem.Equip.cs | 4 --- 7 files changed, 56 insertions(+), 33 deletions(-) diff --git a/Content.Client/Hands/Systems/HandsSystem.cs b/Content.Client/Hands/Systems/HandsSystem.cs index 062958193b..83465dea6f 100644 --- a/Content.Client/Hands/Systems/HandsSystem.cs +++ b/Content.Client/Hands/Systems/HandsSystem.cs @@ -43,9 +43,6 @@ namespace Content.Client.Hands.Systems { base.Initialize(); - SubscribeLocalEvent(HandleItemRemoved); - SubscribeLocalEvent(HandleItemAdded); - SubscribeLocalEvent(HandlePlayerAttached); SubscribeLocalEvent(HandlePlayerDetached); SubscribeLocalEvent(HandleCompAdd); @@ -253,9 +250,11 @@ namespace Content.Client.Hands.Systems #region visuals - private void HandleItemAdded(EntityUid uid, HandsComponent handComp, ContainerModifiedMessage args) + protected override void HandleEntityInserted(EntityUid uid, HandsComponent hands, EntInsertedIntoContainerMessage args) { - if (!handComp.Hands.TryGetValue(args.Container.ID, out var hand)) + base.HandleEntityInserted(uid, hands, args); + + if (!hands.Hands.TryGetValue(args.Container.ID, out var hand)) return; UpdateHandVisuals(uid, args.Entity, hand); _stripSys.UpdateUi(uid); @@ -269,9 +268,11 @@ namespace Content.Client.Hands.Systems OnPlayerHandBlocked?.Invoke(hand.Name); } - private void HandleItemRemoved(EntityUid uid, HandsComponent handComp, ContainerModifiedMessage args) + protected override void HandleEntityRemoved(EntityUid uid, HandsComponent hands, EntRemovedFromContainerMessage args) { - if (!handComp.Hands.TryGetValue(args.Container.ID, out var hand)) + base.HandleEntityRemoved(uid, hands, args); + + if (!hands.Hands.TryGetValue(args.Container.ID, out var hand)) return; UpdateHandVisuals(uid, args.Entity, hand); _stripSys.UpdateUi(uid); diff --git a/Content.Server/Hands/Systems/HandsSystem.cs b/Content.Server/Hands/Systems/HandsSystem.cs index 92b91ab75f..84605b8e7c 100644 --- a/Content.Server/Hands/Systems/HandsSystem.cs +++ b/Content.Server/Hands/Systems/HandsSystem.cs @@ -54,8 +54,6 @@ namespace Content.Server.Hands.Systems SubscribeLocalEvent(HandlePullStarted); SubscribeLocalEvent(HandlePullStopped); - SubscribeLocalEvent(HandleEntityRemoved); - SubscribeLocalEvent(HandleBodyPartAdded); SubscribeLocalEvent(HandleBodyPartRemoved); @@ -109,8 +107,10 @@ namespace Content.Server.Hands.Systems RaiseNetworkEvent(new PickupAnimationEvent(item, initialPosition, finalPosition), filter); } - private void HandleEntityRemoved(EntityUid uid, HandsComponent component, EntRemovedFromContainerMessage args) + protected override void HandleEntityRemoved(EntityUid uid, HandsComponent hands, EntRemovedFromContainerMessage args) { + base.HandleEntityRemoved(uid, hands, args); + if (!Deleted(args.Entity) && TryComp(args.Entity, out HandVirtualItemComponent? @virtual)) _virtualSystem.Delete(@virtual, uid); } diff --git a/Content.Shared/Actions/SharedActionsSystem.cs b/Content.Shared/Actions/SharedActionsSystem.cs index 3a096336a7..c94d914b97 100644 --- a/Content.Shared/Actions/SharedActionsSystem.cs +++ b/Content.Shared/Actions/SharedActionsSystem.cs @@ -361,7 +361,7 @@ public abstract class SharedActionsSystem : EntitySystem // for client-exclusive actions, the client shouldn't mark the comp as dirty. Otherwise that just leads to // unnecessary prediction resetting and state handling. - if (dirty) + if (dirty && !action.ClientExclusive) Dirty(comp); } @@ -380,12 +380,15 @@ public abstract class SharedActionsSystem : EntitySystem { comp ??= EnsureComp(uid); + bool allClientExclusive = true; + foreach (var action in actions) { AddAction(uid, action, provider, comp, false); + allClientExclusive = allClientExclusive && action.ClientExclusive; } - if (dirty) + if (dirty && !allClientExclusive) Dirty(comp); } diff --git a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Drop.cs b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Drop.cs index 4d4d8ef085..a1ddd17c13 100644 --- a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Drop.cs +++ b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Drop.cs @@ -7,6 +7,25 @@ namespace Content.Shared.Hands.EntitySystems; public abstract partial class SharedHandsSystem : EntitySystem { + private void InitializeDrop() + { + SubscribeLocalEvent(HandleEntityRemoved); + } + + protected virtual void HandleEntityRemoved(EntityUid uid, HandsComponent hands, EntRemovedFromContainerMessage args) + { + if (!TryGetHand(uid, args.Container.ID, out var hand)) + { + return; + } + + var gotUnequipped = new GotUnequippedHandEvent(uid, args.Entity, hand); + RaiseLocalEvent(args.Entity, gotUnequipped, false); + + var didUnequip = new DidUnequipHandEvent(uid, args.Entity, hand); + RaiseLocalEvent(uid, didUnequip, false); + } + /// /// Checks if the contents of a hand is able to be removed from its container. /// @@ -154,12 +173,6 @@ public abstract partial class SharedHandsSystem : EntitySystem if (doDropInteraction) _interactionSystem.DroppedInteraction(uid, entity); - var gotUnequipped = new GotUnequippedHandEvent(uid, entity, hand); - RaiseLocalEvent(entity, gotUnequipped, false); - - var didUnequip = new DidUnequipHandEvent(uid, entity, hand); - RaiseLocalEvent(uid, didUnequip, true); - if (hand == handsComp.ActiveHand) RaiseLocalEvent(entity, new HandDeselectedEvent(uid), false); } diff --git a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Pickup.cs b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Pickup.cs index 846d67849c..a5d4833fc4 100644 --- a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Pickup.cs +++ b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Pickup.cs @@ -10,6 +10,25 @@ namespace Content.Shared.Hands.EntitySystems; public abstract partial class SharedHandsSystem : EntitySystem { + private void InitializePickup() + { + SubscribeLocalEvent(HandleEntityInserted); + } + + protected virtual void HandleEntityInserted(EntityUid uid, HandsComponent hands, EntInsertedIntoContainerMessage args) + { + if (!TryGetHand(uid, args.Container.ID, out var hand)) + { + return; + } + + var didEquip = new DidEquipHandEvent(uid, args.Entity, hand); + RaiseLocalEvent(uid, didEquip, false); + + var gotEquipped = new GotEquippedHandEvent(uid, args.Entity, hand); + RaiseLocalEvent(args.Entity, gotEquipped, false); + } + /// /// Maximum pickup distance for which the pickup animation plays. /// @@ -208,17 +227,6 @@ public abstract partial class SharedHandsSystem : EntitySystem Dirty(hands); - var didEquip = new DidEquipHandEvent(uid, entity, hand); - RaiseLocalEvent(uid, didEquip, false); - - var gotEquipped = new GotEquippedHandEvent(uid, entity, hand); - RaiseLocalEvent(entity, gotEquipped, true); - - // TODO this should REALLY be a cancellable thing, not a handled event. - // If one of the interactions resulted in the item being dropped, return early. - if (gotEquipped.Handled) - return; - if (hand == hands.ActiveHand) RaiseLocalEvent(entity, new HandSelectedEvent(uid), false); } diff --git a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.cs b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.cs index 2af4cb456f..1cb7617a49 100644 --- a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.cs +++ b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.cs @@ -23,6 +23,8 @@ public abstract partial class SharedHandsSystem : EntitySystem base.Initialize(); InitializeInteractions(); + InitializeDrop(); + InitializePickup(); } public override void Shutdown() diff --git a/Content.Shared/Inventory/InventorySystem.Equip.cs b/Content.Shared/Inventory/InventorySystem.Equip.cs index 0baa270cad..25dcf56921 100644 --- a/Content.Shared/Inventory/InventorySystem.Equip.cs +++ b/Content.Shared/Inventory/InventorySystem.Equip.cs @@ -146,10 +146,6 @@ public abstract partial class InventorySystem if (!_handsSystem.CanDropHeld(actor, hands.ActiveHand!, checkActionBlocker: false)) return; - var gotUnequipped = new GotUnequippedHandEvent(actor, held.Value, hands.ActiveHand!); - var didUnequip = new DidUnequipHandEvent(actor, held.Value, hands.ActiveHand!); - RaiseLocalEvent(held.Value, gotUnequipped, false); - RaiseLocalEvent(actor, didUnequip, true); RaiseLocalEvent(held.Value, new HandDeselectedEvent(actor), false); TryEquip(actor, actor, held.Value, ev.Slot, predicted: true, inventory: inventory, force: true);