From d8714b627bb9b2e33e43a005f442c28f7c09c093 Mon Sep 17 00:00:00 2001 From: ALittlePatate Date: Sat, 10 Sep 2022 21:23:03 +0200 Subject: [PATCH] add: now using coroutine for getting the localplayer --- DevourClient/Hacks/Misc.cs | 8 +++--- DevourClient/Helpers/StateHelper.cs | 41 ++++++++++++++++------------- DevourClient/MelonMain.cs | 3 ++- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/DevourClient/Hacks/Misc.cs b/DevourClient/Hacks/Misc.cs index 61bf32f..5f9e375 100644 --- a/DevourClient/Hacks/Misc.cs +++ b/DevourClient/Hacks/Misc.cs @@ -48,13 +48,13 @@ namespace DevourClient.Hacks public static void WalkInLobby(bool walk) { try { - if (Helpers.LocalPlayer.GetLocalPlayer().GetComponent() == null) + if (Helpers.LocalPlayer.LocalPlayer_.GetComponent() == null) { - Helpers.LocalPlayer.GetLocalPlayer().AddComponent(); - Helpers.LocalPlayer.GetLocalPlayer().GetComponent().enabled = false; + Helpers.LocalPlayer.LocalPlayer_.AddComponent(); + Helpers.LocalPlayer.LocalPlayer_.GetComponent().enabled = false; } - Helpers.LocalPlayer.GetLocalPlayer().GetComponent().enabled = walk; + Helpers.LocalPlayer.LocalPlayer_.GetComponent().enabled = walk; } catch { return; } } diff --git a/DevourClient/Helpers/StateHelper.cs b/DevourClient/Helpers/StateHelper.cs index 789cd99..77e2176 100644 --- a/DevourClient/Helpers/StateHelper.cs +++ b/DevourClient/Helpers/StateHelper.cs @@ -1,8 +1,9 @@ using UnityEngine; using Opsive.UltimateCharacterController.Character; using System.Collections.Generic; +using System.Collections; -namespace DevourClient.Helpers +namespace DevourClient.Helpers { public class Player { @@ -19,14 +20,12 @@ using System.Collections.Generic; public static NolanBehaviour GetPlayer() { - foreach (NolanBehaviour nb in UnityEngine.Object.FindObjectsOfType()) + if (LocalPlayer.LocalPlayer_ == null) { - if (nb.entity.IsOwner) - { - return nb; - } + return null; } - return null; + + return LocalPlayer.LocalPlayer_.GetComponent(); } public static bool IsPlayerCrawling() @@ -45,22 +44,26 @@ using System.Collections.Generic; public class LocalPlayer { - public static GameObject player; + public static GameObject LocalPlayer_; - public static GameObject GetLocalPlayer() + public static IEnumerator GetLocalPlayer() { - GameObject[] currentPlayers = GameObject.FindGameObjectsWithTag("Player"); - - for (int i = 0; i < currentPlayers.Length; i++) + for (;;) { - if (currentPlayers[i].GetComponent().entity.IsOwner) - { - player = currentPlayers[i]; - break; - } - } + GameObject[] currentPlayers = GameObject.FindGameObjectsWithTag("Player"); - return player; + for (int i = 0; i < currentPlayers.Length; i++) + { + if (currentPlayers[i].GetComponent().entity.IsOwner) + { + LocalPlayer_ = currentPlayers[i]; + break; + } + } + + // Wait 5 seconds before caching objects again. + yield return new WaitForSeconds(5f); + } } public static List GetAllPlayers() diff --git a/DevourClient/MelonMain.cs b/DevourClient/MelonMain.cs index de683f6..7020c02 100644 --- a/DevourClient/MelonMain.cs +++ b/DevourClient/MelonMain.cs @@ -39,6 +39,7 @@ namespace DevourClient public override void OnApplicationStart() { MelonLogger.Msg("For the Queen !"); + MelonCoroutines.Start(Helpers.LocalPlayer.GetLocalPlayer()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -129,7 +130,7 @@ namespace DevourClient { try { - Helpers.LocalPlayer.GetLocalPlayer().GetComponent().TimeScale = this._PlayerSpeedMultiplier; + Helpers.LocalPlayer.LocalPlayer_.GetComponent().TimeScale = this._PlayerSpeedMultiplier; } catch { return; } }