From 0b1b3e2dc05660c1c9c8d95328b6e326e23d2dcf Mon Sep 17 00:00:00 2001 From: Salikh Gurgenidze Date: Sun, 28 Dec 2025 00:52:51 +0400 Subject: [PATCH] fix: tooltip --- Core/Database.lua | 53 ++++++++++++++++++++++++++++++++++++ Core/Tooltip.lua | 11 +++++++- Core/Utils.lua | 11 +++++--- Data/MailboxScanner.lua | 60 +++++++++++++++++++++-------------------- UI/ItemButton.lua | 15 ++++++++++- 5 files changed, 116 insertions(+), 34 deletions(-) diff --git a/Core/Database.lua b/Core/Database.lua index 50bb90c..f77621e 100644 --- a/Core/Database.lua +++ b/Core/Database.lua @@ -319,6 +319,59 @@ function DB:GetCharacterMailbox(fullName) return char and char.mailbox or {} end +-- Find an item ID and link by name in any character's data +function DB:FindItemByName(name) + if not name or name == "" or not Guda_DB or not Guda_DB.characters then return nil, nil end + + for fullName, char in pairs(Guda_DB.characters) do + -- Check bags + if char.bags then + for bagID, bagData in pairs(char.bags) do + if type(bagData) == "table" and bagData.slots then + for slotID, item in pairs(bagData.slots) do + if item and item.name == name and item.link then + local itemID = addon.Modules.Utils:ExtractItemID(item.link) + if itemID then return itemID, item.link end + end + end + end + end + end + -- Check bank + if char.bank then + for bagID, bagData in pairs(char.bank) do + if type(bagData) == "table" and bagData.slots then + for slotID, item in pairs(bagData.slots) do + if item and item.name == name and item.link then + local itemID = addon.Modules.Utils:ExtractItemID(item.link) + if itemID then return itemID, item.link end + end + end + end + end + end + -- Check equipped + if char.equipped then + for slot, item in pairs(char.equipped) do + if item and item.name == name and item.link then + local itemID = addon.Modules.Utils:ExtractItemID(item.link) + if itemID then return itemID, item.link end + end + end + end + -- Check mailbox + if char.mailbox then + for _, mail in ipairs(char.mailbox) do + if mail.item and mail.item.name == name and mail.item.link then + local itemID = addon.Modules.Utils:ExtractItemID(mail.item.link) + if itemID then return itemID, mail.item.link end + end + end + end + end + return nil, nil +end + -- Get character's equipped items function DB:GetCharacterEquipped(fullName) local char = Guda_DB.characters[fullName] diff --git a/Core/Tooltip.lua b/Core/Tooltip.lua index b9201e9..c5539ce 100644 --- a/Core/Tooltip.lua +++ b/Core/Tooltip.lua @@ -7,7 +7,10 @@ addon.Modules.Tooltip = Tooltip -- Helper function to get item ID from link (Lua 5.0 compatible) local function GetItemIDFromLink(link) if not link then return nil end - local _, _, itemID = strfind(link, "item:(%d+):?") + if type(link) == "number" then return link end + + -- Try to find itemID in a standard link or a raw item:ID string + local _, _, itemID = string.find(link, "item:(%d+)") return itemID and tonumber(itemID) or nil end local function CountCurrentCharacterItems(itemID) @@ -277,6 +280,12 @@ function Tooltip:AddInventoryInfo(tooltip, link) return end + -- Guard against double-adding for the same item on the same tooltip + if tooltip.GudaInventoryAdded == itemID then + return + end + tooltip.GudaInventoryAdded = itemID + local totalBags = 0 local totalBank = 0 local totalMail = 0 diff --git a/Core/Utils.lua b/Core/Utils.lua index 9951ea7..6e65be3 100644 --- a/Core/Utils.lua +++ b/Core/Utils.lua @@ -103,15 +103,20 @@ scanTooltip:SetOwner(WorldFrame, "ANCHOR_NONE") function Utils:GetInboxItemLink(index, itemIndex) -- Try global function first (if it exists on this server/version) if GetInboxItemLink then - local link = GetInboxItemLink(index) + -- Turtle WoW might support (index, itemIndex) for multiple attachments + local link = GetInboxItemLink(index, itemIndex or 1) + if link then return link end + + -- Fallback to single argument if that failed + link = GetInboxItemLink(index) if link then return link end end -- In 1.12.1, GameTooltip:GetHyperlink() does not exist. -- Let's try to use GetItemInfo(name) as the primary way. - local name = GetInboxItem(index, itemIndex) + local name, texture, count, quality = GetInboxItem(index, itemIndex or 1) if name then - local _, link = GetItemInfo(name) + local itemName, link = GetItemInfo(name) if link then return link end diff --git a/Data/MailboxScanner.lua b/Data/MailboxScanner.lua index f513b65..7f70e3d 100644 --- a/Data/MailboxScanner.lua +++ b/Data/MailboxScanner.lua @@ -36,46 +36,48 @@ function MailboxScanner:ScanMailItemRows(index) local rows = {} if hasItem then - local itemIndex = 1 - -- GetInboxItem(index, itemIndex) returns: name, texture, count, quality, canUse - local name, texture, count, quality, canUse = GetInboxItem(index, itemIndex) - if name then - local itemLink = addon.Modules.Utils:GetInboxItemLink(index, itemIndex) + -- Turtle WoW supports up to 12 attachments per mail + for itemIndex = 1, 12 do + -- GetInboxItem(index, itemIndex) returns: name, texture, count, quality, canUse + local name, texture, count, quality, canUse = GetInboxItem(index, itemIndex) + if not name then break end + local itemLink = addon.Modules.Utils:GetInboxItemLink(index, itemIndex) + local itemID = itemLink and addon.Modules.Utils:ExtractItemID(itemLink) + + -- Fallback 1: If link/itemID is missing, try GetItemInfo(name) which might be cached now + if not itemID or not itemLink then + local _, link = GetItemInfo(name) + if link then + itemLink = link + itemID = addon.Modules.Utils:ExtractItemID(link) + addon:Debug("Recovered link from GetItemInfo for %s", name) + end + end + + -- Fallback 2: If still missing, try to recover from existing database (any character) + if not itemID or not itemLink then + itemID, itemLink = addon.Modules.DB:FindItemByName(name) + if itemID then + addon:Debug("Recovered link from DB for %s", name) + end + end + local itemData = { link = itemLink, texture = texture or "Interface\\Icons\\INV_Misc_Bag_08", count = count or 1, quality = quality or 0, name = name, - itemID = addon.Modules.Utils:ExtractItemID(itemLink), + itemID = itemID, } - -- Fallback: If link/itemID is missing, try to recover from existing database - if not itemData.itemID or not itemData.link then - local existingMailbox = addon.Modules.DB:GetCharacterMailbox(addon.Modules.DB:GetPlayerFullName()) - for _, oldMail in ipairs(existingMailbox) do - -- Match by sender, subject and item name - if oldMail.sender == sender and oldMail.subject == subject and oldMail.item and oldMail.item.name == name then - if not itemData.link and oldMail.item.link then - itemData.link = oldMail.item.link - addon:Debug("Recovered link from database for %s", name) - end - if not itemData.itemID and oldMail.item.itemID then - itemData.itemID = oldMail.item.itemID - addon:Debug("Recovered itemID from database for %s", name) - end - if itemData.link and itemData.itemID then break end - end - end - end - - -- If we still don't have itemID but have a link (or vice versa), fix it - if itemData.link and not itemData.itemID then - itemData.itemID = addon.Modules.Utils:ExtractItemID(itemData.link) + -- Ensure we have a link if we have an itemID + if itemData.itemID and not itemData.link then + itemData.link = "item:" .. itemData.itemID .. ":0:0:0" end - -- If we have a link, try to get more detailed info + -- If we have a link, try to get more detailed info from cache if itemData.link then local itemName, link, itemQuality, iLevel, itemCategory, itemType, itemStackCount, itemSubType, itemTexture, itemEquipLoc, itemSellPrice = addon.Modules.Utils:GetItemInfo(itemData.link) if itemName then diff --git a/UI/ItemButton.lua b/UI/ItemButton.lua index 99a1562..ec4bae8 100644 --- a/UI/ItemButton.lua +++ b/UI/ItemButton.lua @@ -1004,6 +1004,7 @@ function Guda_ItemButton_OnEnter(self) -- Add mailbox metadata if available local mailData = self.mailData + if mailData then -- If we already added a line (for money/generic), or SetHyperlink/SetInboxItem added lines, -- we might want a separator if we're adding sender info. @@ -1027,6 +1028,17 @@ function Guda_ItemButton_OnEnter(self) GameTooltip:AddLine("Days left: " .. math.floor(mailData.daysLeft), 0.5, 0.5, 0.5) end end + + -- Add Inventory counts for mailbox items at the very bottom + if addon.Modules.Tooltip and addon.Modules.Tooltip.AddInventoryInfo then + local link = self.itemData and (self.itemData.link or (self.itemData.itemID and ("item:" .. self.itemData.itemID .. ":0:0:0"))) + if not link and self.mailIndex and isMailboxOpen then + link = addon.Modules.Utils:GetInboxItemLink(self.mailIndex, self.mailItemIndex or 1) + end + if link then + addon.Modules.Tooltip:AddInventoryInfo(GameTooltip, link) + end + end GameTooltip:Show() return @@ -1098,8 +1110,9 @@ end -- OnLeave handler function Guda_ItemButton_OnLeave(self) -- Clear any viewed character hint on the tooltip when leaving - if GameTooltip and GameTooltip.GudaViewedCharacter then + if GameTooltip then GameTooltip.GudaViewedCharacter = nil + GameTooltip.GudaInventoryAdded = nil end GameTooltip:Hide() ResetCursor()