diff --git a/NewHorizons/Handlers/HeldItemHandler.cs b/NewHorizons/Handlers/HeldItemHandler.cs index ef61cd3f..d6e05c26 100644 --- a/NewHorizons/Handlers/HeldItemHandler.cs +++ b/NewHorizons/Handlers/HeldItemHandler.cs @@ -17,7 +17,7 @@ public static class HeldItemHandler /// Dictionary of system name to item path /// If we travel to multiple systems within a single loop, this will hold the items we move between systems /// - private static Dictionary> _pathOfItemTakenFromSystem = new(); + private static Dictionary> _pathOfItemTakenFromSystem = new(); public static bool WasAWCTakenFromATP => _pathOfItemTakenFromSystem.TryGetValue("SolarSystem", out var list) && list.Contains(ADVANCED_WARP_CORE); @@ -45,16 +45,10 @@ public static class HeldItemHandler _currentStarSystem = Main.Instance.CurrentStarSystem; - // If we took the AWC out of the main system make sure to disable time loop - if (_currentStarSystem != "SolarSystem" && WasAWCTakenFromATP) - { - TimeLoop.SetTimeLoopEnabled(false); - } - if (!_isInitialized) { _isInitialized = true; - Main.Instance.StarSystemChanging += OnStarSystemChanging; + Main.Instance.OnChangeStarSystem.AddListener(OnStarSystemChanging); Main.Instance.OnStarSystemLoaded.AddListener(OnSystemReady); GlobalMessenger.AddListener("PlayerDeath", OnPlayerDeath); } @@ -71,8 +65,6 @@ public static class HeldItemHandler private static GameObject MakePerfectCopy(GameObject go) { - //go.SetActive(false); - var owItem = go.GetComponent(); var tempParent = new GameObject(); @@ -98,7 +90,7 @@ public static class HeldItemHandler _pathOfItemTakenFromSystem[Main.Instance.CurrentStarSystem].Add(path); } - private static void OnStarSystemChanging() + private static void OnStarSystemChanging(string _) { if (_currentlyHeldItem != null) { @@ -109,7 +101,7 @@ public static class HeldItemHandler } NHLogger.Log($"Scene unloaded, preserved inactive held item {_currentlyHeldItem.name}"); - // For some reason, the original will get destroyed no matter what do we make a copy + // For some reason, the original will get destroyed no matter what we do. To avoid, we make a copy _currentlyHeldItem = MakePerfectCopy(_currentlyHeldItem).DontDestroyOnLoad(); } @@ -155,7 +147,7 @@ public static class HeldItemHandler } NHLogger.Log($"Unsocketed {item.name}"); } - GameObject.Destroy(item.gameObject); + item.gameObject.SetActive(false); } catch (Exception e) { @@ -219,4 +211,19 @@ public static class HeldItemHandler NHLogger.Log($"Player is now holding {item?.name ?? "nothing"}"); _currentlyHeldItem = item?.gameObject; } + + [HarmonyPostfix, HarmonyPatch(typeof(ItemTool))] + [HarmonyPatch(nameof(ItemTool.SocketItem))] + [HarmonyPatch(nameof(ItemTool.DropItem))] + [HarmonyPatch(nameof(ItemTool.StartUnsocketItem))] + private static void HeldItemChanged2(ItemTool __instance) + { + if (!_isSystemReady) + { + return; + } + + NHLogger.Log($"Player is now holding nothing"); + _currentlyHeldItem = null; + } } diff --git a/NewHorizons/Handlers/PlayerSpawnHandler.cs b/NewHorizons/Handlers/PlayerSpawnHandler.cs index 8ee1a007..352b4eff 100644 --- a/NewHorizons/Handlers/PlayerSpawnHandler.cs +++ b/NewHorizons/Handlers/PlayerSpawnHandler.cs @@ -51,11 +51,21 @@ namespace NewHorizons.Handlers Delay.StartCoroutine(SpawnCoroutine(30)); } - var cloak = GetDefaultSpawn()?.GetAttachedOWRigidbody()?.GetComponentInChildren(); - if (cloak != null) + + // It was NREing in here when it was all ?. so explicit null checks + var spawn = GetDefaultSpawn(); + if (spawn != null) { - // Ensures it has invoked everything and actually placed the player in the cloaking field #671 - cloak._firstUpdate = true; + var attachedOWRigidBody = spawn.GetAttachedOWRigidbody(); + if (attachedOWRigidBody != null) + { + var cloak = attachedOWRigidBody.GetComponentInChildren(); + if (cloak != null) + { + // Ensures it has invoked everything and actually placed the player in the cloaking field #671 + cloak._firstUpdate = true; + } + } } // Spawn ship diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 200d9fdd..29d4c0a0 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -949,7 +949,6 @@ namespace NewHorizons #endregion Load #region Change star system - public Action StarSystemChanging { get; set; } public void ChangeCurrentStarSystem(string newStarSystem, bool warp = false, bool vessel = false) { // If we're just on the title screen set the system for later @@ -978,8 +977,6 @@ namespace NewHorizons if (LoadManager.GetCurrentScene() == OWScene.SolarSystem || LoadManager.GetCurrentScene() == OWScene.EyeOfTheUniverse) { - StarSystemChanging?.Invoke(); - IsWarpingFromShip = warp; IsWarpingFromVessel = vessel; DidWarpFromVessel = false; diff --git a/NewHorizons/Patches/TimeLoopPatches.cs b/NewHorizons/Patches/TimeLoopPatches.cs index 5628fa15..d2b266fe 100644 --- a/NewHorizons/Patches/TimeLoopPatches.cs +++ b/NewHorizons/Patches/TimeLoopPatches.cs @@ -1,4 +1,5 @@ using HarmonyLib; +using NewHorizons.Handlers; namespace NewHorizons.Patches { @@ -14,5 +15,16 @@ namespace NewHorizons.Patches [HarmonyPatch(typeof(GlobalMusicController), nameof(GlobalMusicController.UpdateEndTimesMusic))] [HarmonyPatch(typeof(TimeLoop), nameof(TimeLoop.Update))] public static bool DisableWithoutTimeLoop() => Main.Instance.TimeLoopEnabled; + + [HarmonyPostfix] + [HarmonyPatch(typeof(TimeLoop), nameof(TimeLoop.Start))] + public static void TimeLoop_Start(TimeLoop __instance) + { + // If we took the AWC out of the main system make sure to disable time loop + if (Main.Instance.CurrentStarSystem != "SolarSystem" && HeldItemHandler.WasAWCTakenFromATP) + { + TimeLoop.SetTimeLoopEnabled(false); + } + } } }