Quantcast

Clean up Ammo code

urnati [08-09-19 - 16:39]
Clean up Ammo code
Filename
TitanClassicAmmo/TitanClassicAmmo.lua
diff --git a/TitanClassicAmmo/TitanClassicAmmo.lua b/TitanClassicAmmo/TitanClassicAmmo.lua
index d64118b..86fef0e 100644
--- a/TitanClassicAmmo/TitanClassicAmmo.lua
+++ b/TitanClassicAmmo/TitanClassicAmmo.lua
@@ -1,10 +1,18 @@
 -- **************************************************************************
 -- * TitanAmmo.lua
 -- *
--- * By: TitanMod, Dark Imakuni, Adsertor and the Titan Development Team
--- *     (HonorGoG, jaketodd422, joejanko, Lothayer, Tristanian)
+-- * By: Titan Development Team
 -- **************************************************************************
-
+-- 2019 Aug - reverted and updated for Classic
+--
+-- This will track the number of ammo (bows and guns) or thrown (knives) the character
+-- has.
+-- Ammo is placed in the 'ammo' slot where Blizzard counts ALL of that *type of ammo*
+-- regardless of where it is in your bags.
+-- Thrown is placed in the actual weapon slot where Blizzard counts ALL of that *type of thrown*
+-- as being in that slot.
+-- The difference forces a different routine to be used. This causes the routine to always check
+-- which is being used.
 -- ******************************** Constants *******************************
 local _G = getfenv(0);
 local TITAN_AMMO_ID = "Ammo";
@@ -30,142 +38,92 @@ local L = LibStub("AceLocale-3.0"):GetLocale("Titan", true)
 -- DESC : Registers the plugin upon it loading
 -- **************************************************************************
 function TitanPanelAmmoButton_OnLoad(self)
-     self.registry = {
-          id = TITAN_AMMO_ID,
---          builtIn = 1,
-		category = "Built-ins",
-          version = TITAN_VERSION,
-          menuText = L["TITAN_AMMO_MENU_TEXT"],
-          buttonTextFunction = "TitanPanelAmmoButton_GetButtonText",
-          tooltipTitle = L["TITAN_AMMO_TOOLTIP"],
-          icon = "Interface\\AddOns\\TitanAmmo\\TitanThrown",
-          iconWidth = 16,
-		controlVariables = {
+	self.registry = {
+			id = TITAN_AMMO_ID,
+			--builtIn = 1,
+			category = "Built-ins",
+			version = TITAN_VERSION,
+			menuText = L["TITAN_AMMO_MENU_TEXT"],
+			buttonTextFunction = "TitanPanelAmmoButton_GetButtonText",
+			tooltipTitle = L["TITAN_AMMO_TOOLTIP"],
+			icon = "Interface\\AddOns\\TitanAmmo\\TitanThrown",
+			iconWidth = 16,
+			controlVariables = {
 			ShowIcon = true,
 			ShowLabelText = true,
 			ShowRegularText = false,
 			ShowColoredText = true,
 			DisplayOnRightSide = false
 		},
-          savedVariables = {
-               ShowIcon = 1,
-               ShowLabelText = 1,
-               ShowColoredText = 1,
-               ShowAmmoName = false,
-          }
-     };
---TitanDebug("TitanPanelAmmoButton_OnLoad")
+		savedVariables = {
+			ShowIcon = 1,
+			ShowLabelText = 1,
+			ShowColoredText = 1,
+			ShowAmmoName = false,
+		}
+	};

-self:SetScript("OnEvent",  function(_, event, arg1, ...)
-
---TitanDebug("OnEvent")
+	self:SetScript("OnEvent",  function(_, event, arg1, ...)
 		if event == "PLAYER_LOGIN" then
---TitanDebug("PLAYER_LOGIN")
 			TitanPanelAmmoButton_PLAYER_LOGIN()
 		elseif event == "UNIT_INVENTORY_CHANGED" then
---TitanDebug("UNIT_INVENTORY_CHANGED")
 			TitanPanelAmmoButton_UNIT_INVENTORY_CHANGED(arg1, ...)
 		elseif event == "UPDATE_INVENTORY_DURABILITY" then
---TitanDebug("UPDATE_INVENTORY_DURABILITY")
 			TitanPanelAmmoButton_UPDATE_INVENTORY_DURABILITY()
 		elseif event == "MERCHANT_CLOSED" or event == "PLAYER_ENTERING_WORLD" then
---TitanDebug("MERCHANT_CLOSED / PLAYER_ENTERING_WORLD")
 			TitanPanelAmmoButton_MERCHANT_CLOSED()
 		elseif event == "ACTIONBAR_HIDEGRID" then
 			TitanPanelAmmoButton_ACTIONBAR_HIDEGRID()
 		end
-
-end)
+	end)
+
 	TitanPanelAmmoButton:RegisterEvent("PLAYER_LOGIN")
---[[
-	TitanPanelAmmoButton:RegisterEvent("UNIT_INVENTORY_CHANGED")
-	TitanPanelAmmoButton:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
-	TitanPanelAmmoButton:RegisterEvent("MERCHANT_CLOSED")
-	TitanPanelAmmoButton:RegisterEvent("PLAYER_ENTERING_WORLD")
-	TitanPanelAmmoButton:RegisterEvent("ACTIONBAR_HIDEGRID")
---]]
-end
---[[
-function TitanClassicAmmo_OnEvent(self, event, ...)
-	if (event == "PLAYER_LOGIN") then
-			TitanPanelAmmoButton_PLAYER_LOGIN()
-		return;
-	end
-	if (event == "UNIT_INVENTORY_CHANGED") then
-			TitanPanelAmmoButton_UNIT_INVENTORY_CHANGED()
-		return;
-	end
-	if (event == "UPDATE_INVENTORY_DURABILITY") then
-			TitanPanelAmmoButton_UPDATE_INVENTORY_DURABILITY()
-		return;
-	end
-	if (event == "MERCHANT_CLOSED" or event == "PLAYER_ENTERING_WORLD") then
-			TitanPanelAmmoButton_MERCHANT_CLOSED()
-		return;
-	end
-	if (event == "ACTIONBAR_HIDEGRID") then
-			TitanPanelAmmoButton_ACTIONBAR_HIDEGRID()
-		return;
-	end
 end
---]]
+
 function TitanPanelAmmoButton_PLAYER_LOGIN()
--- Class check
-if class ~= "ROGUE" and class ~= "WARRIOR" and class ~= "HUNTER" then
-	TitanPanelAmmoButton_PLAYER_LOGIN = nil
-	return
-end
+	-- Class check
+	if class ~= "ROGUE" and class ~= "WARRIOR" and class ~= "HUNTER" then
+		TitanPanelAmmoButton_PLAYER_LOGIN = nil
+		return
+	end

 --TitanDebug("TitanPanelAmmoButton_PLAYER_LOGIN")
 	local itemlink = GetInventoryItemLink("player", rangedSlotID)
 	currentlink = itemlink;
 	local loc = "";
-		if itemlink then
-			loc = select(9, GetItemInfo(itemlink))
-		end
-		if loc == "INVTYPE_THROWN" then
-			TitanPanelAmmoButton:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
-			isThrown = true;
-			isAmmo = nil;
-		else
-			TitanPanelAmmoButton:RegisterEvent("ACTIONBAR_HIDEGRID")
-			isAmmo = true;
-			isThrown = nil;
-		end
-		TitanPanelAmmoButton:RegisterEvent("UNIT_INVENTORY_CHANGED")
-		TitanPanelAmmoButton:RegisterEvent("MERCHANT_CLOSED")
-		TitanPanelAmmoButton:RegisterEvent("PLAYER_ENTERING_WORLD")
-		TitanPanelAmmoButton_PLAYER_LOGIN = nil
+	if itemlink then
+		loc = select(9, GetItemInfo(itemlink))
+	end
+	if loc == "INVTYPE_THROWN" then
+		TitanPanelAmmoButton:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
+		isThrown = true;
+		isAmmo = nil;
+	else
+		TitanPanelAmmoButton:RegisterEvent("ACTIONBAR_HIDEGRID")
+		isAmmo = true;
+		isThrown = nil;
+	end
+	TitanPanelAmmoButton:RegisterEvent("UNIT_INVENTORY_CHANGED")
+	TitanPanelAmmoButton:RegisterEvent("MERCHANT_CLOSED")
+	TitanPanelAmmoButton:RegisterEvent("PLAYER_ENTERING_WORLD")
+	TitanPanelAmmoButton_PLAYER_LOGIN = nil
 end


 function TitanPanelAmmoButton_UNIT_INVENTORY_CHANGED(arg1, ...)
  if arg1 == "player" then
---TitanDebug("TitanPanelAmmoButton_UNIT_INVENTORY_CHANGED")
  	TitanPanelAmmoUpdateDisplay();
- if isThrown then
-	count = GetInventoryItemCount("player", rangedSlotID) or count
- elseif isAmmo then --and GetInventoryItemLink("player", ammoSlotID)
-	count = GetInventoryItemCount("player", ammoSlotID) or count
-	-- AmmoName = GetItemInfo(GetInventoryItemLink("player", ammoSlotID)) or _G["UNKNOWN"]
- else
- --isThrown = nil;
- 	count = 0;
- 	AmmoName = "";
- end
- 		TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
--- 	if isAmmo then
---	TitanDebug("ammoSlotID: " .. ammoSlotID);
---	TitanDebug("GetInventoryItemLink: " .. tostring(GetInventoryItemLink("player", ammoSlotID)));
---  		if GetInventoryItemLink("player", ammoSlotID) then
---				count = GetInventoryItemCount("player", ammoSlotID) or count
---				AmmoName = GetItemInfo(GetInventoryItemLink("player", ammoSlotID)) or _G["UNKNOWN"]
---			else
---				count = 0;
---				AmmoName = "";
---			end
--- 		TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
---	end
+	if isThrown then
+		count = GetInventoryItemCount("player", rangedSlotID) or count
+	elseif isAmmo then --and GetInventoryItemLink("player", ammoSlotID)
+		count = GetInventoryItemCount("player", ammoSlotID) or count
+		-- AmmoName = GetItemInfo(GetInventoryItemLink("player", ammoSlotID)) or _G["UNKNOWN"]
+	else
+		--isThrown = nil;
+		count = 0;
+		AmmoName = "";
+	end
+	TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
  end
 end

@@ -175,17 +133,15 @@ function TitanPanelAmmoButton_UPDATE_INVENTORY_DURABILITY()
 end

 function TitanPanelAmmoButton_MERCHANT_CLOSED()
- if isThrown then
-	count = GetInventoryItemCount("player", rangedSlotID) or count
--- 	count = GetInventoryItemDurability(rangedSlotID) or count
- elseif isAmmo then --and GetInventoryItemLink("player", ammoSlotID)
-	count = GetInventoryItemCount("player", ammoSlotID) or count
-	-- AmmoName = GetItemInfo(GetInventoryItemLink("player", ammoSlotID)) or _G["UNKNOWN"]
- else
- --isThrown = nil;
- 	count = 0;
- 	AmmoName = "";
- end
+	if isThrown then
+		count = GetInventoryItemCount("player", rangedSlotID) or count
+	elseif isAmmo then --and GetInventoryItemLink("player", ammoSlotID)
+		count = GetInventoryItemCount("player", ammoSlotID) or count
+		-- AmmoName = GetItemInfo(GetInventoryItemLink("player", ammoSlotID)) or _G["UNKNOWN"]
+	else
+		count = 0;
+		AmmoName = "";
+	end
 	TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
 end

@@ -196,11 +152,7 @@ function TitanPanelAmmoButton_ACTIONBAR_HIDEGRID()
 		prev = prev + e
 		if prev > 2 then
 			TitanPanelAmmoButton:SetScript("OnUpdate", nil)
---			if GetInventoryItemLink("player", ammoSlotID) then
-				count = GetInventoryItemCount("player", ammoSlotID) or count
---			else
---				count = 0;
---			end
+			count = GetInventoryItemCount("player", ammoSlotID) or count
 			TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
 		end
 	end)
