Advertisement
Wetxius

Untitled

Feb 12th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 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(Cloudy Unit Info by Cloudyfa)
  6. ----------------------------------------------------------------------------------------
  7. --- Variables
  8. local currentUNIT, currentGUID
  9. local GearDB = {}
  10.  
  11. local nextInspectRequest = 0
  12. local lastInspectRequest = 0
  13.  
  14. local prefixColor = "|cffF9D700"
  15. local detailColor = "|cffffffff"
  16.  
  17. local gearPrefix = STAT_AVERAGE_ITEM_LEVEL..": "
  18.  
  19. --- Create Frame
  20. local f = CreateFrame("Frame", "CloudyUnitInfo")
  21. f:RegisterEvent("UNIT_INVENTORY_CHANGED")
  22. f:RegisterEvent("INSPECT_READY")
  23.  
  24. --- Set Unit Info
  25. local function SetUnitInfo(gear)
  26. if not gear then return end
  27.  
  28. local _, unit = GameTooltip:GetUnit()
  29. if (not unit) or (UnitGUID(unit) ~= currentGUID) then return end
  30.  
  31. local gearLine
  32. for i = 2, GameTooltip:NumLines() do
  33. local line = _G["GameTooltipTextLeft" .. i]
  34. local text = line:GetText()
  35.  
  36. if text and strfind(text, gearPrefix) then
  37. gearLine = line
  38. end
  39. end
  40.  
  41. if gear then
  42. gear = prefixColor..gearPrefix..detailColor..gear
  43.  
  44. if gearLine then
  45. gearLine:SetText(gear)
  46. else
  47. GameTooltip:AddLine(gear)
  48. end
  49. end
  50.  
  51. GameTooltip:Show()
  52. end
  53.  
  54. --- PVP Item Detect
  55. local function IsPVPItem(link)
  56. local itemStats = GetItemStats(link)
  57. for stat in pairs(itemStats) do
  58. if stat == "ITEM_MOD_RESILIENCE_RATING_SHORT" or stat == "ITEM_MOD_PVP_POWER_SHORT" then
  59. return true
  60. end
  61. end
  62.  
  63. return false
  64. end
  65.  
  66. local upgrades = {
  67. ["1"] = 8, ["373"] = 4, ["374"] = 8, ["375"] = 4, ["376"] = 4, ["377"] = 4,
  68. ["379"] = 4, ["380"] = 4, ["446"] = 4, ["447"] = 8, ["452"] = 8, ["454"] = 4,
  69. ["455"] = 8, ["457"] = 8, ["459"] = 4, ["460"] = 8, ["461"] = 12, ["462"] = 16,
  70. ["466"] = 4, ["467"] = 8, ["469"] = 4, ["470"] = 8, ["471"] = 12, ["472"] = 16,
  71. ["477"] = 4, ["478"] = 8, ["480"] = 8, ["492"] = 4, ["493"] = 8, ["495"] = 4,
  72. ["496"] = 8, ["497"] = 12, ["498"] = 16, ["504"] = 12, ["505"] = 16, ["506"] = 20,
  73. ["507"] = 24, ["530"] = 5, ["531"] = 10
  74. }
  75.  
  76. --- Unit Gear Info
  77. local function UnitGear(unit)
  78. if (not unit) or (UnitGUID(unit) ~= currentGUID) then return end
  79.  
  80. local ulvl = UnitLevel(unit)
  81. local class = select(2, UnitClass(unit))
  82.  
  83. local ilvl, boa, pvp = 0, 0, 0
  84. local total, count, delay = 0, 16, nil
  85. local mainhand, offhand, twohand = 1, 1, 0
  86.  
  87. for i = 1, 17 do
  88. if i ~= 4 then
  89. local itemTexture = GetInventoryItemTexture(unit, i)
  90.  
  91. if itemTexture then
  92. local itemLink = GetInventoryItemLink(unit, i)
  93.  
  94. if not itemLink then
  95. delay = true
  96. else
  97. local _, _, quality, level, _, _, _, _, slot = GetItemInfo(itemLink)
  98.  
  99. if (not quality) or (not level) then
  100. delay = true
  101. else
  102. local upgrade = itemLink:match(":(%d+)\124h%[")
  103. if upgrades[upgrade] == nil then upgrades[upgrade] = 0 end
  104.  
  105. total = total + (level + upgrades[upgrade])
  106.  
  107. if quality == 7 then
  108. boa = boa + 1
  109. else
  110. if IsPVPItem(itemLink) then
  111. pvp = pvp + 1
  112. end
  113. end
  114.  
  115. if i >= 16 then
  116. if (slot == "INVTYPE_2HWEAPON") or (slot == "INVTYPE_RANGED") or ((slot == "INVTYPE_RANGEDRIGHT") and (class == "HUNTER")) then
  117. twohand = twohand + 1
  118. end
  119. end
  120. end
  121. end
  122. else
  123. if i == 16 then
  124. mainhand = 0
  125. elseif i == 17 then
  126. offhand = 0
  127. end
  128. end
  129. end
  130. end
  131.  
  132. if (mainhand == 0) and (offhand == 0) or (twohand == 1) then
  133. count = count - 1
  134. end
  135.  
  136. if not delay then
  137. if unit == "player" and GetAverageItemLevel() > 0 then
  138. _, ilvl = GetAverageItemLevel()
  139. else
  140. ilvl = total / count
  141. end
  142.  
  143. if ilvl > 0 then ilvl = string.format("%.1f", ilvl) end
  144. if boa > 0 then ilvl = ilvl.." |cffe6cc80"..boa.." "..HEIRLOOMS end
  145. if pvp > 0 then ilvl = ilvl.." |cffa335ee"..pvp.." "..PVP end
  146. else
  147. ilvl = nil
  148. end
  149.  
  150. return ilvl
  151. end
  152.  
  153. --- Scan Current Unit
  154. local function ScanUnit(unit, forced)
  155. local cachedGear
  156.  
  157. if UnitIsUnit(unit, "player") then
  158. cachedGear = UnitGear("player")
  159.  
  160. SetUnitInfo(cachedGear or CONTINUED)
  161. else
  162. if (not unit) or (UnitGUID(unit) ~= currentGUID) then return end
  163.  
  164. cachedGear = GearDB[currentGUID]
  165.  
  166. -- cachedGear? ok...skip get gear
  167. if cachedGear and not forced then
  168. SetUnitInfo(cachedGear)
  169. end
  170.  
  171. if not (IsShiftKeyDown() or forced) then
  172. if UnitAffectingCombat("player") then return end
  173. end
  174.  
  175. if not UnitIsVisible(unit) then return end
  176. if UnitIsDeadOrGhost("player") or UnitOnTaxi("player") then return end
  177. if InspectFrame and InspectFrame:IsShown() then return end
  178.  
  179. -- Press SHIFT to refresh
  180. if IsShiftKeyDown() then
  181. SetUnitInfo(CONTINUED, CONTINUED)
  182. else
  183. SetUnitInfo(cachedGear or CONTINUED)
  184. end
  185.  
  186. local timeSinceLastInspect = GetTime() - lastInspectRequest
  187. if timeSinceLastInspect >= 1.5 then
  188. nextInspectRequest = 0
  189. else
  190. nextInspectRequest = 1.5 - timeSinceLastInspect
  191. end
  192. f:Show()
  193. end
  194. end
  195.  
  196. --- Character Info Sheet
  197. hooksecurefunc("PaperDollFrame_SetItemLevel", function(self, unit)
  198. if unit ~= "player" then return end
  199.  
  200. local total, equip = GetAverageItemLevel()
  201. if total > 0 then total = string.format("%.1f", total) end
  202. if equip > 0 then equip = string.format("%.1f", equip) end
  203.  
  204. local ilvl = equip
  205. if equip ~= total then
  206. ilvl = equip.." / "..total
  207. end
  208.  
  209. local ilvlLine = _G[self:GetName().."StatText"]
  210. ilvlLine:SetText(ilvl)
  211.  
  212. self.tooltip = detailColor..STAT_AVERAGE_ITEM_LEVEL.." "..ilvl
  213. end)
  214.  
  215. --- Handle Events
  216. f:SetScript("OnEvent", function(self, event, ...)
  217. if event == "UNIT_INVENTORY_CHANGED" then
  218. local unit = ...
  219. if UnitGUID(unit) == currentGUID then
  220. ScanUnit(unit, true)
  221. end
  222. elseif event == "INSPECT_READY" then
  223. local guid = ...
  224. if guid ~= currentGUID then return end
  225.  
  226. local gear = UnitGear(currentUNIT)
  227. GearDB[currentGUID] = gear
  228.  
  229. if not gear then
  230. ScanUnit(currentUNIT, true)
  231. else
  232. SetUnitInfo(gear)
  233. end
  234. end
  235. end)
  236.  
  237. f:SetScript("OnUpdate", function(self, elapsed)
  238. nextInspectRequest = nextInspectRequest - elapsed
  239. if nextInspectRequest > 0 then return end
  240.  
  241. self:Hide()
  242.  
  243. if currentUNIT and (UnitGUID(currentUNIT) == currentGUID) then
  244. lastInspectRequest = GetTime()
  245. NotifyInspect(currentUNIT)
  246. end
  247. end)
  248.  
  249. GameTooltip:HookScript("OnTooltipSetUnit", function(self)
  250. local _, unit = self:GetUnit()
  251.  
  252. if (not unit) or (not CanInspect(unit)) then return end
  253. if (UnitLevel(unit) > 0) and (UnitLevel(unit) < 10) then return end
  254.  
  255. currentUNIT, currentGUID = unit, UnitGUID(unit)
  256. ScanUnit(unit)
  257. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement