LibPetJournal-2.0

67.5k Downloads

Requires: LibStub, CallbackHandler-1.0

With 5.0 a new C_PetJournal API was introduced, and unfortunately the old GetCompanionInfo API no longer works properly (it does not, for example, know anything about non-combat pets obtained before the account-wide merge, and you also can no longer summon non-combat pets using it).

This library provides the list of player owned pet ids and all species ids, which can be used with C_PetJournal.GetPetInfoByPetID() and C_PetJournal.GetPetInfoBySpeciesID().

It primarily deals with two problems with scanning the pet journal:

  • Filters: The Pet Journal filters are cleared and then restored, as they affect the return values given from C_PetJournal.GetPetInfoByIndex()
  • Updating in response to event: Manipulating the Pet Journal filters causes PET_JOURNAL_LIST_UPDATE to fire, which is also the only event that always fires when you lose or gain a pet. Filter manipulation is detected and we never try to rescan pets in response. When this is not the case, a CallbackHandler event is fired which can be used to safely detect changes to the pet list.

Links

Getting Started
API Documentation
WoWAce Thread

Example

local LibPetJournal = LibStub("LibPetJournal-2.0")
local function ScanPets()
    for _,petid in LibPetJournal:IteratePetIDs() do 
        local speciesID, customName, level, xp, maxXp, displayID, isFavorite, 
               name, icon, petType, creatureID, sourceText, description, isWild,
               canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID(petid)
        if name == "Feline Familiar" then
            print("Player has a cat in a hat.")
        end
    end
end
ScanPets()

If you wanted to automatically scan for little cats in hats when the pet list changes, you could register a callback, such as:

LibPetJournal.RegisterCallback(addon, "PetListUpdated", ScanPets)

Obviously you would normally use the speciesID or creatureID return values from C_PetJournal.GetPetInfoByPetID() to find a particular pet, but this is just a silly example.