@@ -208,56 +160,51 @@ end


 function TitanPanelAmmoUpdateDisplay()
--- Manual Display update in case the rangedSlot it switched
-local itemlink = GetInventoryItemLink("player", rangedSlotID)
-local loc = "";
+	-- Manual Display update in case the rangedSlot it switched
+	local itemlink = GetInventoryItemLink("player", rangedSlotID)
+	local loc = "";

-if itemlink == currentlink then
-	return
-else
-	currentlink = itemlink
-end
+	if itemlink == currentlink then
+		return
+	else
+		currentlink = itemlink
+	end

-		if itemlink then
-			loc = select(9, GetItemInfo(itemlink))
-		end
-		if loc == "INVTYPE_THROWN" then
+	if itemlink then
+		loc = select(9, GetItemInfo(itemlink))
+	end
+	if loc == "INVTYPE_THROWN" then
+
+	  if TitanPanelAmmoButton:IsEventRegistered("ACTIONBAR_HIDEGRID") then
+		TitanPanelAmmoButton:UnregisterEvent("ACTIONBAR_HIDEGRID")
+		TitanPanelAmmoButton:SetScript("OnUpdate", nil)
+	  end
+
+	  if not TitanPanelAmmoButton:IsEventRegistered("UPDATE_INVENTORY_DURABILITY") then
+			TitanPanelAmmoButton:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
+		end

