PeepingTom/Peeping Tom/Plugin.cs

148 lines
5.0 KiB
C#
Raw Normal View History

2020-07-28 22:46:16 +00:00
using Dalamud.Game.Command;
using Dalamud.Plugin;
2021-01-27 03:37:09 +00:00
using System.Collections.Generic;
using System.Globalization;
2021-08-23 03:33:57 +00:00
using Dalamud.Game.ClientState.Objects;
using Dalamud.IoC;
2023-09-28 06:56:38 +00:00
using Dalamud.Plugin.Services;
2021-01-27 03:37:09 +00:00
using Lumina.Excel.GeneratedSheets;
using PeepingTom.Resources;
using XivCommon;
2020-07-28 22:46:16 +00:00
namespace PeepingTom {
2021-04-15 15:46:10 +00:00
// ReSharper disable once ClassNeverInstantiated.Global
2023-09-28 06:56:38 +00:00
public class Plugin : IDalamudPlugin {
internal static string Name => "Peeping Tom";
[PluginService]
internal static IPluginLog Log { get; private set; } = null!;
2020-07-28 22:46:16 +00:00
2021-08-23 03:33:57 +00:00
[PluginService]
internal DalamudPluginInterface Interface { get; init; } = null!;
[PluginService]
2023-09-28 06:56:38 +00:00
internal IChatGui ChatGui { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal IClientState ClientState { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
private ICommandManager CommandManager { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal ICondition Condition { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal IDataManager DataManager { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal IFramework Framework { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal IGameGui GameGui { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal IObjectTable ObjectTable { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal ITargetManager TargetManager { get; init; } = null!;
2021-08-23 03:33:57 +00:00
[PluginService]
2023-09-28 06:56:38 +00:00
internal IToastGui ToastGui { get; init; } = null!;
2021-08-23 03:33:57 +00:00
internal Configuration Config { get; }
internal PluginUi Ui { get; }
internal TargetWatcher Watcher { get; }
internal XivCommonBase Common { get; }
internal IpcManager IpcManager { get; }
2020-07-28 22:46:16 +00:00
2021-01-27 03:37:09 +00:00
internal bool InPvp { get; private set; }
2023-09-28 06:56:38 +00:00
public Plugin() {
2023-10-03 21:42:20 +00:00
this.Common = new XivCommonBase(this.Interface);
this.Config = this.Interface.GetPluginConfig() as Configuration ?? new Configuration();
this.Config.Initialize(this.Interface);
this.Watcher = new TargetWatcher(this);
2020-12-29 16:08:21 +00:00
this.Ui = new PluginUi(this);
2021-08-23 03:33:57 +00:00
this.IpcManager = new IpcManager(this);
2020-07-28 22:46:16 +00:00
OnLanguageChange(this.Interface.UiLanguage);
2021-08-23 03:33:57 +00:00
this.Interface.LanguageChanged += OnLanguageChange;
2021-08-23 03:33:57 +00:00
this.CommandManager.AddHandler("/ppeepingtom", new CommandInfo(this.OnCommand) {
2021-01-27 03:37:09 +00:00
HelpMessage = "Use with no arguments to show the list. Use with \"c\" or \"config\" to show the config",
2020-07-28 22:46:16 +00:00
});
2021-08-23 03:33:57 +00:00
this.CommandManager.AddHandler("/ptom", new CommandInfo(this.OnCommand) {
2021-01-27 03:37:09 +00:00
HelpMessage = "Alias for /ppeepingtom",
2020-07-28 22:46:16 +00:00
});
2021-08-23 03:33:57 +00:00
this.CommandManager.AddHandler("/ppeep", new CommandInfo(this.OnCommand) {
2021-01-27 03:37:09 +00:00
HelpMessage = "Alias for /ppeepingtom",
2020-07-28 22:46:16 +00:00
});
2021-08-23 03:33:57 +00:00
this.ClientState.Login += this.OnLogin;
this.ClientState.Logout += this.OnLogout;
this.ClientState.TerritoryChanged += this.OnTerritoryChange;
this.Interface.UiBuilder.Draw += this.DrawUi;
this.Interface.UiBuilder.OpenConfigUi += this.ConfigUi;
2020-07-28 22:46:16 +00:00
}
public void Dispose() {
2021-08-23 03:33:57 +00:00
this.Interface.UiBuilder.OpenConfigUi -= this.ConfigUi;
this.Interface.UiBuilder.Draw -= this.DrawUi;
this.ClientState.TerritoryChanged -= this.OnTerritoryChange;
this.ClientState.Logout -= this.OnLogout;
this.ClientState.Login -= this.OnLogin;
this.CommandManager.RemoveHandler("/ppeep");
this.CommandManager.RemoveHandler("/ptom");
this.CommandManager.RemoveHandler("/ppeepingtom");
this.Interface.LanguageChanged -= OnLanguageChange;
this.IpcManager.Dispose();
this.Ui.Dispose();
2021-08-23 03:33:57 +00:00
this.Watcher.Dispose();
this.Common.Dispose();
}
private static void OnLanguageChange(string langCode) {
Language.Culture = new CultureInfo(langCode);
}
2023-09-28 06:56:38 +00:00
private void OnTerritoryChange(ushort e) {
2021-01-27 03:37:09 +00:00
try {
2021-08-23 03:33:57 +00:00
var territory = this.DataManager.GetExcelSheet<TerritoryType>()!.GetRow(e);
this.InPvp = territory?.IsPvpZone == true;
2021-01-27 03:37:09 +00:00
} catch (KeyNotFoundException) {
2023-09-28 06:56:38 +00:00
Log.Warning("Could not get territory for current zone");
2021-01-27 03:37:09 +00:00
}
}
2020-07-28 22:46:16 +00:00
private void OnCommand(string command, string args) {
2021-04-15 15:46:10 +00:00
if (args is "config" or "c") {
this.Ui.SettingsOpen = true;
2020-07-28 22:46:16 +00:00
} else {
this.Ui.WantsOpen = true;
2020-07-28 22:46:16 +00:00
}
}
2023-09-28 06:56:38 +00:00
private void OnLogin() {
2020-08-15 21:11:06 +00:00
if (!this.Config.OpenOnLogin) {
return;
}
this.Ui.WantsOpen = true;
}
2023-09-28 06:56:38 +00:00
private void OnLogout() {
2020-08-15 21:11:06 +00:00
this.Ui.WantsOpen = false;
this.Watcher.ClearPrevious();
}
2020-12-29 16:08:21 +00:00
private void DrawUi() {
this.Ui.Draw();
2020-07-28 22:46:16 +00:00
}
2021-08-23 03:33:57 +00:00
private void ConfigUi() {
this.Ui.SettingsOpen = true;
2020-07-28 22:46:16 +00:00
}
}
}