forked from Kruithne/AutoCage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoCage.lua
More file actions
313 lines (270 loc) · 10.3 KB
/
Copy pathAutoCage.lua
File metadata and controls
313 lines (270 loc) · 10.3 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
--[[
AutoCage (C) @Kruithne <kruithne@gmail.com>
Licensed under GNU General Public Licence version 3.
https://github.com/Kruithne/AutoCage
AutoCage.lua - Core engine file for the addon.
]]
local petCagedPattern = string.gsub(BATTLE_PET_NEW_PET, "%%s", ".*Hbattlepet:(%%d+).*");
local acHasHooked = false;
L_AUTOCAGE_CAGED_MESSAGE = {
["frFR"] = "Dupliquer animal de compagnie; la mise en cage pour vous!",
["deDE"] = "Doppeltes Haustier; Sperre es in einen Käfig für dich!",
["enUS"] = "Duplicate pet; caging it for you!",
["itIT"] = "Duplicare animale; messa in gabbia per voi!",
["koKR"] = "애완 동물을 복제 ; 케이지 에 넣어!",
["zhCN"] = "重复宠物;把它关在笼子里!",
["zhTW"] = "重複寵物;把它關在籠子裡!",
["ruRU"] = "Повторяющиеся питомца; положить его в клетку!",
["esES"] = "Duplicar mascota; ponerlo en una jaula!",
["esMX"] = "Duplicar mascota; ponerlo en una jaula!",
["ptBR"] = "Duplicar para animais de estimação ; colocá-lo em uma gaiola!"
};
L_AUTOCAGE_LOADED = {
["frFR"] = "Chargé!",
["deDE"] = "Geladen!",
["enUS"] = "Loaded!",
["itIT"] = "Caricato!",
["koKR"] = "로드!",
["zhCN"] = "加载!",
["zhTW"] = "載入!",
["ruRU"] = "Загружен!",
["esES"] = "Cargado!",
["esMX"] = "Cargado!",
["ptBR"] = "Carregado!"
};
L_AUTOCAGE_DUPLICATE_PETS_BUTTON = {
["deDE"] = "Haustiere einsperren", -- missing "duplicate" due to length
["enUS"] = "Cage Duplicate Pets",
};
L_AUTOCAGE_DUPLICATE_PETS_BUTTON_TOOLTIP = {
["deDE"] = "Sperrt alle Haustiere in einen Käfig, die weder favorisiert sind, noch Level eins übersteigen.",
["enUS"] = "Cages all duplicate pets that are neither favourited or above level one.",
};
L_AUTOCAGE_CHECKBOX = {
["frFR"] = "Automatiquement les doublons de cage",
["deDE"] = "Haustiere automatisch einsperren", -- missing "duplicate" due to length
["enUS"] = "Automatically cage duplicates",
["itIT"] = "Automaticamente i duplicati di gabbia",
["koKR"] = "자동으로 케이지 중복",
["zhCN"] = "自动笼子里重复",
["zhTW"] = "自動籠子裡重複",
["ruRU"] = "Автоматически Кейдж дубликаты",
["esES"] = "Automáticamente duplicados de jaula",
["esMX"] = "Automáticamente duplicados de jaula",
["ptBR"] = "Duplicatas de gaiola automaticamente",
};
L_AUTOCAGE_CHECKBOX_TOOLTIP = {
["frFR"] = "Si activé, les animaux de compagnie en double qui s'apprend on mettra automatiquement dans une cage.",
["deDE"] = "Wenn aktiviert werden doppelte Haustiere automatisch in einen Käfig gesetzt, sobald sie erlernt werden.",
["enUS"] = "If enabled, duplicate pets that get learnt will automatically be put in a cage.",
["itIT"] = "Se abilitata, animali duplicati che avere imparati metterà automaticamente in una gabbia.",
["koKR"] = "사용 하면 배운 얻을 중복 애완 동물 감 금 소에 게 자동으로 됩니다.",
["zhCN"] = "如果启用,把学到的重复宠物自动将会在一个笼子里。",
["zhTW"] = "如果啟用,把學到的重複寵物自動將會在一個籠子裡。",
["ruRU"] = "Если этот параметр включен, повторяющиеся животные, которые научились получать автоматически ставится в клетке.",
["esES"] = "Si se activa, automáticamente se pondrá mascotas duplicadas que haz aprendidas en una jaula.",
["esMX"] = "Si se activa, automáticamente se pondrá mascotas duplicadas que haz aprendidas en una jaula.",
["ptBR"] = "Se habilitado, animais de estimação duplicados que se aprendeu serão automaticamente colocados em uma gaiola.",
};
L_AUTOCAGE_AUTO_ENABLED = {
["enUS"] = "Duplicate pets will now be automatically caged when obtained.",
};
L_AUTOCAGE_AUTO_DISABLED = {
["enUS"] = "Duplicate pets will no longer be automatically caged when obtained.",
};
L_AUTOCAGE_CAGING = {
["enUS"] = "Caging duplicate pets...",
};
L_AUTOCAGE_COMMANDS_AVAILABLE = {
["enUS"] = "Available Commands:",
};
L_AUTOCAGE_COMMANDS_HELP = {
["enUS"] = "List available commands.",
};
L_AUTOCAGE_COMMANDS_CAGE = {
["enUS"] = "Toggle auto-caging functionality.",
};
L_AUTOCAGE_COMMANDS_TOGGLE = {
["enUS"] = "Cage all duplicate pets.",
};
--[[
AutoCage_GetLocalizedString
Selects the localized string from a localization table.
]]
function AutoCage_GetLocalizedString(strings)
return strings[GetLocale()] or strings["enUS"] or "Unknown";
end
--[[
AutoCage_HandleAutoCaging
Find all duplicates and cage them.
]]
local petsToCage = {};
function AutoCage_HandleAutoCaging(petToCageID)
C_PetJournal.ClearSearchFilter(); -- Clear filter so we have a full pet list.
C_PetJournal.SetPetSortParameter(LE_SORT_BY_LEVEL); -- Sort by level, ensuring higher level pets are encountered first.
local total, owned = C_PetJournal.GetNumPets();
local petCache = {};
petsToCage = {};
if petToCageID ~= nil then
petToCageID = tonumber(petToCageID);
end
for index = 1, owned do -- Loop every pet owned (unowned will be over the offset).
local pGuid, pBattlePetID, _, pNickname, pLevel, pIsFav, _, pName, _, _, _, _, _, _, _, pIsTradeable = C_PetJournal.GetPetInfoByIndex(index);
if petToCageID == nil or petToCageID == pBattlePetID then
if petCache[pBattlePetID] == true then
if pLevel == 1 and not pIsFav and pIsTradeable then
AutoCage_Message(pName .. " :: " .. AutoCage_GetLocalizedString(L_AUTOCAGE_CAGED_MESSAGE));
table.insert(petsToCage, pGuid);
end
else
petCache[pBattlePetID] = true;
end
end
end
end
--[[
AutoCage_Load
Run set-up tasks when addon is loaded.
]]
function AutoCage_Load()
if AutoCageEnabled == nil then
-- No global enabled variable found, setting default.
AutoCageEnabled = true;
end
-- Set the checkbox to the correct state now we've loaded.
-- Technically AutoCage_Load is called before AutoCage_JournalHook but
-- if for some reason it's not, this is a safety net for that.
if AutoCage_EnabledButton ~= nil then
AutoCage_EnabledButton:SetChecked(AutoCageEnabled);
end
end
--[[
AutoCage_JournalHook
Hook our enable checkbox onto the journal frame.
]]
function AutoCage_JournalHook()
if acHasHooked or IsAddOnLoaded("Rematch") then
return;
end
-- Add a caging button
cageButton = CreateFrame("Button", "AutoCage_CageButton", PetJournal, "MagicButtonTemplate");
cageButton:SetPoint("LEFT", PetJournalSummonButton, "RIGHT", 0, 0);
cageButton:SetWidth(150);
cageButton:SetText(AutoCage_GetLocalizedString(L_AUTOCAGE_DUPLICATE_PETS_BUTTON));
cageButton:SetScript("OnClick", function() AutoCage_HandleAutoCaging(nil) end);
cageButton:SetScript("OnEnter",
function(self)
GameTooltip:SetOwner (self, "ANCHOR_RIGHT");
GameTooltip:SetText(AutoCage_GetLocalizedString(L_AUTOCAGE_DUPLICATE_PETS_BUTTON), 1, 1, 1);
GameTooltip:AddLine(AutoCage_GetLocalizedString(L_AUTOCAGE_DUPLICATE_PETS_BUTTON_TOOLTIP), nil, nil, nil, true);
GameTooltip:Show();
end
);
cageButton:SetScript("OnLeave",
function()
GameTooltip:Hide();
end
);
-- Set-up enable/disable check-button.
checkButton = CreateFrame("CheckButton", "AutoCage_EnabledButton", PetJournal, "ChatConfigCheckButtonTemplate");
checkButton:SetPoint("LEFT", AutoCage_CageButton, "RIGHT", 10, -2);
checkButton:SetChecked(AutoCageEnabled);
AutoCage_EnabledButtonText:SetPoint("LEFT", checkButton, "RIGHT", -1, 0);
AutoCage_EnabledButtonText:SetText(AutoCage_GetLocalizedString(L_AUTOCAGE_CHECKBOX));
checkButton.tooltip = AutoCage_GetLocalizedString(L_AUTOCAGE_CHECKBOX_TOOLTIP);
checkButton:SetScript("OnClick",
function()
if AutoCageEnabled then
AutoCageEnabled = false;
else
AutoCageEnabled = true;
end
end
);
acHasHooked = true;
end
--[[
AutoCage_Message
Prints a formatted message to the default chat frame.
]]
function AutoCage_Message(msg)
DEFAULT_CHAT_FRAME:AddMessage("\124cffc79c6eAutoCage:\124r \124cff69ccf0" .. msg .."\124r");
end
-- Event handling frame.
local eventFrame = CreateFrame("FRAME");
eventFrame:RegisterEvent("CHAT_MSG_SYSTEM");
eventFrame:RegisterEvent("ADDON_LOADED");
eventFrame.elapsed = 0;
eventFrame.pendingUpdate = false;
eventFrame.cageTimer = 0;
eventFrame:SetScript("OnEvent", function(self, event, ...)
if event == "CHAT_MSG_SYSTEM" and AutoCageEnabled then
local msg, format = ...;
local match = string.match(msg, petCagedPattern);
if match ~= nil then
self.pendingUpdate = true;
self.petID = match;
end
elseif event == "ADDON_LOADED" then
local addon = ...;
if addon == "Blizzard_Collections" then
AutoCage_JournalHook();
elseif addon == "AutoCage" then
AutoCage_Load();
if IsAddOnLoaded("Blizzard_Collections") then
AutoCage_JournalHook();
end
end
end
end);
eventFrame:SetScript("OnUpdate", function(self, elapsed)
if self.pendingUpdate then
if self.elapsed >= 1 then
AutoCage_HandleAutoCaging(self.petID);
self.elapsed = 0;
self.pendingUpdate = false;
else
self.elapsed = self.elapsed + elapsed;
end
end
if self.cageTimer >= 1 then
if #petsToCage > 0 then
for i=1, #petsToCage do
C_PetJournal.CagePetByID(petsToCage[i]);
end
end
self.cageTimer = 0;
else
self.cageTimer = self.cageTimer + elapsed;
end
end);
local AutoCageCommands = {};
function AutoCage_CommandToggle()
if AutoCageEnabled then
AutoCageEnabled = false;
AutoCage_Message(AutoCage_GetLocalizedString(L_AUTOCAGE_AUTO_DISABLED));
else
AutoCageEnabled = true;
AutoCage_Message(AutoCage_GetLocalizedString(L_AUTOCAGE_AUTO_ENABLED));
end
end
function AutoCage_CommandCage()
AutoCage_HandleAutoCaging(nil);
AutoCage_Message(AutoCage_GetLocalizedString(L_AUTOCAGE_CAGING));
end
function AutoCage_CommandDefault()
AutoCage_Message(AutoCage_GetLocalizedString(L_AUTOCAGE_COMMANDS_AVAILABLE));
local commandFormat = " /%s - %s";
for id, node in pairs(AutoCageCommands) do
AutoCage_Message(commandFormat:format(id, node.info));
end
end
AutoCageCommands["help"] = { func = AutoCage_CommandDefault, info = AutoCage_GetLocalizedString(L_AUTOCAGE_COMMANDS_HELP) };
AutoCageCommands["toggle"] = { func = AutoCage_CommandToggle, info = AutoCage_GetLocalizedString(L_AUTOCAGE_COMMANDS_TOGGLE) };
AutoCageCommands["cage"] = { func = AutoCage_CommandCage, info = AutoCage_GetLocalizedString(L_AUTOCAGE_COMMANDS_CAGE) };
-- Commands
SLASH_AUTOCAGE1 = "/autocage";
SlashCmdList["AUTOCAGE"] = function(text, editbox)
local command = AutoCageCommands[text] or AutoCageCommands["help"];
command.func();
end;