From 7dc78ed8cbfe9cde6ee17400e6bad96c6ba550cc Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Fri, 9 Aug 2024 23:58:19 +0200 Subject: [PATCH] Check that you have the status 'OccupiedInQuestEvent' for at least a frame for interacting --- Questionable/Controller/QuestController.cs | 8 ++++ .../Controller/Steps/IConditionChangeAware.cs | 8 ++++ .../Controller/Steps/Interactions/Interact.cs | 47 ++++++++++++------- Questionable/DalamudInitializer.cs | 26 +++++++++- Questionable/QuestionablePlugin.cs | 4 +- 5 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 Questionable/Controller/Steps/IConditionChangeAware.cs diff --git a/Questionable/Controller/QuestController.cs b/Questionable/Controller/QuestController.cs index 34ff86b0..a737166e 100644 --- a/Questionable/Controller/QuestController.cs +++ b/Questionable/Controller/QuestController.cs @@ -81,6 +81,8 @@ internal sealed class QuestController : MiniTaskController _configuration = configuration; _yesAlreadyIpc = yesAlreadyIpc; _taskFactories = taskFactories.ToList().AsReadOnly(); + + _condition.ConditionChange += OnConditionChange; } public (QuestProgress Progress, ECurrentQuestType Type)? CurrentQuestDetails @@ -730,6 +732,12 @@ internal sealed class QuestController : MiniTaskController return IsRunning || DateTime.Now <= _lastTaskUpdate.Add(timeSpan); } + private void OnConditionChange(ConditionFlag flag, bool value) + { + if (_currentTask is IConditionChangeAware conditionChangeAware) + conditionChangeAware.OnConditionChange(flag, value); + } + public sealed record StepProgress( DateTime StartedAt, int PointMenuCounter = 0); diff --git a/Questionable/Controller/Steps/IConditionChangeAware.cs b/Questionable/Controller/Steps/IConditionChangeAware.cs new file mode 100644 index 00000000..4e41d638 --- /dev/null +++ b/Questionable/Controller/Steps/IConditionChangeAware.cs @@ -0,0 +1,8 @@ +using Dalamud.Game.ClientState.Conditions; + +namespace Questionable.Controller.Steps; + +public interface IConditionChangeAware +{ + void OnConditionChange(ConditionFlag flag, bool value); +} diff --git a/Questionable/Controller/Steps/Interactions/Interact.cs b/Questionable/Controller/Steps/Interactions/Interact.cs index cb2b3d6c..e45a2da7 100644 --- a/Questionable/Controller/Steps/Interactions/Interact.cs +++ b/Questionable/Controller/Steps/Interactions/Interact.cs @@ -44,10 +44,10 @@ internal static class Interact } internal sealed class DoInteract(GameFunctions gameFunctions, ICondition condition, ILogger logger) - : ITask + : ITask, IConditionChangeAware { private bool _needsUnmount; - private bool _interacted; + private EInteractionState _interactionState = EInteractionState.None; private DateTime _continueAt = DateTime.MinValue; private uint DataId { get; set; } @@ -84,9 +84,11 @@ internal static class Interact return true; } - if (IsTargetable(gameObject) && HasAnyMarker(gameObject)) + if (gameObject.IsTargetable && HasAnyMarker(gameObject)) { - _interacted = gameFunctions.InteractWith(gameObject); + _interactionState = gameFunctions.InteractWith(gameObject) + ? EInteractionState.InteractionTriggered + : EInteractionState.None; _continueAt = DateTime.Now.AddSeconds(0.5); return true; } @@ -111,18 +113,18 @@ internal static class Interact _needsUnmount = false; } - if (!_interacted) - { - IGameObject? gameObject = gameFunctions.FindObjectByDataId(DataId); - if (gameObject == null || !IsTargetable(gameObject) || !HasAnyMarker(gameObject)) - return ETaskResult.StillRunning; + if (_interactionState == EInteractionState.InteractionConfirmed) + return ETaskResult.TaskComplete; - _interacted = gameFunctions.InteractWith(gameObject); - _continueAt = DateTime.Now.AddSeconds(0.5); + IGameObject? gameObject = gameFunctions.FindObjectByDataId(DataId); + if (gameObject == null || !gameObject.IsTargetable || !HasAnyMarker(gameObject)) return ETaskResult.StillRunning; - } - return ETaskResult.TaskComplete; + _interactionState = gameFunctions.InteractWith(gameObject) + ? EInteractionState.InteractionTriggered + : EInteractionState.None; + _continueAt = DateTime.Now.AddSeconds(0.5); + return ETaskResult.StillRunning; } private unsafe bool HasAnyMarker(IGameObject gameObject) @@ -134,11 +136,24 @@ internal static class Interact return gameObjectStruct->NamePlateIconId != 0; } - private static bool IsTargetable(IGameObject gameObject) + public override string ToString() => $"Interact({DataId})"; + + public void OnConditionChange(ConditionFlag flag, bool value) { - return gameObject.IsTargetable; + logger.LogDebug("Condition change: {Flag} = {Value}", flag, value); + if (_interactionState == EInteractionState.InteractionTriggered && + flag == ConditionFlag.OccupiedInQuestEvent && value) + { + logger.LogInformation("Interaction was most likely triggered"); + _interactionState = EInteractionState.InteractionConfirmed; + } } - public override string ToString() => $"Interact({DataId})"; + private enum EInteractionState + { + None, + InteractionTriggered, + InteractionConfirmed, + } } } diff --git a/Questionable/DalamudInitializer.cs b/Questionable/DalamudInitializer.cs index 76678de6..239fd64c 100644 --- a/Questionable/DalamudInitializer.cs +++ b/Questionable/DalamudInitializer.cs @@ -1,7 +1,10 @@ using System; +using Dalamud.Game.Gui.Toast; +using Dalamud.Game.Text.SeStringHandling; using Dalamud.Interface.Windowing; using Dalamud.Plugin; using Dalamud.Plugin.Services; +using Microsoft.Extensions.Logging; using Questionable.Controller; using Questionable.Windows; @@ -17,6 +20,8 @@ internal sealed class DalamudInitializer : IDisposable private readonly WindowSystem _windowSystem; private readonly QuestWindow _questWindow; private readonly ConfigWindow _configWindow; + private readonly IToastGui _toastGui; + private readonly ILogger _logger; public DalamudInitializer( IDalamudPluginInterface pluginInterface, @@ -31,7 +36,9 @@ internal sealed class DalamudInitializer : IDisposable ConfigWindow configWindow, QuestSelectionWindow questSelectionWindow, QuestValidationWindow questValidationWindow, - JournalProgressWindow journalProgressWindow) + JournalProgressWindow journalProgressWindow, + IToastGui toastGui, + ILogger logger) { _pluginInterface = pluginInterface; _framework = framework; @@ -41,6 +48,8 @@ internal sealed class DalamudInitializer : IDisposable _windowSystem = windowSystem; _questWindow = questWindow; _configWindow = configWindow; + _toastGui = toastGui; + _logger = logger; _windowSystem.AddWindow(questWindow); _windowSystem.AddWindow(configWindow); @@ -54,6 +63,9 @@ internal sealed class DalamudInitializer : IDisposable _pluginInterface.UiBuilder.OpenConfigUi += _configWindow.Toggle; _framework.Update += FrameworkUpdate; _framework.RunOnTick(gameUiController.HandleCurrentDialogueChoices, TimeSpan.FromMilliseconds(200)); + _toastGui.Toast += OnToast; + _toastGui.ErrorToast += OnErrorToast; + _toastGui.QuestToast += OnQuestToast; } private void FrameworkUpdate(IFramework framework) @@ -71,8 +83,20 @@ internal sealed class DalamudInitializer : IDisposable } } + private void OnToast(ref SeString message, ref ToastOptions options, ref bool isHandled) + => _logger.LogInformation("Normal Toast: {Message}", message); + + private void OnErrorToast(ref SeString message, ref bool isHandled) + => _logger.LogInformation("Error Toast: {Message}", message); + + private void OnQuestToast(ref SeString message, ref QuestToastOptions options, ref bool isHandled) + => _logger.LogInformation("Quest Toast: {Message}", message); + public void Dispose() { + _toastGui.QuestToast -= OnQuestToast; + _toastGui.ErrorToast -= OnErrorToast; + _toastGui.Toast -= OnToast; _framework.Update -= FrameworkUpdate; _pluginInterface.UiBuilder.OpenConfigUi -= _configWindow.Toggle; _pluginInterface.UiBuilder.OpenMainUi -= _questWindow.Toggle; diff --git a/Questionable/QuestionablePlugin.cs b/Questionable/QuestionablePlugin.cs index 3c0c7842..df2fb357 100644 --- a/Questionable/QuestionablePlugin.cs +++ b/Questionable/QuestionablePlugin.cs @@ -46,7 +46,8 @@ public sealed class QuestionablePlugin : IDalamudPlugin ICommandManager commandManager, IAddonLifecycle addonLifecycle, IKeyState keyState, - IContextMenu contextMenu) + IContextMenu contextMenu, + IToastGui toastGui) { ArgumentNullException.ThrowIfNull(pluginInterface); ArgumentNullException.ThrowIfNull(chatGui); @@ -72,6 +73,7 @@ public sealed class QuestionablePlugin : IDalamudPlugin serviceCollection.AddSingleton(addonLifecycle); serviceCollection.AddSingleton(keyState); serviceCollection.AddSingleton(contextMenu); + serviceCollection.AddSingleton(toastGui); serviceCollection.AddSingleton(new WindowSystem(nameof(Questionable))); serviceCollection.AddSingleton((Configuration?)pluginInterface.GetPluginConfig() ?? new Configuration());