Files
wwdpublic/Content.Shared/_Goobstation/Grab/GrabbingItemSystem.cs
Eris 19834bbb5e Port Changelings From Goobstation (Funky PR 387 Included) (#1855)
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

<!--
Explain this PR in as much detail as applicable

Some example prompts to consider:
How might this affect the game? The codebase?
What might be some alternatives to this?
How/Who does this benefit/hurt [the game/codebase]?
-->

Finally gets around to porting over Changelings from Goobstation, as
well as a *certain evil PR* from FunkyStation (with the fixes it comes
with).
---

# TODO

<!--
A list of everything you have to do before this PR is "complete"
You probably won't have to complete everything before merging but it's
good to leave future references
-->

- [x] Wait for #1856 to be merged
- [x] Wait for #1860 to be merged
- [x] Fix broken code to make it actually compile (right now is just
porting prototypes, code, and locale)
- [X] Port That Funky PR I Mentioned Earlier
- [] Throw bricks at the codebase until it stops failing tests
- [X] Maybe do some local testing

---

<!--
This is default collapsed, readers click to expand it and see all your
media
The PR media section can get very large at times, so this is a good way
to keep it clean
The title is written using HTML tags
The title must be within the <summary> tags or you won't see it
-->

<details><summary><h1>Media</h1></summary>
<p>

![image](https://github.com/user-attachments/assets/04473aef-d076-4dd0-a700-0c656428b88a)

</p>
</details>

---

# Changelog

<!--
You can add an author after the `🆑` to change the name that appears
in the changelog (ex: `🆑 Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->

🆑
- add: Changelings have been ported!

---------

Signed-off-by: Eris <erisfiregamer1@gmail.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
(cherry picked from commit ee4f7aa7097f260d07af3ab0583a2a5c14a38c74)
2025-03-21 17:06:45 +03:00

132 lines
4.3 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Content.Shared.Body.Systems;
using Content.Shared.Cuffs;
using Content.Shared.DoAfter;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Pulling.Events;
using Content.Shared.Movement.Pulling.Systems;
using Content.Shared.Pulling.Events;
using Content.Shared.Weapons.Melee.Events;
using Robust.Shared.Serialization;
namespace Content.Shared._Goobstation.Grab;
public sealed class GrabbingItemSystem : EntitySystem
{
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly PullingSystem _pulling = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GrabbingItemComponent, AttemptMeleeEvent>(OnMeleeAttempt);
SubscribeLocalEvent<GrabbingItemComponent, ComponentRemove>(OnRemove);
SubscribeLocalEvent<BeingGrabbedComponent, BeingPulledAttemptEvent>(OnBeingPulledAttempt);
SubscribeLocalEvent<BeingGrabbedComponent, AttemptStopPullingEvent>(OnAttemptStopPulling,
after: new[] { typeof(SharedBodySystem), typeof(SharedCuffableSystem) });
SubscribeLocalEvent<BeingGrabbedComponent, GrabBreakDoAfterEvent>(OnGrabBreak);
SubscribeLocalEvent<BeingGrabbedComponent, PullStoppedMessage>(OnStopPull);
}
private void OnStopPull(Entity<BeingGrabbedComponent> ent, ref PullStoppedMessage args)
{
if (TryComp(ent.Comp.GrabberItemUid, out GrabbingItemComponent? item))
{
item.GrabbedEntity = null;
Dirty(ent.Comp.GrabberItemUid.Value, item);
}
RemCompDeferred<BeingGrabbedComponent>(ent);
}
private void OnGrabBreak(Entity<BeingGrabbedComponent> ent, ref GrabBreakDoAfterEvent args)
{
if (args.Cancelled || args.Handled)
return;
args.Handled = true;
if (TryComp<PullableComponent>(ent.Owner, out var pullable))
_pulling.TryStopPull(ent.Owner, pullable);
}
private void OnAttemptStopPulling(Entity<BeingGrabbedComponent> ent, ref AttemptStopPullingEvent args)
{
if (!TryComp(ent.Comp.GrabberItemUid, out GrabbingItemComponent? grabbingItem))
{
RemCompDeferred<BeingGrabbedComponent>(ent.Owner);
return;
}
if (args.Cancelled)
return;
if (args.User == null || !Exists(args.User.Value))
return;
if (args.User.Value != ent.Owner)
return;
args.Cancelled = true;
var doAfterArgs = new DoAfterArgs(EntityManager,
ent.Owner,
grabbingItem.GrabBreakDelay,
new GrabBreakDoAfterEvent(),
ent.Owner)
{
CancelDuplicate = false,
};
_doAfter.TryStartDoAfter(doAfterArgs);
}
private void OnRemove(Entity<GrabbingItemComponent> ent, ref ComponentRemove args)
{
if (ent.Comp.GrabbedEntity != null)
RemComp<BeingGrabbedComponent>(ent.Comp.GrabbedEntity.Value);
}
public bool TryGetGrabbingItem(EntityUid uid, [NotNullWhen(true)] out Entity<GrabbingItemComponent>? item)
{
item = null;
foreach (var held in _handsSystem.EnumerateHeld(uid))
{
if (!TryComp(held, out GrabbingItemComponent? grabbingItem) || grabbingItem.GrabbedEntity != null)
continue;
item = (held, grabbingItem);
return true;
}
return false;
}
private void OnBeingPulledAttempt(Entity<BeingGrabbedComponent> ent, ref BeingPulledAttemptEvent args)
{
args.Cancel();
}
private void OnMeleeAttempt(Entity<GrabbingItemComponent> ent, ref AttemptMeleeEvent args)
{
var grabbed = ent.Comp.GrabbedEntity;
if (grabbed == null || args.Cancelled)
return;
args.Cancelled = true;
args.Message = Loc.GetString("grabbing-item-attack-fail",
("item", Identity.Entity(ent.Owner, EntityManager)),
("grabbed", Identity.Entity(grabbed.Value, EntityManager)));
}
}
[Serializable, NetSerializable]
public sealed partial class GrabBreakDoAfterEvent : SimpleDoAfterEvent
{
}