Skip to content

Commit

Permalink
v5.1.0-alpha.2
Browse files Browse the repository at this point in the history
This versions appears to be working. Still needs testing.
  • Loading branch information
donniedice committed Sep 17, 2024
1 parent a6394bd commit 9b2f5ed
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions data/battlepets.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
-- =====================================================================================
-- BLU | Better Level Up! - battlepets.lua
-- =====================================================================================

BLU_L = BLU_L or {}
BLU = BLU or {}

Expand All @@ -10,12 +6,14 @@ BLU.previousPetLevels = {} -- Stores the initial pet levels on login
local lastSoundTime = {}
local SOUND_COOLDOWN = 2

BLU.petData = {} -- This will now be used to store the *current* pet levels during checks

-- Register events
function BLU:RegisterPetEvents()
self:RegisterEvent("PLAYER_LOGIN", "OnPlayerLogin")
self:RegisterEvent("PET_JOURNAL_LIST_UPDATE", "HandlePetJournalUpdate")
-- Additional events for potential level-up triggers
self:RegisterEvent("PET_BATTLE_OPENING_START", "HandlePetBattleEvent")
self:RegisterEvent("PET_BATTLE_CLOSE", "HandlePetBattleEvent")
self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED", "HandleCombatLogEvent")
self:RegisterEvent("PET_BATTLE_LEVEL_CHANGED", "HandlePetLevelUp")
self:RegisterEvent("UNIT_PET_EXPERIENCE", "HandlePetLevelUp")
self:RegisterEvent("BAG_UPDATE_DELAYED", "HandlePetLevelUp")
Expand All @@ -34,12 +32,28 @@ end

-- Handle Pet Journal Updates
function BLU:HandlePetJournalUpdate()
-- Delay to stabilize the Pet Journal data (optional, might not be needed)
C_Timer.After(0.2, function()
-- Directly check for level-ups when the journal updates
self:CheckPetJournalForLevelUps()
end

-- Handle Pet Battle Events
function BLU:HandlePetBattleEvent()
-- Delay to ensure pet data is updated after battle
C_Timer.After(0.2, function()
self:CheckPetJournalForLevelUps()
end)
end

-- Handle Combat Log Events
function BLU:HandleCombatLogEvent(_, _, event, ...)
if event == "UNIT_DIED" or event == "UNIT_DESTROYED" then
-- Potential pet level-up, check after a short delay
C_Timer.After(0.1, function()
self:CheckPetJournalForLevelUps()
end)
end
end

-- Pet Level-Up Event Handlers
function BLU:HandlePetLevelUp(event, ...)
self:PrintDebugMessage(string.format("Handling event: %s", event))
Expand All @@ -51,30 +65,37 @@ function BLU:HandlePetLevelUp(event, ...)
self.db.profile.lastUsedItemID = nil
-- Force Pet Journal update
C_PetJournal.ClearSearchFilter()

-- Directly check after forcing the update
self:CheckPetJournalForLevelUps(true) -- true indicates item triggered
return -- Exit early, level-up handled
end
end

-- Delay to ensure pet journal is updated
C_Timer.After(0.2, function()
self:CheckPetJournalForLevelUps(event == "BAG_UPDATE_DELAYED")
-- For other events, still use a small delay
C_Timer.After(0.1, function() -- Reduced delay
self:CheckPetJournalForLevelUps()
end)
end

-- Check for level-ups and process them
function BLU:CheckPetJournalForLevelUps(isItemTriggered)
isItemTriggered = isItemTriggered or false

self:UpdatePetData() -- Update BLU.petData with current pet levels
for petID, previousLevel in pairs(BLU.previousPetLevels) do
-- Fetch the CURRENT level directly from the journal
local _, _, currentLevel = C_PetJournal.GetPetInfoByPetID(petID)

for petID, currentLevel in pairs(BLU.petData) do
local previousLevel = BLU.previousPetLevels[petID] or 0
if currentLevel > previousLevel then
self:ProcessPetLevelUp(petID, currentLevel, false, isItemTriggered)
else
self:PrintDebugMessage(string.format("No level-up detected for Pet ID: %s, Previous Level: %d, Current Level: %d", petID, previousLevel, currentLevel))
end
end

-- Update stored levels AFTER processing
self:UpdatePetData()

-- Output stored information after processing level-ups
self:PrintStoredData()
end
Expand All @@ -95,7 +116,7 @@ function BLU:UpdatePetData()
end

-- Clear current pet data before updating
wipe(BLU.petData)
wipe(BLU.previousPetLevels) -- Directly update the stored levels

for i = 1, numPets do
-- Get the pet's unique ID from the second argument
Expand All @@ -113,7 +134,7 @@ function BLU:UpdatePetData()

-- Store valid pet data if it's a valid battle pet with a proper level
if canBattle and level and type(level) == "number" and level >= 1 and level <= 25 then
BLU.petData[petID] = level
BLU.previousPetLevels[petID] = level
self:PrintDebugMessage(string.format("Stored Pet ID: %s, Level: %d", tostring(petID), level))
else
self:PrintDebugMessage(string.format("Invalid or non-battle pet data for Pet ID: %s, Level: %d", petID or "N/A", level or -1))
Expand Down Expand Up @@ -155,4 +176,4 @@ function BLU:IsPetLevelUpItem(itemID)
171008, 171009, 171010, 171011, 171012
}
return tContains(petLevelUpItems, itemID)
end
end

0 comments on commit 9b2f5ed

Please sign in to comment.