Quantcast

- Fix for #1282 : An empty bag slot now counts correctly rather than an 'error'

urnati [10-18-20 - 17:37]
- Fix for #1282 : An empty bag slot now counts correctly rather than an 'error'
Filename
TitanBag/TitanBag.lua
diff --git a/TitanBag/TitanBag.lua b/TitanBag/TitanBag.lua
index 9c892ea..6b40140 100644
--- a/TitanBag/TitanBag.lua
+++ b/TitanBag/TitanBag.lua
@@ -159,118 +159,131 @@ function TitanPanelBagButton_OnClick(self, button)
 end

 -- **************************************************************************
--- NAME : isProfBag(id)
--- DESC : Determine if this is a profession bag by getting its id and checking
+-- NAME : isBag(id)
+-- DESC : Determine if this is a bag. Then check for a profession bag using its id
 -- against the table of known ids.
 -- If it is a profession bag then grab its 'color' in case the user requested it.
 -- VARS : name = text (localized) of the bag to check
+-- VARS : pos  = position of the bag to check
 -- **************************************************************************
---[==[
-	local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType,
-		itemStackCount, itemEquipLoc, itemIcon, itemSellPrice, itemClassID, itemSubClassID,
-		bindType, expacID, itemSetID, isCraftingReagent =
-		GetItemInfo(bag_name)
-	local bag_type = false
+	-- using GetItemInfo caused a 'script ran too long' error on Shadowlands ptr (9.x) although it worked in retail (8.x)
+	-- GetItemInfoInstant returns the needed id and uses client files - no server call.
+local function isBag(bag_name, pos)
+--[[
+print("isBag:"
+.." '"..tostring(pos).."'"
+.." '"..tostring(bag_name).."'"
+)
+--]]
+	-- set defaults for no bag found
+	local is_bag = false
+	local bag_type = "none"
 	local color = {r=0,g=0,b=0} -- black (should never be used...)

-	if itemName == nil or itemName == false then
-		-- For some inexplicable reason the Backpack does not return as an item...
-		--TitanDebug("Bag name not found: '"..tostring(name).."'")
+	if bag_name == nil then
+		-- no bag found, return defaults
 	else
-		local _, _, icolor, ltype, id, enchant, gem1, gem2, gem3, gem4,
-			suffix, unique, linkLvl, specID,
-			upgradeID, instance_diff_id, num_bonus_ids, bonus1, bonus2, name = string.find(itemLink,
-		  "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*):?(%d*):?(%-?%d*):?(%d*):?(%d*):?(%d*):?(%d*):|h%[(.-)%]|h?|?r?")
-
-		local string_id = tonumber(id)
-		if bags[string_id] then
-			bag_type = bags[string_id].style
-			color = bag_info[bag_type].color
-		else
-			-- not a profession bag
-		end
+		local itemID, itemType, itemSubType, itemEquipLoc, icon, itemClassID, itemSubClassID = GetItemInfoInstant(bag_name)
 --[[
-TitanDebug("Bag id: "..tostring(string_id).." '"..tostring(bag_type).."'"
-.." ["..tostring(icolor).."]:"
-.." '"..tostring(string.format("%1.2f",color.r)).."' '"..tostring(string.format("%1.2f",color.g)).."' '"..tostring(string.format("%1.2f",color.b)).."'")
+print("isBag:"
+.." ID:'"..tostring(itemID).."'"
+.." ty:'"..tostring(itemType).."'"
+.." sty:'"..tostring(itemSubType).."'"
+.." EL:'"..tostring(itemEquipLoc).."'"
+.." ic:'"..tostring(icon).."'"
+.." CI:'"..tostring(itemClassID).."'"
+.." SCI:'"..tostring(itemSubClassID).."'"
+)
 --]]
-	end
---]==]
-local function isProfBag(bag_name)
-	-- using straight GetItemInfo caused a 'script ran too long' error on Shadowlands ptr although it worked in retail
-	-- GetItemInfoInstant returns the needed id and uses client files - no server call.
-	local itemID, itemType, itemSubType, itemEquipLoc, icon, itemClassID, itemSubClassID = GetItemInfoInstant(bag_name)
-	local bag_type = false
-	local color = {r=0,g=0,b=0} -- black (should never be used...)
-
-	if itemID == nil or itemId == false then
 		-- For some inexplicable reason the Backpack does not return as an item...
-		--TitanDebug("Bag name not found: '"..tostring(name).."'")
-	else
+		-- so create a default so routine is successful
+		itemID = itemID or 0
+		is_bag = true -- assume non nil is a valid bag name
+
 		if bags[itemID] then
 			bag_type = bags[itemID].style
 			color = bag_info[bag_type].color
+			bag_type = "profession"
 		else
-			-- not a profession bag
+			bag_type = "normal"
 		end
 	end

 --[[
-TitanDebug("isProfBag: "..tostring(itemID).." '"..tostring(bag_type)
+TitanDebug("isBag: "..tostring(itemID)
+.." '"..tostring(bag_name).."'"
+.." '"..tostring(is_bag).."'"
+.." '"..tostring(bag_type).."'"
 .." '"..tostring(string.format("%1.2f",color.r)).."' '"..tostring(string.format("%1.2f",color.g)).."' '"..tostring(string.format("%1.2f",color.b)).."'")
 --]]
-	return bag_type, color
+	return is_bag, bag_type, color
 end

-local function TextTest(id)
+local function GetButtonText(id)
 	local button, id = TitanUtils_GetButton(id, true);
-	local totalBagSlots, usedBagSlots, availableBagSlots, bag, bagText, bagRichText, bagType, color;
+	local totalBagSlots, usedBagSlots, availableBagSlots, bag, bagText, bagRichText, bag_type, color;
 	local totalProfBagSlots = {0,0,0,0,0};
 	local usedProfBagSlots = {0,0,0,0,0};
 	local availableProfBagSlots = {0,0,0,0,0};
 	local bagRichTextProf = {"","","","",""};

+--[[
+TitanDebug("GetButtonText: >>>")
+--]]
 	totalBagSlots = 0;
 	usedBagSlots = 0;
 	for bag = 0, 4 do -- assuming 0 (Backpack) will not be a profession bag
-		local bagType, color = isProfBag(GetBagName(bag))
+		local is_bag, bag_type, color = isBag(GetBagName(bag), bag)
+
+		if is_bag then
+			if bag_type == "profession" then -- found a profession bag
+				-- when user wants profession bags counted, they are listed separately in the plugin
+				if TitanGetVar(TITAN_BAG_ID, "CountProfBagSlots") then
+					local size = GetContainerNumSlots(bag);
+					if (size and size > 0) then
+						totalProfBagSlots[bag] = size;
+						for slot = 1, size do
+							if (GetContainerItemInfo(bag, slot)) then
+								usedProfBagSlots[bag] = usedProfBagSlots[bag] + 1;
+							end
+						end
+						availableProfBagSlots[bag] = totalProfBagSlots[bag] - usedProfBagSlots[bag];
+					end

-		if bagType then -- found a profession bag
-			-- when user wants profession bags counted, they are listed separately in the plugin
-			if TitanGetVar(TITAN_BAG_ID, "CountProfBagSlots") then
+					if (TitanGetVar(TITAN_BAG_ID, "ShowUsedSlots")) then
+						bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], usedProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
+					else
+						bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], availableProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
+					end
+					if ( TitanGetVar(TITAN_BAG_ID, "ShowColoredText") ) then
+						bagRichTextProf[bag] = TitanUtils_GetColoredText(bagText, color);
+					else
+						bagRichTextProf[bag] = TitanUtils_GetHighlightText(bagText);
+					end
+				end
+			elseif bag_type == "normal" then -- not a profession bag so get used & available counts
 				local size = GetContainerNumSlots(bag);
 				if (size and size > 0) then
-					totalProfBagSlots[bag] = size;
+					totalBagSlots = totalBagSlots + size;
 					for slot = 1, size do
 						if (GetContainerItemInfo(bag, slot)) then
-							usedProfBagSlots[bag] = usedProfBagSlots[bag] + 1;
+							usedBagSlots = usedBagSlots + 1;
 						end
 					end
-					availableProfBagSlots[bag] = totalProfBagSlots[bag] - usedProfBagSlots[bag];
-				end
-
-				if (TitanGetVar(TITAN_BAG_ID, "ShowUsedSlots")) then
-					bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], usedProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
-				else
-					bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], availableProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
-				end
-				if ( TitanGetVar(TITAN_BAG_ID, "ShowColoredText") ) then
-					bagRichTextProf[bag] = TitanUtils_GetColoredText(bagText, color);
-				else
-					bagRichTextProf[bag] = TitanUtils_GetHighlightText(bagText);
-				end
-			end
-		else -- not a profession bag so get used & available counts
-			local size = GetContainerNumSlots(bag);
-			if (size and size > 0) then
-				totalBagSlots = totalBagSlots + size;
-				for slot = 1, size do
-					if (GetContainerItemInfo(bag, slot)) then
-						usedBagSlots = usedBagSlots + 1;
-					end
 				end
+			else -- catch all...
 			end
+		else -- no bag in slot
 		end
+--[[
+TitanDebug("GetButtonText:"
+.." "..tostring(bag)
+.." total '"..tostring(totalBagSlots).."'"
+.." used '"..tostring(usedBagSlots).."'"
+.." '"..tostring(is_bag).."'"
+.." '"..tostring(bag_type).."'"
+)
+--]]
 	end
 	availableBagSlots = totalBagSlots - usedBagSlots;

@@ -289,104 +302,14 @@ local function TextTest(id)

 	bagRichText = bagRichText..bagRichTextProf[1]..bagRichTextProf[2]..bagRichTextProf[3]..bagRichTextProf[4]..bagRichTextProf[5];

-	return L["TITAN_BAG_BUTTON_LABEL"], bagRichText;
-end
-
-local function UpdateButton(id)
-	local button, id = TitanUtils_GetButton(id, true);
-	local totalBagSlots, usedBagSlots, availableBagSlots, bag, bagText, bagRichText, color;
-	local totalProfBagSlots = {0,0,0,0,0};
-	local usedProfBagSlots = {0,0,0,0,0};
-	local availableProfBagSlots = {0,0,0,0,0};
-	local bagRichTextProf = {"","","","",""};
-
-	totalBagSlots = 0;
-	usedBagSlots = 0;
-	for bag = 0, 4 do
-
-		local bagType, color = TitanBag_IsProfBag(GetBagName(bag))
-
-		if not TitanBag_IsProfBag(GetBagName(bag)) then
-			local size = GetContainerNumSlots(bag);
-			if (size and size > 0) then
-				totalBagSlots = totalBagSlots + size;
-				for slot = 1, size do
-					if (GetContainerItemInfo(bag, slot)) then
-						usedBagSlots = usedBagSlots + 1;
-					end
-				end
-			end
-		end
-		if TitanGetVar(TITAN_BAG_ID, "CountProfBagSlots") and TitanBag_IsProfBag(GetBagName(bag)) then
-			local size = GetContainerNumSlots(bag);
-			if (size and size > 0) then
-				totalProfBagSlots[bag+1] = size;
-				for slot = 1, size do
-					if (GetContainerItemInfo(bag, slot)) then
-						usedProfBagSlots[bag+1] = usedProfBagSlots[bag+1] + 1;
-					end
-				end
-				availableProfBagSlots[bag+1] = totalProfBagSlots[bag+1] - usedProfBagSlots[bag+1];
-			end
-		end
-	end
-	availableBagSlots = totalBagSlots - usedBagSlots;
-
-	if (TitanGetVar(TITAN_BAG_ID, "ShowUsedSlots")) then
-		bagText = format(L["TITAN_BAG_FORMAT"], usedBagSlots, totalBagSlots);
-	else
-		bagText = format(L["TITAN_BAG_FORMAT"], availableBagSlots, totalBagSlots);
-	end
-
-	if ( TitanGetVar(TITAN_BAG_ID, "ShowColoredText") ) then
-		color = TitanUtils_GetThresholdColor(TITAN_BAG_THRESHOLD_TABLE, usedBagSlots / totalBagSlots);
-		bagRichText = TitanUtils_GetColoredText(bagText, color);
-	else
-		bagRichText = TitanUtils_GetHighlightText(bagText);
-	end
-
-	for bag = 1, 5 do
-		if totalProfBagSlots[bag] > 0 then
-			if (TitanGetVar(TITAN_BAG_ID, "ShowUsedSlots")) then
-				bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], usedProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
-			else
-				bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], availableProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
-			end
-			if ( TitanGetVar(TITAN_BAG_ID, "ShowColoredText") ) then
-				bagType, color = TitanBag_IsProfBag(GetBagName(bag-1));
-				bagRichTextProf[bag] = TitanUtils_GetColoredText(bagText, color);
-			else
-				bagRichTextProf[bag] = TitanUtils_GetHighlightText(bagText);
-			end
-		end
-	end
-	bagRichText = bagRichText..bagRichTextProf[1]..bagRichTextProf[2]..bagRichTextProf[3]..bagRichTextProf[4]..bagRichTextProf[5];
-
+--[[
+TitanDebug("GetButtonText: <<<")
+--]]
 	return L["TITAN_BAG_BUTTON_LABEL"], bagRichText;
 end

 function TitanPanelBagButton_GetButtonText(id)
