From fd1b913f6c11da2dbc7b90820ce5c5711391cc24 Mon Sep 17 00:00:00 2001 From: fox Date: Tue, 17 Sep 2024 20:53:22 +0300 Subject: [PATCH] Recreate the leash join if it was destroyed by RT --- .../Floofstation/Leash/LeashSystem.cs | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/Content.Shared/Floofstation/Leash/LeashSystem.cs b/Content.Shared/Floofstation/Leash/LeashSystem.cs index 3f87649753d..9aa13f21500 100644 --- a/Content.Shared/Floofstation/Leash/LeashSystem.cs +++ b/Content.Shared/Floofstation/Leash/LeashSystem.cs @@ -41,9 +41,8 @@ public override void Initialize() UpdatesBefore.Add(typeof(SharedPhysicsSystem)); SubscribeLocalEvent(OnAnchorUnequipping); - SubscribeLocalEvent(OnLeashedInserting); - SubscribeLocalEvent(OnJointRemoved); SubscribeLocalEvent>(OnGetEquipmentVerbs); + SubscribeLocalEvent(OnJointRemoved, after: [typeof(SharedJointSystem)]); SubscribeLocalEvent>(OnGetLeashedVerbs); SubscribeLocalEvent(OnAttachDoAfter); @@ -109,34 +108,6 @@ private void OnAnchorUnequipping(Entity ent, ref BeingUneq args.Cancel(); } - private void OnLeashedInserting(Entity ent, ref ContainerGettingInsertedAttemptEvent args) - { - // Prevent the entity from entering crates and the like because that would instantly break all joints on it, including the leash - if (_timing.ApplyingState - || !Exists(ent.Comp.Puller) - || !Exists(ent.Comp.Anchor) - || !TryComp(ent.Comp.Puller, out var leashPuller) - || !TryComp(ent.Comp.Anchor, out var leashAnchor)) - return; - - args.Cancel(); - // This is hella unsafe to do, but we recreate the joint because dumb storage system removes it before raising the event. - // We have to pray that OnJointRemoved already was called and that it deferred the removal of everything that used to exist - // I HATE STORAGE - DoLeash((ent.Comp.Anchor.Value, leashAnchor), (ent.Comp.Puller.Value, leashPuller), ent); - } - - private void OnJointRemoved(Entity ent, ref JointRemovedEvent args) - { - var id = args.Joint.ID; - if (!ent.Comp.Leashed.TryFirstOrDefault(it => it.JointId == id, out var data) - || !TryGetEntity(data.Pulled, out var leashedEnt) - || !TryComp(leashedEnt, out var leashed)) - return; - - RemoveLeash((leashedEnt.Value, leashed), ent!, false); - } - private void OnGetEquipmentVerbs(Entity ent, ref GetVerbsEvent args) { if (!args.CanAccess @@ -188,6 +159,25 @@ private void OnGetLeashedVerbs(Entity ent, ref GetVerbsEvent ent, ref JointRemovedEvent args) + { + var id = args.Joint.ID; + if (_timing.ApplyingState + || ent.Comp.LifeStage >= ComponentLifeStage.Removing + || ent.Comp.Puller is not { } puller + || !TryComp(ent.Comp.Anchor, out var anchor) + || !TryComp(puller, out var leash) + || !Transform(ent).Coordinates.TryDistance(EntityManager, Transform(puller).Coordinates, out var dst) + || dst > leash.MaxDistance + ) + return; + + // If the entity still has a leashed comp, and is on the same map, and is within the max distance of the leash + // Then the leash was likely broken due to some weird unforeseen fucking robust toolbox magic. We can try to recreate it. + // This is hella unsafe to do. It will crash in debug builds under certain conditions. Luckily, release builds are safe. + DoLeash((ent.Comp.Anchor.Value, anchor), (puller, leash), ent); + } + private void OnAttachDoAfter(Entity ent, ref LeashAttachDoAfterEvent args) { if (args.Cancelled || args.Handled