From 1d644155f081f8fb671e6115386e2624907d9bee Mon Sep 17 00:00:00 2001
From: Tharre <tharre3@gmail.com>
Date: Sun, 9 Jun 2024 21:26:06 +0200
Subject: [PATCH] Fix mage talent exports being broken

There's a "talent" at index 21 that doesn't exist, but GetNumTalents()
considers there to be 22 talents, because there's a valid index after it
at 22.

Fixes #17.
---
 ExportStructures/Misc.lua |  1 +
 extras.lua                | 20 ++++++++++++++------
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/ExportStructures/Misc.lua b/ExportStructures/Misc.lua
index 4c13495..a303e0e 100644
--- a/ExportStructures/Misc.lua
+++ b/ExportStructures/Misc.lua
@@ -55,6 +55,7 @@ end
 ---@return string
 function Env.CreateTalentString()
     local GetTalentRank = Env.GetTalentRankOrdered
+    local GetNumTalents = Env.GetNumTalentsFixed
     local tabs = {}
     for tab = 1, GetNumTalentTabs() do
         local talents = {}
diff --git a/extras.lua b/extras.lua
index 9419896..6125b5b 100644
--- a/extras.lua
+++ b/extras.lua
@@ -14,10 +14,12 @@ do
             temp[tab] = {}
             local products = {}
             for i = 1, GetNumTalents(tab) do
-                local name, _, tier, column = GetTalentInfo(tab, i)
-                local product = (tier - 1) * 4 + column
-                temp[tab][product] = i
-                table.insert(products, product)
+                local name, _, tier, column, _, _, _, avail, _ = GetTalentInfo(tab, i)
+                if avail then -- sometimes indices are empty ...
+                    local product = (tier - 1) * 4 + column
+                    temp[tab][product] = i
+                    table.insert(products, product)
+                end
             end
 
             table.sort(products)
@@ -25,8 +27,10 @@ do
             orderedTalentCache[tab] = {}
             local j = 1
             for _, product in ipairs(products) do
-                orderedTalentCache[tab][j] = temp[tab][product]
-                j = j + 1
+                if product then
+                    orderedTalentCache[tab][j] = temp[tab][product]
+                    j = j + 1
+                end
             end
         end
         f:UnregisterEvent("SPELLS_CHANGED")
@@ -41,6 +45,10 @@ function Env.GetTalentRankOrdered(tab, num)
     return select(5, GetTalentInfo(tab, orderedTalentCache[tab][num]))
 end
 
+function Env.GetNumTalentsFixed(tab)
+    return #orderedTalentCache[tab]
+end
+
 -- table extension contains
 function table.contains(table, element)
     for _, value in pairs(table) do