-
---TitanDebug("Bags ...")
---	local str1, str2 = UpdateButton(id)
---TitanDebug("> '"..tostring(str1).."' '"..tostring(str2).."'")
---TitanDebug("Bags new ...")
-	local strA, strB = TextTest(id)
---TitanDebug("> '"..tostring(strA).."' '"..tostring(strB).."'")
---[[
-	local cmp = ""
-
-	if str1 == strA and str2 == strB then
-		-- old & new are the same
-		cmp = " == "
-	else
-		cmp = " ~= "
-	end
---]]
---	TitanDebug("BagText "..cmp.." '"..tostring(str1).."' '"..tostring(strA).."'")
---	TitanDebug(" >>>>>> ".." '"..tostring(str2).."' '"..tostring(strB).."'")
-
-
+	local strA, strB = GetButtonText(id)
 	return strA, strB
 end

@@ -537,80 +460,3 @@ end
 function TitanPanelBagButton_ShowDetailedInfo()
 	TitanToggleVar(TITAN_BAG_ID, "ShowDetailedInfo");
 end
-
--- **************************************************************************
--- NAME : TitanBag_IsProfBag(name)
--- DESC : est to see if bag is a profession bag
--- VARS : name = item name
--- OUT  : bagType = type of profession matching bag name
---        color = the color associated with the profession
--- **************************************************************************
-function TitanBag_IsProfBag(name)
-	local bagType, color;
-	if (name) then
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_ENCHANTING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "ENCHANTING";
-				color = {r=0,g=0,b=1}; -- BLUE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_ENGINEERING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "ENGINEERING";
-				color = {r=1,g=0.49,b=0.04}; -- ORANGE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_HERBALISM"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "HERBALISM";
-				color = {r=0,g=1,b=0}; -- GREEN
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_INSCRIPTION"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "INSCRIPTION";
-				color = {r=0.58,g=0.51,b=0.79}; -- PURPLE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_JEWELCRAFTING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "JEWELCRAFTING";
-				color = {r=1,g=0,b=0}; -- RED
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_LEATHERWORKING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "LEATHERWORKING";
-				color = {r=0.78,g=0.61,b=0.43}; -- TAN
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_MINING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "MINING";
-				color = {r=1,g=1,b=1}; -- WHITE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_FISHING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "FISHING";
-				color = {r=0.41,g=0.8,b=0.94}; -- LIGHT_BLUE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_COOKING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "COOKING";
-				color = {r=0.96,g=0.55,b=0.73}; -- PINK
-				return bagType, color;
-			end
-		end
-	end
-	return false;
-end
\ No newline at end of file