PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Interface/AddOns/LunaUnitFrames/libs/DruidManaLib-1.0/DruidManaLib-1.0.lua

https://gitlab.com/kidaa/wow-vanilla-ui
Lua | 291 lines | 241 code | 25 blank | 25 comment | 72 complexity | 4af4cf997a00f4ed72f8837c0b0a0463 MD5 | raw file
  1. --[[
  2. Name: DruidManaLib-1.0
  3. Revision: $Rev: 10000 $
  4. Author(s): aviana
  5. Website: https://github.com/Aviana
  6. Description: A library to provide mana values while in shape shift.
  7. Dependencies: AceLibrary, AceEvent-2.0
  8. ]]
  9. local MAJOR_VERSION = "DruidManaLib-1.0"
  10. local MINOR_VERSION = "$Revision: 10000 $"
  11. if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary") end
  12. if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end
  13. if not AceLibrary:HasInstance("AceEvent-2.0") then error(MAJOR_VERSION .. " requires AceEvent-2.0") end
  14. DruidManaLib = CreateFrame("Frame")
  15. local DruidManaLibTip = CreateFrame("GameTooltip", "DruidManaLibTip", nil, "GameTooltipTemplate")
  16. local DruidManaLibOnUpdateFrame = CreateFrame("Frame")
  17. DruidManaLibOnUpdateFrame:RegisterEvent("PLAYER_LOGIN")
  18. ------------------------------------------------
  19. -- Locales
  20. ------------------------------------------------
  21. local L = {}
  22. if( GetLocale() == "deDE" ) then
  23. L["Equip: Restores %d+ mana per 5 sec."] = "Anlegen: Stellt alle 5 Sek. %d+ Punkt(e) Mana wieder her.";
  24. L["Mana Regen %d+ per 5 sec."] = "Manaregeneration %d+ per 5 Sek.";
  25. L["Equip: Restores (%d+) mana per 5 sec."] = "Anlegen: Stellt alle 5 Sek. (%d+) Punkt(e) Mana wieder her."
  26. L["Mana Regen (%d+) per 5 sec."] = "Manaregeneration (%d+) per 5 Sek.";
  27. L[" "] = " ";
  28. elseif( GetLocale() == "frFR" ) then
  29. L["Equip: Restores %d+ mana per 5 sec."] = "Equip\195\169 : Rend %d+ points de mana toutes les 5 secondes.";
  30. L["Mana Regen %d+ per 5 sec."] = "R\195\169cup. mana %d+/5 sec.";
  31. L["Equip: Restores (%d+) mana per 5 sec."] = "Equip\195\169 : Rend (%d+) points de mana toutes les 5 secondes."
  32. L["Mana Regen (%d+) per 5 sec."] = "R\195\169cup. mana (%d+)/5 sec.";
  33. L[" "] = ":";
  34. else
  35. L["Equip: Restores %d+ mana per 5 sec."] = "Equip: Restores %d+ mana per 5 sec.";
  36. L["Mana Regen %d+ per 5 sec."] = "Mana Regen %d+ per 5 sec.";
  37. L["Equip: Restores (%d+) mana per 5 sec."] = "Equip: Restores (%d+) mana per 5 sec."
  38. L["Mana Regen (%d+) per 5 sec."] = "Mana Regen (%d+) per 5 sec.";
  39. L[" "] = " ";
  40. end
  41. ------------------------------------------------
  42. -- activate, enable, disable
  43. ------------------------------------------------
  44. local function activate(self, oldLib, oldDeactivate)
  45. DruidManaLib = self
  46. if oldLib then
  47. oldLib:UnregisterAllEvents()
  48. oldLib:CancelAllScheduledEvents()
  49. end
  50. if oldDeactivate then oldDeactivate(oldLib) end
  51. end
  52. local function external(self, major, instance)
  53. if major == "AceEvent-2.0" then
  54. DruidManaLib.SpecialEventScheduler = instance
  55. DruidManaLib.SpecialEventScheduler:embed(self)
  56. self:UnregisterAllEvents()
  57. self:CancelAllScheduledEvents()
  58. if DruidManaLib.SpecialEventScheduler:IsFullyInitialized() then
  59. self:AceEvent_FullyInitialized()
  60. else
  61. self:RegisterEvent("AceEvent_FullyInitialized", "AceEvent_FullyInitialized", true)
  62. end
  63. end
  64. end
  65. function DruidManaLib:Enable()
  66. -- not used anymore, but as addons still might be calling this method, we're keeping it.
  67. end
  68. function DruidManaLib:Disable()
  69. -- not used anymore, but as addons still might be calling this method, we're keeping it.
  70. end
  71. ------------------------------------------------
  72. -- Internal functions
  73. ------------------------------------------------
  74. function DruidManaLib:AceEvent_FullyInitialized()
  75. self:TriggerEvent("DruidManaLib_Enabled")
  76. self:RegisterEvent("UNIT_MANA", DruidManaLib.OnEvent)
  77. self:RegisterEvent("UNIT_MAXMANA", DruidManaLib.OnEvent)
  78. self:RegisterEvent("PLAYER_REGEN_ENABLED", DruidManaLib.OnEvent)
  79. self:RegisterEvent("PLAYER_REGEN_DISABLED", DruidManaLib.OnEvent)
  80. self:RegisterEvent("UNIT_INVENTORY_CHANGED", DruidManaLib.OnEvent)
  81. self:RegisterEvent("PLAYER_AURAS_CHANGED", DruidManaLib.OnEvent)
  82. self:RegisterEvent("UPDATE_SHAPESHIFT_FORMS", DruidManaLib.OnEvent)
  83. self:RegisterEvent("SPELLCAST_STOP", DruidManaLib.OnEvent)
  84. end
  85. ------------------------------------------------
  86. -- Addon Code
  87. ------------------------------------------------
  88. DruidManaLib.keepthemana = 0
  89. DruidManaLib.maxmana = 10
  90. DruidManaLib.int = 0
  91. DruidManaLib.subtractmana = 0
  92. DruidManaLib.extra = 0
  93. DruidManaLib.lowregentimer = 0
  94. DruidManaLib.fullmanatimer = 0
  95. DruidManaLib.waitonce = nil
  96. _, DruidManaLib.init = UnitClass("player")
  97. DruidManaLib.inform = (UnitPowerType("player") ~= 0)
  98. DruidManaLibTip:SetOwner(DruidManaLib, "ANCHOR_NONE")
  99. local function DruidManaLib_GetShapeshiftCost()
  100. DruidManaLib.subtractmana = 0;
  101. local a, b, c, d = GetSpellTabInfo(4);
  102. local spelltexture
  103. for i = 1, c+d, 1 do
  104. spelltexture = GetSpellTexture(i, BOOKTYPE_SPELL);
  105. if spelltexture and spelltexture == "Interface\\Icons\\Ability_Druid_CatForm" then
  106. DruidManaLibTip:SetSpell(i, 1);
  107. local msg = DruidManaLibTipTextLeft2:GetText();
  108. local params;
  109. if msg then
  110. local index = strfind(msg, L[" "]);
  111. if index then
  112. if (GetLocale() == "frFR" or GetLocale() == "koKR") then params = strsub(msg, index+1); else params = strsub(msg, 1, index-1); end
  113. DruidManaLib.subtractmana = tonumber(params);
  114. if DruidManaLib.subtractmana and DruidManaLib.subtractmana > 0 then return; end
  115. end
  116. end
  117. end
  118. end
  119. end
  120. local function DruidManaLib_MaxManaScript()
  121. local _, int = UnitStat("player", 4);
  122. DruidManaLib_GetShapeshiftCost();
  123. if UnitPowerType("player") == 0 then
  124. if UnitManaMax("player") > 0 then
  125. DruidManaLib.maxmana = UnitManaMax("player");
  126. DruidManaLib.keepthemana = UnitMana("player");
  127. DruidManaLib.int = int;
  128. end
  129. elseif UnitPowerType("player") ~= 0 then
  130. if DruidManaLib.int ~= int then
  131. if int > DruidManaLib.int then
  132. local dif = int - DruidManaLib.int;
  133. DruidManaLib.maxmana = DruidManaLib.maxmana + (dif * 15);
  134. DruidManaLib.int = int;
  135. elseif int < DruidManaLib.int then
  136. local dif = DruidManaLib.int - int;
  137. DruidManaLib.maxmana = DruidManaLib.maxmana - (dif * 15);
  138. DruidManaLib.int = int;
  139. end
  140. end
  141. if DruidManaLib.keepthemana > DruidManaLib.maxmana then
  142. DruidManaLib.keepthemana = DruidManaLib.maxmana;
  143. end
  144. end
  145. DruidManaLib.extra = 0;
  146. for i = 1, 18 do
  147. DruidManaLibTip:ClearLines();
  148. DruidManaLibTip:SetInventoryItem("player", i);
  149. for j = 1, DruidManaLibTip:NumLines() do
  150. local strchek = getglobal("DruidManaLibTipTextLeft"..j):GetText();
  151. if strchek then
  152. if strfind(strchek, L["Equip: Restores %d+ mana per 5 sec."]) then
  153. DruidManaLib.extra = DruidManaLib.extra + string.gsub(strchek, L["Equip: Restores (%d+) mana per 5 sec."], "%1")
  154. end
  155. if strfind(strchek, L["Mana Regen %d+ per 5 sec."]) then
  156. DruidManaLib.extra = DruidManaLib.extra + string.gsub(strchek, L["Mana Regen (%d+) per 5 sec."], "%1");
  157. end
  158. end
  159. end
  160. end
  161. DruidManaLib.extra = ceil((DruidManaLib.extra * 2) / 5);
  162. end
  163. local function DruidManaLib_Subtract()
  164. local j = 1;
  165. local icon
  166. while (UnitBuff("player",j)) do
  167. icon = UnitBuff("player",j)
  168. if icon and icon == "Interface\\Icons\\Inv_Misc_Rune_06" then
  169. return
  170. end
  171. j = j + 1
  172. end
  173. DruidManaLib.keepthemana = DruidManaLib.keepthemana - DruidManaLib.subtractmana;
  174. DruidManaLib.SpecialEventScheduler:TriggerEvent("DruidManaLib_Manaupdate")
  175. end
  176. local function DruidManaLib_ReflectionCheck()
  177. local managain = 0;
  178. local j = 1;
  179. local icon
  180. while (UnitBuff("player",j)) do
  181. icon = UnitBuff("player",j)
  182. if icon and icon == "Interface\\Icons\\Spell_Nature_Lightning" then
  183. return ((ceil(UnitStat(arg1,5) / 5)+15) * 5);
  184. end
  185. j = j + 1;
  186. end
  187. if DruidManaLib.lowregentimer > 0 then
  188. if DruidManaLib.waitonce then
  189. local name, iconTexture, tier, column, rank, maxRank, isExceptional, meetsPrereq = GetTalentInfo(3, 6);
  190. if rank == 0 then return 0; else
  191. managain = ceil(((UnitStat("player",5) / 5)+15) * (0.05 * rank));
  192. end
  193. else
  194. DruidManaLib.waitonce = true;
  195. end
  196. elseif DruidManaLib.lowregentimer <= 0 then
  197. managain = (ceil(UnitStat("player",5) / 5)+15);
  198. end
  199. return managain;
  200. end
  201. DruidManaLib.OnEvent = function()
  202. if DruidManaLib.init and DruidManaLib.init == "DRUID" then
  203. if event == "UNIT_MAXMANA" and arg1 == "player" then
  204. DruidManaLib_MaxManaScript();
  205. elseif event == "UNIT_INVENTORY_CHANGED" and arg1 == "player" then
  206. DruidManaLib_MaxManaScript();
  207. elseif event == "UNIT_MANA" and arg1 == "player" then
  208. if UnitPowerType(arg1) == 0 then
  209. DruidManaLib.keepthemana = UnitMana(arg1);
  210. elseif DruidManaLib.keepthemana < DruidManaLib.maxmana then
  211. local add = DruidManaLib_ReflectionCheck();
  212. DruidManaLib.keepthemana = DruidManaLib.keepthemana + add + DruidManaLib.extra;
  213. if DruidManaLib.keepthemana > DruidManaLib.maxmana then DruidManaLib.keepthemana = DruidManaLib.maxmana; end
  214. end
  215. DruidManaLib.fullmanatimer = 0
  216. elseif event == "PLAYER_AURAS_CHANGED" or event == "UPDATE_SHAPESHIFT_FORMS" then
  217. if UnitPowerType("player") == 1 and not DruidManaLib.inform then
  218. --Bear
  219. DruidManaLib.inform = true
  220. DruidManaLib_Subtract()
  221. elseif UnitPowerType("player") == 3 and not DruidManaLib.inform then
  222. --Cat
  223. DruidManaLib.inform = true
  224. DruidManaLib_Subtract()
  225. elseif UnitPowerType("player") == 0 and DruidManaLib.inform then
  226. DruidManaLib.inform = nil
  227. DruidManaLib.keepthemana = UnitMana("player")
  228. DruidManaLib.maxmana = UnitManaMax("player")
  229. --player/aqua/travel
  230. end
  231. elseif (event == "SPELLCAST_STOP") then
  232. if UnitPowerType("player") == 0 then
  233. DruidManaLib.lowregentimer = 5
  234. DruidManaLib.waitonce = nil
  235. end
  236. end
  237. end
  238. end
  239. local timer = 0
  240. function DruidManaLib_OnUpdate()
  241. timer = timer + arg1
  242. if DruidManaLib.init and DruidManaLib.init == "DRUID" then
  243. if DruidManaLib.lowregentimer > 0 then
  244. DruidManaLib.lowregentimer = DruidManaLib.lowregentimer - arg1;
  245. if DruidManaLib.lowregentimer <= 0 then DruidManaLib.lowregentimer = 0; end
  246. end
  247. if UnitPowerType("player") ~= 0 then
  248. DruidManaLib.fullmanatimer = DruidManaLib.fullmanatimer + arg1;
  249. if DruidManaLib.fullmanatimer > 6 and floor((DruidManaLib.keepthemana*100) / DruidManaLib.maxmana) > 90 then
  250. DruidManaLib.keepthemana = DruidManaLib.maxmana;
  251. end
  252. end
  253. end
  254. if timer > 2 then
  255. timer = 0
  256. if DruidManaLib.init and DruidManaLib.init == "DRUID" and UnitPowerType("player") ~= 0 then
  257. DruidManaLib.SpecialEventScheduler:TriggerEvent("DruidManaLib_Manaupdate")
  258. end
  259. end
  260. end
  261. DruidManaLibOnUpdateFrame:SetScript("OnUpdate", DruidManaLib_OnUpdate)
  262. DruidManaLibOnUpdateFrame:SetScript("OnEvent", DruidManaLib_MaxManaScript)
  263. function DruidManaLib:GetMana()
  264. return DruidManaLib.keepthemana, DruidManaLib.maxmana
  265. end
  266. AceLibrary:Register(DruidManaLib, MAJOR_VERSION, MINOR_VERSION, activate, nil, external)