diff --git a/GemStacker.lua b/GemStacker.lua index f0738a7..6752035 100644 --- a/GemStacker.lua +++ b/GemStacker.lua @@ -1,40 +1,40 @@ -GemStacker = LibStub('AceAddon-3.0'):NewAddon('GemStacker','AceConsole-3.0','AceEvent-3.0'); -local core = GemStacker; +GemStacker = LibStub('AceAddon-3.0'):NewAddon('GemStacker','AceConsole-3.0','AceEvent-3.0') +local core = GemStacker function core:OnInitialize() end function core:OnEnable() - self:RegisterEvent('CHAT_MSG_LOOT','StackGems'); + self:RegisterEvent('CHAT_MSG_LOOT','StackGems') end function core:StackGems(_,message) - if(not string.find(message,'You create')) then return end; - local gem = message:match("%[(.-)%]"); - - local sourceContainer,sourceSlot,destContainer,destSlot = -1,-1,-1,-1; - for bag = 0,NUM_BAG_SLOTS do - for slot = 1,GetContainerNumSlots(bag) do - local itemId = GetContainerItemID(bag,slot) - if(itemId) then - local itemName,_,_,_,_,_,_,maxStack = GetItemInfo(itemId); - if(gem == itemName and maxStack ~= 1) then - local _,count = GetContainerItemInfo(bag,slot); - if(count == 1 and sourceContainer < 0) then - sourceContainer = bag; - sourceSlot = slot; - elseif(count < 20 and (count > 1 or destContainer < 0)) then - destContainer = bag; - destSlot = slot; + local lastTradeskill = message:match("%[(.-)%]") + local combineSlots = {} + + --loop through bags in reverse + for bag = 0, NUM_BAG_SLOTS do + for slot = 1, GetContainerNumSlots(bag) do + local itemID = GetContainerItemID(bag, slot) + if(itemID) then + local itemName, _, _, _, _, _, _, maxStack = GetItemInfo(itemID) + + if lastTradeskill == itemName and maxStack ~= 1 then + print('Found', itemName) + local _, count = GetContainerItemInfo(bag, slot) + + if count < 20 and #combineSlots < 2 then + combineSlots[#combineSlots + 1] = { bag, slot } end end end end end - - if(sourceContainer ~= -1 and destContainer ~= -1) then - PickupContainerItem(sourceContainer,sourceSlot); - PickupContainerItem(destContainer,destSlot) + + --and then stack the gems + if #combineSlots > 1 then + PickupContainerItem(combineSlots[1][1], combineSlots[1][2]) + PickupContainerItem(combineSlots[2][1], combineSlots[2][2]) end end \ No newline at end of file