-- Quest Item Tip: adds "Quest: [level] name have / need" to item tooltips, -- and says so when a quest item you carry is no longer needed. local ADDON_NAME = ... local ITEM_CLASS_QUEST = 12 local ITEM_BIND_TYPE_QUEST = 4 -- Lowercased item name -> list of { questID, title, level, need }. -- Holds only what is fixed for the life of a quest; have-counts are read when -- the tooltip is shown, so looting never needs a rebuild. local index = {} -- Item ID -> list of { questID, title, level } for items a quest handed you. local provided = {} -- List of { questID, title, level, text } with each quest's lowercased log text, -- for delivery quests, which name their item only in prose. local questTexts = {} -- Saved per character. Lowercased name or item ID -> the quest last seen using -- it, which is how a quest can still be named after it has left the log. local db local dirty = true local incomplete = false local rebuildOnNextUpdate = false local rebuilding = false local lastNumQuests -- "Linen Cloth: 3/5" or "3/5 Linen Cloth (Optional)" -> "Linen Cloth" local function ObjectiveItemName(text) local name = text:match("^%d+/%d+%s+(.+)$") or text:match("^(.-):?%s*%d+/%d+") if name then name = name:gsub("%s+%(Optional%)$", "") -- " : 0/1" is what the client gives before the item's name has loaded. name = name:match("^%s*(.-)%s*$") if name ~= "" then return name end end end local function ReadEntries() local entries = {} for i = 1, (C_QuestLog.GetNumQuestLogEntries()) do local info = C_QuestLog.GetInfo(i) if info then info.logIndex = i entries[#entries + 1] = info end end return entries end -- Returns the titles of collapsed headers whose quests are not being listed. -- Those have to be opened to be read, then closed again. local function ExpandHiddenHeaders(entries) local reopened for i = #entries, 1, -1 do local info, nextInfo = entries[i], entries[i + 1] if info.isHeader and info.isCollapsed and (not nextInfo or nextInfo.isHeader) then reopened = reopened or {} reopened[info.title] = true ExpandQuestHeader(i) end end return reopened end local function CollapseHeaders(titles) local entries = ReadEntries() for i = #entries, 1, -1 do local info = entries[i] if info.isHeader and titles[info.title] then CollapseQuestHeader(i) end end end local function QuestLevel(info) if info.level and info.level > 0 then return info.level end return C_QuestLog.GetQuestDifficultyLevel and C_QuestLog.GetQuestDifficultyLevel(info.questID) or nil end local function IsRealQuest(info) return not info.isHeader and not info.isHidden and info.questID and info.questID > 0 end local function Remember(key, info) if db then db[key] = { questID = info.questID, title = info.title, level = QuestLevel(info) } end end -- Same steps as Blizzard's abandon-quest button, which is the only place the -- client exposes these. Skipped while that button's own popup is on screen. local function ReadProvidedItems(entries) if StaticPopup_Visible("ABANDON_QUEST") or StaticPopup_Visible("ABANDON_QUEST_WITH_ITEMS") then return end local new = {} local function Add(itemID, info) new[itemID] = new[itemID] or {} table.insert(new[itemID], { questID = info.questID, title = info.title, level = QuestLevel(info) }) Remember(itemID, info) end local selected = C_QuestLog.GetSelectedQuest() for _, info in ipairs(entries) do if IsRealQuest(info) then C_QuestLog.SetSelectedQuest(info.questID) C_QuestLog.SetAbandonQuest() for _, itemID in ipairs(C_QuestLog.GetAbandonQuestItems() or {}) do Add(itemID, info) end end end C_QuestLog.SetSelectedQuest(selected) provided = new end local function Rebuild() if rebuilding then return end rebuilding = true rebuildOnNextUpdate = false lastNumQuests = select(2, C_QuestLog.GetNumQuestLogEntries()) local entries = ReadEntries() local reopened = ExpandHiddenHeaders(entries) if reopened then entries = ReadEntries() end local new = {} local texts = {} incomplete = false for _, info in ipairs(entries) do if IsRealQuest(info) then local description, objectiveText = GetQuestLogQuestText(info.logIndex) if (description or "") == "" and (objectiveText or "") == "" then incomplete = true else texts[#texts + 1] = { questID = info.questID, title = info.title, level = QuestLevel(info), text = ((objectiveText or "") .. "\n" .. (description or "")):lower(), } end local objectives = C_QuestLog.GetQuestObjectives(info.questID) if not objectives then incomplete = true else for _, objective in ipairs(objectives) do if objective.type == "item" then if not objective.text or objective.text == "" then incomplete = true else local name = ObjectiveItemName(objective.text) if name then local key = name:lower() new[key] = new[key] or {} table.insert(new[key], { questID = info.questID, title = info.title, level = QuestLevel(info), need = objective.numRequired, }) Remember(key, info) else incomplete = true end end end end end end end ReadProvidedItems(entries) if reopened then CollapseHeaders(reopened) end index = new questTexts = texts dirty = false rebuilding = false end local function QuestLabel(prefix, quest) local left = prefix if quest.level then left = left .. "[" .. quest.level .. "] " end left = left .. quest.title local color = quest.level and GetQuestDifficultyColor and GetQuestDifficultyColor(quest.level) if color then return left, color.r, color.g, color.b end return left, 1, 0.82, 0 end local function IsQuestItem(id) local _, _, _, _, _, classID = C_Item.GetItemInfoInstant(id) return classID == ITEM_CLASS_QUEST or select(14, C_Item.GetItemInfo(id)) == ITEM_BIND_TYPE_QUEST end local function StartsQuest(data) local startsQuest = ITEM_STARTS_QUEST or "This Item Begins a Quest" for _, line in ipairs(data.lines or {}) do if line.leftText == startsQuest then return true end end return false end local function QuestMentioning(name) local lowered = name:lower() for _, quest in ipairs(questTexts) do if quest.text:find(lowered, 1, true) then return quest end end end -- For an item no quest in the log is using right now. local function AddLeftoverLine(tooltip, data, id, name) if (C_Item.GetItemCount(id, true) or 0) == 0 then return false end -- Quest items only: ordinary goods turn up in quest prose all the time. local mentioned = IsQuestItem(id) and QuestMentioning(name) if mentioned then Remember(id, mentioned) tooltip:AddLine(QuestLabel("Quest: ", mentioned)) return true end local record = db and (db[id] or db[name:lower()]) if record then if C_QuestLog.IsOnQuest(record.questID) then local left, r, g, b = QuestLabel("Quest: ", record) tooltip:AddLine(left, r, g, b) else local prefix = C_QuestLog.IsQuestFlaggedCompleted(record.questID) and "Quest done: " or "Quest dropped: " tooltip:AddDoubleLine((QuestLabel(prefix, record)), "can delete", 1, 0.3, 0.3, 1, 0.3, 0.3) end return true end if IsQuestItem(id) and not StartsQuest(data) then tooltip:AddLine("Quest item: no quest in your log uses it", 1, 0.5, 0.1) return true end return false end local function AddQuestLines(tooltip, data) if tooltip ~= GameTooltip and tooltip ~= ItemRefTooltip then return end local id = data and data.id if not id then return end local name = C_Item.GetItemNameByID(id) or tooltip:GetItem() if not name then return end if dirty then Rebuild() end local quests = index[name:lower()] local givenBy = provided[id] if not quests and not givenBy then if AddLeftoverLine(tooltip, data, id, name) then tooltip:Show() end return end local listed = {} if quests then -- Only bag items count towards an objective, so the bank is shown apart. local bags = C_Item.GetItemCount(id) or 0 local bank = (C_Item.GetItemCount(id, true) or bags) - bags for _, quest in ipairs(quests) do listed[quest.questID] = true local left, r, g, b = QuestLabel("Quest: ", quest) local right = bags .. " / " .. (quest.need or "?") if bank > 0 then right = right .. " (+" .. bank .. " bank)" end if quest.need and bags >= quest.need then tooltip:AddDoubleLine(left, right, r, g, b, 0.25, 1, 0.25) else tooltip:AddDoubleLine(left, right, r, g, b, 1, 1, 1) end end end for _, quest in ipairs(givenBy or {}) do if not listed[quest.questID] then local left, r, g, b = QuestLabel("Quest: ", quest) tooltip:AddLine(left, r, g, b) end end tooltip:Show() end TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Item, AddQuestLines) local frame = CreateFrame("Frame") for _, event in ipairs({ "ADDON_LOADED", "PLAYER_ENTERING_WORLD", "QUEST_LOG_UPDATE", "QUEST_ACCEPTED", "QUEST_REMOVED", "QUEST_TURNED_IN" }) do if not C_EventUtils or C_EventUtils.IsEventValid(event) then frame:RegisterEvent(event) end end frame:SetScript("OnEvent", function(_, event, arg1) if event == "ADDON_LOADED" then if arg1 == ADDON_NAME then QuestItemTipDB = QuestItemTipDB or {} db = QuestItemTipDB end elseif event == "QUEST_LOG_UPDATE" then -- Also fires when headers are toggled, including by Rebuild itself, -- so only react if the quests themselves changed. Rebuilding now, -- not at the next tooltip, is what records a quest before it ends. local changed = select(2, C_QuestLog.GetNumQuestLogEntries()) ~= lastNumQuests if changed or incomplete then dirty = true end if changed or rebuildOnNextUpdate then rebuildOnNextUpdate = false Rebuild() end else dirty = true rebuildOnNextUpdate = true end end) -- /qit prints what was read from the quest log. SLASH_QUESTITEMTIP1 = "/qit" SlashCmdList.QUESTITEMTIP = function() Rebuild() local print = function(text) DEFAULT_CHAT_FRAME:AddMessage("|cffffd100QuestItemTip:|r " .. text) end local found = false for name, quests in pairs(index) do for _, quest in ipairs(quests) do found = true print(name .. " -> [" .. tostring(quest.level) .. "] " .. quest.title .. ", need " .. tostring(quest.need)) end end for itemID, quests in pairs(provided) do for _, quest in ipairs(quests) do found = true print("item " .. itemID .. " given by [" .. tostring(quest.level) .. "] " .. quest.title) end end if not found then print("no item objectives in your quest log") end end