fixed sorting personal and realm banks (#9)

This commit is contained in:
Kian Soltani
2025-12-25 01:18:10 +01:00
committed by GitHub
parent 81842487b5
commit 53749e8ec5
2 changed files with 189 additions and 32 deletions
+177 -10
View File
@@ -16,12 +16,74 @@ local moves = {};
local frame = CreateFrame("Frame");
local t = 0;
local current = nil;
local isGuildBankSort = false;
local function GetIDFromLink(link)
return link and tonumber(string.match(link, "item:(%d+)"));
end
local function DoMoves()
local function GetAscensionBankType()
if GuildBankFrame and GuildBankFrame.IsPersonalBank then
return "personal"
elseif GuildBankFrame and GuildBankFrame.IsRealmBank then
return "realm"
else
return "guild"
end
end
local function DoGuildBankMoves()
while (current ~= nil or #moves > 0) do
if current ~= nil then
if CursorHasItem() then
local _, id = GetCursorInfo();
if (current ~= nil and current.id == id) then
if (current.sourcetab ~= nil) then
PickupGuildBankItem(current.targettab, current.targetslot);
local link = GetGuildBankItemLink(current.targettab, current.targetslot);
if (current.id ~= GetIDFromLink(link)) then
return;
end
end
else
moves = {};
current = nil;
frame:Hide();
return;
end
else
if (current.sourcetab ~= nil) then
local link = GetGuildBankItemLink(current.targettab, current.targetslot);
if (current.id ~= GetIDFromLink(link)) then
return;
end
end
current = nil;
end
else
if (#moves > 0) then
current = table.remove(moves, 1);
if (current.sourcetab ~= nil) then
PickupGuildBankItem(current.sourcetab, current.sourceslot);
if CursorHasItem() == false then
return;
end
PickupGuildBankItem(current.targettab, current.targetslot);
local link = GetGuildBankItemLink(current.targettab, current.targetslot);
if (current.id == GetIDFromLink(link)) then
current = nil;
else
return;
end
end
end
end
end
frame:Hide();
isGuildBankSort = false;
end
local function DoContainerMoves()
while (current ~= nil or #moves > 0) do
if current ~= nil then
if CursorHasItem() then
@@ -72,6 +134,14 @@ local function DoMoves()
frame:Hide();
end
local function DoMoves()
if isGuildBankSort then
DoGuildBankMoves()
else
DoContainerMoves()
end
end
local function CompareItems(lItem, rItem)
if (rItem.id == nil) then
return true;
@@ -101,6 +171,38 @@ local function BeginSort()
ClearCursor();
end
local function SortGuildBankTab(tabItems)
for i = 1, #tabItems, 1 do
local lowest = i;
for j = #tabItems, i + 1, -1 do
if (CompareItems(tabItems[lowest], tabItems[j]) == false) then
lowest = j;
end
end
if (i ~= lowest) then
local move = {};
move.id = tabItems[lowest].id;
move.name = tabItems[lowest].name;
move.sourcetab = tabItems[lowest].tab;
move.sourceslot = tabItems[lowest].slot;
move.targettab = tabItems[i].tab;
move.targetslot = tabItems[i].slot;
table.insert(moves, move);
local tmp = tabItems[i];
tabItems[i] = tabItems[lowest];
tabItems[lowest] = tmp;
tmp = tabItems[i].slot;
tabItems[i].slot = tabItems[lowest].slot;
tabItems[lowest].slot = tmp;
tmp = tabItems[i].tab;
tabItems[i].tab = tabItems[lowest].tab;
tabItems[lowest].tab = tmp;
end
end
end
local function SortBag(bag)
for i = 1, #bag, 1 do
local lowest = i;
@@ -111,7 +213,7 @@ local function SortBag(bag)
end
if (i ~= lowest) then
-- store move
move = {};
local move = {};
move.id = bag[lowest].id;
move.name = bag[lowest].name;
move.sourcebag = bag[lowest].bag;
@@ -161,6 +263,28 @@ local function CreateBagFromID(bagID)
return bag;
end
local function CreateGuildBankTabItems(tabID)
local items = {};
local numSlots = 98;
for i = 1, numSlots, 1 do
local item = {};
local texture, count, locked = GetGuildBankItemInfo(tabID, i);
local link = GetGuildBankItemLink(tabID, i);
item.tab = tabID;
item.slot = i;
item.name = "<EMPTY>";
item.id = GetIDFromLink(link);
if (item.id ~= nil) then
item.count = count or 1;
item.name, _, item.quality, _, _, item.class, item.subclass, _, item.type, _, item.price = GetItemInfo(item.id);
end
table.insert(items, item);
end
return items;
end
frame:SetScript("OnUpdate", function()
t = t + arg1;
if t > 0.03 then
@@ -212,6 +336,7 @@ function SortBtn:OnClick()
local bags = {};
if self.frameID == "inventory" then
isGuildBankSort = false;
for i = 0, NUM_BAG_FRAMES, 1 do
local bag = CreateBagFromID(i);
local type = select(2, GetContainerNumFreeSlots(i));
@@ -228,9 +353,8 @@ function SortBtn:OnClick()
end
end
end
end
if self.frameID == "bank" then
elseif self.frameID == "bank" then
isGuildBankSort = false;
local i = -1
local bag = CreateBagFromID(i);
local type = select(2, GetContainerNumFreeSlots(i));
@@ -263,15 +387,58 @@ function SortBtn:OnClick()
end
end
end
end
elseif self.frameID == "guildbank" then
isGuildBankSort = true;
local currentTab = GetCurrentGuildBankTab and GetCurrentGuildBankTab() or 0
BeginSort();
for k, v in pairs(bags) do
if v ~= nil then
SortBag(v);
if currentTab and currentTab > 0 then
local bankType = GetAscensionBankType()
local canSort = false
if bankType == "personal" or bankType == "realm" then
canSort = true
else
local _, _, canView, canDeposit, _, remainingWithdrawals = GetGuildBankTabInfo(currentTab)
if canDeposit and (remainingWithdrawals == -1 or remainingWithdrawals > 0) then
canSort = true
end
end
if canSort then
local tabItems = CreateGuildBankTabItems(currentTab)
bags["GUILDBANK"] = tabItems
else
return
end
else
return
end
end
local bagCount = 0
for k, v in pairs(bags) do
if v ~= nil then
bagCount = bagCount + 1
end
end
if bagCount == 0 then
return
end
BeginSort();
for k, v in pairs(bags) do
if v ~= nil then
if isGuildBankSort then
SortGuildBankTab(v);
else
SortBag(v);
end
end
end
frame:Show();
end
+12 -22
View File
@@ -16,6 +16,14 @@ BagnonDB:RegisterEvent('ADDON_LOADED')
ASC_PERSONAL_BANK_OFFSET = 1000;
ASC_REALM_BANK_OFFSET = 2000;
local function IsPersonalBank()
return GuildBankFrame and GuildBankFrame.IsPersonalBank
end
local function IsRealmBank()
return GuildBankFrame and GuildBankFrame.IsRealmBank
end
--constants
local L = BAGNON_FOREVER_LOCALS
local CURRENT_VERSION = GetAddOnMetadata('Bagnon_Forever', 'Version')
@@ -145,7 +153,6 @@ function BagnonDB:PLAYER_LOGIN()
self:RegisterEvent('GUILDBANKFRAME_OPENED')
self:RegisterEvent('GUILDBANKFRAME_CLOSED')
self:RegisterEvent('GUILDBANKBAGSLOTS_CHANGED')
self:RegisterEvent('BANKFRAME_OPENED')
self:RegisterEvent('BANKFRAME_CLOSED')
@@ -192,19 +199,7 @@ end
function BagnonDB:GUILDBANKFRAME_OPENED()
if HasJsonCacheData("BANK_PERMISSIONS_PAYLOAD", 0) then
local json = GetJsonCacheData("BANK_PERMISSIONS_PAYLOAD", 0)
if json then
local jsonObject = C_Serialize:FromJSON(json)
if jsonObject then
self.IsPersonalBank = jsonObject.IsPersonalBank
self.IsRealmBank = jsonObject.IsRealmBank
end
end
end
if (self.IsPersonalBank) then
if IsPersonalBank() then
for i = 1, 6 do
local avail = GetGuildBankTabInfo(i)
if type(avail) == "string" then
@@ -214,7 +209,7 @@ function BagnonDB:GUILDBANKFRAME_OPENED()
return
end
if (self.IsRealmBank) then
if IsRealmBank() then
for i = 1, 6 do
local avail = GetGuildBankTabInfo(i)
if type(avail) == "string" then
@@ -224,13 +219,8 @@ function BagnonDB:GUILDBANKFRAME_OPENED()
end
end
function BagnonDB:GUILDBANKFRAME_CLOSED()
self.IsPersonalBank = nil
self.IsRealmBank = nil
end
function BagnonDB:GUILDBANKBAGSLOTS_CHANGED()
if (self.IsPersonalBank) then
if IsPersonalBank() then
for i = 1, 6 do
local avail = GetGuildBankTabInfo(i)
if type(avail) == "string" then
@@ -240,7 +230,7 @@ function BagnonDB:GUILDBANKBAGSLOTS_CHANGED()
return
end
if (self.IsRealmBank) then
if IsRealmBank() then
for i = 1, 6 do
local avail = GetGuildBankTabInfo(i)
if type(avail) == "string" then