Quantcast

Small fix for Item Not Found error... well hopefully.

Xruptor [09-19-16 - 20:35]
Small fix for Item Not Found error... well hopefully.
Filename
xanAutoMail.lua
diff --git a/xanAutoMail.lua b/xanAutoMail.lua
index 4f7c3f5..a656e1e 100644
--- a/xanAutoMail.lua
+++ b/xanAutoMail.lua
@@ -212,12 +212,14 @@ end
 local delayCount = {}
 local moneyCount = 0
 local errorCount = 0
+local mailCount = 0
 local errorCheckCount = 0
 local currentStatus = "STOP"

 xanAutoMail:RegisterEvent("MAIL_CLOSED")
-xanAutoMail:RegisterEvent("MAIL_INBOX_UPDATE")
 xanAutoMail:RegisterEvent("MAIL_SHOW")
+xanAutoMail:RegisterEvent("MAIL_INBOX_UPDATE")
+xanAutoMail:RegisterEvent("UI_ERROR_MESSAGE")

 local function colorMoneyText(value)
 	if not value then return "" end
@@ -318,19 +320,19 @@ function xanAutoMail:MAIL_SHOW()
 end

 function xanAutoMail:MAIL_CLOSED()
-	inboxFullCheck()
-	delayCount = {} --reset any delay in queue
-	inboxInfoText:SetText("")
+	xanAutoMail:StopMailGrab(true)
 end

 function xanAutoMail:MAIL_INBOX_UPDATE()
 	if currentStatus == "STOP" then return end
-	inboxFullCheck()
-	xanAutoMail:Delay("mailGrabNextItem", 0.2, xanAutoMail.GrabNextMailItem)
+	xanAutoMail:Delay("mailCheckMailUpdate", 0.3, xanAutoMail.CheckMailUpdate)
+	xanAutoMail:Delay("mailInfoText", 1, xanAutoMail.UpdateInfoText, true)
 end

 function xanAutoMail:UI_ERROR_MESSAGE(event, arg1)
+	if currentStatus == "STOP" then return end
 	local stopMailGrab = false
+
 	if arg1 == ERR_MAIL_DATABASE_ERROR then
 		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: (ERROR) There was a mailbox database error from the server.")
 		stopMailGrab = true
@@ -347,6 +349,19 @@ function xanAutoMail:UI_ERROR_MESSAGE(event, arg1)
 	end
 end

+--this function makes the delay wait until the inbox has decreased in size when we loot something
+--if we don't do this then the it will continously try to autoloot an item from the same mail
+--this leads to the ERR_ITEM_NOT_FOUND error.
+function xanAutoMail:CheckMailUpdate()
+	--check to see if our inbox has decreased by one since we last autolooted
+	local nItem, nTotal = GetInboxNumItems()
+	if nItem <= mailCount - 1 or nTotal == 0 then
+		xanAutoMail:Delay("mailGrabNextItem", 0.3, xanAutoMail.GrabNextMailItem)
+	else
+		xanAutoMail:Delay("mailCheckMailUpdate", 0.3, xanAutoMail.CheckMailUpdate)
+	end
+end
+
 function xanAutoMail:GrabNextMailItem()
 	if currentStatus == "STOP" then return end

@@ -362,7 +377,7 @@ function xanAutoMail:GrabNextMailItem()

 	xanAutoMail:UpdateInfoText()

-	--check to see if the last messages were read or if we have nothing to work with
+	--check to see if the last messages were read or if we have nothing to work with, or we have done more than 10 error checks
 	if nTotal == errorCount or nTotal <= 0 or errorCheckCount > 10 then
 		xanAutoMail:StopMailGrab()
 		return
@@ -381,6 +396,7 @@ function xanAutoMail:GrabNextMailItem()
 	end

 	errorCount = 0
+	mailCount = 0

 	for i = 1, nItem do
 		local _, _, _, _, money, COD, _, numItems, wasRead, _, _, _, isGM = GetInboxHeaderInfo(i)
@@ -390,6 +406,7 @@ function xanAutoMail:GrabNextMailItem()
 				errorCount = errorCount + 1
 			else
 				if money > 0 then moneyCount = moneyCount + money end
+				mailCount = GetInboxNumItems()
 				AutoLootMailItem(i)
 				--we looted something so lets wait for next update
 				return
@@ -404,8 +421,8 @@ function xanAutoMail:GrabNextMailItem()
 		xanAutoMail:StopMailGrab()
 		return
 	else
-		--there was an error so lets try to grab again just in case our total = total errors
-		xanAutoMail:Delay("mailGrabNextItem", 0.2, xanAutoMail.GrabNextMailItem)
+		--there was an error so lets try to grab again just in case our total = total errors (which could mean all mail has been read)
+		xanAutoMail:Delay("mailGrabNextItem", 0.3, xanAutoMail.GrabNextMailItem)
 		errorCheckCount = errorCheckCount + 1 --this is another emergency stop, don't do more than 10 error checks
 	end
 end
@@ -414,27 +431,23 @@ function xanAutoMail:StartMailGrab()
 	if GetInboxNumItems() == 0 then return end
 	currentStatus = "START"
 	inboxAllButton:Disable()
-
-	--register for inventory full error
-	xanAutoMail:RegisterEvent("UI_ERROR_MESSAGE")
-
 	moneyCount = 0
 	errorCount = 0
 	errorCheckCount = 0
-	xanAutoMail:Delay("mailGrabNextItem", 0.2, xanAutoMail.GrabNextMailItem)
+	xanAutoMail:Delay("mailGrabNextItem", 0.3, xanAutoMail.GrabNextMailItem)
 end

-function xanAutoMail:StopMailGrab()
+function xanAutoMail:StopMailGrab(force)
 	currentStatus = "STOP"
 	delayCount = {}
 	inboxAllButton:Enable()
-	xanAutoMail:UnregisterEvent("UI_ERROR_MESSAGE")
-	if moneyCount > 0 then
-		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: Total money from mailbox ["..colorMoneyText(moneyCount).."]")
+	if not force then
+		if moneyCount > 0 then
+			DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: Total money from mailbox ["..colorMoneyText(moneyCount).."]")
+		end
+		CheckInbox()
+		xanAutoMail:Delay("mailInfoText", 1, xanAutoMail.UpdateInfoText, true)
 	end
-
-	CheckInbox()
-	xanAutoMail:Delay("mailInfoText", 1, xanAutoMail.UpdateInfoText, true)
 end