From 399073c675175c62c2f6ddee91d1bea5a66cfbef Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 27 Aug 2022 02:28:10 -0400 Subject: [PATCH] Fix syncing state, current user of probe launcher --- .../StationaryProbeLauncherMessage.cs | 6 +-- .../Patches/StationaryProbeLauncherPatches.cs | 8 ++-- .../QSBStationaryProbeLauncher.cs | 44 +++++++++++-------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/QSB/StationaryProbeLauncherSync/Messages/StationaryProbeLauncherMessage.cs b/QSB/StationaryProbeLauncherSync/Messages/StationaryProbeLauncherMessage.cs index a4ac5e49..a4e9eb19 100644 --- a/QSB/StationaryProbeLauncherSync/Messages/StationaryProbeLauncherMessage.cs +++ b/QSB/StationaryProbeLauncherSync/Messages/StationaryProbeLauncherMessage.cs @@ -3,9 +3,9 @@ using QSB.StationaryProbeLauncherSync.WorldObjects; namespace QSB.StationaryProbeLauncherSync.Messages; -public class StationaryProbeLauncherMessage : QSBWorldObjectMessage +public class StationaryProbeLauncherMessage : QSBWorldObjectMessage { - public StationaryProbeLauncherMessage(bool inUse) : base(inUse) { } + public StationaryProbeLauncherMessage(bool inUse, uint userID) : base((inUse, userID)) { } - public override void OnReceiveRemote() => WorldObject.OnRemoteUseStateChanged(Data, From); + public override void OnReceiveRemote() => WorldObject.OnRemoteUseStateChanged(Data.Item1, Data.Item2); } diff --git a/QSB/StationaryProbeLauncherSync/Patches/StationaryProbeLauncherPatches.cs b/QSB/StationaryProbeLauncherSync/Patches/StationaryProbeLauncherPatches.cs index dd0665f1..3e0b8bbb 100644 --- a/QSB/StationaryProbeLauncherSync/Patches/StationaryProbeLauncherPatches.cs +++ b/QSB/StationaryProbeLauncherSync/Patches/StationaryProbeLauncherPatches.cs @@ -1,6 +1,7 @@ using HarmonyLib; using QSB.Messaging; using QSB.Patches; +using QSB.Player; using QSB.StationaryProbeLauncherSync.Messages; using QSB.StationaryProbeLauncherSync.WorldObjects; using QSB.WorldSync; @@ -14,9 +15,6 @@ public class StationaryProbeLauncherPatches : QSBPatch [HarmonyPostfix] [HarmonyPatch(typeof(StationaryProbeLauncher), nameof(StationaryProbeLauncher.FinishExitSequence))] - public static void StationaryProbeLauncher_FinishExitSequence(StationaryProbeLauncher __instance) - { - var qsbStationaryProbe = __instance.GetWorldObject(); - qsbStationaryProbe.SendMessage(new StationaryProbeLauncherMessage(false)); - } + public static void StationaryProbeLauncher_FinishExitSequence(StationaryProbeLauncher __instance) => + __instance.GetWorldObject().OnLocalUseStateChanged(false); } diff --git a/QSB/StationaryProbeLauncherSync/WorldObjects/QSBStationaryProbeLauncher.cs b/QSB/StationaryProbeLauncherSync/WorldObjects/QSBStationaryProbeLauncher.cs index d4e2de74..e1aca33e 100644 --- a/QSB/StationaryProbeLauncherSync/WorldObjects/QSBStationaryProbeLauncher.cs +++ b/QSB/StationaryProbeLauncherSync/WorldObjects/QSBStationaryProbeLauncher.cs @@ -13,6 +13,8 @@ namespace QSB.StationaryProbeLauncherSync.WorldObjects; public class QSBStationaryProbeLauncher : QSBProbeLauncher, ILinkedWorldObject { + private uint _currentUser = uint.MaxValue; + public StationaryProbeLauncherVariableSync NetworkBehaviour { get; private set; } public void SetNetworkBehaviour(NetworkBehaviour networkBehaviour) => NetworkBehaviour = (StationaryProbeLauncherVariableSync)networkBehaviour; @@ -21,8 +23,8 @@ public class QSBStationaryProbeLauncher : QSBProbeLauncher, ILinkedWorldObject OnLocalUseStateChanged(true); public override void SendInitialState(uint to) { base.SendInitialState(to); - // even tho from is always host here, it's okay because it won't set authority - // (since OnRemoteUseStateChanged is only called from OnReceiveRemote) - this.SendMessage(new StationaryProbeLauncherMessage(_isInUse) { To = to }); + this.SendMessage(new StationaryProbeLauncherMessage(_isInUse, _currentUser) { To = to }); } public void OnRemoteUseStateChanged(bool isInUse, uint user) { + _isInUse = isInUse; + + _currentUser = isInUse ? user : uint.MaxValue; + // Whoever is using it needs authority to be able to rotate it if (QSBCore.IsHost) { - NetworkBehaviour.netIdentity.SetAuthority(isInUse ? user : uint.MaxValue); + NetworkBehaviour.netIdentity.SetAuthority(_currentUser); } + UpdateUse(); + } + + public void OnLocalUseStateChanged(bool isInUse) + { _isInUse = isInUse; - UpdateUse(); + _currentUser = isInUse ? QSBPlayerManager.LocalPlayerId : uint.MaxValue; + + // Whoever is using it needs authority to be able to rotate it + if (QSBCore.IsHost) + { + NetworkBehaviour.netIdentity.SetAuthority(_currentUser); + } + + this.SendMessage(new StationaryProbeLauncherMessage(isInUse, _currentUser)); } private void UpdateUse()