Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
codelastnight committed Jul 5, 2024
1 parent d941e24 commit efe8655
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 68 deletions.
21 changes: 21 additions & 0 deletions OofPlugin/Dalamud.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Dalamud.IoC;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;

namespace OofPlugin;

public class Dalamud
{
[PluginService] internal static IFramework Framework { get; private set; } = null!;
[PluginService] internal static IDalamudPluginInterface PluginInterface { get; private set; } = null!;
[PluginService] internal static ICommandManager CommandManager { get; private set; } = null!;
[PluginService] internal static ICondition Condition { get; private set; } = null!;
[PluginService] internal static IPartyList PartyList { get; private set; } = null!;

[PluginService] public static ITextureProvider TextureProvider { get; private set; } = null!;
[PluginService] public static IPluginLog Log { get; private set; } = null!;
[PluginService] internal static IClientState ClientState { get; private set; } = null!;

public static void Initialize(IDalamudPluginInterface pluginInterface)
=> pluginInterface.Create<Dalamud>();
}
107 changes: 47 additions & 60 deletions OofPlugin/OofPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.Command;
using Dalamud.Interface.Windowing;
using Dalamud.IoC;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Dalamud.Utility;
using OofPlugin.Windows;
using System;
using System.Linq;

//shoutout anna clemens

Expand All @@ -20,17 +18,9 @@ public sealed class OofPlugin : IDalamudPlugin
private const string oofCommand = "/oof";
private const string oofSettings = "/oofsettings";
private const string oofVideo = "/oofvideo";
[PluginService] internal static IFramework Framework { get; private set; } = null!;
[PluginService] internal static IDalamudPluginInterface PluginInterface { get; private set; } = null!;
[PluginService] internal static ICommandManager CommandManager { get; private set; } = null!;
[PluginService] internal static IClientState ClientState { get; private set; } = null!;
[PluginService] internal static ICondition Condition { get; private set; } = null!;
[PluginService] internal static IPartyList PartyList { get; private set; } = null!;
[PluginService] internal static IPluginLog PluginLog { get; set; } = null!;
[PluginService] internal static ITextureProvider TextureProvider { get; private set; } = null!;
public Configuration Configuration { get; init; }

public readonly WindowSystem WindowSystem = new("OofPlugin");
public Configuration Configuration { get; init; }
private ConfigWindow ConfigWindow { get; init; }
internal DeadPlayersList DeadPlayersList { get; init; }
internal SoundManager SoundManager { get; init; }
Expand All @@ -44,11 +34,12 @@ public sealed class OofPlugin : IDalamudPlugin
private bool wasJumping { get; set; } = false;


public OofPlugin()
public OofPlugin(IDalamudPluginInterface pluginInterface)
{
Dalamud.Initialize(pluginInterface);

Configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
Configuration.Initialize(PluginInterface);
Configuration = pluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
Configuration.Initialize(pluginInterface);

SoundManager = new SoundManager(this);
DeadPlayersList = new DeadPlayersList();
Expand All @@ -57,29 +48,27 @@ public OofPlugin()
ConfigWindow = new ConfigWindow(this);
WindowSystem.AddWindow(ConfigWindow);

PluginInterface.UiBuilder.Draw += DrawUI;
PluginInterface.UiBuilder.OpenConfigUi += ToggleConfigUI;
Framework.Update += FrameworkOnUpdate;
Dalamud.PluginInterface.UiBuilder.Draw += DrawUI;
Dalamud.PluginInterface.UiBuilder.OpenConfigUi += ToggleConfigUI;
Dalamud.Framework.Update += FrameworkOnUpdate;

CommandManager.AddHandler(oofCommand, new CommandInfo(OnCommand)
Dalamud.CommandManager.AddHandler(oofCommand, new CommandInfo(OnCommand)
{
HelpMessage = "play oof sound"
});
CommandManager.AddHandler(oofSettings, new CommandInfo(OnCommand)
Dalamud.CommandManager.AddHandler(oofSettings, new CommandInfo(OnCommand)
{
HelpMessage = "change oof settings"
});
CommandManager.AddHandler(oofVideo, new CommandInfo(OnCommand)
Dalamud.CommandManager.AddHandler(oofVideo, new CommandInfo(OnCommand)
{
HelpMessage = "open Hbomberguy video on OOF.mp3"
});

}


private void DrawUI() => WindowSystem.Draw();
public void ToggleConfigUI() => ConfigWindow.Toggle();

private void OnCommand(string command, string args)
{
if (command == oofCommand) SoundManager.Play(SoundManager.CancelToken.Token);
Expand All @@ -90,10 +79,10 @@ private void OnCommand(string command, string args)

private void FrameworkOnUpdate(IFramework framework)
{
if (ClientState == null || ClientState.LocalPlayer == null) return;
if (Dalamud.ClientState == null || Dalamud.ClientState.LocalPlayer == null) return;
// dont run btwn moving areas
if (Condition[ConditionFlag.BetweenAreas] || Condition[ConditionFlag.BetweenAreas51] || Condition[ConditionFlag.BeingMoved]) return;
if (Condition[ConditionFlag.WatchingCutscene] || Condition[ConditionFlag.WatchingCutscene78]) return;
if (Dalamud.Condition[ConditionFlag.BetweenAreas] || Dalamud.Condition[ConditionFlag.BetweenAreas51] || Dalamud.Condition[ConditionFlag.BeingMoved]) return;
if (Dalamud.Condition[ConditionFlag.WatchingCutscene] || Dalamud.Condition[ConditionFlag.WatchingCutscene78]) return;


try
Expand All @@ -103,7 +92,7 @@ private void FrameworkOnUpdate(IFramework framework)
}
catch (Exception e)
{
PluginLog.Error("failed to check for oof condition:", e.Message);
Dalamud.Log.Error("failed to check for oof condition:", e.Message);
}
}

Expand All @@ -114,55 +103,53 @@ private void FrameworkOnUpdate(IFramework framework)
private void CheckDeath()
{

if (!Configuration.OofOnDeathBattle && Condition[ConditionFlag.InCombat]) return;
if (!Configuration.OofOnDeathBattle && Dalamud.Condition[ConditionFlag.InCombat]) return;

if (PartyList != null && PartyList.Any())
// if not in party
if (Configuration.OofOnDeathSelf && (Dalamud.PartyList == null || Dalamud.PartyList.Length == 0))
{
if (Configuration.OofOnDeathAlliance && PartyList.Length == 8 && PartyList.GetAllianceMemberAddress(0) != IntPtr.Zero) // the worst "is alliance" check
DeadPlayersList.AddRemoveDeadPlayer(Dalamud.ClientState.LocalPlayer!);
return;
}
// if in alliance
if (Configuration.OofOnDeathAlliance && Dalamud.PartyList.Length == 8 && Dalamud.PartyList.GetAllianceMemberAddress(0) != IntPtr.Zero) // the worst "is alliance" check
{
try
{
try
{
for (int i = 0; i < 16; i++)
{
var allianceMemberAddress = PartyList.GetAllianceMemberAddress(i);
if (allianceMemberAddress == IntPtr.Zero) throw new NullReferenceException("allience member address is null");

var allianceMember = PartyList.CreateAllianceMemberReference(allianceMemberAddress) ?? throw new NullReferenceException("allience reference is null");
DeadPlayersList.AddRemoveDeadPlayer(allianceMember);
}
}
catch (Exception e)
for (int i = 0; i < 16; i++)
{
PluginLog.Error("failed alliance check", e.Message);
var allianceMemberAddress = Dalamud.PartyList.GetAllianceMemberAddress(i);
if (allianceMemberAddress == IntPtr.Zero) throw new NullReferenceException("allience member address is null");

var allianceMember = Dalamud.PartyList.CreateAllianceMemberReference(allianceMemberAddress) ?? throw new NullReferenceException("allience reference is null");
DeadPlayersList.AddRemoveDeadPlayer(allianceMember);
}
}
if (Configuration.OofOnDeathParty)
catch (Exception e)
{
foreach (var member in PartyList)
{
DeadPlayersList.AddRemoveDeadPlayer(member, member.Territory.Id == ClientState.TerritoryType);
}
Dalamud.Log.Error("failed alliance check", e.Message);
}

}
else
//if in party
if (Configuration.OofOnDeathParty)
{
if (!Configuration.OofOnDeathSelf) return;
DeadPlayersList.AddRemoveDeadPlayer(ClientState.LocalPlayer!);
foreach (var member in Dalamud.PartyList)
{
DeadPlayersList.AddRemoveDeadPlayer(member, member.Territory.Id == Dalamud.ClientState.TerritoryType);
}
}

}

/// <summary>
/// check if player has taken fall damage (brute force way)
/// </summary>
private void CheckFallen()
{
if (!Configuration.OofOnFallBattle && Condition[ConditionFlag.InCombat]) return;
if (!Configuration.OofOnFallMounted && (Condition[ConditionFlag.Mounted] || Condition[ConditionFlag.Mounted2])) return;
if (!Configuration.OofOnFallBattle && Dalamud.Condition[ConditionFlag.InCombat]) return;
if (!Configuration.OofOnFallMounted && (Dalamud.Condition[ConditionFlag.Mounted] || Dalamud.Condition[ConditionFlag.Mounted2])) return;

var isJumping = Condition[ConditionFlag.Jumping];
var pos = ClientState!.LocalPlayer!.Position.Y;
var isJumping = Dalamud.Condition[ConditionFlag.Jumping];
var pos = Dalamud.ClientState!.LocalPlayer!.Position.Y;
var velocity = prevPos - pos;

if (isJumping && !wasJumping)
Expand Down Expand Up @@ -198,11 +185,11 @@ public void Dispose()
ConfigWindow.Dispose();
SoundManager.Dispose();

CommandManager.RemoveHandler(oofCommand);
CommandManager.RemoveHandler(oofSettings);
CommandManager.RemoveHandler(oofVideo);
Dalamud.CommandManager.RemoveHandler(oofCommand);
Dalamud.CommandManager.RemoveHandler(oofSettings);
Dalamud.CommandManager.RemoveHandler(oofVideo);

Framework.Update -= FrameworkOnUpdate;
Dalamud.Framework.Update -= FrameworkOnUpdate;

}
}
14 changes: 7 additions & 7 deletions OofPlugin/SoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void LoadFile()
{
if (Configuration.DefaultSoundImportPath.Length == 0)
{
var path = Path.Combine(OofPlugin.PluginInterface.AssemblyLocation.Directory?.FullName!, "oof.wav");
var path = Path.Combine(Dalamud.PluginInterface.AssemblyLocation.Directory?.FullName!, "oof.wav");
soundFile = path;
return;
}
Expand Down Expand Up @@ -68,7 +68,7 @@ public void Play(CancellationToken token, float volume = 1)
catch (Exception ex)
{
isSoundPlaying = false;
OofPlugin.PluginLog.Error("Failed read file", ex);
Dalamud.Log.Error("Failed read file", ex);
return;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ void OnPlaybackStopped(object? sender, StoppedEventArgs e)
catch (Exception ex)
{
isSoundPlaying = false;
OofPlugin.PluginLog.Error("Failed play sound", ex);
Dalamud.Log.Error("Failed play sound", ex);
return;
}
}
Expand All @@ -123,15 +123,15 @@ private async Task OofAudioPolling(CancellationToken token)
await Task.Delay(200, token);
if (token.IsCancellationRequested) break;
if (!DeadPlayersList.DeadPlayers.Any()) continue;
if (OofPlugin.ClientState!.LocalPlayer! == null) continue;
if (Dalamud.ClientState!.LocalPlayer! == null) continue;
foreach (var player in DeadPlayersList.DeadPlayers)
{
if (player.DidPlayOof) continue;
float volume = 1f;
if (Configuration.DistanceBasedOof && player.Distance != OofPlugin.ClientState!.LocalPlayer!.Position)
if (Configuration.DistanceBasedOof && player.Distance != Dalamud.ClientState!.LocalPlayer!.Position)
{
var dist = 0f;
if (player.Distance != Vector3.Zero) dist = Vector3.Distance(OofPlugin.ClientState!.LocalPlayer!.Position, player.Distance);
if (player.Distance != Vector3.Zero) dist = Vector3.Distance(Dalamud.ClientState!.LocalPlayer!.Position, player.Distance);
volume = VolumeFromDist(dist);
}
Play(token, volume);
Expand Down Expand Up @@ -182,7 +182,7 @@ public void Dispose()
}
catch (Exception e)
{
OofPlugin.PluginLog.Error("Failed to dispose oofplugin controller", e.Message);
Dalamud.Log.Error("Failed to dispose oofplugin controller", e.Message);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion OofPlugin/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ private void OnFallConfigUI()
{
Configuration.OofOnFallMounted = value;
});

ImGui.Columns(1);
}
/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions OofPlugin/Windows/UIComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,14 @@ public void Checkbox(string name, bool value, Action<bool> callback)
Configuration.Save();
}
}
public void CheckboxTest(string name, ref bool value)
{
var isChecked = value;
if (ImGui.Checkbox(name, ref isChecked))
{
value = isChecked;
Configuration.Save();
}
}
}

0 comments on commit efe8655

Please sign in to comment.