Skip to content

Example Usage

Wardz edited this page Jul 30, 2024 · 34 revisions

The following is only pseudo-code to illustrate how the lib is intended to be used. For real world usage examples see Gladdy, GladiusEx, Diminish or TellMeWhen.


Addon.toc

## Interface: 80200
## Title: Your AddOn

libs/DRList-1.0/libs/LibStub/LibStub.lua # Only needed if LibStub is not already embedded
libs/DRList-1.0/DRList-1.0.xml

Addon.lua

Addon.lua

-- Get lib instance.
local DRList = LibStub("DRList-1.0")

-- Table for storing all DRs
local trackedPlayers = {}

local function StartOrUpdateDRTimer(drCategory, unitGUID)
    trackedPlayers[unitGUID] = trackedPlayers[unitGUID] or {}
    trackedPlayers[unitGUID][drCategory] = trackedPlayers[unitGUID][drCategory] or {}

    local data = trackedPlayers[unitGUID][drCategory]

    if data.diminished == nil or GetTime() >= (data.expirationTime or 0) then -- is nil or DR expired
        data.diminished = 1
    else
        data.diminished = DRList:NextDR(data.diminished, drCategory)
    end

    data.expirationTime = GetTime() + DRList:GetResetTime(drCategory)

    -- Do your stuff here, start a frame timer, etc.
    -- Then make sure to delete the category data after 18 seconds / DRList:GetResetTime().
    -- You might also want to delete all data on UNIT_DIED and loading screen events.
    -- (trackedPlayers[unitGUID] can be referenced in future unit events, make sure to check the expiration time per category)
end


-- Register a combat log event handler
local addon = CreateFrame("Frame")
addon:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
addon:SetScript("OnEvent", function(self, event)
    local _, eventType, _, _, _, _, _, destGUID, _, destFlags, _, spellID, _, _, auraType = CombatLogGetCurrentEventInfo()

    -- Check all debuffs found in the combat log
    if auraType == "DEBUFF" then
        -- Get the DR category or exit immediately if current debuff doesn't have a DR
        local category, sharedCategories = DRList:GetCategoryBySpellID(spellID)
        if not category then return end

        -- Check if unit that got the debuff is a player
        -- You might also want to check if it's a hostile or friendly unit depending on your needs
        local isPlayer = bit.band(destFlags, COMBATLOG_OBJECT_TYPE_PLAYER) ~= 0
        if not isPlayer then return end
        -- for PvE too: if not isPlayer and not DRList:IsPvECategory(category) then return end

        -- The debuff has faded or refreshed, DR timer starts
        if eventType == "SPELL_AURA_REMOVED" or eventType == "SPELL_AURA_REFRESH" then
            local drCategories = sharedCategories or { category }
            --local unitID = UnitTokenFromGUID(destGUID)

            for i = 1, #drCategories do -- A debuff can trigger several DR categories at once
                StartOrUpdateDRTimer(drCategories[i], destGUID)
            end
        end
    end
end)
Clone this wiki locally