From 6d15066c65392010398eb7a125db493cf9885af6 Mon Sep 17 00:00:00 2001 From: Debug <49997488+DebugOk@users.noreply.github.com> Date: Fri, 29 Dec 2023 20:38:03 +0100 Subject: [PATCH] Prevent pseudoitems from being transferred between bags (#553) * Update PseudoItemSystem.cs * Rider full cleanup * Also abort in sharedstorage * Make the comp shared * Why drop the bags? --- .../Item/PseudoItem/PseudoItemComponent.cs | 18 -- .../Item/PseudoItem/PseudoItemSystem.cs | 253 ++++++++++-------- .../Item/Components/PseudoItemComponent.cs | 15 ++ .../EntitySystems/SharedStorageSystem.cs | 4 + 4 files changed, 153 insertions(+), 137 deletions(-) delete mode 100644 Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemComponent.cs create mode 100644 Content.Shared/Nyanotrasen/Item/Components/PseudoItemComponent.cs diff --git a/Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemComponent.cs b/Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemComponent.cs deleted file mode 100644 index 73ff83f0d1d..00000000000 --- a/Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemComponent.cs +++ /dev/null @@ -1,18 +0,0 @@ - -using Content.Shared._NF.Cloning; - -namespace Content.Server.Item.PseudoItem -{ - /// - /// For entities that behave like an item under certain conditions, - /// but not under most conditions. - /// - [RegisterComponent] - public sealed partial class PseudoItemComponent : Component, ITransferredByCloning - { - [DataField("size")] - public int Size = 120; - - public bool Active = false; - } -} diff --git a/Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemSystem.cs b/Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemSystem.cs index 579366b1451..9f5bd596d76 100644 --- a/Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemSystem.cs +++ b/Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemSystem.cs @@ -1,159 +1,174 @@ -using System.Threading; -using Content.Shared.Verbs; -using Content.Shared.Item; -using Content.Shared.Hands; +using Content.Server.DoAfter; +using Content.Server.Storage.EntitySystems; using Content.Shared.DoAfter; +using Content.Shared.Hands; using Content.Shared.IdentityManagement; -using Content.Shared.Pseudo; -using Content.Server.Storage.Components; -using Content.Server.Storage.EntitySystems; -using Content.Server.DoAfter; +using Content.Shared.Item; +using Content.Shared.Item.PseudoItem; using Content.Shared.Storage; using Content.Shared.Tag; +using Content.Shared.Verbs; using Robust.Shared.Containers; -namespace Content.Server.Item.PseudoItem +namespace Content.Server.Item.PseudoItem; + +public sealed class PseudoItemSystem : EntitySystem { - public sealed class PseudoItemSystem : EntitySystem - { - [Dependency] private readonly StorageSystem _storageSystem = default!; - [Dependency] private readonly ItemSystem _itemSystem = default!; - [Dependency] private readonly SharedDoAfterSystem _doAfter = default!; - [Dependency] private readonly TagSystem _tagSystem = default!; + [Dependency] private readonly StorageSystem _storageSystem = default!; + [Dependency] private readonly ItemSystem _itemSystem = default!; + [Dependency] private readonly DoAfterSystem _doAfter = default!; + [Dependency] private readonly TagSystem _tagSystem = default!; [ValidatePrototypeId] - private const string PreventTag = "PreventLabel";public override void Initialize() - { - base.Initialize(); - SubscribeLocalEvent>(AddInsertVerb); - SubscribeLocalEvent>(AddInsertAltVerb); - SubscribeLocalEvent(OnEntRemoved); - SubscribeLocalEvent(OnGettingPickedUpAttempt); - SubscribeLocalEvent(OnDropAttempt); - SubscribeLocalEvent(OnDoAfter); - } + private const string PreventTag = "PreventLabel"; - private void AddInsertVerb(EntityUid uid, PseudoItemComponent component, GetVerbsEvent args) - { - if (!args.CanInteract || !args.CanAccess) - return; + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent>(AddInsertVerb); + SubscribeLocalEvent>(AddInsertAltVerb); + SubscribeLocalEvent(OnEntRemoved); + SubscribeLocalEvent(OnGettingPickedUpAttempt); + SubscribeLocalEvent(OnDropAttempt); + SubscribeLocalEvent(OnDoAfter); + SubscribeLocalEvent(OnInsertAttempt); + } - if (component.Active) - return; + private void AddInsertVerb(EntityUid uid, PseudoItemComponent component, GetVerbsEvent args) + { + if (!args.CanInteract || !args.CanAccess) + return; - if (!TryComp(args.Target, out var targetStorage)) - return; + if (component.Active) + return; - if (component.Size > targetStorage.StorageCapacityMax - targetStorage.StorageUsed) - return; + if (!TryComp(args.Target, out var targetStorage)) + return; - if (Transform(args.Target).ParentUid == uid) - return; + if (component.Size > targetStorage.StorageCapacityMax - targetStorage.StorageUsed) + return; - InnateVerb verb = new() - { - Act = () => - { - TryInsert(args.Target, uid, component, targetStorage); - }, - Text = Loc.GetString("action-name-insert-self"), - Priority = 2 - }; - args.Verbs.Add(verb); - } + if (Transform(args.Target).ParentUid == uid) + return; - private void AddInsertAltVerb(EntityUid uid, PseudoItemComponent component, GetVerbsEvent args) + InnateVerb verb = new() { - if (!args.CanInteract || !args.CanAccess) - return; + Act = () => + { + TryInsert(args.Target, uid, component, targetStorage); + }, + Text = Loc.GetString("action-name-insert-self"), + Priority = 2 + }; + args.Verbs.Add(verb); + } - if (args.User == args.Target) - return; + private void AddInsertAltVerb(EntityUid uid, PseudoItemComponent component, GetVerbsEvent args) + { + if (!args.CanInteract || !args.CanAccess) + return; - if (args.Hands == null) - return; + if (args.User == args.Target) + return; - if (!TryComp(args.Hands.ActiveHandEntity, out var targetStorage)) - return; + if (args.Hands == null) + return; - AlternativeVerb verb = new() - { - Act = () => - { - StartInsertDoAfter(args.User, uid, args.Hands.ActiveHandEntity.Value, component); - }, - Text = Loc.GetString("action-name-insert-other", ("target", Identity.Entity(args.Target, EntityManager))), - Priority = 2 - }; - args.Verbs.Add(verb); - } + if (!TryComp(args.Hands.ActiveHandEntity, out var targetStorage)) + return; - private void OnEntRemoved(EntityUid uid, PseudoItemComponent component, EntGotRemovedFromContainerMessage args) + AlternativeVerb verb = new() { - if (!component.Active) - return; + Act = () => + { + StartInsertDoAfter(args.User, uid, args.Hands.ActiveHandEntity.Value, component); + }, + Text = Loc.GetString("action-name-insert-other", ("target", Identity.Entity(args.Target, EntityManager))), + Priority = 2 + }; + args.Verbs.Add(verb); + } - RemComp(uid); - component.Active = false; - } + private void OnEntRemoved(EntityUid uid, PseudoItemComponent component, EntGotRemovedFromContainerMessage args) + { + if (!component.Active) + return; - private void OnGettingPickedUpAttempt(EntityUid uid, PseudoItemComponent component, GettingPickedUpAttemptEvent args) - { - if (args.User == args.Item) - return; + RemComp(uid); + component.Active = false; + } + + private void OnGettingPickedUpAttempt(EntityUid uid, PseudoItemComponent component, + GettingPickedUpAttemptEvent args) + { + if (args.User == args.Item) + return; - Transform(uid).AttachToGridOrMap(); + Transform(uid).AttachToGridOrMap(); + args.Cancel(); + } + + private void OnDropAttempt(EntityUid uid, PseudoItemComponent component, DropAttemptEvent args) + { + if (component.Active) args.Cancel(); - } + } - private void OnDropAttempt(EntityUid uid, PseudoItemComponent component, DropAttemptEvent args) - { - if (component.Active) - args.Cancel(); - } - private void OnDoAfter(EntityUid uid, PseudoItemComponent component, DoAfterEvent args) - { - if (args.Handled || args.Cancelled || args.Args.Used == null) - return; + private void OnDoAfter(EntityUid uid, PseudoItemComponent component, DoAfterEvent args) + { + if (args.Handled || args.Cancelled || args.Args.Used == null) + return; - args.Handled = TryInsert(args.Args.Used.Value, uid, component); - } + args.Handled = TryInsert(args.Args.Used.Value, uid, component); + } - public bool TryInsert(EntityUid storageUid, EntityUid toInsert, PseudoItemComponent component, StorageComponent? storage = null) - { - if (!Resolve(storageUid, ref storage)) - return false; + public bool TryInsert(EntityUid storageUid, EntityUid toInsert, PseudoItemComponent component, + StorageComponent? storage = null) + { + if (!Resolve(storageUid, ref storage)) + return false; - if (component.Size > storage.StorageCapacityMax - storage.StorageUsed) - return false; + if (component.Size > storage.StorageCapacityMax - storage.StorageUsed) + return false; - var item = EnsureComp(toInsert); - _tagSystem.TryAddTag(toInsert, PreventTag); + var item = EnsureComp(toInsert); + _tagSystem.TryAddTag(toInsert, PreventTag); _itemSystem.SetSize(toInsert, component.Size, item); - if (!_storageSystem.Insert(storageUid, toInsert, out _, storageComp: storage)) - { - component.Active = false; - RemComp(toInsert); - return false; - } + if (!_storageSystem.Insert(storageUid, toInsert, out _, null, storage)) + { + component.Active = false; + RemComp(toInsert); + return false; + } - component.Active = true; - Transform(storageUid).AttachToGridOrMap(); - return true; + component.Active = true; + return true; + } - } - private void StartInsertDoAfter(EntityUid inserter, EntityUid toInsert, EntityUid storageEntity, PseudoItemComponent? pseudoItem = null) + private void StartInsertDoAfter(EntityUid inserter, EntityUid toInsert, EntityUid storageEntity, + PseudoItemComponent? pseudoItem = null) + { + if (!Resolve(toInsert, ref pseudoItem)) + return; + + var ev = new PseudoItemInsertDoAfterEvent(); + var args = new DoAfterArgs(EntityManager, inserter, 5f, ev, toInsert, toInsert, storageEntity) { - if (!Resolve(toInsert, ref pseudoItem)) - return; + BreakOnTargetMove = true, + BreakOnUserMove = true, + NeedHand = true + }; - _doAfter.TryStartDoAfter(new DoAfterArgs(EntityManager, inserter, 5f, new PseudoDoAfterEvent(), toInsert, target: toInsert, used: storageEntity) - { - BreakOnTargetMove = true, - BreakOnUserMove = true, - NeedHand = true - }); - } + _doAfter.TryStartDoAfter(args); + } + + private void OnInsertAttempt(EntityUid uid, PseudoItemComponent component, + ContainerGettingInsertedAttemptEvent args) + { + if (!component.Active) + return; + // This hopefully shouldn't trigger, but this is a failsafe just in case so we dont bluespace them cats + args.Cancel(); } } diff --git a/Content.Shared/Nyanotrasen/Item/Components/PseudoItemComponent.cs b/Content.Shared/Nyanotrasen/Item/Components/PseudoItemComponent.cs new file mode 100644 index 00000000000..04117c98112 --- /dev/null +++ b/Content.Shared/Nyanotrasen/Item/Components/PseudoItemComponent.cs @@ -0,0 +1,15 @@ + +namespace Content.Shared.Item.PseudoItem; + +/// +/// For entities that behave like an item under certain conditions, +/// but not under most conditions. +/// +[RegisterComponent] +public sealed partial class PseudoItemComponent : Component +{ + [DataField("size")] + public int Size = 120; + + public bool Active = false; +} diff --git a/Content.Shared/Storage/EntitySystems/SharedStorageSystem.cs b/Content.Shared/Storage/EntitySystems/SharedStorageSystem.cs index 5faec99fd5b..efd77b1e91a 100644 --- a/Content.Shared/Storage/EntitySystems/SharedStorageSystem.cs +++ b/Content.Shared/Storage/EntitySystems/SharedStorageSystem.cs @@ -10,6 +10,7 @@ using Content.Shared.Implants.Components; using Content.Shared.Interaction; using Content.Shared.Item; +using Content.Shared.Item.PseudoItem; using Content.Shared.Lock; using Content.Shared.Placeable; using Content.Shared.Popups; @@ -439,6 +440,9 @@ public void TransferEntities(EntityUid source, EntityUid target, EntityUid? user foreach (var entity in entities.ToArray()) { + if (HasComp(entity)) // Nyanotrasen - They dont transfer properly + continue; + Insert(target, entity, out _, user: user, targetComp, playSound: false); }