CurrencyAlertClassic/SamplePlugin/Plugin.cs

90 lines
3.0 KiB
C#
Raw Permalink Normal View History

2022-01-05 14:46:15 +00:00
using CurrencyAlert.Enum;
2022-01-06 09:56:52 +00:00
using Dalamud.Game.Command;
2022-01-05 14:46:15 +00:00
using Dalamud.Game.Gui;
2022-01-06 09:56:52 +00:00
using Dalamud.IoC;
using Dalamud.Plugin;
2022-01-05 14:46:15 +00:00
using FFXIVClientStructs.FFXIV.Client.Game;
2022-01-06 09:56:52 +00:00
using System.IO;
using System.Reflection;
namespace CurrencyAlert
{
public sealed class Plugin : IDalamudPlugin
{
public string Name => "Currency Alert";
private const string commandName = "/currencyalert";
private DalamudPluginInterface PluginInterface { get; init; }
private CommandManager CommandManager { get; init; }
private Configuration Configuration { get; init; }
private PluginUI PluginUI { get; init; }
[PluginService] public static ChatGui Chat { get; private set; } = null!;
public Plugin(
[RequiredVersion("1.0")] DalamudPluginInterface pluginInterface,
[RequiredVersion("1.0")] CommandManager commandManager)
{
this.PluginInterface = pluginInterface;
this.CommandManager = commandManager;
this.Configuration = this.PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
this.Configuration.Initialize(this.PluginInterface);
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
this.PluginUI = new PluginUI(this.Configuration);
this.CommandManager.AddHandler(commandName, new CommandInfo(OnCommand)
{
HelpMessage = "Lets you configure alert thresholds for various currencies"
2022-01-05 14:46:15 +00:00
});
2022-01-06 09:56:52 +00:00
this.PluginInterface.UiBuilder.Draw += DrawUI;
this.PluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
2022-01-05 14:46:15 +00:00
}
2022-01-06 09:56:52 +00:00
public void Dispose()
{
this.PluginUI.Dispose();
this.CommandManager.RemoveHandler(commandName);
}
private void OnCommand(string command, string args)
{
this.DrawConfigUI();
}
private void DrawUI()
{
// TODO: move this logic elsewhere
2022-01-05 14:46:15 +00:00
unsafe
{
InventoryManager* inventoryManager = InventoryManager.Instance();
EnumHelper.Each<Currency>(currency =>
{
2022-01-19 23:01:25 +00:00
var itemID = EnumHelper.GetAttributeOfType<ItemIDAttribute>(currency).Value;
int quantity = inventoryManager->GetInventoryItemCount((uint)itemID);
2022-01-05 14:46:15 +00:00
if (this.Configuration.AlertEnabled[currency] && quantity >= this.Configuration.Threshold[currency])
{
this.PluginUI.AlertVisible[currency] = true;
}
else
{
this.PluginUI.AlertVisible[currency] = false;
}
2022-01-06 09:56:52 +00:00
});
}
this.PluginUI.Draw();
}
private void DrawConfigUI()
{
this.PluginUI.SettingsVisible = true;
}
}
}