settings added & error fixes

esorochinskiy [05-30-22 - 19:04]
settings added & error fixes
Filename
UI.lua
WellEater.lua
WellEater.txt
lang/en.lua
lang/ru.lua
diff --git a/UI.lua b/UI.lua
new file mode 100644
index 0000000..90da634
--- /dev/null
+++ b/UI.lua
@@ -0,0 +1,116 @@
+WellEater = WellEater or {}
+
+function WellEater:InitSettingsMenu()
+
+    local L = self:getLocale()
+    local LAM = LibAddonMenu2
+    local optionsTable = {}
+    local index = 0
+
+    local function MakeControlEntry(data, category, key)
+
+        if (category and key) then
+            -- for the majority of the settings
+            data.category = category
+            data.key      = key
+
+            -- build simple table with zero-based values for choices
+            if data.choices and not data.choicesValues then
+                data.choicesValues = {}
+                for i=1, #data.choices do
+                    tinsert(data.choicesValues, i-1)
+                end
+            end
+
+            -- setup default value
+            if not data.default then
+                local default = self:getUserDefault(key)
+                data.default = default
+            end
+
+            if not data.noAlert then
+                index = index + 1
+                data.reference = "WESettingCtrl"..index
+            end
+
+            -- add get/set functions if they were not provided
+            if not data.getFunc then
+                data.getFunc = function()
+                    return self:getUserPreference(data.key)
+                end
+            end
+            if not data.setFunc then
+                data.setFunc = function(value)
+                    self:setUserPreference(data.key, value)
+                end
+            end
+
+        end
+
+        -- add to appropriate table
+        table.insert(optionsTable, data)
+
+    end
+
+
+    self.panelData = {
+        type = "panel",
+        name = self:getDisplayName(),
+        author = self:getAuthor(),
+        version = self:getVersion(),
+        registerForRefresh = true,
+        registerForDefaults = true,
+    }
+
+    MakeControlEntry({
+        type = "description",
+        text = L.generalSetupDescription,
+    })
+
+    MakeControlEntry({
+        type = "header",
+        name = L.generalSetupHeader,
+    })
+
+    MakeControlEntry({
+        type = "dropdown",
+        name = L.foodQualityMinHeader,
+        tooltip = L.foodQualityMinHeader,
+        choices = {
+            L.foodGreen,
+            L.foodBlue,
+            L.foodCyan,
+            L.foodGold
+        }, choicesValues = {
+            ITEM_QUALITY_MAGIC,
+            ITEM_QUALITY_ARCANE,
+            ITEM_QUALITY_ARTIFACT,
+            ITEM_QUALITY_LEGENDARY
+        },
+        noAlert = true,
+    }, "general", "minQuality")
+
+    MakeControlEntry({
+        type = "dropdown",
+        name = L.foodQualityMaxHeader,
+        tooltip = L.foodQualityMaxHeader,
+        choices = {
+            L.foodGreen,
+            L.foodBlue,
+            L.foodCyan,
+            L.foodGold
+        }, choicesValues = {
+            ITEM_QUALITY_MAGIC,
+            ITEM_QUALITY_ARCANE,
+            ITEM_QUALITY_ARTIFACT,
+            ITEM_QUALITY_LEGENDARY
+        },
+        noAlert = true,
+    }, "general", "maxQuality")
+
+    self.optionsData = optionsTable
+
+    local myLAMAddonPanel = LAM:RegisterAddonPanel(self:getAddonName() .. "_Settings_Panel", self.panelData)
+    LAM:RegisterOptionControls(self:getAddonName() .. "_Settings_Panel", self.optionsData)
+    return myLAMAddonPanel
+end
\ No newline at end of file
diff --git a/WellEater.lua b/WellEater.lua
index fc4f65a..79eabeb 100644
--- a/WellEater.lua
+++ b/WellEater.lua
@@ -1,8 +1,25 @@
 WellEater = WellEater or {}
 WellEater.WELLEATER_SAVED_VERSION = 1
 WellEater.AddonName = "WellEater"
+WellEater.DisplayName = "|cFFFFFFWell |c0099FFEater|r"
 WellEater.Version = "1.0.0"
-WellEater.Author = "esorochinskiy"
+WellEater.Author = "|c5EFFF5esorochinskiy|r"
+local NAMESPACE = {}
+NAMESPACE.settingsDefaults = {
+    enabled = true,
+    updateTime = 2000,
+    maxQuality = ITEM_QUALITY_ARCANE,
+    minQuality = ITEM_QUALITY_ARCANE
+}
+
+function WellEater:getAddonName()
+    return self.AddonName
+end
+
+function WellEater:getDisplayName()
+    return self.DisplayName
+end
+

 function WellEater:getVersion()
     return self.Version
@@ -16,6 +33,10 @@ function WellEater:getUserPreference(setting)
     return self.settingsUser and self.settingsUser[setting]
 end

+function WellEater:getUserDefault(setting)
+    return NAMESPACE.settingsDefaults and NAMESPACE.settingsDefaults[setting]
+end
+
 function WellEater:setUserPreference(setting, value)
     self.settingsUser = self.settingsUser or {}
     self.settingsUser[setting] = value
@@ -33,17 +54,11 @@ function WellEater:isAddonEnabled()
 end

 function WellEater:prepareToAnalize()
-    return self:isAddonEnabled() and not IsUnitInCombat("player") and not IsUnitSwimming("player")
+    return self:isAddonEnabled() and not IsUnitInCombat("player")
+            and not IsUnitSwimming("player") and not IsUnitDead("player")
 end
 -- local functions

