fix: tooltip inventory

This commit is contained in:
Salikh Gurgenidze
2025-11-18 03:49:35 +04:00
parent 8f47879955
commit e433e45e19
+285 -153
View File
@@ -6,189 +6,321 @@ addon.Modules.Tooltip = Tooltip
-- Helper function to get item ID from link (Lua 5.0 compatible) -- Helper function to get item ID from link (Lua 5.0 compatible)
local function GetItemIDFromLink(link) local function GetItemIDFromLink(link)
if not link then return nil end if not link then return nil end
-- Use strfind instead of string.match -- Use strfind instead of string.match
local _, _, itemID = strfind(link, "item:(%d+):?") local _, _, itemID = strfind(link, "item:(%d+):?")
return itemID and tonumber(itemID) or nil return itemID and tonumber(itemID) or nil
end end
-- Count items for a specific character -- Count items for a specific character
local function CountItemsForCharacter(itemID, characterData) local function CountItemsForCharacter(itemID, characterData)
local bagCount = 0 local bagCount = 0
local bankCount = 0 local bankCount = 0
-- Count bags -- Count bags
if characterData.bags then if characterData.bags then
for bagID, bagData in pairs(characterData.bags) do for bagID, bagData in pairs(characterData.bags) do
if bagData and bagData.slots then if bagData and bagData.slots then
for slotID, itemData in pairs(bagData.slots) do for slotID, itemData in pairs(bagData.slots) do
if itemData and itemData.link then if itemData and itemData.link then
local slotItemID = GetItemIDFromLink(itemData.link) local slotItemID = GetItemIDFromLink(itemData.link)
if slotItemID == itemID then if slotItemID == itemID then
bagCount = bagCount + (itemData.count or 1) bagCount = bagCount + (itemData.count or 1)
end end
end end
end end
end end
end end
end end
-- Count bank -- Count bank
if characterData.bank then if characterData.bank then
for bagID, bagData in pairs(characterData.bank) do for bagID, bagData in pairs(characterData.bank) do
if bagData and bagData.slots then if bagData and bagData.slots then
for slotID, itemData in pairs(bagData.slots) do for slotID, itemData in pairs(bagData.slots) do
if itemData and itemData.link then if itemData and itemData.link then
local slotItemID = GetItemIDFromLink(itemData.link) local slotItemID = GetItemIDFromLink(itemData.link)
if slotItemID == itemID then if slotItemID == itemID then
bankCount = bankCount + (itemData.count or 1) bankCount = bankCount + (itemData.count or 1)
end end
end end
end end
end end
end end
end end
return bagCount, bankCount return bagCount, bankCount
end end
-- Get class color -- Get class color
local function GetClassColor(classToken) local function GetClassColor(classToken)
if not classToken then return 1.0, 1.0, 1.0 end if not classToken then return 1.0, 1.0, 1.0 end
local color = RAID_CLASS_COLORS[classToken] local color = RAID_CLASS_COLORS[classToken]
if color then return color.r, color.g, color.b end if color then return color.r, color.g, color.b end
return 1.0, 1.0, 1.0 return 1.0, 1.0, 1.0
end end
-- Add inventory info to tooltip -- Add inventory info to tooltip
function Tooltip:AddInventoryInfo(tooltip, link) function Tooltip:AddInventoryInfo(tooltip, link)
if not Guda_DB or not Guda_DB.characters then if not Guda_DB or not Guda_DB.characters then
addon:Debug("No Guda_DB or characters found") addon:Debug("No Guda_DB or characters found")
return return
end end
local itemID = GetItemIDFromLink(link)
if not itemID then
addon:Debug("Could not get item ID from link: " .. tostring(link))
return
end
addon:Debug("Processing item ID: " .. itemID)
local totalBags = 0
local totalBank = 0
local characterCounts = {}
-- Count items across all characters local itemID = GetItemIDFromLink(link)
for charName, charData in pairs(Guda_DB.characters) do if not itemID then
local bagCount, bankCount = CountItemsForCharacter(itemID, charData) addon:Debug("Could not get item ID from link: " .. tostring(link))
if bagCount > 0 or bankCount > 0 then return
totalBags = totalBags + bagCount end
totalBank = totalBank + bankCount
table.insert(characterCounts, {
name = charData.name or charName,
classToken = charData.classToken,
bagCount = bagCount,
bankCount = bankCount
})
addon:Debug("Found " .. (bagCount + bankCount) .. " items on " .. charName .. " (Bags: " .. bagCount .. ", Bank: " .. bankCount .. ")")
end
end
local totalCount = totalBags + totalBank addon:Debug("Processing item ID: " .. itemID)
-- Only add to tooltip if we found items local totalBags = 0
if totalCount > 0 then local totalBank = 0
addon:Debug("Adding inventory info - Total: " .. totalCount) local characterCounts = {}
tooltip:AddLine(" ") -- Count items across all characters
tooltip:AddLine("Inventory", 0.7, 0.7, 0.7) for charName, charData in pairs(Guda_DB.characters) do
tooltip:AddDoubleLine("Total: " .. totalCount, "(Bags: " .. totalBags .. " | Bank: " .. totalBank .. ")", 1, 1, 1, 0.7, 0.7, 0.7) local bagCount, bankCount = CountItemsForCharacter(itemID, charData)
totalBags = totalBags + bagCount
totalBank = totalBank + bankCount
table.insert(characterCounts, {
name = charData.name or charName,
classToken = charData.classToken,
bagCount = bagCount,
bankCount = bankCount
})
addon:Debug("Found " .. (bagCount + bankCount) .. " items on " .. charName .. " (Bags: " .. bagCount .. ", Bank: " .. bankCount .. ")")
end
-- Sort characters by name local totalCount = totalBags + totalBank
table.sort(characterCounts, function(a, b)
return a.name < b.name
end)
-- Add character lines with bag and bank breakdown -- ALWAYS add to tooltip, even if 0 items
for _, charInfo in ipairs(characterCounts) do addon:Debug("Adding inventory info - Total: " .. totalCount)
local r, g, b = GetClassColor(charInfo.classToken)
local countText = ""
if charInfo.bagCount > 0 and charInfo.bankCount > 0 then
countText = "Bags: " .. charInfo.bagCount .. " | Bank: " .. charInfo.bankCount
elseif charInfo.bagCount > 0 then
countText = "Bags: " .. charInfo.bagCount
else
countText = "Bank: " .. charInfo.bankCount
end
tooltip:AddDoubleLine(charInfo.name, countText, r, g, b, 0.7, 0.7, 0.7)
end
tooltip:Show() tooltip:AddLine(" ")
else tooltip:AddLine("Inventory", 0.7, 0.7, 0.7)
addon:Debug("No items found for ID: " .. itemID) tooltip:AddDoubleLine("Total: " .. totalCount, "(Bags: " .. totalBags .. " | Bank: " .. totalBank .. ")", 1, 1, 1, 0.7, 0.7, 0.7)
end
-- Sort characters by name
table.sort(characterCounts, function(a, b)
return a.name < b.name
end)
-- Add character lines with bag and bank breakdown
for _, charInfo in ipairs(characterCounts) do
local r, g, b = GetClassColor(charInfo.classToken)
local countText = ""
if charInfo.bagCount > 0 and charInfo.bankCount > 0 then
countText = "Bags: " .. charInfo.bagCount .. " | Bank: " .. charInfo.bankCount
elseif charInfo.bagCount > 0 then
countText = "Bags: " .. charInfo.bagCount
else
countText = "Bank: " .. charInfo.bankCount
end
tooltip:AddDoubleLine(charInfo.name, countText, r, g, b, 0.7, 0.7, 0.7)
end
tooltip:Show()
end end
function Tooltip:Initialize() function Tooltip:Initialize()
addon:Print("Initializing tooltip module...") addon:Print("Initializing tooltip module...")
-- Hook SetBagItem -- Hook SetBagItem
local oldSetBagItem = GameTooltip.SetBagItem local oldSetBagItem = GameTooltip.SetBagItem
function GameTooltip:SetBagItem(bag, slot) function GameTooltip:SetBagItem(bag, slot)
local ret = oldSetBagItem(self, bag, slot) local ret = oldSetBagItem(self, bag, slot)
local link = GetContainerItemLink(bag, slot) local link = GetContainerItemLink(bag, slot)
if link then if link then
Tooltip:AddInventoryInfo(self, link) Tooltip:AddInventoryInfo(self, link)
end end
return ret return ret
end end
-- Hook SetHyperlink for chat links -- Hook SetHyperlink for chat links
local oldSetHyperlink = GameTooltip.SetHyperlink local oldSetHyperlink = GameTooltip.SetHyperlink
function GameTooltip:SetHyperlink(link) function GameTooltip:SetHyperlink(link)
local ret = oldSetHyperlink(self, link) local ret = oldSetHyperlink(self, link)
if link and strfind(link, "item:") then if link and strfind(link, "item:") then
Tooltip:AddInventoryInfo(self, link) Tooltip:AddInventoryInfo(self, link)
end end
return ret return ret
end end
-- Hook SetInventoryItem for character paperdoll -- Hook SetInventoryItem for character paperdoll
local oldSetInventoryItem = GameTooltip.SetInventoryItem local oldSetInventoryItem = GameTooltip.SetInventoryItem
function GameTooltip:SetInventoryItem(unit, slot) function GameTooltip:SetInventoryItem(unit, slot)
local ret = oldSetInventoryItem(self, unit, slot) local ret = oldSetInventoryItem(self, unit, slot)
local link = GetInventoryItemLink(unit, slot) local link = GetInventoryItemLink(unit, slot)
if link then if link then
Tooltip:AddInventoryInfo(self, link) Tooltip:AddInventoryInfo(self, link)
end end
return ret return ret
end end
-- Also hook ItemRefTooltip for chat links -- Hook SetLootItem for loot windows
if ItemRefTooltip then local oldSetLootItem = GameTooltip.SetLootItem
local oldItemRefSetHyperlink = ItemRefTooltip.SetHyperlink function GameTooltip:SetLootItem(slot)
function ItemRefTooltip:SetHyperlink(link) local ret = oldSetLootItem(self, slot)
oldItemRefSetHyperlink(self, link) local link = GetLootSlotLink(slot)
if link and strfind(link, "item:") then if link then
Tooltip:AddInventoryInfo(self, link) Tooltip:AddInventoryInfo(self, link)
end end
end return ret
end end
-- Clear cache function -- Hook SetQuestItem for quest rewards
function Tooltip:ClearCache() local oldSetQuestItem = GameTooltip.SetQuestItem
addon:Debug("Tooltip cache cleared") function GameTooltip:SetQuestItem(itemType, index)
end local ret = oldSetQuestItem(self, itemType, index)
local link = GetQuestItemLink(itemType, index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Clear cache on bag updates -- Hook SetQuestLogItem for quest log items
local frame = CreateFrame("Frame") local oldSetQuestLogItem = GameTooltip.SetQuestLogItem
frame:RegisterEvent("BAG_UPDATE") function GameTooltip:SetQuestLogItem(itemType, index)
frame:SetScript("OnEvent", function() local ret = oldSetQuestLogItem(self, itemType, index)
if event == "BAG_UPDATE" then local link = GetQuestLogItemLink(itemType, index)
Tooltip:ClearCache() if link then
end Tooltip:AddInventoryInfo(self, link)
end) end
return ret
end
addon:Print("Tooltip item-count integration enabled") -- Hook SetTradeSkillItem for tradeskill windows
local oldSetTradeSkillItem = GameTooltip.SetTradeSkillItem
function GameTooltip:SetTradeSkillItem(skillIndex, reagentIndex)
local ret = oldSetTradeSkillItem(self, skillIndex, reagentIndex)
local link
if reagentIndex then
link = GetTradeSkillReagentItemLink(skillIndex, reagentIndex)
else
link = GetTradeSkillItemLink(skillIndex)
end
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetCraftItem for craft windows
local oldSetCraftItem = GameTooltip.SetCraftItem
function GameTooltip:SetCraftItem(skillIndex, reagentIndex)
local ret = oldSetCraftItem(self, skillIndex, reagentIndex)
local link = GetCraftReagentItemLink(skillIndex, reagentIndex)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetAuctionItem for auction house
local oldSetAuctionItem = GameTooltip.SetAuctionItem
function GameTooltip:SetAuctionItem(type, index)
local ret = oldSetAuctionItem(self, type, index)
local link = GetAuctionItemLink(type, index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetInboxItem for mail
local oldSetInboxItem = GameTooltip.SetInboxItem
function GameTooltip:SetInboxItem(index)
local ret = oldSetInboxItem(self, index)
local link = GetInboxItemLink(index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetSendMailItem for sending mail
local oldSetSendMailItem = GameTooltip.SetSendMailItem
function GameTooltip:SetSendMailItem(index)
local ret = oldSetSendMailItem(self, index)
local link = GetSendMailItemLink(index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetMerchantItem for vendor items
local oldSetMerchantItem = GameTooltip.SetMerchantItem
function GameTooltip:SetMerchantItem(index)
local ret = oldSetMerchantItem(self, index)
local link = GetMerchantItemLink(index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetBuybackItem for buyback items
local oldSetBuybackItem = GameTooltip.SetBuybackItem
function GameTooltip:SetBuybackItem(index)
local ret = oldSetBuybackItem(self, index)
local link = GetBuybackItemLink(index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetTradePlayerItem for trade window (player side)
local oldSetTradePlayerItem = GameTooltip.SetTradePlayerItem
function GameTooltip:SetTradePlayerItem(index)
local ret = oldSetTradePlayerItem(self, index)
local link = GetTradePlayerItemLink(index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Hook SetTradeTargetItem for trade window (target side)
local oldSetTradeTargetItem = GameTooltip.SetTradeTargetItem
function GameTooltip:SetTradeTargetItem(index)
local ret = oldSetTradeTargetItem(self, index)
local link = GetTradeTargetItemLink(index)
if link then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
-- Also hook ItemRefTooltip for chat links
if ItemRefTooltip then
local oldItemRefSetHyperlink = ItemRefTooltip.SetHyperlink
function ItemRefTooltip:SetHyperlink(link)
local ret = oldItemRefSetHyperlink(self, link)
if link and strfind(link, "item:") then
Tooltip:AddInventoryInfo(self, link)
end
return ret
end
end
-- Clear cache function
function Tooltip:ClearCache()
addon:Debug("Tooltip cache cleared")
end
-- Clear cache on bag updates
local frame = CreateFrame("Frame")
frame:RegisterEvent("BAG_UPDATE")
frame:SetScript("OnEvent", function()
if event == "BAG_UPDATE" then
Tooltip:ClearCache()
end
end)
addon:Print("Tooltip item-count integration enabled")
end end