-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPetPet.lua
107 lines (88 loc) · 2.99 KB
/
PetPet.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
local function FindAura(unit, spellID, filter)
for i = 1, 100 do
local aura = C_UnitAuras.GetAuraDataByIndex(unit, i, filter)
if aura == nil then return nil end
if spellID == aura.spellId then
return aura
end
end
end
local function HasActivePet(numPets)
if C_PetJournal.IsCurrentlySummoned == nil then
return false
end
local activePet = false
local ownedPets = {}
for i = 1, numPets do
local petID, _, owned, _, _, _, _, _ = C_PetJournal.GetPetInfoByIndex(i)
if owned then
table.insert(ownedPets, petID)
end
end
for i = 1, #ownedPets do
local petID = ownedPets[i]
local isSummoned = C_PetJournal.IsCurrentlySummoned(petID)
if isSummoned then
activePet = true
end
end
return activePet
end
local function InitialisePetPet()
local PetPetListener = CreateFrame("Frame")
PetPetListener:RegisterEvent("PLAYER_STARTED_MOVING")
PetPetListener:SetScript("OnEvent", function()
local favouritePets = {}
local numPets, numOwned = C_PetJournal.GetNumPets()
local petActive = HasActivePet(numPets) or C_PetJournal.GetSummonedPetGUID() ~= nil
local canSummon = not UnitAffectingCombat("player")
-- Mounted / Flying / In a vehicle:
and not IsMounted() and not IsFlying() and not UnitHasVehicleUI("player")
-- If player is mind-controlling:
and not (UnitIsControlling("player") and UnitChannelInfo("player"))
-- Stealthed / Dead
and not IsStealthed() and not UnitIsGhost("player")
-- Camouflage:
and not FindAura("player", 199483, "HELPFUL")
-- Invisibility:
and not FindAura("player", 32612, "HELPFUL")
-- Greater Invisibility:
and not FindAura("player", 110960, "HELPFUL")
-- Gas Cloud:
and not UnitChannelInfo("player")
-- Does not have a pet summoned:
and not petActive
-- Has at least 1 pet in their Companions list:
and numOwned > 0
if canSummon then
for i = 1, numPets do
local petID, _, owned, _, _, favorite, _, _ = C_PetJournal.GetPetInfoByIndex(i)
if owned and favorite then
table.insert(favouritePets, tostring(petID))
end
end
if #favouritePets > 0 then
C_PetJournal.SummonPetByGUID(favouritePets[math.random(#favouritePets)])
end
end
end)
end
local loadingEvents = CreateFrame("Frame")
loadingEvents:RegisterEvent("ADDON_LOADED")
loadingEvents:RegisterEvent("PLAYER_ENTERING_WORLD")
loadingEvents:SetScript(
"OnEvent",
function(_, event, arg1)
if event == "ADDON_LOADED" and arg1 == "PetPet" then
-- Clean up old config settings:
if PetPetDB ~= nil then
PetPetDB = {}
end
loadingEvents:UnregisterEvent("ADDON_LOADED")
end
if event == "PLAYER_ENTERING_WORLD" then
InitialisePetPet()
loadingEvents:UnregisterEvent("PLAYER_ENTERING_WORLD")
end
end
)