-local NAMESPACE = {}
-NAMESPACE.settingsDefaults = {
-    enabled = true,
-    updateTime = 2000,
-    maxQuality = ITEM_QUALITY_ARCANE,
-    minQuality = ITEM_QUALITY_ARCANE
-}
 -- Raid Notifier algorithm. Thanx memus
 NAMESPACE.blackList = {
     [43752] = true, -- Soul Summons / Seelenbeschwörung
@@ -61,7 +76,7 @@ NAMESPACE.blackList = {
     [91369] = true, -- erhöhter Erfahrungsgewinn der Narrenpastete
 }

-local function GetActiveFoodBuff(abilityId)
+local function getActiveFoodBuff(abilityId)
     if NAMESPACE.blackList[abilityId] then
         return false
     end
@@ -76,13 +91,19 @@ local function GetActiveFoodBuff(abilityId)
         local cost, mechanic = GetAbilityCost(abilityId)
         local channeled, castTime = GetAbilityCastInfo(abilityId)
         local minRangeCM, maxRangeCM = GetAbilityRange(abilityId)
-        if cost > 0 or mechanic > 0 or channeled or castTime > 0 or minRangeCM > 0 or maxRangeCM > 0 or GetAbilityDescription(abilityId) == "" then
-            return false
-        end
-        return true
+        local abilityDescription = GetAbilityDescription(abilityId)
+        return not (cost > 0 or mechanic > 0 or channeled or castTime > 0 or
+                minRangeCM > 0 or maxRangeCM > 0 or abilityDescription == "")
     end
 end

+local function tryToUseItem(bagId, slotId)
+    if IsProtectedFunction("UseItem") then
+        CallSecureProtected("UseItem", bagId, slotId)
+    else
+        UseItem(bagId, slotId)
+    end
+end

 local function processAutoEat()
     if not WellEater:prepareToAnalize() then
@@ -123,12 +144,7 @@ local function processAutoEat()
                             d("Description = " .. abilityDescription)
                         end

-
-                        if IsProtectedFunction("UseItem") then
-                            CallSecureProtected("UseItem", bagId, slotId)
-                        else
-                            UseItem(bagId, slotId)
-                        end
+                        tryToUseItem(bagId, slotId)

                         break
                     end
@@ -151,7 +167,7 @@ local function TimersUpdate()
     for i = 1, numBuffs do
         local timeEnding, abilityId, canClickOff
         _, _, timeEnding, _, _, _, _, _, _, _, abilityId, canClickOff = GetUnitBuffInfo("player", i)
-        local bFood = (GetActiveFoodBuff(abilityId) and canClickOff)
+        local bFood = (getActiveFoodBuff(abilityId) and canClickOff)
         foodQuantity = timeEnding * 1000 - now
         haveFood = (bFood and (foodQuantity > 0))
         if haveFood then
@@ -166,7 +182,6 @@ local function TimersUpdate()
 end

 local function StartUp()
-
     if not WellEater:isAddonEnabled() then
         return
     end
@@ -180,14 +195,14 @@ local function StartUp()
 end

 local function ShutDown()
+    d("Shutdown minQ = " .. WellEater.settingsUser.minQuality)
+    d("Shutdown maxQ = " .. WellEater.settingsUser.maxQuality)
+    d(WellEater.AddonName .. " Timer cancelled")
     EVENT_MANAGER:UnregisterForUpdate(WellEater.AddonName .. "_TimersUpdate")
 end

-local function InitOnLoad(_, addonName)
-    if WellEater.AddonName ~= addonName then
-        return
-    end
-    EVENT_MANAGER:UnregisterForEvent(WellEater.AddonName, EVENT_ADD_ON_LOADED)
+local function OnSettingsClosed()
+    ShutDown()
     StartUp()
 end

@@ -202,60 +217,97 @@ local function OnUIError(_,errorString)
     end
 end

--- Settings initialization
-WellEater.settingsUser = ZO_SavedVars:NewCharacterIdSettings( "WellEater_Settings",
-        WellEater.WELLEATER_SAVED_VERSION,
-        "general",
-        NAMESPACE.settingsDefaults)
-
--- Init Hook --
-EVENT_MANAGER:RegisterForEvent(
-        WellEater.AddonName, EVENT_ADD_ON_LOADED, InitOnLoad)
+local function InitOnLoad(_, addonName)
+    if WellEater.AddonName ~= addonName then
+        return
+    end

-EVENT_MANAGER:RegisterForEvent(
-        WellEater.AddonName,
-        EVENT_PLAYER_COMBAT_STATE,
-        function(_, arg)
+    EVENT_MANAGER:UnregisterForEvent(WellEater.AddonName, EVENT_ADD_ON_LOADED)
+    -- Settings initialization
+    WellEater.settingsUser = ZO_SavedVars:NewCharacterIdSettings( "WellEater_Settings",
+            WellEater.WELLEATER_SAVED_VERSION,
+            "general",
+            NAMESPACE.settingsDefaults)
+
+    EVENT_MANAGER:RegisterForEvent(
+            WellEater.AddonName,
+            EVENT_PLAYER_COMBAT_STATE,
+            function(_, arg)
+
+                if not WellEater:isAddonEnabled() then
+                    return
+                end

-            if not WellEater:isAddonEnabled() then
-                return
+                if not arg then
+                    d(WellEater.AddonName .. " Combat exited")
+                    StartUp()
+                else
+                    d(WellEater.AddonName .. " Combat entered")
+                    ShutDown()
+                end
             end
+    )
+

-            if not arg then
-                d(WellEater.AddonName .. " Combat exited")
+    EVENT_MANAGER:RegisterForEvent(
+            WellEater.AddonName,
+            EVENT_PLAYER_DEAD,
+            function()
+                if not WellEater:isAddonEnabled() then
+                    return
+                end
+                d(WellEater.AddonName .. " Dead")
+                ShutDown()
+            end
+    )
+
+    EVENT_MANAGER:RegisterForEvent(
+            WellEater.AddonName,
+            EVENT_PLAYER_ALIVE,
+            function()
+                if not WellEater:isAddonEnabled() then
+                    return
+                end
+                d(WellEater.AddonName .. " Alive")
                 StartUp()
-            else
-                d(WellEater.AddonName .. " Combat entered")
+            end
+    )
+
+    EVENT_MANAGER:RegisterForEvent(
+            WellEater.AddonName,
+            EVENT_PLAYER_SWIMMING,
+            function()
+                if not WellEater:isAddonEnabled() then
+                    return
+                end
+                d(WellEater.AddonName .. " Swim enter")
                 ShutDown()
             end
-        end
-)
+    )
+
+    EVENT_MANAGER:RegisterForEvent(
+            WellEater.AddonName,
+            EVENT_PLAYER_NOT_SWIMMING,
+            function()
+                if not WellEater:isAddonEnabled() then
+                    return
+                end

-EVENT_MANAGER:RegisterForEvent(
-        WellEater.AddonName,
-        EVENT_PLAYER_SWIMMING,
-        function()
-            if not WellEater:isAddonEnabled() then
-                return
+                d(WellEater.AddonName .. " Swim exit")
+                StartUp()
             end
-            d(WellEater.AddonName .. " Swim enter")
-            ShutDown()
-        end
-)
+    )

-EVENT_MANAGER:RegisterForEvent(
-        WellEater.AddonName,
-        EVENT_PLAYER_NOT_SWIMMING,
-        function()
-            if not WellEater:isAddonEnabled() then
-                return
-            end
+    EVENT_MANAGER:RegisterForEvent(WellEater.AddonName, EVENT_LUA_ERROR, OnUIError)

-            d(WellEater.AddonName .. " Swim exit")
-            StartUp()
-        end
-)
+    local lamPanel = WellEater:InitSettingsMenu()
+    lamPanel:SetHandler("OnEffectivelyHidden", OnSettingsClosed)
+    StartUp()
+end
+
+-- Init Hook --
+EVENT_MANAGER:RegisterForEvent(
+        WellEater.AddonName, EVENT_ADD_ON_LOADED, InitOnLoad)

-EVENT_MANAGER:RegisterForEvent(WellEater.AddonName, EVENT_LUA_ERROR, OnUIError)


diff --git a/WellEater.txt b/WellEater.txt
index 9504c10..b376356 100644
--- a/WellEater.txt
+++ b/WellEater.txt
@@ -4,5 +4,10 @@
 ## Description: Auto eat your preferred meals provided by your inventory after food or drink buff expiration.
 ## Version: 1.0.0
 ## SavedVariables: WellEater_Settings
+## DependsOn: LibAddonMenu-2.0>=28

 WellEater.lua
+UI.lua
+lang/en.lua
+lang/$(language).lua
+
diff --git a/lang/en.lua b/lang/en.lua
new file mode 100644
index 0000000..1b72ffa
--- /dev/null
+++ b/lang/en.lua
@@ -0,0 +1,15 @@
+WellEater = WellEater or {}
+local L = {
+    generalSetupHeader = "Food search criteria to scan your inventory",
+    generalSetupDescription = "Auto eat your preferred meals provided by your inventory after food or drink buff expiration",
+    foodQualityMinHeader = "Food Quality Min Range",
+    foodQualityMaxHeader = "Food Quality Min Range",
+    foodGreen = "Normal (green)",
+    foodBlue = "Excellent (blue)",
+    foodCyan = "Artifact (cyan)",
+    foodGold = "Legendary (gold)",
+}
+
+function WellEater:getLocale()
+    return L
+end
\ No newline at end of file
diff --git a/lang/ru.lua b/lang/ru.lua
new file mode 100644
index 0000000..29b7b22
--- /dev/null
+++ b/lang/ru.lua
@@ -0,0 +1,15 @@
+WellEater = WellEater or {}
+local L = {
+    generalSetupHeader = "Критерии поиска еды в инвентаре",
+    generalSetupDescription = "После истечения времени эффектов, которые дает еда, позволяет автоматически съесть найденное в инвентаре блюдо",
+    foodQualityMinHeader = "Минимум качества еды",
+    foodQualityMaxHeader = "Максимум качества еды",
+    foodGreen = "Хорошее (зеленое)",
+    foodBlue = "Превосходное (синее)",
+    foodCyan = "Эпическое (фиолетовое)",
+    foodGold = "Легендарное (золотое)",
+}
+
+function WellEater:getLocale()
+    return L
+end
\ No newline at end of file