From eb1f84600def1490596933ce7c798024d949eb90 Mon Sep 17 00:00:00 2001 From: themias <89101928+themias@users.noreply.github.com> Date: Mon, 10 Feb 2025 22:29:37 -0500 Subject: [PATCH] Pulled item interaction fix (#34587) --- .../SharedHandsSystem.Interactions.cs | 12 +++++++----- .../Interaction/SharedInteractionSystem.cs | 5 +++-- .../VirtualItem/SharedVirtualItemSystem.cs | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Interactions.cs b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Interactions.cs index fc5adfaf15..3b22917fe8 100644 --- a/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Interactions.cs +++ b/Content.Shared/Hands/EntitySystems/SharedHandsSystem.Interactions.cs @@ -188,11 +188,13 @@ public abstract partial class SharedHandsSystem : EntitySystem if (args.Handled) return; - // TODO: this pattern is super uncommon, but it might be worth changing GetUsedEntityEvent to be recursive. - if (TryComp(component.ActiveHandEntity, out var virtualItem)) - args.Used = virtualItem.BlockingEntity; - else - args.Used = component.ActiveHandEntity; + if (component.ActiveHandEntity.HasValue) + { + // allow for the item to return a different entity, e.g. virtual items + RaiseLocalEvent(component.ActiveHandEntity.Value, ref args); + } + + args.Used ??= component.ActiveHandEntity; } //TODO: Actually shows all items/clothing/etc. diff --git a/Content.Shared/Interaction/SharedInteractionSystem.cs b/Content.Shared/Interaction/SharedInteractionSystem.cs index f53895cf8d..1eb7bd112d 100644 --- a/Content.Shared/Interaction/SharedInteractionSystem.cs +++ b/Content.Shared/Interaction/SharedInteractionSystem.cs @@ -1413,7 +1413,7 @@ namespace Content.Shared.Interaction /// If there is an entity being used. public bool TryGetUsedEntity(EntityUid user, [NotNullWhen(true)] out EntityUid? used, bool checkCanUse = true) { - var ev = new GetUsedEntityEvent(); + var ev = new GetUsedEntityEvent(user); RaiseLocalEvent(user, ref ev); used = ev.Used; @@ -1464,8 +1464,9 @@ namespace Content.Shared.Interaction /// Raised directed by-ref on an entity to determine what item will be used in interactions. /// [ByRefEvent] - public record struct GetUsedEntityEvent() + public record struct GetUsedEntityEvent(EntityUid User) { + public EntityUid User = User; public EntityUid? Used = null; public bool Handled => Used != null; diff --git a/Content.Shared/Inventory/VirtualItem/SharedVirtualItemSystem.cs b/Content.Shared/Inventory/VirtualItem/SharedVirtualItemSystem.cs index 8e62c5163d..9eac60adc4 100644 --- a/Content.Shared/Inventory/VirtualItem/SharedVirtualItemSystem.cs +++ b/Content.Shared/Inventory/VirtualItem/SharedVirtualItemSystem.cs @@ -46,6 +46,8 @@ public abstract class SharedVirtualItemSystem : EntitySystem SubscribeLocalEvent(OnBeforeRangedInteract); SubscribeLocalEvent(OnGettingInteractedWithAttemptEvent); + + SubscribeLocalEvent(OnGetUsedEntity); } /// @@ -81,6 +83,23 @@ public abstract class SharedVirtualItemSystem : EntitySystem args.Cancelled = true; } + private void OnGetUsedEntity(Entity ent, ref GetUsedEntityEvent args) + { + if (args.Handled) + return; + + // if the user is holding the real item the virtual item points to, + // we allow them to use it in the interaction + foreach (var hand in _handsSystem.EnumerateHands(args.User)) + { + if (hand.HeldEntity == ent.Comp.BlockingEntity) + { + args.Used = ent.Comp.BlockingEntity; + return; + } + } + } + #region Hands /// -- 2.51.2