Hello again!
I tried a lot of different things. I got some help from people on discord and this is my current result.
It doesn't work 100%. I believe the issue is that slots ain't updated if they are moved, so they might try to move even if they are at the correct spot, or trade spot with a item that is at the correct spot.
Example of issue:
First Slot A updates:
Slot A <-> Slot B
Then its Slot B's turn
Slot B <-> Slot A
Any tips on how to sort this better to avoid this kind of behavior?
- Couple other issues I haven't done much about
:
Currently its only for main bag.
What if a item swap with another and they merge? Will that break something
Code:
local AllItems = {}
local waitTable = {}
local function PrintAll() -- Debugging only, Ignore this
print('------')
for i = table.getn(AllItems), 1, -1
do
print(AllItems[i].itemName, AllItems[i].itemSellPrice)
end
print('------')
end
local function GetAllItemsFromBag(Insert)
-- for slot=0, NUM_BAG_SLOTS do
for index=1, GetContainerNumSlots(0) do
local item = GetContainerItemID(0, index)
if(item) then
--print(item)
local _, _, _, _, _, _, _, _, _, itemID = GetContainerItemInfo(0, index)
if(itemID ~= nil) then
--print(itemID)
local itemName, _, _, _, _, _, _, _, _, _, itemSellPrice = GetItemInfo(itemID)
table.insert(Insert, {index = index, itemID = itemID, itemSellPrice = itemSellPrice, itemName = itemName})
end
end
end
-- end
end
local function SaferSwapItems(bag1, bag2, slot1, slot2)
ClearCursor()
print(slot1, 'To', slot2)
local _, _, locked1 = GetContainerItemInfo(bag1, slot1)
local _, _, locked2 = GetContainerItemInfo(bag2, slot2)
if(locked1 or locked2) then
print(bag1, bag2, slot1, slot2)
table.insert(waitTable, {bag1 = bag1, bag2 = bag2, slot1 = slot1, slot2 = slot2})
else
--print("Swapping bag", bag1, "slot", slot1, "......for bag", bag2, "slot", slot2)
PickupContainerItem(bag1, slot1)
PickupContainerItem(bag2, slot2)
end
end
local function SortBag()
local bagNr = 0
for i = table.getn(AllItems), 1, -1 do
local currentTable = AllItems[i]
print(currentTable.itemName , 'CurSlot', currentTable.index ,'TargetSlot', i)
SaferSwapItems(0,0, currentTable.index, i)
end
end
local frame = CreateFrame("FRAME", "FooAddonFrame");
local button
local ntex
local htex
local ptex
local function ADDON_LOADED()
button = CreateFrame("Button", nil, UIParent)
button:SetPoint("CENTER")
button:SetWidth(100)
button:SetHeight(25)
button:SetText("Sort")
button:SetNormalFontObject("GameFontNormal")
ntex = button:CreateTexture()
ntex:SetTexture("Interface/Buttons/UI-Panel-Button-Up")
ntex:SetTexCoord(0, 0.625, 0, 0.6875)
ntex:SetAllPoints()
button:SetNormalTexture(ntex)
htex = button:CreateTexture()
htex:SetTexture("Interface/Buttons/UI-Panel-Button-Highlight")
htex:SetTexCoord(0, 0.625, 0, 0.6875)
htex:SetAllPoints()
button:SetHighlightTexture(htex)
ptex = button:CreateTexture()
ptex:SetTexture("Interface/Buttons/UI-Panel-Button-Down")
ptex:SetTexCoord(0, 0.625, 0, 0.6875)
ptex:SetAllPoints()
button:SetPushedTexture(ptex)
button:SetScript("OnClick", function()
AllItems = {}
GetAllItemsFromBag(AllItems)
table.sort(AllItems, function(i, k)
return i.itemSellPrice > k.itemSellPrice
end)
SortBag()
end)
end
local function ITEM_LOCK_CHANGED(self, event, ...)
if table.getn(waitTable) > 0 then
local currentTable = waitTable[1]
C_Timer.After(0.5, function() SaferSwapItems(currentTable.bag1, currentTable.bag2, currentTable.slot1, currentTable.slot2) end)
waitTable = table.remove(waitTable, 1)
end
end
local Events = {
["ADDON_LOADED"] = ADDON_LOADED,
["ITEM_LOCK_CHANGED"] = ITEM_LOCK_CHANGED
}
local function eventHandler(self, event, ...)
if(Events[event]) then
Events[event](...)
end
end
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("ITEM_LOCK_CHANGED");
frame:SetScript("OnEvent", eventHandler);