Guest User

Gathering Glove Swap Old

a guest
Mar 18th, 2017
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. -- Old version of the glove swap AddOn.
  2.  
  3. -- Get localized name for Herbalism and Mining for tooltip scanning
  4. local LOCALIZED_HERBALISM = GetSpellInfo(184251)
  5. local LOCALIZED_MINING = GetSpellInfo(32606)
  6.  
  7. local NUM_BAG_SLOTS = NUM_BAG_SLOTS
  8.  
  9. local select = select
  10. local tonumber = tonumber
  11.  
  12. local GetContainerNumSlots = GetContainerNumSlots
  13. local GetContainerItemLink = GetContainerItemLink
  14. local PickupContainerItem = PickupContainerItem
  15. local AutoEquipCursorItem = AutoEquipCursorItem
  16. local InCombatLockdown = InCombatLockdown
  17. local GetMouseFocus = GetMouseFocus
  18. local UnitCastingInfo = UnitCastingInfo
  19.  
  20. local GameTooltip = GameTooltip
  21. local GameTooltipTextLeft2 = GameTooltipTextLeft2
  22.  
  23. -- Create a frame to handle events
  24. local f = CreateFrame("Frame")
  25.  
  26. f:SetScript("OnEvent", function(self, event, ...)
  27.     if self[event] then
  28.         self[event](...)
  29.     end
  30. end)
  31.  
  32. local enchantIDs = {
  33.     5444, --Herbalism
  34.     5445, --Mining
  35.     5446, --Skinning
  36. }
  37.  
  38. local lastGloves = {}
  39. local gatheringGlovesEquipped = false
  40.  
  41. -- Scan bags for gathering gloves with the right enchant
  42. -- For the profession argument:
  43. -- 1 = Herbalism
  44. -- 2 = Mining
  45. local function ScanForGloves(profession)
  46.     for containerID = 0, NUM_BAG_SLOTS do
  47.         for slotID = 1, GetContainerNumSlots(containerID) do
  48.             local itemLink = GetContainerItemLink(containerID, slotID)
  49.             if itemLink then
  50.                 local itemString = itemLink:match("|H(.*)|h")
  51.                 local enchantID = select(3, string.split(":", itemString))
  52.                 enchantID = tonumber(enchantID)
  53.                 if enchantID == enchantIDs[profession] then
  54.                     PickupContainerItem(containerID, slotID)
  55.                     AutoEquipCursorItem()
  56.                     if not gatheringGlovesEquipped and not f["UNIT_INVENTORY_CHANGED"] then
  57.                         f:RegisterEvent("UNIT_INVENTORY_CHANGED")
  58.                         f["UNIT_INVENTORY_CHANGED"] = function()
  59.                             lastGloves.containerID = containerID
  60.                             lastGloves.slotID = slotID
  61.                             gatheringGlovesEquipped = true
  62.                             f["UNIT_INVENTORY_CHANGED"] = nil
  63.                             f:UnregisterEvent("UNIT_INVENTORY_CHANGED")
  64.                         end
  65.                     end
  66.                 end
  67.             end
  68.         end
  69.     end
  70. end
  71.  
  72. local function EquipStandardGloves()
  73.     -- Don't try to equip the normal gloves, if player is in combat or there's an earlier glove change pending
  74.     if gatheringGlovesEquipped and not InCombatLockdown() and not f["UNIT_INVENTORY_CHANGED"] then
  75.         if not UnitCastingInfo("player") then
  76.             PickupContainerItem(lastGloves.containerID, lastGloves.slotID)
  77.             AutoEquipCursorItem()
  78.             f:RegisterEvent("UNIT_INVENTORY_CHANGED")
  79.             f["UNIT_INVENTORY_CHANGED"] = function()
  80.                 gatheringGlovesEquipped = false
  81.                 f["UNIT_INVENTORY_CHANGED"] = nil
  82.                 f:UnregisterEvent("UNIT_INVENTORY_CHANGED")
  83.             end
  84.         else
  85.             -- If the user is casting, wait for the cast to stop before trying to re-equip the gloves
  86.             f:RegisterEvent("UNIT_SPELLCAST_STOP")
  87.         end
  88.     end
  89. end
  90.  
  91. f["UNIT_SPELLCAST_STOP"] = function(unitID)
  92.     if unitID ~= "player" then return end
  93.     f:UnregisterEvent("UNIT_SPELLCAST_STOP")
  94.     EquipStandardGloves()
  95. end
  96.  
  97. local function ScanTooltip()
  98.     if InCombatLockdown() or GameTooltip:NumLines() == 0 then return end
  99.     if GetMouseFocus():GetName() ~= "WorldFrame" then EquipStandardGloves(); return end
  100.     local text = GameTooltipTextLeft2:GetText()
  101.     if text then
  102.         if text == LOCALIZED_MINING then
  103.             ScanForGloves(2)
  104.         elseif text == LOCALIZED_HERBALISM then
  105.             ScanForGloves(1)
  106.         else
  107.             EquipStandardGloves()
  108.         end
  109.     else
  110.         EquipStandardGloves()
  111.     end
  112. end
  113.  
  114. -- Change tooltip size to force OnSizeChanged script handler to run after the tooltip changes for the gathering node
  115. -- The tooltip cannot be scanned at this point, because it will have no contents yet
  116. GameTooltip:HookScript("OnTooltipSetDefaultAnchor", function(self)
  117.     self:SetWidth(1)
  118. end)
  119.  
  120. -- OnSizeChanged runs after the contents of the tooltip have been updated, so the tooltip can be reliably scanned
  121. GameTooltip:HookScript("OnSizeChanged", ScanTooltip)
  122.  
  123. GameTooltip:HookScript("OnHide", EquipStandardGloves)
Advertisement
Add Comment
Please, Sign In to add comment