Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. local ADDON_NAME, PRIVATE_TABLE = ...
  2.  
  3. local _G = getfenv(0)
  4. local UIParent = _G.UIParent
  5. local WorldFrame = _G.WorldFrame
  6.  
  7. local GetTime = _G.GetTime
  8. local UnitGUID = _G.UnitGUID
  9. local IsMounted = _G.IsMounted
  10. local CanLootUnit = _G.CanLootUnit
  11. local CreateFrame = _G.CreateFrame
  12. local MouselookStop = _G.MouselookStop
  13. local IsMouselooking = _G.IsMouselooking
  14. local InCombatLockdown = _G.InCombatLockdown
  15. local IsEquippedItemType = _G.IsEquippedItemType
  16. local CanScanResearchSite = _G.CanScanResearchSite
  17. local ClearOverrideBindings = _G.ClearOverrideBindings
  18. local SetOverrideBindingClick = _G.SetOverrideBindingClick
  19.  
  20. local LibStub = _G.LibStub
  21. local AceAddon = LibStub("AceAddon-3.0")
  22.  
  23. PRIVATE_TABLE.addon = PRIVATE_TABLE.addon or {}
  24.  
  25. local BUTTON_NAME = "LootARangMaticOld"
  26. local ACTION_DOUBLE_WAIT = 0.4
  27. local MIN_ACTION_DOUBLECLICK = 0.05
  28. local LOOT_A_RANG_1_ID = 60854
  29. local LOOT_A_RANG_2_ID = 109167
  30. local FETCH_SPELL_ID = 125050
  31. local COMBAT_MOUNT_SPELL_IDS = {[164222]=true, [165803]=true}
  32.  
  33. --settings to be
  34. local hideErrors = true
  35.  
  36. local Addon = AceAddon:NewAddon(PRIVATE_TABLE.addon, ADDON_NAME, "AceEvent-3.0", "AceHook-3.0")
  37.  
  38.  
  39. local options = {
  40. type = "group",
  41. args = {
  42. engineeringOnlyWarn = {
  43. name = "Engineering Only Warn",
  44. desc = "Warn when character doesn't have needed 125 engineering skill.",
  45. type = "toggle",
  46. set = function(info,val) Addon.settings.global.engineeringOnlyWarn = val end,
  47. get = function(info) return Addon.settings.global.engineeringOnlyWarn end
  48. },
  49. draenorOnlyWarn = {
  50. name = "Draenor Only Warn",
  51. desc = "Warn when not in Draenor.",
  52. type = "toggle",
  53. set = function(info,val) Addon.settings.global.draenorOnlyWarn = val end,
  54. get = function(info) return Addon.settings.global.draenorOnlyWarn end
  55. },
  56. onlyLootTargetOrMouseover = {
  57. name = "Only loot Target or Mouseover",
  58. desc = "",
  59. type = "toggle",
  60. set = function(info,val) Addon.settings.global.onlyLootTargetOrMouseover = val end,
  61. get = function(info) return Addon.settings.global.onlyLootTargetOrMouseover end
  62. },
  63. onlyLootTargetOrMouseoverInDigsite = {
  64. name = "Only loot Target or Mouseover while in a Digsite",
  65. desc = "",
  66. type = "toggle",
  67. set = function(info,val) Addon.settings.global.onlyLootTargetOrMouseoverInDigsite = val end,
  68. get = function(info) return Addon.settings.global.onlyLootTargetOrMouseoverInDigsite end
  69. },
  70. onlyLootTargetOrMouseoverFishing = {
  71. name = "Only loot Target or Mouseover while Fishing Pole is Equipped",
  72. desc = "",
  73. type = "toggle",
  74. set = function(info,val) Addon.settings.global.onlyLootTargetOrMouseoverFishing = val end,
  75. get = function(info) return Addon.settings.global.onlyLootTargetOrMouseoverFishing end
  76. },
  77. hideErrors = {
  78. name = "Try to Hide Errors",
  79. desc = "",
  80. type = "toggle",
  81. set = function(info,val) Addon.settings.global.hideErrors = val end,
  82. get = function(info) return Addon.settings.global.hideErrors end
  83. },
  84. }
  85. }
  86.  
  87. local FISHING_POLES_NAME = GetItemSubClassInfo(LE_ITEM_CLASS_WEAPON, LE_ITEM_WEAPON_FISHINGPOLE);
  88.  
  89. local function HoldingFishingPole()
  90. if not FISHING_POLES_NAME then
  91. FISHING_POLES_NAME = GetItemSubClassInfo(LE_ITEM_CLASS_WEAPON, LE_ITEM_WEAPON_FISHINGPOLE);
  92. end
  93. return IsEquippedItemType(FISHING_POLES_NAME)
  94. end
  95.  
  96. local function InCombat()
  97. return PRIVATE_TABLE.inCombat or InCombatLockdown()
  98. end
  99.  
  100. local function CheckForLoot()
  101. return Addon.settings.global.onlyLootTargetOrMouseover
  102. or (Addon.settings.global.onlyLootTargetOrMouseoverInDigsite and CanScanResearchSite())
  103. or (Addon.settings.global.onlyLootTargetOrMouseoverFishing and HoldingFishingPole())
  104. end
  105.  
  106. local function TargetOrMouseoverHasLoot()
  107. local targetGUID, mouseoverGUID = UnitGUID("target"), UnitGUID("mouseover")
  108. return (targetGUID and CanLootUnit(targetGUID)) or (mouseoverGUID and CanLootUnit(mouseoverGUID))
  109. end
  110.  
  111. local function HasEngineering()
  112. prof1, prof2 = GetProfessions()
  113. _, _, rank1, _, _, _, skillID1 = GetProfessionInfo(prof1)
  114. _, _, rank2, _, _, _, skillID2 = GetProfessionInfo(prof2)
  115. return (skillID1 == 202 and rank1 >= 1) or (skillID2 == 202 and rank2 >= 1)
  116. end
  117.  
  118. local function MountBlockingCast()
  119. if not IsMounted() then
  120. return false;
  121. end
  122. local i = 1;
  123. local name, _, _, _, _, _, _, _, _, _, spellID = UnitBuff("player", i, "CANCELABLE");
  124. while name do
  125. if COMBAT_MOUNT_SPELL_IDS[spellID] then
  126. return false;
  127. end
  128. i = i + 1;
  129. name, _, _, _, _, _, _, _, _, _, spellID = UnitBuff("player", i, "CANCELABLE");
  130. end;
  131. return true;
  132. end
  133.  
  134. local function SafeToLoot()
  135. if PRIVATE_TABLE.moveing or MountBlockingCast() or InCombat() then
  136. return false
  137. end
  138. if CheckForLoot() and not TargetOrMouseoverHasLoot() then
  139. return false
  140. end
  141. return true
  142. end
  143.  
  144. function Addon:WantsToLoot()
  145. return SafeToLoot() and (
  146. (PlayerHasToy(LOOT_A_RANG_1_ID) and select(1, GetItemCooldown(LOOT_A_RANG_1_ID)) == 0)
  147. or (PlayerHasToy(LOOT_A_RANG_2_ID) and select(1, GetItemCooldown(LOOT_A_RANG_2_ID)) == 0)
  148. or (IsSpellKnown(FETCH_SPELL_ID) and select(1, GetSpellCooldown(FETCH_SPELL_ID)) == 0)
  149. )
  150. end
  151.  
  152. function Addon:RequestSkip()
  153. PRIVATE_TABLE.skipLoot = true
  154. end
  155.  
  156. local Sound_EnableErrorSpeech = GetCVar("Sound_EnableErrorSpeech")
  157. function Addon:OnInitialize()
  158. if not PRIVATE_TABLE.secureActionButton then
  159. local button = CreateFrame("Button", BUTTON_NAME, UIParent, "SecureActionButtonTemplate")
  160. button:Hide()
  161. button:EnableMouse(true)
  162. button:RegisterForClicks("RightButtonUp")
  163. button:SetAttribute("action", nil)
  164. button:SetScript("PreClick", function(self, mouse_button, is_down)
  165. if hideErrors then
  166. Sound_EnableErrorSpeech = GetCVar("Sound_EnableErrorSpeech")
  167. SetCVar("Sound_EnableErrorSpeech",0)
  168. UIErrorsFrame:UnregisterEvent("UI_ERROR_MESSAGE")
  169. end
  170. end)
  171. button:SetScript("PostClick", function(self, mouse_button, is_down)
  172. C_Timer.After(0.2, function()
  173. if hideErrors then
  174. UIErrorsFrame:RegisterEvent("UI_ERROR_MESSAGE")
  175. SetCVar("Sound_EnableErrorSpeech",Sound_EnableErrorSpeech)
  176. end
  177. end)
  178. if PRIVATE_TABLE.bindingOn and not InCombat() then
  179. ClearOverrideBindings(self)
  180. PRIVATE_TABLE.bindingOn = false
  181. else
  182. PRIVATE_TABLE.bindingStuck = true
  183. end
  184. end)
  185.  
  186. PRIVATE_TABLE.secureActionButton = button
  187. end
  188.  
  189. Addon.settings = LibStub("AceDB-3.0"):New("LootARangMatic", {
  190. global = {
  191. engineeringOnlyWarn = false,
  192. draenorOnlyWarn = true,
  193. onlyLootTargetOrMouseover = true,
  194. onlyLootTargetOrMouseoverInDigsite = true,
  195. onlyLootTargetOrMouseoverFishing = true,
  196. hideErrors = true,
  197. }
  198. })
  199. LibStub("AceConfig-3.0"):RegisterOptionsTable("LootARangMatic", options, {"lrm", "lootarangmatic"})
  200. end
  201.  
  202. function Addon:OnEnable()
  203. self:RegisterEvent("PLAYER_REGEN_DISABLED", "OnRegenDisabled")
  204. self:RegisterEvent("PLAYER_REGEN_ENABLED", "OnRegenEnabled")
  205. self:RegisterEvent("PLAYER_STARTED_MOVING", "OnPlayerMoveStart")
  206. self:RegisterEvent("PLAYER_STOPPED_MOVING", "OnPlayerMoveStop")
  207. self:SecureHookScript(WorldFrame, "OnMouseDown", "OnMouseDown")
  208. end
  209.  
  210. function Addon:OnDisable()
  211. self:UnregisterEvent("PLAYER_REGEN_DISABLED")
  212. self:UnregisterEvent("PLAYER_REGEN_ENABLED")
  213. self:UnregisterEvent("PLAYER_STARTED_MOVING")
  214. self:UnregisterEvent("PLAYER_STOPPED_MOVING")
  215. self:Unhook(WorldFrame, "OnMouseDown")
  216. end
  217.  
  218. function Addon:OnRegenDisabled()
  219. PRIVATE_TABLE.inCombat = true
  220. if PRIVATE_TABLE.bindingOn or PRIVATE_TABLE.bindingStuck then
  221. ClearOverrideBindings(PRIVATE_TABLE.secureActionButton)
  222. PRIVATE_TABLE.bindingOn = false
  223. PRIVATE_TABLE.bindingStuck = false
  224. end
  225. end
  226.  
  227. function Addon:OnRegenEnabled()
  228. PRIVATE_TABLE.inCombat = false
  229. if PRIVATE_TABLE.bindingStuck then
  230. ClearOverrideBindings(PRIVATE_TABLE.secureActionButton)
  231. PRIVATE_TABLE.bindingOn = false
  232. PRIVATE_TABLE.bindingStuck = false
  233. end
  234. end
  235.  
  236. function Addon:OnPlayerMoveStart()
  237. PRIVATE_TABLE.moveing = true
  238. end
  239.  
  240. function Addon:OnPlayerMoveStop()
  241. PRIVATE_TABLE.moveing = false
  242. end
  243.  
  244. function Addon:OnMouseDown(frame, button, down)
  245. if button == "RightButton" then
  246. if PRIVATE_TABLE.lastClicked then
  247. local doubleTime = GetTime() - PRIVATE_TABLE.lastClicked
  248. if doubleTime < ACTION_DOUBLE_WAIT and doubleTime > MIN_ACTION_DOUBLECLICK then
  249. PRIVATE_TABLE.lastClicked = nil
  250. if SafeToLoot() then
  251. if PRIVATE_TABLE.skipLoot then
  252. PRIVATE_TABLE.skipLoot = false
  253. else
  254. local useIssue, bind = "", false
  255. if not bind and PlayerHasToy(LOOT_A_RANG_1_ID) and select(2, GetItemCooldown(LOOT_A_RANG_1_ID)) == 0 then
  256. if HasEngineering() then
  257. PRIVATE_TABLE.secureActionButton:SetAttribute("type", "item")
  258. PRIVATE_TABLE.secureActionButton:SetAttribute("item", select(1, GetItemInfo(LOOT_A_RANG_1_ID)))
  259. bind = true;
  260. else
  261. if Addon.settings.global.engineeringOnlyWarn then
  262. useIssue = format("Can't use %s without 125 skill in Engineering", select(2, GetItemInfo(LOOT_A_RANG_1_ID)))
  263. end
  264. end
  265. end
  266. if not bind and PlayerHasToy(LOOT_A_RANG_2_ID) and select(2, GetItemCooldown(LOOT_A_RANG_2_ID)) == 0 then
  267. local currentMapId = C_Map.GetBestMapForUnit("player")
  268. if(currentMapId) then
  269. if MapUtil.GetMapParentInfo(currentMapId, Enum.UIMapType.Continent).mapID == 572 then
  270. PRIVATE_TABLE.secureActionButton:SetAttribute("type", "item")
  271. PRIVATE_TABLE.secureActionButton:SetAttribute("item", select(1, GetItemInfo(LOOT_A_RANG_2_ID)))
  272. bind = true;
  273. else
  274. if Addon.settings.global.draenorOnlyWarn then
  275. useIssue = format("Can only use use %s in Draenor", select(2, GetItemInfo(LOOT_A_RANG_2_ID)))
  276. end
  277. end
  278. end
  279. end
  280. if not bind and IsSpellKnown(FETCH_SPELL_ID) and select(2, GetSpellCooldown(FETCH_SPELL_ID)) == 0 then
  281. PRIVATE_TABLE.secureActionButton:SetAttribute("type", "spell")
  282. PRIVATE_TABLE.secureActionButton:SetAttribute("spell", select(1, GetSpellInfo(FETCH_SPELL_ID)))
  283. bind = true;
  284. end
  285. if not bind and not PlayerHasToy(LOOT_A_RANG_1_ID) and not PlayerHasToy(LOOT_A_RANG_2_ID) and not IsSpellKnown(FETCH_SPELL_ID) then
  286. useIssue = "No Loot-A-Rang or Fetch glyph found."
  287. end
  288. if bind then
  289. if IsMouselooking() then
  290. MouselookStop()
  291. end
  292. SetOverrideBindingClick(PRIVATE_TABLE.secureActionButton, true, "BUTTON2", BUTTON_NAME)
  293. PRIVATE_TABLE.bindingOn = true
  294. else
  295. print(useIssue)
  296. end
  297. end
  298. end
  299. elseif doubleTime >= ACTION_DOUBLE_WAIT then
  300. PRIVATE_TABLE.lastClicked = GetTime()
  301. end
  302. else
  303. PRIVATE_TABLE.lastClicked = GetTime()
  304. end
  305. end
  306. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement