Guest User

Gathering Glove Swap Clean

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