Advertisement
Wetxius

Untitled

Feb 12th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. local T, C, L, _ = unpack(select(2, ...))
  2. if C.tooltip.enable ~= true or C.tooltip.average_lvl ~= true then return end
  3.  
  4. ----------------------------------------------------------------------------------------
  5. -- Equipped average item level(EquippedItemLevel by Villiv)
  6. ----------------------------------------------------------------------------------------
  7. -- Additional strings
  8. local WAITING = CONTINUED
  9. local PENDING = CONTINUED..CONTINUED
  10.  
  11. local upgrades = {
  12. ["1"] = 8, ["373"] = 4, ["374"] = 8, ["375"] = 4, ["376"] = 4, ["377"] = 4,
  13. ["379"] = 4, ["380"] = 4, ["446"] = 4, ["447"] = 8, ["452"] = 8, ["454"] = 4,
  14. ["455"] = 8, ["457"] = 8, ["459"] = 4, ["460"] = 8, ["461"] = 12, ["462"] = 16,
  15. ["466"] = 4, ["467"] = 8, ["469"] = 4, ["470"] = 8, ["471"] = 12, ["472"] = 16,
  16. ["477"] = 4, ["478"] = 8, ["480"] = 8, ["492"] = 4, ["493"] = 8, ["495"] = 4,
  17. ["496"] = 8, ["497"] = 12, ["498"] = 16, ["504"] = 12, ["505"] = 16, ["506"] = 20,
  18. ["507"] = 24, ["530"] = 5, ["531"] = 10
  19. }
  20. -- Output prefix
  21. local PREFIX = STAT_FORMAT:format(STAT_AVERAGE_ITEM_LEVEL).."|Heqppditmlvl|h |h"..HIGHLIGHT_FONT_COLOR_CODE
  22.  
  23. local f = CreateFrame("Frame")
  24. f:SetScript("OnEvent", function(self, event, ...) return self[event](self, event, ...)end)
  25. f:Hide()
  26.  
  27. local playerGUID, inCombat, updateTimer
  28. local currentUnit, currentGUID
  29. local isDelayed, isForced, isNotified, isReady
  30.  
  31. local function GetTipUnit()
  32. local _, unit = GameTooltip:GetUnit()
  33. if not unit then
  34. local mouseFocus = GetMouseFocus()
  35. unit = mouseFocus and (mouseFocus.unit or mouseFocus:GetAttribute("unit"))
  36. end
  37.  
  38. return unit and UnitIsPlayer(unit) and unit
  39. end
  40.  
  41. local SetTipText
  42. do
  43. local function search(line, numLines)
  44. if line > numLines then return end
  45.  
  46. local fontString = _G["GameTooltipTextLeft"..line]
  47. local stringText = fontString and fontString:GetText()
  48. if stringText and stringText:match(PREFIX) then
  49. return fontString
  50. end
  51.  
  52. return search(line + 1, numLines)
  53. end
  54.  
  55. function SetTipText(text)
  56. if not text then return end
  57.  
  58. local fontString = search(1, GameTooltip:NumLines())
  59. if fontString then
  60. fontString:SetText(PREFIX..text)
  61. else
  62. GameTooltip:AddLine(PREFIX..text)
  63. end
  64.  
  65. return GameTooltip:Show()
  66. end
  67. end
  68.  
  69. local CanSafeInspect
  70. do
  71. local limit, period = 6, 11
  72. local count, startTime = 0, 0
  73.  
  74. hooksecurefunc("NotifyInspect", function()
  75. local currentTime = GetTime()
  76. if currentTime - startTime > period then
  77. count, startTime = 1, currentTime
  78. return
  79. end
  80.  
  81. count = count + 1
  82. end)
  83.  
  84. function CanSafeInspect(unit)
  85. if not CanInspect(unit) or InspectFrame and InspectFrame:IsShown() or Examiner and Examiner:IsShown() then return end
  86.  
  87. local pending = count > limit and period - (GetTime() - startTime)
  88. return true, pending and pending > 0 and pending
  89. end
  90. end
  91.  
  92. local UnitItemLevel
  93. do
  94. local formatString = "%.0f"
  95.  
  96. local function scan(unit, slot, total, count, twoHanded, incomplete)
  97. if slot > INVSLOT_LAST_EQUIPPED then
  98. if count == 0 then return end
  99. return formatString:format(total / (twoHanded and count - 1 or count)), incomplete
  100. end
  101.  
  102. if slot == INVSLOT_BODY or slot == INVSLOT_RANGED or slot == INVSLOT_TABARD then
  103. return scan(unit, slot + 1, total, count, twoHanded, incomplete)
  104. end
  105.  
  106. local hasItem = GetInventoryItemTexture(unit, slot) and true
  107. local _, level, equipLoc
  108.  
  109. local link = hasItem and GetInventoryItemLink(unit, slot)
  110. if link then
  111. repeat
  112. _, _, _, level, _, _, _, _, equipLoc = GetItemInfo(link)
  113. if level and level >= 458 then
  114. local upgrade = link:match("item:%d+:%d+:%d+:%d+:%d+:%d+:%-?%d+:%-?%d+:%d+:(%d+)")
  115. if (upgrade and upgrades[upgrade]) then
  116. level = level + upgrades[upgrade]
  117. end
  118. end
  119. until level and equipLoc
  120.  
  121. total = total + level
  122. end
  123.  
  124. -- Two-handed weapon and Titan's Grip
  125. if slot == INVSLOT_MAINHAND then
  126. twoHanded = equipLoc == "INVTYPE_2HWEAPON" and 1 or equipLoc == "INVTYPE_RANGED" and 1 or equipLoc == "INVTYPE_RANGEDRIGHT" and 1 or not hasItem and 0
  127. elseif slot == INVSLOT_OFFHAND then
  128. twoHanded = twoHanded == 1 and not hasItem or twoHanded == 0 and equipLoc == "INVTYPE_2HWEAPON"
  129. end
  130.  
  131. local failed = hasItem and not link
  132. return scan(unit, slot + 1, total, failed and count or count + 1, twoHanded, incomplete or failed)
  133. end
  134.  
  135. function UnitItemLevel(unit)
  136. if unit == "player" or UnitIsUnit(unit, "player") then
  137. local _, level = GetAverageItemLevel()
  138. return formatString:format(level)
  139. end
  140.  
  141. return scan(unit, INVSLOT_FIRST_EQUIPPED, 0, 0)
  142. end
  143. end
  144.  
  145. local UpdateItemLevel
  146. do
  147. local cache = {}
  148. local cachedLevel
  149.  
  150. local function update(unit, guid)
  151. local level, incomplete = UnitItemLevel(unit)
  152. if not level then return end
  153. local myLevel = level - UnitItemLevel("player")
  154.  
  155. if incomplete then
  156. updateTimer = TOOLTIP_UPDATE_TIME
  157. f:Show()
  158. level = cachedLevel or level
  159. return SetTipText(WAITING and level..WAITING or level)
  160. end
  161.  
  162. if isReady then
  163. cache[guid] = level
  164. return SetTipText(level.." ("..((myLevel > 0) and "|cff00ff00+" or "|cffff0000")..myLevel.."|r|cffffffff)|r")
  165. end
  166.  
  167. level = cachedLevel or level
  168. return SetTipText(WAITING and level..WAITING or level)
  169. end
  170.  
  171. function UpdateItemLevel()
  172. cachedLevel = cache[currentGUID]
  173.  
  174. if inCombat then
  175. return SetTipText(cachedLevel)
  176. end
  177.  
  178. if isReady then
  179. return update(currentUnit, currentGUID)
  180. end
  181.  
  182. if not isForced and cachedLevel then
  183. return SetTipText(cachedLevel)
  184. end
  185.  
  186. if currentGUID == playerGUID then
  187. local level = UnitItemLevel("player")
  188. cache[playerGUID] = level
  189. return SetTipText(level)
  190. end
  191.  
  192. local canInspect, pending = CanSafeInspect(currentUnit)
  193. if not canInspect then
  194. return SetTipText(cachedLevel)
  195. end
  196.  
  197. if pending then
  198. updateTimer = pending
  199. f:Show()
  200. return SetTipText(cachedLevel and cachedLevel..PENDING or PENDING)
  201. end
  202.  
  203. if not isDelayed then
  204. isDelayed = true
  205. updateTimer = TOOLTIP_UPDATE_TIME
  206. f:Show()
  207. return SetTipText(cachedLevel and (WAITING and cachedLevel..WAITING or cachedLevel) or PENDING)
  208. end
  209.  
  210. if not isNotified then
  211. isNotified = true
  212. NotifyInspect(currentUnit)
  213. end
  214.  
  215. return update(currentUnit, currentGUID)
  216. end
  217. end
  218.  
  219. local function OnTooltipSetUnit()
  220. currentUnit, currentGUID, isDelayed, isForced, isNotified, isReady = GetTipUnit(), nil, nil, nil, nil, nil
  221. if not currentUnit then return end
  222.  
  223. currentGUID, isForced = UnitGUID(currentUnit), UnitIsUnit(currentUnit, "target")
  224.  
  225. return UpdateItemLevel()
  226. end
  227. GameTooltip:HookScript("OnTooltipSetUnit", OnTooltipSetUnit)
  228.  
  229. f:SetScript("OnUpdate", function(self, elapsed)
  230. updateTimer = updateTimer - elapsed
  231. if updateTimer > 0 then return end
  232. self:Hide()
  233.  
  234. if not currentGUID then return end
  235.  
  236. local tipUnit = GetTipUnit()
  237. if not tipUnit or UnitGUID(tipUnit) ~= currentGUID then return end
  238.  
  239. return UpdateItemLevel()
  240. end)
  241.  
  242. function f:INSPECT_READY(_, guid)
  243. if not currentGUID or guid ~= currentGUID then return end
  244.  
  245. local tipUnit = GetTipUnit()
  246. if not tipUnit or UnitGUID(tipUnit) ~= currentGUID then return end
  247.  
  248. isReady = true
  249.  
  250. return UpdateItemLevel()
  251. end
  252. f:RegisterEvent("INSPECT_READY")
  253.  
  254. function f:UNIT_INVENTORY_CHANGED(_, unit)
  255. if not currentGUID or UnitGUID(unit) ~= currentGUID then return end
  256.  
  257. local tipUnit = GetTipUnit()
  258. if not tipUnit or UnitGUID(tipUnit) ~= currentGUID then return end
  259.  
  260. isForced, isNotified, isReady = true, nil, nil
  261.  
  262. return UpdateItemLevel()
  263. end
  264. f:RegisterEvent("UNIT_INVENTORY_CHANGED")
  265.  
  266. function f:PLAYER_TARGET_CHANGED()
  267. return self:UNIT_INVENTORY_CHANGED(nil, "target")
  268. end
  269. f:RegisterEvent("PLAYER_TARGET_CHANGED")
  270.  
  271. function f:PLAYER_REGEN_DISABLED()
  272. inCombat = true
  273. end
  274. f:RegisterEvent("PLAYER_REGEN_DISABLED")
  275.  
  276. function f:PLAYER_REGEN_ENABLED()
  277. inCombat = nil
  278. end
  279. f:RegisterEvent("PLAYER_REGEN_ENABLED")
  280.  
  281. function f:PLAYER_LOGIN()
  282. self:UnregisterEvent("PLAYER_LOGIN")
  283. self.PLAYER_LOGIN = nil
  284.  
  285. playerGUID = UnitGUID("player")
  286. end
  287.  
  288. if IsLoggedIn() then
  289. f:PLAYER_LOGIN()
  290. inCombat = InCombatLockdown()
  291. return OnTooltipSetUnit()
  292. end
  293.  
  294. return f:RegisterEvent("PLAYER_LOGIN")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement