CurrencyAlertClassic/SamplePlugin/Plugin.cs

92 lines
3.1 KiB
C#
Raw Normal View History

2022-01-05 14:46:15 +00:00
using CurrencyAlert.Enum;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
2021-08-26 14:11:18 +00:00
using Dalamud.IoC;
using Dalamud.Plugin;
2022-01-05 14:46:15 +00:00
using FFXIVClientStructs.FFXIV.Client.Game;
using System.IO;
using System.Reflection;
2021-12-11 13:27:58 +00:00
namespace CurrencyAlert
{
2021-08-26 14:11:18 +00:00
public sealed class Plugin : IDalamudPlugin
{
2021-12-11 11:02:01 +00:00
public string Name => "Currency Alert";
2021-12-11 11:02:01 +00:00
private const string commandName = "/currencyalert";
2021-08-26 14:11:18 +00:00
private DalamudPluginInterface PluginInterface { get; init; }
private CommandManager CommandManager { get; init; }
private Configuration Configuration { get; init; }
2022-01-05 14:46:15 +00:00
private PluginUI PluginUI { get; init; }
[PluginService] public static ChatGui Chat { get; private set; } = null!;
2021-08-26 14:11:18 +00:00
public Plugin(
[RequiredVersion("1.0")] DalamudPluginInterface pluginInterface,
[RequiredVersion("1.0")] CommandManager commandManager)
{
2021-08-26 14:11:18 +00:00
this.PluginInterface = pluginInterface;
this.CommandManager = commandManager;
this.Configuration = this.PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
this.Configuration.Initialize(this.PluginInterface);
// you might normally want to embed resources and load them from the manifest stream
2021-08-26 14:11:18 +00:00
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
2022-01-05 14:46:15 +00:00
this.PluginUI = new PluginUI(this.Configuration);
2021-08-26 14:11:18 +00:00
this.CommandManager.AddHandler(commandName, new CommandInfo(OnCommand)
{
2022-01-05 14:46:15 +00:00
HelpMessage = "Lets you configure alert thresholds for various currencies"
});
2021-08-26 14:11:18 +00:00
this.PluginInterface.UiBuilder.Draw += DrawUI;
this.PluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
2022-01-05 14:46:15 +00:00
}
public void Dispose()
{
2022-01-05 14:46:15 +00:00
this.PluginUI.Dispose();
2021-08-26 14:11:18 +00:00
this.CommandManager.RemoveHandler(commandName);
}
private void OnCommand(string command, string args)
{
2021-12-11 13:27:58 +00:00
this.DrawConfigUI();
}
private void DrawUI()
{
2022-01-05 14:46:15 +00:00
// TODO: move this logic elsewhere
unsafe
{
InventoryManager* inventoryManager = InventoryManager.Instance();
InventoryContainer* currencyContainer = inventoryManager->GetInventoryContainer(InventoryType.Currency);
EnumHelper.Each<Currency>(currency =>
{
var slot = EnumHelper.GetAttributeOfType<SlotAttribute>(currency).Value;
uint quantity = currencyContainer->GetInventorySlot((int)slot)->Quantity;
if (this.Configuration.AlertEnabled[currency] && quantity >= this.Configuration.Threshold[currency])
{
this.PluginUI.AlertVisible[currency] = true;
}
else
{
this.PluginUI.AlertVisible[currency] = false;
}
});
}
this.PluginUI.Draw();
}
private void DrawConfigUI()
{
2022-01-05 14:46:15 +00:00
this.PluginUI.SettingsVisible = true;
}
}
}