From e6946128cecd1d5f010631b463af702ad4e6ffcc Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sun, 27 Feb 2022 07:19:57 -0800 Subject: [PATCH] custom attach / attach watcher: improve --- QSB/Player/PlayerAttachWatcher.cs | 23 +++++++++++++++++++---- QSB/ShipSync/ShipCustomAttach.cs | 11 ++--------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/QSB/Player/PlayerAttachWatcher.cs b/QSB/Player/PlayerAttachWatcher.cs index 47addd6b..16971db0 100644 --- a/QSB/Player/PlayerAttachWatcher.cs +++ b/QSB/Player/PlayerAttachWatcher.cs @@ -10,7 +10,6 @@ namespace QSB.Player private void Awake() { Harmony.CreateAndPatchAll(typeof(PlayerAttachWatcher)); - QSBSceneManager.OnSceneLoaded += (_, _, _) => Current = null; Destroy(this); } @@ -18,10 +17,26 @@ namespace QSB.Player [HarmonyPrefix] [HarmonyPatch(nameof(PlayerAttachPoint.AttachPlayer))] - private static void AttachPlayer(PlayerAttachPoint __instance) => Current = __instance; + private static void AttachPlayer(PlayerAttachPoint __instance) + { + if (Current != null) + { + Current.DetachPlayer(); + } + + Current = __instance; + } [HarmonyPrefix] [HarmonyPatch(nameof(PlayerAttachPoint.DetachPlayer))] - private static void DetachPlayer() => Current = null; + private static void DetachPlayer(PlayerAttachPoint __instance) + { + if (!__instance.enabled) + { + return; + } + + Current = null; + } } -} \ No newline at end of file +} diff --git a/QSB/ShipSync/ShipCustomAttach.cs b/QSB/ShipSync/ShipCustomAttach.cs index 6598638a..366521bc 100644 --- a/QSB/ShipSync/ShipCustomAttach.cs +++ b/QSB/ShipSync/ShipCustomAttach.cs @@ -1,5 +1,4 @@ -using QSB.Player; -using UnityEngine; +using UnityEngine; namespace QSB.ShipSync { @@ -39,7 +38,7 @@ namespace QSB.ShipSync return; } - var attachedToUs = PlayerAttachWatcher.Current == _playerAttachPoint; + var attachedToUs = _playerAttachPoint.enabled; _detachPrompt.SetVisibility(attachedToUs); if (attachedToUs && OWInput.IsNewlyPressed(InputLibrary.cancel, InputMode.Character)) { @@ -49,12 +48,6 @@ namespace QSB.ShipSync if (!attachedToUs) { - if (_playerAttachPoint.enabled) - { - // attached to us, then attached to something else - _playerAttachPoint.enabled = false; - } - if (PlayerState.IsAttached()) { return;