forked from new-frontiers-14/frontier-station-14
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent pseudoitems from being transferred between bags (new-frontier…
…s-14#553) * Update PseudoItemSystem.cs * Rider full cleanup * Also abort in sharedstorage * Make the comp shared * Why drop the bags?
- Loading branch information
Showing
4 changed files
with
153 additions
and
137 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemComponent.cs
This file was deleted.
Oops, something went wrong.
253 changes: 134 additions & 119 deletions
253
Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TagPrototype>] | ||
private const string PreventTag = "PreventLabel";public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
SubscribeLocalEvent<PseudoItemComponent, GetVerbsEvent<InnateVerb>>(AddInsertVerb); | ||
SubscribeLocalEvent<PseudoItemComponent, GetVerbsEvent<AlternativeVerb>>(AddInsertAltVerb); | ||
SubscribeLocalEvent<PseudoItemComponent, EntGotRemovedFromContainerMessage>(OnEntRemoved); | ||
SubscribeLocalEvent<PseudoItemComponent, GettingPickedUpAttemptEvent>(OnGettingPickedUpAttempt); | ||
SubscribeLocalEvent<PseudoItemComponent, DropAttemptEvent>(OnDropAttempt); | ||
SubscribeLocalEvent<PseudoItemComponent, DoAfterEvent>(OnDoAfter); | ||
} | ||
private const string PreventTag = "PreventLabel"; | ||
|
||
private void AddInsertVerb(EntityUid uid, PseudoItemComponent component, GetVerbsEvent<InnateVerb> args) | ||
{ | ||
if (!args.CanInteract || !args.CanAccess) | ||
return; | ||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
SubscribeLocalEvent<PseudoItemComponent, GetVerbsEvent<InnateVerb>>(AddInsertVerb); | ||
SubscribeLocalEvent<PseudoItemComponent, GetVerbsEvent<AlternativeVerb>>(AddInsertAltVerb); | ||
SubscribeLocalEvent<PseudoItemComponent, EntGotRemovedFromContainerMessage>(OnEntRemoved); | ||
SubscribeLocalEvent<PseudoItemComponent, GettingPickedUpAttemptEvent>(OnGettingPickedUpAttempt); | ||
SubscribeLocalEvent<PseudoItemComponent, DropAttemptEvent>(OnDropAttempt); | ||
SubscribeLocalEvent<PseudoItemComponent, PseudoItemInsertDoAfterEvent>(OnDoAfter); | ||
SubscribeLocalEvent<PseudoItemComponent, ContainerGettingInsertedAttemptEvent>(OnInsertAttempt); | ||
} | ||
|
||
if (component.Active) | ||
return; | ||
private void AddInsertVerb(EntityUid uid, PseudoItemComponent component, GetVerbsEvent<InnateVerb> args) | ||
{ | ||
if (!args.CanInteract || !args.CanAccess) | ||
return; | ||
|
||
if (!TryComp<StorageComponent>(args.Target, out var targetStorage)) | ||
return; | ||
if (component.Active) | ||
return; | ||
|
||
if (component.Size > targetStorage.StorageCapacityMax - targetStorage.StorageUsed) | ||
return; | ||
if (!TryComp<StorageComponent>(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<AlternativeVerb> 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<AlternativeVerb> args) | ||
{ | ||
if (!args.CanInteract || !args.CanAccess) | ||
return; | ||
|
||
if (args.Hands == null) | ||
return; | ||
if (args.User == args.Target) | ||
return; | ||
|
||
if (!TryComp<StorageComponent>(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<StorageComponent>(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<ItemComponent>(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<ItemComponent>(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<ItemComponent>(toInsert); | ||
_tagSystem.TryAddTag(toInsert, PreventTag); | ||
var item = EnsureComp<ItemComponent>(toInsert); | ||
_tagSystem.TryAddTag(toInsert, PreventTag); | ||
_itemSystem.SetSize(toInsert, component.Size, item); | ||
|
||
if (!_storageSystem.Insert(storageUid, toInsert, out _, storageComp: storage)) | ||
{ | ||
component.Active = false; | ||
RemComp<ItemComponent>(toInsert); | ||
return false; | ||
} | ||
if (!_storageSystem.Insert(storageUid, toInsert, out _, null, storage)) | ||
{ | ||
component.Active = false; | ||
RemComp<ItemComponent>(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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Content.Shared/Nyanotrasen/Item/Components/PseudoItemComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
namespace Content.Shared.Item.PseudoItem; | ||
|
||
/// <summary> | ||
/// For entities that behave like an item under certain conditions, | ||
/// but not under most conditions. | ||
/// </summary> | ||
[RegisterComponent] | ||
public sealed partial class PseudoItemComponent : Component | ||
{ | ||
[DataField("size")] | ||
public int Size = 120; | ||
|
||
public bool Active = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters