Quantcast

Add new script condition HasEquippedItem(itemId).

Johnny C. Lam [10-15-13 - 01:26]
Add new script condition HasEquippedItem(itemId).

This is a generic condition that can test whether a particular itemId is
currently equipped.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1060 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleCondition.lua
OvaleEquipement.lua
diff --git a/OvaleCondition.lua b/OvaleCondition.lua
index c94eae6..4822348 100644
--- a/OvaleCondition.lua
+++ b/OvaleCondition.lua
@@ -1706,6 +1706,52 @@ OvaleCondition.conditions.glyph = function(condition)
 	return TestBoolean(OvaleSpellBook:IsActiveGlyph(condition[1]), condition[2])
 end

+--- Test if the player has a particular item equipped.
+-- @name HasEquippedItem
+-- @paramsig boolean
+-- @param item Item to be checked whether it is equipped.
+-- @param yesno Optional. If yes, then return true if the item is equipped. If no, then return true if it isn't equipped.
+--     Default is yes.
+--     Valid values: yes, no.
+-- @param ilevel Optional.  Checks the item level of the equipped item.  If not specified, then any item level is valid.
+--     Defaults to not specified.
+--     Valid values: ilevel=N, where N is any number.
+-- @param slot Optional. Sets the inventory slot to check.  If not specified, then all slots are checked.
+--     Defaults to not specified.
+--     Valid values: slot=SLOTNAME, where SLOTNAME is a valid slot name, e.g., HandSlot.
+
+OvaleCondition.conditions.hasequippeditem = function(condition)
+	local itemId, yesno = condition[1], condition[2]
+	local ilevel, slot = condition.ilevel, condition.slot
+	local slotId
+	if type(itemId) == "number" then
+		slotId = OvaleEquipement:HasEquippedItem(itemId, slot)
+		if slotId then
+			if ilevel then
+				if ilevel == OvaleEquipement:GetEquippedItemLevel(slotId) then
+					return TestBoolean(true, yesno)
+				end
+			else
+				return TestBoolean(true, yesno)
+			end
+		end
+	elseif OvaleData.itemList[itemId] then
+		for _, v in pairs(OvaleData.itemList[itemId]) do
+			slotId = OvaleEquipement:HasEquippedItem(v, slot)
+			if slotId then
+				if ilevel then
+					if ilevel == OvaleEquipement:GetEquippedItemLevel(slotId) then
+						return TestBoolean(true, yesno)
+					end
+				else
+					return TestBoolean(true, yesno)
+				end
+			end
+		end
+	end
+	return TestBoolean(false, yesno)
+end
+
 --- Test if the player has full control, i.e., isn't feared, charmed, etc.
 -- @name HasFullControl
 -- @paramsig boolean
diff --git a/OvaleEquipement.lua b/OvaleEquipement.lua
index a79a42a..07e7856 100644
--- a/OvaleEquipement.lua
+++ b/OvaleEquipement.lua
@@ -1391,6 +1391,25 @@ function OvaleEquipement:GetEquippedItemLevel(slotId)
 	return self_equippedItemLevels[slotId]
 end

+function OvaleEquipement:HasEquippedItem(itemId, slotId)
+	if slotId and type(slotId) ~= "number" then
+		if not OVALE_SLOTNAME[slotId] then return nil end
+		slotId = API_GetInventorySlotInfo(slotId)
+	end
+	if slotId then
+		if self_equippedItems[slotId] == itemId then
+			return slotId
+		end
+	else
+		for slotId, equippedItemId in pairs(self_equippedItems) do
+			if equippedItemId == itemId then
+				return slotId
+			end
+		end
+	end
+	return nil
+end
+
 function OvaleEquipement:HasMainHandWeapon()
 	return self_mainHandItemType == "INVTYPE_WEAPON"
 		or self_mainHandItemType == "INVTYPE_WEAPONMAINHAND"
@@ -1407,8 +1426,8 @@ function OvaleEquipement:HasShield()
 end

 function OvaleEquipement:HasTrinket(itemId)
-	return self:GetEquippedItem(INVSLOT_TRINKET1) == itemId
-		or self:GetEquippedItem(INVSLOT_TRINKET2) == itemId
+	return self:HasEquippedItem(itemId, INVSLOT_TRINKET1)
+		or self:HasEquippedItem(itemId, INVSLOT_TRINKET2)
 end

 function OvaleEquipement:HasTwoHandedWeapon()