diff --git a/Core.lua b/Core.lua index 94ecf8a..0dfb208 100644 --- a/Core.lua +++ b/Core.lua @@ -1,51 +1,72 @@ -local addonName, ns = ... - --- WoW functions alliases - -local GetContainerItemInfo = C_Container and C_Container.GetContainerItemInfo or GetContainerItemInfo -local GetContainerNumSlots = C_Container and C_Container.GetContainerNumSlots or GetContainerNumSlots -local SplitContainerItem = C_Container and C_Container.SplitContainerItem or SplitContainerItem -local PickupContainerItem = C_Container and C_Container.PickupContainerItem or PickupContainerItem - --- Addon variables - -local f = CreateFrame("Frame") -- Main event frame - -local ContainerScope = {} -ContainerScope.PERSONAL = { - Enum.BagIndex.Backpack, - Enum.BagIndex.Bag_1, - Enum.BagIndex.Bag_2, - Enum.BagIndex.Bag_3, - Enum.BagIndex.Bag_4 - } - - ContainerScope.PERSONAL_REGENTS = { - Enum.BagIndex.ReagentBag - } - - ContainerScope.PERSONAL_WITH_REGENTS = ns.mergeTables({ Enum.BagIndex.ReagentBag }, ContainerScope.PERSONAL) - - ContainerScope.BANK = { - Enum.BagIndex.Bank, - Enum.BagIndex.BankBag_1, - Enum.BagIndex.BankBag_2, - Enum.BagIndex.BankBag_3, - Enum.BagIndex.BankBag_4, - Enum.BagIndex.BankBag_5, - Enum.BagIndex.BankBag_6, - Enum.BagIndex.BankBag_7 - } - - ContainerScope.BANK_REGENTS_WITH_BANK = ns.mergeTables({ Enum.BagIndex.Reagentbank }, ContainerScope.BANK) - - ContainerScope.WARBAND_BANK = { - Enum.BagIndex.AccountBankTab_1, - Enum.BagIndex.AccountBankTab_2, - Enum.BagIndex.AccountBankTab_3, - Enum.BagIndex.AccountBankTab_4, - Enum.BagIndex.AccountBankTab_5, - } +local addonName, ns = ... + +-- WoW functions alliases + +local GetContainerItemInfo = C_Container and C_Container.GetContainerItemInfo or GetContainerItemInfo +local GetContainerNumSlots = C_Container and C_Container.GetContainerNumSlots or GetContainerNumSlots +local SplitContainerItem = C_Container and C_Container.SplitContainerItem or SplitContainerItem +local PickupContainerItem = C_Container and C_Container.PickupContainerItem or PickupContainerItem + +-- Addon variables + +local f = CreateFrame("Frame") -- Main event frame + +local BAG_BACKPACK = (Enum and Enum.BagIndex and Enum.BagIndex.Backpack) or BACKPACK_CONTAINER or 0 +local BAG_REAGENT = (Enum and Enum.BagIndex and Enum.BagIndex.ReagentBag) or _G.REAGENTBAG_CONTAINER +local BAG_BANK = (Enum and Enum.BagIndex and Enum.BagIndex.Bank) or BANK_CONTAINER or -1 +local BAG_REAGENT_BANK = (Enum and Enum.BagIndex and Enum.BagIndex.Reagentbank) or _G.REAGENTBANK_CONTAINER +local NUM_PERSONAL_BAGS = NUM_BAG_SLOTS or 4 +local NUM_BANK_BAGS = NUM_BANKBAGSLOTS or 7 + +local function BuildPersonalBags() + local bags = {} + if BAG_BACKPACK ~= nil then + table.insert(bags, BAG_BACKPACK) + end + for i = 1, NUM_PERSONAL_BAGS do + table.insert(bags, i) + end + return bags +end + +local function BuildBankBags() + local bags = {} + if BAG_BANK ~= nil then + table.insert(bags, BAG_BANK) + end + for i = 1, NUM_BANK_BAGS do + table.insert(bags, NUM_PERSONAL_BAGS + i) + end + return bags +end + +local function BuildSingleBag(bagIndex) + if bagIndex ~= nil then + return { bagIndex } + end + return {} +end + +local function BuildWarbandBank() + if Enum and Enum.BagIndex and Enum.BagIndex.AccountBankTab_1 then + return { + Enum.BagIndex.AccountBankTab_1, + Enum.BagIndex.AccountBankTab_2, + Enum.BagIndex.AccountBankTab_3, + Enum.BagIndex.AccountBankTab_4, + Enum.BagIndex.AccountBankTab_5, + } + end + return {} +end + +local ContainerScope = {} +ContainerScope.PERSONAL = BuildPersonalBags() +ContainerScope.PERSONAL_REGENTS = BuildSingleBag(BAG_REAGENT) +ContainerScope.PERSONAL_WITH_REGENTS = ns.mergeTables(ContainerScope.PERSONAL_REGENTS, ContainerScope.PERSONAL) +ContainerScope.BANK = BuildBankBags() +ContainerScope.BANK_REGENTS_WITH_BANK = ns.mergeTables(BuildSingleBag(BAG_REAGENT_BANK), ContainerScope.BANK) +ContainerScope.WARBAND_BANK = BuildWarbandBank() local dialog = nil @@ -74,20 +95,30 @@ end --[[ Get item information ]] -local function CollectItemInfo(isGuildBank, sourceBagIndex, sourceSlotIndex) - if isGuildBank then - local texture, itemCount, locked, isFiltered, quality = GetGuildBankItemInfo(sourceBagIndex, sourceSlotIndex) - if not texture then - return nil - end - local itemInfo = {} -- We try to replcate of a return structure of GetContainerItemInfo - itemInfo.stackCount = itemCount - itemInfo.isLocked = locked - return itemInfo - else - return GetContainerItemInfo(sourceBagIndex, sourceSlotIndex) - end -end +local function CollectItemInfo(isGuildBank, sourceBagIndex, sourceSlotIndex) + if isGuildBank then + local texture, itemCount, locked, isFiltered, quality = GetGuildBankItemInfo(sourceBagIndex, sourceSlotIndex) + if not texture then + return nil + end + local itemInfo = {} -- We try to replcate of a return structure of GetContainerItemInfo + itemInfo.stackCount = itemCount + itemInfo.isLocked = locked + return itemInfo + else + local texture, itemCount, locked = GetContainerItemInfo(sourceBagIndex, sourceSlotIndex) + if type(texture) == "table" then + return texture + end + if not texture then + return nil + end + return { + stackCount = itemCount, + isLocked = locked + } + end +end --[[ Finds an empty slot in the player's bags. @@ -114,11 +145,11 @@ local function FindEmptySlot(exclude, isGuildBank, sourceBagIndex, sourceSlotInd local bags = ContainerScope.PERSONAL - if sourceBagIndex == Enum.BagIndex.ReagentBag then - bags = ContainerScope.PERSONAL_WITH_REGENTS - end - - if sourceBagIndex == Enum.BagIndex.Reagentbank then + if BAG_REAGENT and sourceBagIndex == BAG_REAGENT then + bags = ContainerScope.PERSONAL_WITH_REGENTS + end + + if BAG_REAGENT_BANK and sourceBagIndex == BAG_REAGENT_BANK then bags = ContainerScope.BANK_REGENTS_WITH_BANK end @@ -135,7 +166,7 @@ local function FindEmptySlot(exclude, isGuildBank, sourceBagIndex, sourceSlotInd end if forceBankScope then - if ns.containsValue(ContainerScope.BANK, sourceBagIndex) or sourceBagIndex == Enum.BagIndex.Reagentbank then + if ns.containsValue(ContainerScope.BANK, sourceBagIndex) or sourceBagIndex == BAG_REAGENT_BANK then bags = ContainerScope.BANK else bags = { sourceBagIndex } @@ -149,13 +180,13 @@ local function FindEmptySlot(exclude, isGuildBank, sourceBagIndex, sourceSlotInd if isGuildBank then startSlot = sourceSlotIndex + 1 maxSlots = 98 - elseif bag == Enum.BagIndex.Reagentbank then - maxSlots = 98 - elseif bag == Enum.BagIndex.Bank then - maxSlots = 28 - else - maxSlots = GetContainerNumSlots(bag) - end + elseif BAG_REAGENT_BANK and bag == BAG_REAGENT_BANK then + maxSlots = 98 + elseif bag == BAG_BANK then + maxSlots = 28 + else + maxSlots = GetContainerNumSlots(bag) + end for slot = startSlot, maxSlots do -- Check if the current bag-slot pair is excluded @@ -269,45 +300,43 @@ end ]] local function SourceLocation(parent) - local parentName = parent:GetName() - local parentParentName = nil - local parentParentParentName = nil - - if parent:GetParent() and parent:GetParent():GetName() then - parentParentName = parent:GetParent():GetName() - end - - if parent:GetParent() and parent:GetParent():GetParent() and parent:GetParent():GetParent():GetName() then - parentParentParentName = parent:GetParent():GetParent():GetName() - end - + local parentName = parent and parent.GetName and parent:GetName() or nil + local parentParent = parent and parent.GetParent and parent:GetParent() or nil + local parentParentName = parentParent and parentParent.GetName and parentParent:GetName() or nil + local parentParentParent = parentParent and parentParent.GetParent and parentParent:GetParent() or nil + local parentParentParentName = parentParentParent and parentParentParent.GetName and parentParentParent:GetName() or nil + local isGuildBank = false - local bagIndex = parent:GetParent():GetID() - local slotIndex = parent:GetID() + local bagIndex = parentParent and parentParent.GetID and parentParent:GetID() or nil + local slotIndex = parent and parent.GetID and parent:GetID() or nil local forceBankScope = false local hasItemLocation = false local locationBagIndex, locationSlotIndex = nil, nil - if parent and parent:GetItemLocation() and parent:GetItemLocation():GetBagAndSlot() then + if parent and parent.GetItemLocation and parent:GetItemLocation() and parent:GetItemLocation().GetBagAndSlot and parent:GetItemLocation():GetBagAndSlot() then locationBagIndex, locationSlotIndex = parent:GetItemLocation():GetBagAndSlot() hasItemLocation = locationBagIndex ~= nil and locationSlotIndex ~= nil end - - ns.printParentNames(parent) - - ns.Log.debug("Current guild bank tab opened:", GetCurrentGuildBankTab()) - - -- This should work everywhere except guild bank as it's container numeration is not shared with other spaces - if locationBagIndex and locationSlotIndex then - ns.Log.debug("BI", locationBagIndex, "SI", locationSlotIndex, "GBankTab", GetCurrentGuildBankTab()) - bagIndex = locationBagIndex - slotIndex = locationSlotIndex - end + + ns.printParentNames(parent) + + if GetCurrentGuildBankTab then + ns.Log.debug("Current guild bank tab opened:", GetCurrentGuildBankTab()) + end + + -- This should work everywhere except guild bank as it's container numeration is not shared with other spaces + if locationBagIndex and locationSlotIndex then + ns.Log.debug("BI", locationBagIndex, "SI", locationSlotIndex, "GBankTab", GetCurrentGuildBankTab()) + bagIndex = locationBagIndex + slotIndex = locationSlotIndex + end -- Baganator guild bank -- if ns.isParentNameInHierarchy(parent, "Baganator_SingleViewGuildViewFrameblizzard") then isGuildBank = true - bagIndex = GetCurrentGuildBankTab() + if GetCurrentGuildBankTab then + bagIndex = GetCurrentGuildBankTab() + end end -- Baganator personal bank @@ -316,26 +345,34 @@ local function SourceLocation(parent) end -- Bagnon guild bank - if parentName and string.sub(parentName, 1, 15) == "BagnonGuildItem" and locationSlotIndex then - isGuildBank = true - bagIndex = GetCurrentGuildBankTab() - slotIndex = locationSlotIndex - end - - -- Blizzard Guild Bank Frame - if parent:GetParent():GetParent():GetName() == "GuildBankFrame" then - isGuildBank = true - bagIndex = GetCurrentGuildBankTab() - end - - -- Blizzard combined bags mode - if parent:GetParent():GetName() == "ContainerFrameCombinedBags" then - bagIndex, slotIndex = parent:GetItemLocation():GetBagAndSlot() - end + if parentName and string.sub(parentName, 1, 15) == "BagnonGuildItem" and locationSlotIndex then + isGuildBank = true + if GetCurrentGuildBankTab then + bagIndex = GetCurrentGuildBankTab() + end + slotIndex = locationSlotIndex + end + + -- Blizzard Guild Bank Frame + if parentParentParentName == "GuildBankFrame" then + isGuildBank = true + if GetCurrentGuildBankTab then + bagIndex = GetCurrentGuildBankTab() + end + end + + -- Blizzard combined bags mode + if parentParentName == "ContainerFrameCombinedBags" and parent.GetItemLocation and parent:GetItemLocation() and parent:GetItemLocation().GetBagAndSlot then + bagIndex, slotIndex = parent:GetItemLocation():GetBagAndSlot() + end -- Blizzard personal bank (ensure auto-split stays in bank slots) if BankFrame + and BankFrame.IsVisible and BankFrame:IsVisible() + and BankFrame.GetActiveBankType + and Enum + and Enum.BankType and BankFrame:GetActiveBankType() == Enum.BankType.Character and ns.isParentNameInHierarchy(parent, "BankFrame") then forceBankScope = true @@ -348,23 +385,45 @@ local function SourceLocation(parent) bagIndex = locationBagIndex slotIndex = locationSlotIndex else - bagIndex = Enum.BagIndex.Bank + bagIndex = BAG_BANK slotIndex = parent:GetID() end end -- Blizzard Warband bank - if BankFrame and BankFrame:IsVisible() and BankFrame:GetActiveBankType() == Enum.BankType.Account then - bagIndex = parent:GetBankTabID() - slotIndex = parent:GetContainerSlotID() + if BankFrame + and BankFrame.IsVisible + and BankFrame:IsVisible() + and BankFrame.GetActiveBankType + and Enum + and Enum.BankType + and BankFrame:GetActiveBankType() == Enum.BankType.Account then + if parent.GetBankTabID and parent.GetContainerSlotID then + bagIndex = parent:GetBankTabID() + slotIndex = parent:GetContainerSlotID() + end end - + return isGuildBank, bagIndex, slotIndex, forceBankScope, hasItemLocation end -- Overrides standard function for split dialog local function OpenFrame(self, maxStack, parent, anchor, anchorTo, stackCount) + if type(self) == "number" then + stackCount = anchorTo + anchorTo = anchor + anchor = parent + parent = maxStack + maxStack = self + self = StackSplitFrame + end + + if not parent then + parent = UIParent + anchor = "CENTER" + anchorTo = "CENTER" + end ClearDialog() dialog = CreateItemSplitterDialog(maxStack) @@ -395,6 +454,8 @@ local function OpenFrame(self, maxStack, parent, anchor, anchorTo, stackCount) local splitValue = dialog:GetValue() if self and type(self.split) == "function" then self.split(splitValue) + elseif StackSplitFrame and type(StackSplitFrame.SplitStack) == "function" then + StackSplitFrame:SplitStack(splitValue) else parent.SplitStack(parent, splitValue) -- original behaviour end @@ -409,9 +470,15 @@ end -- Init block -local function Init() - StackSplitFrame.OpenStackSplitFrame = OpenFrame -end +local function Init() + if StackSplitFrame and StackSplitFrame.OpenStackSplitFrame then + StackSplitFrame.OpenStackSplitFrame = OpenFrame + elseif type(OpenStackSplitFrame) == "function" then + OpenStackSplitFrame = OpenFrame + else + ns.Log.warn("Stack split override not available") + end +end -- Frame update diff --git a/ItemSplitter.toc b/ItemSplitter.toc index ada47ab..594dcab 100644 --- a/ItemSplitter.toc +++ b/ItemSplitter.toc @@ -1,6 +1,7 @@ -## Interface: 110207 +## Interface: 110207 +## Interface-Classic: 11507 ## Title: Item Splitter -## Version: 11.2.7 +## Version: 11.2.7 ## Author: outlying ## IconTexture: Interface\Icons\misc_torgast_theboxofmanythings ## Description: Allows users to split stacks of items into smaller quantities within the inventory.