From a217df91683defea781d41fe101b9a2c3120fab3 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 13 May 2022 15:00:58 -0700 Subject: [PATCH] ConversationStartEndMessage: make it a QSBWorldObjectMessage --- QSB/ConversationSync/ConversationManager.cs | 12 +++--- .../Messages/ConversationStartEndMessage.cs | 42 +++++-------------- .../Patches/ConversationPatches.cs | 4 +- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/QSB/ConversationSync/ConversationManager.cs b/QSB/ConversationSync/ConversationManager.cs index 38993a91..2b1a78f7 100644 --- a/QSB/ConversationSync/ConversationManager.cs +++ b/QSB/ConversationSync/ConversationManager.cs @@ -45,11 +45,9 @@ public class ConversationManager : WorldObjectManager QSBWorldSync.Init(); } - public uint GetPlayerTalkingToTree(CharacterDialogueTree tree) - { - var player = QSBPlayerManager.PlayerList.FirstOrDefault(x => x.CurrentCharacterDialogueTree?.AttachedObject == tree); - return player != null ? player.PlayerId : uint.MaxValue; - } + public uint GetPlayerTalkingToTree(CharacterDialogueTree tree) => + QSBPlayerManager.PlayerList.FirstOrDefault(x => x.CurrentCharacterDialogueTree?.AttachedObject == tree) + ?.PlayerId ?? uint.MaxValue; public void SendPlayerOption(string text) => new ConversationMessage(ConversationType.Player, (int)QSBPlayerManager.LocalPlayerId, text).Send(); @@ -63,8 +61,8 @@ public class ConversationManager : WorldObjectManager public void CloseBoxCharacter(int id) => new ConversationMessage(ConversationType.CloseCharacter, id).Send(); - public void SendConvState(int charId, bool state) - => new ConversationStartEndMessage(charId, state).Send(); + public void SendConvState(QSBCharacterDialogueTree tree, bool state) + => tree.SendMessage(new ConversationStartEndMessage(state)); public void DisplayPlayerConversationBox(uint playerId, string text) { diff --git a/QSB/ConversationSync/Messages/ConversationStartEndMessage.cs b/QSB/ConversationSync/Messages/ConversationStartEndMessage.cs index d8c24fef..ce88fc4b 100644 --- a/QSB/ConversationSync/Messages/ConversationStartEndMessage.cs +++ b/QSB/ConversationSync/Messages/ConversationStartEndMessage.cs @@ -1,47 +1,27 @@ -using OWML.Common; -using QSB.ConversationSync.WorldObjects; +using QSB.ConversationSync.WorldObjects; using QSB.Messaging; using QSB.Player; using QSB.Utility; -using QSB.WorldSync; namespace QSB.ConversationSync.Messages; -public class ConversationStartEndMessage : QSBMessage<(int TreeId, bool Start)> +public class ConversationStartEndMessage : QSBWorldObjectMessage { - public ConversationStartEndMessage(int treeId, bool start) : base((treeId, start)) { } - - public override bool ShouldReceive => QSBWorldSync.AllObjectsReady; + public ConversationStartEndMessage(bool start) : base(start) { } public override void OnReceiveRemote() { - var dialogueTree = Data.TreeId.GetWorldObject(); - - if (Data.Start) + if (Data) { - StartConversation(From, dialogueTree); + QSBPlayerManager.GetPlayer(From).CurrentCharacterDialogueTree = WorldObject; + WorldObject.AttachedObject.GetInteractVolume().DisableInteraction(); + WorldObject.AttachedObject.RaiseEvent(nameof(CharacterDialogueTree.OnStartConversation)); } else { - EndConversation(From, dialogueTree); + QSBPlayerManager.GetPlayer(From).CurrentCharacterDialogueTree = null; + WorldObject.AttachedObject.GetInteractVolume().EnableInteraction(); + WorldObject.AttachedObject.RaiseEvent(nameof(CharacterDialogueTree.OnEndConversation)); } } - - private static void StartConversation( - uint playerId, - QSBCharacterDialogueTree tree) - { - QSBPlayerManager.GetPlayer(playerId).CurrentCharacterDialogueTree = tree; - tree.AttachedObject.GetInteractVolume().DisableInteraction(); - tree.AttachedObject.RaiseEvent(nameof(CharacterDialogueTree.OnStartConversation)); - } - - private static void EndConversation( - uint playerId, - QSBCharacterDialogueTree tree) - { - QSBPlayerManager.GetPlayer(playerId).CurrentCharacterDialogueTree = null; - tree.AttachedObject.GetInteractVolume().EnableInteraction(); - tree.AttachedObject.RaiseEvent(nameof(CharacterDialogueTree.OnEndConversation)); - } -} \ No newline at end of file +} diff --git a/QSB/ConversationSync/Patches/ConversationPatches.cs b/QSB/ConversationSync/Patches/ConversationPatches.cs index d3200624..7b835540 100644 --- a/QSB/ConversationSync/Patches/ConversationPatches.cs +++ b/QSB/ConversationSync/Patches/ConversationPatches.cs @@ -27,7 +27,7 @@ public class ConversationPatches : QSBPatch var worldObject = __instance.GetWorldObject(); QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree = worldObject; - ConversationManager.Instance.SendConvState(worldObject.ObjectId, true); + ConversationManager.Instance.SendConvState(worldObject, true); } [HarmonyPrefix] @@ -45,7 +45,7 @@ public class ConversationPatches : QSBPatch return true; } - ConversationManager.Instance.SendConvState(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree.ObjectId, false); + ConversationManager.Instance.SendConvState(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree, false); ConversationManager.Instance.CloseBoxCharacter(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree.ObjectId); QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree = null; ConversationManager.Instance.CloseBoxPlayer();