Files
coa-altoholic/DataStore_Pets/DataStore_Pets.lua
T
florian.berthold f4f3de929b
release / release (push) Successful in 5s
coa.28: fix login scan in 9 DataStore modules (the 'data not saved' root cause)
Quests, Achievements, Reputations, Pets, Stats, Skills, Crafts, Spells, Talents all had
the ghost-gated PLAYER_ALIVE scan (DEBUG 2025-07-21 leftover): they only scanned when the
player died and released spirit, so their data never populated on a normal login. Now
scan once per session at login (addon.coaScannedThisSession guard), matching the earlier
DataStore_Characters/_Inventory fix. This is why reputations/recipes/quests/pets/etc were
'not saved'.
2026-05-29 23:55:29 +02:00

150 lines
4.1 KiB
Lua

--[[ *** DataStore_Pets ***
Written by : Thaoky, EU-Marécages de Zangar
June 22st, 2009
--]]
if not DataStore then return end
local addonName = "DataStore_Pets"
_G[addonName] = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0")
local addon = _G[addonName]
local THIS_ACCOUNT = "Default"
local AddonDB_Defaults = {
global = {
Characters = {
['*'] = { -- ["Account.Realm.Name"]
lastUpdate = nil,
CRITTER = {}, -- companion types are used as table names
MOUNT = {},
}
}
}
}
-- *** Scanning functions ***
local COMPANION_ICON_PATH = "Interface\\Icons\\"
local function ScanCompanions(companionType)
local list = addon.ThisCharacter[companionType]
wipe(list)
for i = 1, GetNumCompanions(companionType) do
local modelID, name, spellID, icon = GetCompanionInfo(companionType, i);
if modelID and name and spellID and icon then
-- trim icon path to save memory
list[i] = modelID .. "|" .. name .. "|" .. spellID .. "|" .. string.gsub(icon, COMPANION_ICON_PATH, "")
end
end
addon.ThisCharacter.lastUpdate = time()
end
-- *** Event Handlers ***
local function OnPlayerAlive()
-- print("DataStore_Pets.lua") -- DEBUG 2025 07 21
if addon.coaScannedThisSession then return end -- CoA: scan once at login (was ghost-gated, so data never saved on a normal login - the cause of "data not saved")
addon.coaScannedThisSession = true
ScanCompanions("CRITTER")
ScanCompanions("MOUNT")
end
local function OnCompanionUpdate()
-- COMPANION_UPDATE is triggered very often, but after the very first call, pets & mounts can be scanned automatically. After that, we only need to track COMPANION_LEARNED
addon:UnregisterEvent("COMPANION_UPDATE")
ScanCompanions("CRITTER")
ScanCompanions("MOUNT")
end
local function OnCompanionLearned()
ScanCompanions("CRITTER")
ScanCompanions("MOUNT")
end
-- ** Mixins **
local function _GetPets(character, companionType)
return character[companionType]
end
local function _GetNumPets(pets)
if type(pets) ~= "table" then return 0 end -- CoA: char may have no scanned CRITTER/MOUNT table yet; upstream assert() crashed TabCharacters
return #pets
end
local function _GetPetInfo(pets, index)
local pet = pets[index]
if pet then
local modelID, name, spellID, icon = strsplit("|", pet)
return tonumber(modelID), name, tonumber(spellID), "Interface\\Icons\\" .. icon
end
end
local function _IsPetKnown(character, companionType, spellID)
local pets = _GetPets(character, companionType)
for i = 1, #pets do
local _, _, id = _GetPetInfo(pets, i)
if id == spellID then
return true -- returns true if a given spell ID is a known pet or mount
end
end
end
local function _GetMountList()
return addon.MountList
end
local function _GetMountSpellID(itemID)
-- returns nil if id is not in the DB, returns the spellID otherwise
return addon.MountToSpellID[itemID]
end
local function _GetCompanionList()
return addon.CompanionList
end
local function _GetCompanionSpellID(itemID)
-- returns nil if id is not in the DB, returns the spellID otherwise
return addon.CompanionToSpellID[itemID]
end
local function _GetCompanionLink(spellID)
local name = GetSpellInfo(spellID)
return format("|cff71d5ff|Hspell:%s|h[%s]|h|r", spellID, name)
end
local PublicMethods = {
GetPets = _GetPets,
GetNumPets = _GetNumPets,
GetPetInfo = _GetPetInfo,
IsPetKnown = _IsPetKnown,
GetMountList = _GetMountList,
GetMountSpellID = _GetMountSpellID,
GetCompanionList = _GetCompanionList,
GetCompanionSpellID = _GetCompanionSpellID,
GetCompanionLink = _GetCompanionLink,
}
function addon:OnInitialize()
addon.db = LibStub("AceDB-3.0"):New(addonName .. "DB", AddonDB_Defaults)
DataStore:RegisterModule(addonName, addon, PublicMethods)
DataStore:SetCharacterBasedMethod("GetPets")
DataStore:SetCharacterBasedMethod("IsPetKnown")
end
function addon:OnEnable()
addon:RegisterEvent("PLAYER_ALIVE", OnPlayerAlive)
addon:RegisterEvent("COMPANION_UPDATE", OnCompanionUpdate)
addon:RegisterEvent("COMPANION_LEARNED", OnCompanionLearned)
end
function addon:OnDisable()
addon:UnregisterEvent("PLAYER_ALIVE")
addon:UnregisterEvent("COMPANION_LEARNED")
end