-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApi.lua
More file actions
193 lines (166 loc) · 5.97 KB
/
Api.lua
File metadata and controls
193 lines (166 loc) · 5.97 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
local _, ADDON = ...
ADDON.Api = {}
function ADDON.Api:MountIdToOriginalIndex(mountId)
ADDON:ResetIngameFilter()
local count = C_MountJournal.GetNumDisplayedMounts()
for i = 1, count do
local displayedMountId = C_MountJournal.GetDisplayedMountID(i)
if displayedMountId == mountId then
return i
end
end
return nil
end
function ADDON.Api:GetMountInfoByID(mountId)
local creatureName, spellId, icon, active, isUsable, sourceType, isFavorite, isFaction, faction, hideOnChar, isCollected, mountID, isSteadyFlight, a, b, c, d, e, f, g, h = C_MountJournal.GetMountInfoByID(mountId)
isUsable = isUsable and C_Spell.IsSpellUsable(spellId)
isSteadyFlight = isSteadyFlight and ADDON.isRetail
return creatureName, spellId, icon, active, isUsable, sourceType, isFavorite, isFaction, faction, hideOnChar, isCollected, mountID, isSteadyFlight, a, b, c, d, e, f, g, h
end
function ADDON.Api:GetMountLink(spellId)
local link = C_MountJournal.GetMountLink(spellId)
if not link or strlen(link) > 400 then
-- fallback for broken links (https://github.com/Stanzilla/WoWUIBugs/issues/699)
return C_Spell.GetSpellLink(spellId)
end
return link
end
function ADDON.Api:PickupByID(mountId, ...)
local index = ADDON.Api:MountIdToOriginalIndex(mountId)
if index then
return C_MountJournal.Pickup(index, ...)
end
end
--region favorites
function ADDON.Api:HasFavorites()
local _, _, favorites = ADDON.Api:GetFavoriteProfile()
return #favorites > 0
end
function ADDON.Api:GetFavoriteProfile()
local playerGuid = UnitGUID("player")
local profileIndex = ADDON.settings.favorites.assignments[playerGuid] or 1
if not ADDON.settings.favorites.profiles[profileIndex] then
profileIndex = 1
end
local name = ADDON.settings.favorites.profiles[profileIndex].name
if profileIndex == 1 then
name = ADDON.L.FAVORITE_ACCOUNT_PROFILE
end
return profileIndex, name, ADDON.settings.favorites.profiles[profileIndex].mounts
end
function ADDON.Api:GetIsFavoriteByID(mountId)
local _, _, favorites = ADDON.Api:GetFavoriteProfile()
return tContains(favorites, mountId)
end
function ADDON.Api:SetIsFavoriteByID(mountId, value)
local _, _, favorites = ADDON.Api:GetFavoriteProfile()
local hasChange = false
if true == value and not tContains(favorites, mountId) then
table.insert(favorites, mountId)
hasChange = true
elseif false == value then
local i = tIndexOf(favorites, mountId)
if i then
tUnorderedRemove(favorites, i)
hasChange = true
end
end
if hasChange then
ADDON.Events:TriggerEvent("OnFavoritesChanged")
end
end
function ADDON.Api:SetBulkIsFavorites(filteredProvider, inclusive)
local _, _, profileMounts = ADDON.Api:GetFavoriteProfile()
local mountIdsToAdd = CopyValuesAsKeys(filteredProvider)
local hasChange = false
local index = 1
while index <= #profileMounts do
local mountId = profileMounts[index]
if mountIdsToAdd[mountId] then
mountIdsToAdd[mountId] = nil
index = index + 1
elseif inclusive then
-- skip remove
index = index + 1
else
tUnorderedRemove(profileMounts, index)
hasChange = true
end
end
local tInsert = table.insert
for mountId, shouldAdd in pairs(mountIdsToAdd) do
if shouldAdd then
local isCollected = select(11, C_MountJournal.GetMountInfoByID(mountId))
if isCollected then
tInsert(profileMounts, mountId)
hasChange = true
end
end
end
if hasChange then
ADDON.Events:TriggerEvent("OnFavoritesChanged")
end
end
function ADDON.Api:SwitchFavoriteProfile(newIndex)
local oldIndex = ADDON.Api:GetFavoriteProfile()
if oldIndex ~= newIndex then
ADDON.settings.favorites.assignments[UnitGUID("player")] = newIndex
ADDON.Events:TriggerEvent("OnFavoritesChanged")
ADDON.Events:TriggerEvent("OnFavoriteProfileChanged")
end
end
function ADDON.Api:RemoveFavoriteProfile(index)
if index > 1 then
local profileIndex = ADDON.Api:GetFavoriteProfile()
if profileIndex == index then
ADDON.Api:SwitchFavoriteProfile(1)
end
ADDON.settings.favorites.profiles[index] = nil
-- cleanup all assignments
for guid, profileIndex in pairs(ADDON.settings.favorites.assignments) do
if profileIndex == index then
ADDON.settings.favorites.assignments[guid] = 1
end
end
end
end
--endregion
local selectedCreature
function ADDON.Api:SetSelected(selectedMountID, selectedVariation)
if MountJournal.selectedMountID ~= selectedMountID then
selectedCreature = selectedVariation or nil
MountJournal_SelectByMountID(selectedMountID)
elseif selectedCreature ~= selectedVariation then
selectedCreature = selectedVariation or nil
MountJournal_UpdateMountDisplay(true)
end
end
function ADDON.Api:GetSelected()
return MountJournal.selectedMountID, selectedCreature
end
function ADDON.Api:IsDynamicFlight()
if C_MountJournal.IsDragonridingUnlocked() then
local description = C_Spell.GetSpellDescription(C_MountJournal.GetDynamicFlightModeSpellID())
local pos = string.find(description, ACCESSIBILITY_ADV_FLY_LABEL,1 , true)
if pos then
return pos < 15
end
end
return false
end
local dataProvider
function ADDON.Api:GetDataProvider()
if not dataProvider then
dataProvider = CreateDataProvider()
dataProvider:SetSortComparator(function(a, b)
return ADDON:SortHandler(a, b)
end)
end
return dataProvider
end
ADDON.Events:RegisterCallback("OnJournalLoaded", function()
-- setup hooks
hooksecurefunc("MountJournal_SelectByMountID", function(mountId)
ADDON.Api:SetSelected(mountId)
end)
end, "api hooks")