-		  if TitanPanelAmmoButton:IsEventRegistered("ACTIONBAR_HIDEGRID") then
-		  	TitanPanelAmmoButton:UnregisterEvent("ACTIONBAR_HIDEGRID")
-		  	TitanPanelAmmoButton:SetScript("OnUpdate", nil)
-		  end
-
-		  if not TitanPanelAmmoButton:IsEventRegistered("UPDATE_INVENTORY_DURABILITY") then
-				TitanPanelAmmoButton:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
-			end
-
-			isThrown = true;
-			isAmmo = nil;
-			count = GetInventoryItemCount("player", rangedSlotID) or count
+		isThrown = true;
+		isAmmo = nil;
+		count = GetInventoryItemCount("player", rangedSlotID) or count
 --			count = GetInventoryItemDurability(rangedSlotID);
-
-		else
+
+	else
+
+		if TitanPanelAmmoButton:IsEventRegistered("UPDATE_INVENTORY_DURABILITY") then
+			TitanPanelAmmoButton:UnregisterEvent("UPDATE_INVENTORY_DURABILITY")
+		end

-			if TitanPanelAmmoButton:IsEventRegistered("UPDATE_INVENTORY_DURABILITY") then
-				TitanPanelAmmoButton:UnregisterEvent("UPDATE_INVENTORY_DURABILITY")
-			end
+		if not TitanPanelAmmoButton:IsEventRegistered("ACTIONBAR_HIDEGRID") then
+			TitanPanelAmmoButton:RegisterEvent("ACTIONBAR_HIDEGRID")
+		end

-			if not TitanPanelAmmoButton:IsEventRegistered("ACTIONBAR_HIDEGRID") then
-				TitanPanelAmmoButton:RegisterEvent("ACTIONBAR_HIDEGRID")
-			end
-
-			isAmmo = true;
-			isThrown = nil;
---			if GetInventoryItemLink("player", ammoSlotID) then
-				count = GetInventoryItemCount("player", ammoSlotID)
---			else
---				count = 0;
---			end
-
-		end
+		isAmmo = true;
+		isThrown = nil;
+		count = GetInventoryItemCount("player", ammoSlotID)
+	end

-TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
+	TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
 end


@@ -304,20 +251,19 @@ end
 -- DESC : Display rightclick menu options
 -- **************************************************************************
 function TitanPanelRightClickMenu_PrepareAmmoMenu()
-		 local info = {};
-     TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_AMMO_ID].menuText);
-     TitanPanelRightClickMenu_AddToggleIcon(TITAN_AMMO_ID);
-     TitanPanelRightClickMenu_AddToggleLabelText(TITAN_AMMO_ID);
-     TitanPanelRightClickMenu_AddToggleColoredText(TITAN_AMMO_ID);
-
-     info.text = L["TITAN_AMMO_BULLET_NAME"];
-     info.func = function() TitanPanelRightClickMenu_ToggleVar({TITAN_AMMO_ID, "ShowAmmoName"})
-     TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
-     end
-     info.checked = TitanUtils_Ternary(TitanGetVar(TITAN_AMMO_ID, "ShowAmmoName"), 1, nil);
-     L_UIDropDownMenu_AddButton(info);
-
-
-     TitanPanelRightClickMenu_AddSpacer();
-     TitanPanelRightClickMenu_AddCommand(L["TITAN_PANEL_MENU_HIDE"], TITAN_AMMO_ID, TITAN_PANEL_MENU_FUNC_HIDE);
+	local info = {};
+	TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_AMMO_ID].menuText);
+	TitanPanelRightClickMenu_AddToggleIcon(TITAN_AMMO_ID);
+	TitanPanelRightClickMenu_AddToggleLabelText(TITAN_AMMO_ID);
+	TitanPanelRightClickMenu_AddToggleColoredText(TITAN_AMMO_ID);
+
+	info.text = L["TITAN_AMMO_BULLET_NAME"];
+	info.func = function() TitanPanelRightClickMenu_ToggleVar({TITAN_AMMO_ID, "ShowAmmoName"})
+		TitanPanelButton_UpdateButton(TITAN_AMMO_ID);
+	end
+	info.checked = TitanUtils_Ternary(TitanGetVar(TITAN_AMMO_ID, "ShowAmmoName"), 1, nil);
+	L_UIDropDownMenu_AddButton(info);
+
+	TitanPanelRightClickMenu_AddSpacer();
+	TitanPanelRightClickMenu_AddCommand(L["TITAN_PANEL_MENU_HIDE"], TITAN_AMMO_ID, TITAN_PANEL_MENU_FUNC_HIDE);
 end
\ No newline at end of file