-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMSBTLoot.lua
198 lines (157 loc) · 7.84 KB
/
MSBTLoot.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
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
-------------------------------------------------------------------------------
-- Title: Mik's Scrolling Battle Text Loot
-- Author: Mikord
-------------------------------------------------------------------------------
-- Create module and set its name.
local module = {}
local moduleName = "Loot"
MikSBT[moduleName] = module
-------------------------------------------------------------------------------
-- Imports.
-------------------------------------------------------------------------------
-- Local references to various modules for faster access.
local MSBTProfiles = MikSBT.Profiles
local MSBTParser = MikSBT.Parser
-- Get local references to various functions for faster access.
local string_gsub = string.gsub
local string_format = string.format
local math_ceil = math.ceil
local GetItemInfo = GetItemInfo
local GetItemCount = GetItemCount
local DisplayEvent = MikSBT.Animations.DisplayEvent
-------------------------------------------------------------------------------
-- Constants.
-------------------------------------------------------------------------------
-- Money strings.
local GOLD = string_gsub(GOLD_AMOUNT, "%%d *", "")
local SILVER = string_gsub(SILVER_AMOUNT, "%%d *", "")
local COPPER = string_gsub(COPPER_AMOUNT, "%%d *", "")
-- Localized name for item types.
local ITEM_TYPE_QUEST = _G.GetItemClassInfo(LE_ITEM_CLASS_QUESTITEM)
-------------------------------------------------------------------------------
-- Private variables.
-------------------------------------------------------------------------------
-- Prevent tainting global _.
local _
-- Color patterns for item qualities.
local qualityPatterns = {}
-------------------------------------------------------------------------------
-- Event handlers.
-------------------------------------------------------------------------------
-- ****************************************************************************
-- Handles looted money.
-- ****************************************************************************
local function HandleMoney(parserEvent)
-- Money gain.
local moneyString = parserEvent.moneyString
moneyString = string_gsub(moneyString, GOLD, "|cffffd700%1|r")
moneyString = string_gsub(moneyString, SILVER, "|cff808080%1|r")
moneyString = string_gsub(moneyString, COPPER, "|cffeda55f%1|r")
-- Format the event and display it.
local eventSettings = MSBTProfiles.currentProfile.events.NOTIFICATION_MONEY
if (eventSettings and not eventSettings.disabled) then
local message = eventSettings.message
message = string_gsub(message, "%%e", moneyString)
DisplayEvent(eventSettings, message)
end
end
-- ****************************************************************************
-- Handles looted currency.
-- ****************************************************************************
local function HandleCurrency(parserEvent)
-- Get information about the looted currency.
local itemLink = parserEvent.itemLink
local itemName, numAmount, itemTexture, totalMax, itemQuality, numLootedFromMessage
local currency = C_CurrencyInfo.GetCurrencyInfoFromLink(itemLink)
if currency then
itemName, numAmount, itemTexture, totalMax, itemQuality = currency.name, currency.quantity, currency.iconFileID, currency.maxQuantity, currency.quality
else
if string.match(itemLink,"^, %d+") then
numLootedFromMessage = string.match(itemLink, "%d+")
currency = C_CurrencyInfo.GetCurrencyInfo(1901)
itemName, numAmount, itemTexture, totalMax, itemQuality = currency.name, currency.quantity, currency.iconFileID, currency.maxQuantity, currency.quality
else
return
end
end
-- Determine whether to show the event and ignore it if necessary.
local currentProfile = MSBTProfiles.currentProfile
local showEvent = true
if (currentProfile.itemExclusions[itemName]) then showEvent = false end
if (currentProfile.itemsAllowed[itemName]) then showEvent = true end
if (not showEvent) then return end
-- Format the item name according to its quality.
local qualityColor = ITEM_QUALITY_COLORS[itemQuality]
if (qualityPatterns[itemQuality]) then itemName = string_format (qualityPatterns[itemQuality], itemName) end
local numLooted = parserEvent.amount or numLootedFromMessage or 1
-- Format the event and display it.
local eventSettings = MSBTProfiles.currentProfile.events.NOTIFICATION_CURRENCY
if (eventSettings and not eventSettings.disabled) then
local message = eventSettings.message
message = string_gsub(message, "%%e", itemName)
message = string_gsub(message, "%%a", numLooted)
message = string_gsub(message, "%%t", numAmount)
DisplayEvent(eventSettings, message, itemTexture)
end
end
local function loot_wait(itemLink, itemName, itemTexture, numLooted)
local numItems = GetItemCount(itemLink)
if (numItems == 0) then
numItems = numLooted
end
local numTotal = numItems
-- Format the event and display it.
local eventSettings = MSBTProfiles.currentProfile.events.NOTIFICATION_LOOT
if (eventSettings and not eventSettings.disabled) then
local message = eventSettings.message
message = string_gsub(message, "%%e", itemName)
message = string_gsub(message, "%%a", numLooted)
message = string_gsub(message, "%%t", numTotal)
DisplayEvent(eventSettings, message, itemTexture)
end
end
-- ****************************************************************************
-- Handles looted items.
-- ****************************************************************************
local function HandleItems(parserEvent)
-- Created items are buggy. Ignore them.
if (parserEvent.isCreate) then return end
-- Get information about the looted item.
local itemLink = parserEvent.itemLink
local itemName, _, itemQuality, _, _, itemType, _, _, _, itemTexture = GetItemInfo(itemLink)
-- Determine whether to show the event and ignore it if necessary.
local currentProfile = MSBTProfiles.currentProfile
local showEvent = true
if (currentProfile.qualityExclusions[itemQuality]) then showEvent = false end
if ((itemType == ITEM_TYPE_QUEST) and currentProfile.alwaysShowQuestItems) then showEvent = true end
if (currentProfile.itemExclusions[itemName]) then showEvent = false end
if (currentProfile.itemsAllowed[itemName]) then showEvent = true end
if (not showEvent) then return end
-- Format the item name according to its quality.
local qualityColor = ITEM_QUALITY_COLORS[itemQuality]
if (qualityPatterns[itemQuality]) then itemName = string_format(qualityPatterns[itemQuality], itemName) end
-- Get the number of items already existing in inventory and add the amount
-- looted to it if the item wasn't the result of a conjure.
local numLooted = parserEvent.amount or 1
local lootProcessDelay = 0.15
if (currentProfile.lootProcessDelay) then lootProcessDelay = currentProfile.lootProcessDelay end
C_Timer.After(lootProcessDelay, function() loot_wait(itemLink, itemName, itemTexture, numLooted) end)
end
-- ****************************************************************************
-- Parser events handler.
-- ****************************************************************************
local function ParserEventsHandler(parserEvent)
-- Ignore the event if it isn't for the player or not a loot event.
if (parserEvent.recipientUnit ~= "player" or parserEvent.eventType ~= "loot") then return end
-- Call the correct handler for the loot type.
if (parserEvent.isMoney) then HandleMoney(parserEvent) elseif (parserEvent.isCurrency) then HandleCurrency(parserEvent) elseif (parserEvent.itemLink) then HandleItems(parserEvent) end
end
-------------------------------------------------------------------------------
-- Initialization.
-------------------------------------------------------------------------------
-- Setup the item quality color patterns.
for k, v in pairs(ITEM_QUALITY_COLORS) do
qualityPatterns[k] = string_format("|cFF%02x%02x%02x[%%s]|r", math_ceil(v.r * 255), math_ceil(v.g * 255), math_ceil(v.b * 255))
end
-- Register the parser events handler.
MSBTParser.RegisterHandler(ParserEventsHandler)