diff --git a/ARDiscard/AutoDiscardPlogon.cs b/ARDiscard/AutoDiscardPlogon.cs index 522597b..4df8282 100644 --- a/ARDiscard/AutoDiscardPlogon.cs +++ b/ARDiscard/AutoDiscardPlogon.cs @@ -86,7 +86,7 @@ public class AutoDiscardPlogon : IDalamudPlugin ECommonsMain.Init(_pluginInterface, this); _autoRetainerApi = new(); _taskManager = new(); - _contextMenuIntegration = new(_pluginInterface, _chatGui, itemCache, _configuration, _configWindow); + _contextMenuIntegration = new(_pluginInterface, _chatGui, itemCache, _configuration, _configWindow, _gameGui); _clientState.Login += _discardWindow.Login; _clientState.Logout += _discardWindow.Logout; diff --git a/ARDiscard/ContextMenuIntegration.cs b/ARDiscard/ContextMenuIntegration.cs index 4ab4ada..9ddd59e 100644 --- a/ARDiscard/ContextMenuIntegration.cs +++ b/ARDiscard/ContextMenuIntegration.cs @@ -15,85 +15,128 @@ internal sealed class ContextMenuIntegration : IDisposable private readonly ItemCache _itemCache; private readonly Configuration _configuration; private readonly ConfigWindow _configWindow; - private readonly InventoryContextMenuItem _addItem; - private readonly InventoryContextMenuItem _removeItem; + private readonly IGameGui _gameGui; + private readonly SeString _addItemPayload; + private readonly SeString _removeItemPayload; + private readonly InventoryContextMenuItem _addInventoryItem; + private readonly InventoryContextMenuItem _removeInventoryItem; private readonly DalamudContextMenu _dalamudContextMenu; public ContextMenuIntegration(DalamudPluginInterface pluginInterface, IChatGui chatGui, ItemCache itemCache, - Configuration configuration, ConfigWindow configWindow) + Configuration configuration, ConfigWindow configWindow, IGameGui gameGui) { _chatGui = chatGui; _itemCache = itemCache; _configuration = configuration; _configWindow = configWindow; - _addItem = new InventoryContextMenuItem( + _gameGui = gameGui; + _addItemPayload = new SeString(new UIForegroundPayload(52)) .Append($"\ue05f ") - .Append(new UIForegroundPayload(0)).Append("Add to Auto Discard List"), - AddToDiscardList); - _removeItem = new InventoryContextMenuItem( - new SeString(new UIForegroundPayload(52)) - .Append($"\ue05f ") - .Append(new UIForegroundPayload(0)).Append("Remove from Auto Discard List"), - RemoveFromDiscardList); + .Append(new UIForegroundPayload(0)).Append("Add to Auto Discard List"); + _removeItemPayload = new SeString(new UIForegroundPayload(52)) + .Append($"\ue05f ") + .Append(new UIForegroundPayload(0)).Append("Remove from Auto Discard List"); + _addInventoryItem = new InventoryContextMenuItem(_addItemPayload, AddToDiscardList); + _removeInventoryItem = new InventoryContextMenuItem(_removeItemPayload, RemoveFromDiscardList); _dalamudContextMenu = new(pluginInterface); _dalamudContextMenu.OnOpenInventoryContextMenu += OpenInventoryContextMenu; + _dalamudContextMenu.OnOpenGameObjectContextMenu += OpenGameObjectContextMenu; } - private unsafe void OpenInventoryContextMenu(InventoryContextMenuOpenArgs args) + private void OpenInventoryContextMenu(InventoryContextMenuOpenArgs args) { - if (!_configuration.ContextMenu.Enabled) + if (!IsEnabled()) return; - if (_configuration.ContextMenu.OnlyWhenConfigIsOpen && !_configWindow.IsOpen) - return; - - if (!(args.ParentAddonName is "Inventory" or "InventoryExpansion" or "InventoryLarge" or "ArmouryBoard")) + if (args.ParentAddonName is not ("Inventory" or "InventoryExpansion" or "InventoryLarge" or "ArmouryBoard")) return; if (!_configWindow.CanItemBeConfigured(args.ItemId)) return; if (_configuration.DiscardingItems.Contains(args.ItemId)) - args.AddCustomItem(_removeItem); + args.AddCustomItem(_removeInventoryItem); else if (_itemCache.TryGetItem(args.ItemId, out ItemCache.CachedItemInfo? cachedItemInfo) && cachedItemInfo.CanBeDiscarded()) - args.AddCustomItem(_addItem); + args.AddCustomItem(_addInventoryItem); } - private void AddToDiscardList(InventoryContextMenuItemSelectedArgs args) + private void OpenGameObjectContextMenu(GameObjectContextMenuOpenArgs args) { - if (_configWindow.AddToDiscardList(args.ItemId)) + if (!IsEnabled()) + return; + + if (args.ParentAddonName is not "ChatLog") + return; + + uint itemId = (uint)_gameGui.HoveredItem; + if (itemId > 1_000_000) + itemId -= 1_000_000; + + if (itemId > 500_000) + itemId -= 500_000; + + if (_configuration.DiscardingItems.Contains(itemId)) + args.AddCustomItem(new GameObjectContextMenuItem(_removeItemPayload, _ => RemoveFromDiscardList(itemId))); + else if (_itemCache.TryGetItem(itemId, out ItemCache.CachedItemInfo? cachedItemInfo) && + cachedItemInfo.CanBeDiscarded()) + args.AddCustomItem(new GameObjectContextMenuItem(_addItemPayload, _ => AddToDiscardList(itemId))); + } + + private void AddToDiscardList(InventoryContextMenuItemSelectedArgs args) => AddToDiscardList(args.ItemId); + + private void AddToDiscardList(uint itemId) + { + if (_configWindow.AddToDiscardList(itemId)) { _chatGui.Print(new SeString(new UIForegroundPayload(52)) .Append($"\ue05f ") .Append(new UIForegroundPayload(0)) .Append($"Added ") .Append(new UIForegroundPayload(52)) - .Append(_itemCache.GetItemName(args.ItemId)) + .Append(new ItemPayload(itemId)) + .Append(_itemCache.GetItemName(itemId)) + .Append(RawPayload.LinkTerminator) .Append(new UIForegroundPayload(0)) .Append(" to Auto Discard List.")); } } - private void RemoveFromDiscardList(InventoryContextMenuItemSelectedArgs args) + private void RemoveFromDiscardList(InventoryContextMenuItemSelectedArgs args) => RemoveFromDiscardList(args.ItemId); + + private void RemoveFromDiscardList(uint itemId) { - if (_configWindow.RemoveFromDiscardList(args.ItemId)) + if (_configWindow.RemoveFromDiscardList(itemId)) { _chatGui.Print(new SeString(new UIForegroundPayload(52)) .Append($"\ue05f ") .Append(new UIForegroundPayload(0)) .Append($"Removed ") .Append(new UIForegroundPayload(52)) - .Append(_itemCache.GetItemName(args.ItemId)) + .Append(new ItemPayload(itemId)) + .Append(_itemCache.GetItemName(itemId)) + .Append(RawPayload.LinkTerminator) .Append(new UIForegroundPayload(0)) .Append(" from Auto Discard List.")); } } + private bool IsEnabled() + { + if (!_configuration.ContextMenu.Enabled) + return false; + + if (_configuration.ContextMenu.OnlyWhenConfigIsOpen && !_configWindow.IsOpen) + return false; + + return true; + } + public void Dispose() { + _dalamudContextMenu.OnOpenGameObjectContextMenu -= OpenGameObjectContextMenu; _dalamudContextMenu.OnOpenInventoryContextMenu -= OpenInventoryContextMenu; _dalamudContextMenu.Dispose(); }