using Content.Shared.Interaction; using Content.Shared.IdentityManagement; using Content.Shared.Hands.Components; namespace Content.Shared.OfferItem; public abstract partial class SharedOfferItemSystem : EntitySystem { [Dependency] private readonly SharedTransformSystem _transform = default!; public override void Initialize() { SubscribeLocalEvent(SetInReceiveMode); SubscribeLocalEvent(OnMove); InitializeInteractions(); } private void SetInReceiveMode(EntityUid uid, OfferItemComponent component, InteractUsingEvent args) { if (!TryComp(args.User, out var offerItem)) return; if (args.User == uid || component.IsInReceiveMode || !offerItem.IsInOfferMode || (offerItem.IsInReceiveMode && offerItem.Target != uid)) return; component.IsInReceiveMode = true; component.Target = args.User; Dirty(uid, component); offerItem.Target = uid; offerItem.IsInOfferMode = false; Dirty(args.User, offerItem); if (offerItem.Item == null) return; _popup.PopupEntity(Loc.GetString("offer-item-try-give", ("item", Identity.Entity(offerItem.Item.Value, EntityManager)), ("target", Identity.Entity(uid, EntityManager))), component.Target.Value, component.Target.Value); _popup.PopupEntity(Loc.GetString("offer-item-try-give-target", ("user", Identity.Entity(component.Target.Value, EntityManager)), ("item", Identity.Entity(offerItem.Item.Value, EntityManager))), component.Target.Value, uid); args.Handled = true; } private void OnMove(EntityUid uid, OfferItemComponent component, MoveEvent args) { if (component.Target == null || args.NewPosition.InRange(EntityManager, _transform, Transform(component.Target.Value).Coordinates, component.MaxOfferDistance)) return; UnOffer(uid, component); } /// /// Resets the of the user and the target /// protected void UnOffer(EntityUid uid, OfferItemComponent component) { if (!TryComp(uid, out var hands) || hands.ActiveHand == null) return; if (TryComp(component.Target, out var offerItem) && component.Target != null) { if (component.Item != null) { _popup.PopupEntity(Loc.GetString("offer-item-no-give", ("item", Identity.Entity(component.Item.Value, EntityManager)), ("target", Identity.Entity(component.Target.Value, EntityManager))), uid, uid); _popup.PopupEntity(Loc.GetString("offer-item-no-give-target", ("user", Identity.Entity(uid, EntityManager)), ("item", Identity.Entity(component.Item.Value, EntityManager))), uid, component.Target.Value); } else if (offerItem.Item != null) { _popup.PopupEntity(Loc.GetString("offer-item-no-give", ("item", Identity.Entity(offerItem.Item.Value, EntityManager)), ("target", Identity.Entity(uid, EntityManager))), component.Target.Value, component.Target.Value); _popup.PopupEntity(Loc.GetString("offer-item-no-give-target", ("user", Identity.Entity(component.Target.Value, EntityManager)), ("item", Identity.Entity(offerItem.Item.Value, EntityManager))), component.Target.Value, uid); } offerItem.IsInOfferMode = false; offerItem.IsInReceiveMode = false; offerItem.Hand = null; offerItem.Target = null; offerItem.Item = null; Dirty(component.Target.Value, offerItem); } component.IsInOfferMode = false; component.IsInReceiveMode = false; component.Hand = null; component.Target = null; component.Item = null; Dirty(uid, component); } /// /// Cancels the transfer of the item /// protected void UnReceive(EntityUid uid, OfferItemComponent? component = null, OfferItemComponent? offerItem = null) { if (component == null && !TryComp(uid, out component)) return; if (offerItem == null && !TryComp(component.Target, out offerItem)) return; if (!TryComp(uid, out var hands) || hands.ActiveHand == null || component.Target == null) return; if (offerItem.Item != null) { _popup.PopupEntity(Loc.GetString("offer-item-no-give", ("item", Identity.Entity(offerItem.Item.Value, EntityManager)), ("target", Identity.Entity(uid, EntityManager))), component.Target.Value, component.Target.Value); _popup.PopupEntity(Loc.GetString("offer-item-no-give-target", ("user", Identity.Entity(component.Target.Value, EntityManager)), ("item", Identity.Entity(offerItem.Item.Value, EntityManager))), component.Target.Value, uid); } if (!offerItem.IsInReceiveMode) { offerItem.Target = null; component.Target = null; } offerItem.Item = null; offerItem.Hand = null; component.IsInReceiveMode = false; Dirty(uid, component); } /// /// Returns true if = true /// protected bool IsInOfferMode(EntityUid? entity, OfferItemComponent? component = null) { return entity != null && Resolve(entity.Value, ref component, false) && component.IsInOfferMode; } } // WD EDIT START [Serializable] public sealed class HandedEvent(EntityUid user, EntityUid target) : EntityEventArgs { public EntityUid User = user; public EntityUid Target = target; } // WD EDIT END