From 62fc1ea66b562428b59ecfe50253cc9cb4f41744 Mon Sep 17 00:00:00 2001 From: goaaats Date: Sun, 17 Apr 2022 16:33:41 +0200 Subject: [PATCH] feat: add ClientState IsPvP, EnterPvP, LeavePvP to help plugins with disabling themselves --- Dalamud/Game/ClientState/ClientState.cs | 47 ++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/Dalamud/Game/ClientState/ClientState.cs b/Dalamud/Game/ClientState/ClientState.cs index 0d50c2002..f391d6c55 100644 --- a/Dalamud/Game/ClientState/ClientState.cs +++ b/Dalamud/Game/ClientState/ClientState.cs @@ -1,9 +1,9 @@ using System; using System.Runtime.InteropServices; +using Dalamud.Data; using Dalamud.Game.ClientState.Aetherytes; using Dalamud.Game.ClientState.Buddy; -using Dalamud.Game.ClientState.Conditions; using Dalamud.Game.ClientState.Fates; using Dalamud.Game.ClientState.GamePad; using Dalamud.Game.ClientState.JobGauge; @@ -17,6 +17,7 @@ using Dalamud.Hooking; using Dalamud.IoC; using Dalamud.IoC.Internal; using Dalamud.Utility; +using Lumina.Excel.GeneratedSheets; using Serilog; namespace Dalamud.Game.ClientState @@ -32,6 +33,7 @@ namespace Dalamud.Game.ClientState private readonly Hook setupTerritoryTypeHook; private bool lastConditionNone = true; + private bool lastFramePvP = false; /// /// Initializes a new instance of the class. @@ -95,6 +97,16 @@ namespace Dalamud.Game.ClientState /// public event EventHandler Logout; + /// + /// Event that fires when a character is entering PvP. + /// + public event System.Action EnterPvP; + + /// + /// Event that fires when a character is leaving PvP. + /// + public event System.Action LeavePvP; + /// /// Event that gets fired when a duty is ready. /// @@ -125,12 +137,17 @@ namespace Dalamud.Game.ClientState /// public bool IsLoggedIn { get; private set; } + /// + /// Gets a value indicating whether or not the user is playing PvP. + /// + public bool IsPvP { get; private set; } + /// /// Enable this module. /// public void Enable() { - Service.Get().Enable(); + Service.Get().Enable(); Service.Get().Enable(); this.setupTerritoryTypeHook.Enable(); } @@ -141,7 +158,7 @@ namespace Dalamud.Game.ClientState void IDisposable.Dispose() { this.setupTerritoryTypeHook.Dispose(); - Service.Get().ExplicitDispose(); + Service.Get().ExplicitDispose(); Service.Get().ExplicitDispose(); Service.Get().Update -= this.FrameworkOnOnUpdateEvent; Service.Get().CfPop -= this.NetworkHandlersOnCfPop; @@ -164,8 +181,10 @@ namespace Dalamud.Game.ClientState private void FrameworkOnOnUpdateEvent(Framework framework) { - var condition = Service.Get(); + var condition = Service.Get(); var gameGui = Service.Get(); + var data = Service.Get(); + if (condition.Any() && this.lastConditionNone == true) { Log.Debug("Is login"); @@ -183,6 +202,26 @@ namespace Dalamud.Game.ClientState this.Logout?.Invoke(this, null); gameGui.ResetUiHideState(); } + + if (this.TerritoryType != 0) + { + var terriRow = data.GetExcelSheet()!.GetRow(this.TerritoryType); + this.IsPvP = terriRow?.Bg.RawString.StartsWith("ffxiv/pvp") ?? false; + } + + if (this.IsPvP != this.lastFramePvP) + { + this.lastFramePvP = this.IsPvP; + + if (this.IsPvP) + { + this.EnterPvP?.Invoke(); + } + else + { + this.LeavePvP?.Invoke(); + } + } } } }