Hey, I am quite new to programming and especially Lua.
I am not sure if this kind of topic is allowed on this forum, or if I've posted in the wrong category, if so, please remove.
I've made myself an addon that tries to detect what equipment set you are wearing and tells you if you're, for example, accidentally wearing you're PvP gear in a PvE instance. I was inspired to write this, since I use the same transmogrification sets for both my PvP and PvE gear and often noticed too late that I was wearing the wrong gear.
I realize that detecting what equipment set you are wearing, is difficult, because you might not have every itemslot filled within an equipment set. So, this will only work if you are using full equipment sets. (Shirt and tabard are ignored.)
Now, I have everything working, but I am just wondering if there's things that I might have missed and should improve on.
TLDR: Not really a question, but a request for critique on the following code.
Lua Code:
-- Configuration
local PvPSet = "enha_pvp"
local PvESet = "enha"
local msg = "I think you might have the wrong gear equipped... :("
local point = "CENTER"
local ofsx = 0
local ofsy = 100
local msgfont = "GameFontHighlight"
-- End of configuration
local frame = CreateFrame("Frame")
-- Register events
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
frame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
frame:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
-- Create the frame
MyAddon_eqframe = CreateFrame("Frame",nil,UIParent)
MyAddon_eqframe:SetFrameStrata("BACKGROUND")
MyAddon_eqframe:SetWidth(1)
MyAddon_eqframe:SetHeight(1)
MyAddon_eqframe:SetPoint(point,ofsx,ofsy)
-- Create the fontstring
local fs = MyAddon_eqframe:CreateFontString(nil, "OVERLAY", msgfont)
fs:SetPoint("CENTER")
fs:SetText(msg)
-- Check if the EquipSet and current equipment is the same
function MyAddon_isGearEqSet(equipset)
-- Put the items from an EquipSet in a table
if equipset then
local itemArray = GetEquipmentSetItemIDs(equipset)
local itemArrayEqSet = {}
for i=1,19 do
if itemArray[i] then
if i ~= 4 and i ~= 18 and i ~= 19 then -- Ignore shirt, tabard and ranged slot (4 = Shirt, 18 = Ranged and 19 = Tabard)
table.insert(itemArrayEqSet,i,itemArray[i])
end
end
end
-- Put your character's equipment in a table
local itemArrayPlayerEq = {}
for i=1,19 do
if i ~= 4 and i ~= 18 and i ~= 19 then -- Ignore shirt, tabard and ranged slot (4 = Shirt, 18 = Ranged and 19 = Tabard)
table.insert(itemArrayPlayerEq,i,GetInventoryItemID("player", i))
end
end
-- Compare the
if #itemArrayEqSet ~= #itemArrayPlayerEq then
return false
end
for i=1,#itemArrayEqSet do
if itemArrayEqSet[i] ~= itemArrayPlayerEq[i] then
return false
end
end
return true
end
end
-- Look for what EquipSet you are wearing
function MyAddon_getGearEqSet()
local count = GetNumEquipmentSets()
for i = 1,count do
local eqSetName = GetEquipmentSetInfo(i)
if MyAddon_isGearEqSet(eqSetName) then
return eqSetName
end
end
end
frame:SetScript("OnEvent", function(self, event, ...)
fs:Hide()
-- Figure out what type of zone you're in
local _, zoneType = IsInInstance();
local pvpType = GetZonePVPInfo();
if zoneType == "arena" or zoneType == "pvp" or pvpType == "combat" then
if MyAddon_getGearEqSet() == PvPSet then
fs:Hide()
else
fs:Show()
end
-- You're in a PvP area, do things here
elseif zoneType == "party" or zoneType == "raid" then
if MyAddon_getGearEqSet() == PvESet then
fs:Hide()
else
fs:Show()
end
-- You're in a party or raid, do things here
elseif zoneType == "none" then
-- You're not in an instance, do things here
else
-- You're in some other zone, do things here
end
end)
Thanks!