View Single Post
12-16-19, 08:47 AM   #6
loff93
A Fallenroot Satyr
Join Date: Apr 2017
Posts: 25
Originally Posted by SDPhantom View Post
ITEM_LOCK_CHANGED fires when the lock state changes. BAG_UPDATE_DELAYED is what fires when items are added/deleted/moved in the bags.
ahh, good to know, thanks

the swapping script is not working correctly thought and I'm not sure how I'm gonna fix this.
Example: I have to click multiple times for items to get to the correct spot at times, and sometime it just won't help. I know why this happens, but I don't know how to get around it yet.
Trying to think of ways to swap them around, but I'm so new to Lua/WoW it's hard to think of something
Will update if I find a solution

*UPDATE*

This work as far as I can tell. Is it pretty? Not at all
Tips to improve is appreciated.

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()
	for bagnr = 0, NUM_BAG_SLOTS do
		for slotnr=1, GetContainerNumSlots(bagnr) do
			local item = GetContainerItemID(bagnr, slotnr)
			if(item) then
				--print(item)
				local _, _, _, _, _, _, _, _, _, itemID = GetContainerItemInfo(bagnr, slotnr)
				--if(itemID ~= nil) then
					--print(itemID)
					local itemName, _, _, _, _, _, _, _, _, _, itemSellPrice = GetItemInfo(itemID)
					table.insert(AllItems, {index = slotnr, itemID = itemID, itemSellPrice = itemSellPrice, itemName = itemName, targetSlot = 0, targetBag = 0})
					--print(itemName, index)
				--end
			end
		end
	end
end

local function GetSlotNr(bagnr, item)
	for index=1, GetContainerNumSlots(bagnr) do
		local _, _, _, _, _, _, _, _, _, itemID = GetContainerItemInfo(bagnr, index)
			if(item.itemID == itemID) then
				return index;
		end
	end
end

local function GetBagNr(theItem)
	for bagnr = 0, NUM_BAG_SLOTS do
		for slotnr=1, GetContainerNumSlots(bagnr) do
			local item = GetContainerItemID(bagnr, slotnr)
			if(item) then
				local _, _, _, _, _, _, _, _, _, itemID = GetContainerItemInfo(bagnr, slotnr)
				if(theItem.itemID == itemID) then
					return bagnr
				end
			end
		end
	end
end

local function SaferSwapItems(item)
	ClearCursor()
	local bag1 = GetBagNr(item);
	local bag2 = item.targetBag;
	local slot1 = GetSlotNr(bag1, item)
	local slot2 = item.targetSlot
	if(item.itemID == 11584) then
		print(bag1, bag2, slot1, slot2)
		print(item.itemName,': Move From Bag',bag1,'Slot' , slot1, 'To Bag',bag2, 'Slot', slot2)
	end
	local _, _, locked1 = GetContainerItemInfo(bag1, slot1)
	local _, _, locked2 = GetContainerItemInfo(bag2, slot2)

	if(locked1 or locked2) then
		table.insert(waitTable, {itemID = item.itemID, itemName = item.itemName, targetSlot = item.targetSlot, targetBag = item.targetBag})
	else
		PickupContainerItem(bag1, slot1)
		PickupContainerItem(bag2, slot2)
	end
	
end

local function SortBag()
		local bagNr = 0
		for i = 1, table.getn(AllItems) do
			local currentItem = AllItems[i]
			SaferSwapItems(currentItem)
		end
end

local function GetSlotInBag(index)
	local currentTotalSlot = 0
	for bagnr = 0, NUM_BAG_SLOTS do
		for slotnr = 1, GetContainerNumSlots(bagnr) do
			currentTotalSlot = currentTotalSlot + 1
			if(index == currentTotalSlot) then
				return slotnr
			end
		end
	end
end

local function SetTargetSlot()
	for i = 1, table.getn(AllItems) do
		local currentItem = AllItems[i]
		currentItem.targetSlot = GetSlotInBag(i)
		print(currentItem.targetSlot)
	end
end



local function GetBag(index)
	local currentTotalSlot = 0
	for bagnr = 0, NUM_BAG_SLOTS do
		for slotnr = 1, GetContainerNumSlots(bagnr) do
			currentTotalSlot = currentTotalSlot + 1
			if(index == currentTotalSlot) then
				return bagnr
			end
		end
	end
end

local function SetTargetBag()
	for i = 1, table.getn(AllItems) do 
		AllItems[i].targetBag = GetBag(i)
		--print(AllItems[i].itemName,'bag:', AllItems[i].targetBag)
	end
end

local function BAG_UPDATE_DELAYED(self, event, ...)
	if table.getn(waitTable) > 0 then
		local currentItem = waitTable[1]
		C_Timer.After(0.2, function() SaferSwapItems(currentItem) end)
		waitTable = table.remove(waitTable, 1)
	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()
		table.sort(AllItems, function(i, k)
			return i.itemSellPrice > k.itemSellPrice
		end)
		SetTargetBag()
		SetTargetSlot()
		--PrintAll()
		SortBag()	

	end)
end

local Events = {
	["ADDON_LOADED"] = ADDON_LOADED,
	["BAG_UPDATE_DELAYED"] = BAG_UPDATE_DELAYED
}

local function eventHandler(self, event, ...)
	if(Events[event]) then
		Events[event](...)
	end
end

frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("BAG_UPDATE_DELAYED");
frame:SetScript("OnEvent", eventHandler);
*Update*
Everything works, but 1 issue is that if I have multiple of the same items it and calculcate total price of itemcount * sellprice, it doesn't care what stack the orders are in

Last edited by loff93 : 12-16-19 at 01:15 PM.
  Reply With Quote