- Make last (current) sub-zone stored by character instead of by account

github@spradlin.dev [09-18-21 - 21:37]
- Make last (current) sub-zone stored by character instead of by account
- add display of current subzone in settings panel.
Filename
MuteBards2.lua
MuteBardsOptions.lua
diff --git a/MuteBards2.lua b/MuteBards2.lua
index 3be7e6c..04ab99c 100644
--- a/MuteBards2.lua
+++ b/MuteBards2.lua
@@ -1,14 +1,27 @@
-MuteBards = {}
+MuteBards = MuteBards or {};
 MuteBards.name = "MuteBards2"
 MuteBards.Version = "2.03"

+local savedVars = {}
+MuteBards.sv = savedVars; -- saved vars
+
 local currentSubZone;

-local defaults = {
-  zones = { "Ebonheart", "Nimalten", "Kragenmoor", "Davon's Watch", "Stormhold", "Riften", "Velyn Harbor", "Solitude", "Rimmen", "Belkarth", "Lilmoth", "Bright-Throat Village", "Mournhold Guild Plaza", "Mournhold Residential District" },
-  currentZone = ''
+local accountDefaults = {
+  zones = { "Ebonheart", "Nimalten", "Kragenmoor", "Davon's Watch", "Stormhold", "Riften", "Velyn Harbor", "Solitude", "Rimmen", "Belkarth", "Lilmoth", "Bright-Throat Village", "Mournhold Guild Plaza", "Mournhold Residential District", "Alinor", "Shimmerene" },
+};
+local characterDefaults = {
+  currentSubZone = '',
 };

+local function setSfxVolume(volume)
+  SetSetting(SETTING_TYPE_AUDIO, AUDIO_SETTING_SFX_VOLUME, volume);
+end
+
+local function getSVZones(sv)
+  return sv and sv.account and sv.account.zones;
+end
+
 -- Todo how can I get the current subZone name?  I could not figure that out.  If I can do that, I can add a button to mute current subZone.

 --define Callback
@@ -23,35 +36,33 @@ local function OnAddOnLoaded(eventCode, addOnName)

   --create the default table
   --create the saved variable access object here and assign it to savedVars
-  MuteBards.savedVars = ZO_SavedVars:NewAccountWide("MuteBards_Settings", "1.0.0", "Zones", defaults, GetWorldName());
-  MuteBards:InitSettingsPanel(MuteBards.savedVars.zones);
+  savedVars.account = ZO_SavedVars:NewAccountWide("MuteBards_Settings", "1.0.0", "Zones", accountDefaults, GetWorldName());
+  savedVars.character = ZO_SavedVars:NewCharacterIdSettings("MuteBards_Settings", "1.0.0", "currentSubZone", characterDefaults, GetWorldName());
+
+  if ((savedVars.character.currentSubZone == nil or savedVars.character.currentSubZone == "") and (currentSubZone ~= nil and currentSubZone ~= "")) then
+    savedVars.character.currentSubZone = currentSubZone;
+  end
+
+  MuteBards:InitSettingsPanel(getSVZones(savedVars), savedVars.character);
   MuteBards:Refresh();
 end

 --Register Loaded Callback
 EVENT_MANAGER:RegisterForEvent(MuteBards.name, EVENT_ADD_ON_LOADED, OnAddOnLoaded);

-function MuteBards_CombatState(event_id, inCombat)
+function MuteBards2_OnCombatStateChange(event_id, inCombat)
   if inCombat then
-    MuteBards_EnableEffects()
+    setSfxVolume(70);
   end
 end

-function MuteBards_MuteEffects()
-  SetSetting(SETTING_TYPE_AUDIO, AUDIO_SETTING_SFX_VOLUME, 0)
-end
-
-function MuteBards_EnableEffects()
-  SetSetting(SETTING_TYPE_AUDIO, AUDIO_SETTING_SFX_VOLUME, 70)
-end
-
 function MuteBards:AddZone(zoneName, cb)
   if (MuteBards:ContainsZone(zoneName)) then
     d('Zone ' .. zoneName .. ' already in list');
     return;
   end
   d('Adding zone ' .. zoneName);
-  table.insert(MuteBards.savedVars.zones, zoneName);
+  table.insert(getSVZones(savedVars), zoneName);
   if (cb ~= nil) then
     cb();
   end
@@ -59,9 +70,9 @@ end

 function MuteBards:RemoveZone(zoneName, cb)
   if (MuteBards:ContainsZone(zoneName)) then
-    for i, v in ipairs(MuteBards.savedVars.zones) do
+    for i, v in ipairs(getSVZones(savedVars)) do
       if (v == zoneName) then
-        table.remove(MuteBards.savedVars.zones, i);
+        table.remove(getSVZones(savedVars), i);
         d('Zone ' .. zoneName .. ' removed from list');
         if (cb ~= nil) then
           cb();
@@ -74,10 +85,10 @@ function MuteBards:RemoveZone(zoneName, cb)
 end

 function MuteBards:ContainsZone(zoneName)
-  if (MuteBards.savedVars == nil or MuteBards.savedVars.zones == nil) then
+  if (getSVZones(savedVars) == nil) then
     return false;
   end
-  for i, v in ipairs(MuteBards.savedVars.zones) do
+  for i, v in ipairs(getSVZones(savedVars)) do
     if (v == zoneName) then
       return true;
     end
@@ -85,23 +96,23 @@ function MuteBards:ContainsZone(zoneName)
   return false;
 end

-function MuteBards_AreaChange(eventCode, zoneName, subZoneName, newSubzone)
-  if (MuteBards.savedVars ~= nil) then
-    MuteBards.savedVars.currentSubZone = subZoneName;
+function MuteBards2_OnZoneChange(subZoneName)
+  if (MuteBards.sv ~= nil and MuteBards.sv.character ~= nil) then
+    MuteBards.sv.character.currentSubZone = subZoneName;
   else
     currentSubZone = subZoneName
   end
-  MuteBards_EnableEffects();
+  setSfxVolume(70);
   if (MuteBards:ContainsZone(subZoneName)) then
-    MuteBards_MuteEffects();
+    setSfxVolume(0);
   end
 end

 -- Handle cases where you're interacting with an object?

-EVENT_MANAGER:RegisterForEvent("MuteBardsCombatState", EVENT_PLAYER_COMBAT_STATE, MuteBards_CombatState)
-EVENT_MANAGER:RegisterForEvent("MuteBardsAreaChange", EVENT_ZONE_CHANGED, MuteBards_AreaChange)
+EVENT_MANAGER:RegisterForEvent("MuteBards2_OnCombatStateChange", EVENT_PLAYER_COMBAT_STATE, MuteBards2_OnCombatStateChange)
+EVENT_MANAGER:RegisterForEvent("MuteBards2_OnZoneChange", EVENT_ZONE_CHANGED, function(_, __, subZoneName) MuteBards2_OnZoneChange(subZoneName) end)

 function MuteBards:Refresh()
-  MuteBards_AreaChange(nil, nil, MuteBards.savedVars.currentSubZone or currentSubZone);
+  MuteBards2_OnZoneChange((MuteBards.sv.character and MuteBards.sv.character.currentSubZone) or currentSubZone);
 end
diff --git a/MuteBardsOptions.lua b/MuteBardsOptions.lua
index bfb942f..7eb3f70 100644
--- a/MuteBardsOptions.lua
+++ b/MuteBardsOptions.lua
@@ -1,5 +1,18 @@
+MuteBards = MuteBards or {};
 local isInitialized = false;
-function MuteBards:InitSettingsPanel(svZones)
+function MuteBards:InitSettingsPanel(svZones, svChar)
+
+  local function refreshCallback()
+    MuteBards:Refresh();
+  end
+
+  local function getCurrentZoneName(sv)
+    if (sv ~= nil and sv.currentSubZone ~= ni and sv.currentSubZone ~= "") then
+      return sv.currentSubZone;
+    end
+    return "UNKNOWN"
+  end
+
   local panelData = {
     type = "panel",
     name = "MuteBards 2.0",
@@ -10,7 +23,9 @@ function MuteBards:InitSettingsPanel(svZones)
     registerForRefresh = true, --boolean (optional) (will refresh all options controls when a setting is changed and when the panel is shown)
     registerForDefaults = false, --boolean (optional) (will set all options controls back to default values)
   }
+
   local zoneToAdd, zoneToRemove
+
   local optionsTable = {
     [1] = {
       type = "header",
@@ -24,6 +39,13 @@ function MuteBards:InitSettingsPanel(svZones)
       width = "full", --or "half" (optional)
     },
     [3] = {
+      type = "description",
+      title = "Current Zone:",	--(optional)
+      text = function() return "  " .. getCurrentZoneName(svChar) end,
+
+      width = "full", --or "half" (optional)
+    },
+    [4] = {
       type = "editbox",
       name = "Add Zone:",
       tooltip = "Zone Name",
@@ -37,16 +59,16 @@ function MuteBards:InitSettingsPanel(svZones)
       width = "half", --or "half" (optional)
       default = "", --(optional)
     },
-    [4] = {
+    [5] = {
       type = "button",
       name = "Add",
       func = function()
-        MuteBards:AddZone(zoneToAdd, function() MuteBards:Refresh() end);
+        MuteBards:AddZone(zoneToAdd, refreshCallback);
         zoneToAdd = "";
       end,
       width = "half", --or "half" (optional)
     },
-    [5] = {
+    [6] = {
       type = "description",
       title = "Currently Muted Zones:",
       text = function()
@@ -55,7 +77,7 @@ function MuteBards:InitSettingsPanel(svZones)
       width = "full",
       requiresReload = true,
     },
-    [6] = {
+    [7] = {
       type = "editbox",
       name = "Remove Zone:",
       getFunc = function()
@@ -68,26 +90,26 @@ function MuteBards:InitSettingsPanel(svZones)
       width = "half", --or "full" (optional)
       default = "", --(optional)
     },
-    [7] = {
+    [8] = {
       type = "button",
       name = "Remove",
       func = function()
-        MuteBards:RemoveZone(zoneToRemove, function() MuteBards:Refresh() end);
+        MuteBards:RemoveZone(zoneToRemove, refreshCallback);
         zoneToRemove = "";
       end,
       width = "half", --or "full" (optional)
     },
-    [8] = {
+    [9] = {
       type = "button",
       name = "Force Refresh",
-      func = function() MuteBards:Refresh() end,
+      func = refreshCallback,
       width = "full", --or "half" (optional)
     },
   }

-  local LAM = LibAddonMenu2 or LibStub("LibAddonMenu-2.0")
+  local LAM = LibAddonMenu2
   if (not isInitialized) then
-    LAM:RegisterAddonPanel("MuteBards2", panelData)
+    LAM:RegisterAddonPanel("MuteBards2", panelData);
   end
   LAM:RegisterOptionControls("MuteBards2", optionsTable);