View Single Post
12-12-23, 08:53 AM   #8
Ssesmar
A Deviate Faerie Dragon
Join Date: Oct 2023
Posts: 15
Thank you very much Fizzlemizz for your help, I have now changed the code like this. works perfectly and does exactly what it's supposed to do

Lua Code:
  1. --## SavedVariables: HandyNotes_MapNotesDB
  2.  
  3. local HandyNotes = LibStub("AceAddon-3.0"):GetAddon("HandyNotes", true)
  4. if not HandyNotes then return end
  5. local L = LibStub("AceLocale-3.0"):GetLocale("HandyNotes_MapNotes")
  6. local MBicon = LibStub("LibDBIcon-1.0", true)
  7. local MapNotesMiniButton = LibStub("AceAddon-3.0"):NewAddon("MNMiniMapButton", "AceConsole-3.0")
  8.  
  9. local db = { }
  10. local icons = { }
  11. local nodes = { }
  12. local minimap = { }
  13. local lfgIDs = { }
  14. local assignedIDs = { }
  15.  
  16. SLASH_INFO1, SLASH_INFO2, SLASH_INFO3, SLASH_INFO4, SLASH_INFO5, SLASH_INFO6, SLASH_INFO7, SLASH_INFO8 , SLASH_INFO09 = "/mn", "/MN", "/mapnotes", "/MAPNOTES", "/mnhelp", "/MNHELP", "/mnh", "/MNH", "/handynotes_mapnotes";
  17. function SlashCmdList.INFO(msg, editbox)
  18.   print("|cff00ccff".."------------------------------------------------------------------------------------------")
  19.   print("|cffffff00~~".."|cffff0000Map|r|cff00ccffNotes|r" .. "|cffffff00~~")
  20.   print("|cffffff00".. L["Chat commands:"])
  21.   print("|cffffff00                      • ".. L["to open MapNotes menu: /mno, /MNO"])
  22.   print("|cffffff00                      • ".. L["to close MapNotes menu: /mnc, /MNC"])
  23.   print("|cffffff00                      • ".. L["to show minimap button: /mnb or /MNB"])
  24.   print("|cffffff00                      • ".. L["to hide minimap button: /mnbh or /MNBH"])
  25.   print("|cffffff00~~".."|cffff0000Map|r|cff00ccffNotes|r" .. "|cffffff00~~")
  26.   print("|cff00ccff".."------------------------------------------------------------------------------------------")
  27. end
  28.  
  29. SLASH_OPEN1, SLASH_OPEN2 = "/mno", "/MNO";
  30. function SlashCmdList.OPEN(msg, editbox)
  31.   LibStub("AceConfigDialog-3.0"):Open("MNMiniMapButton")
  32.   print("|cffff0000Map|r|cff00ccffNotes|r".."|cffffff00 • ".. L["MapNotes menu window"], "|cff00ff00" .. L["is activated"])
  33. end
  34.  
  35. SLASH_CLOSE1, SLASH_CLOSE2 = "/mnc", "/MNC";
  36. function SlashCmdList.CLOSE(msg, editbox)
  37.   LibStub("AceConfigDialog-3.0"):Close("MNMiniMapButton")
  38.   print("|cffff0000Map|r|cff00ccffNotes|r".."|cffffff00 • ".. L["MapNotes menu window"], "|cffff0000" .. L["is deactivated"])
  39. end
  40.  
  41. SLASH_MMBSHOW1, SLASH_MMBSHOW2 = "/mnb", "/MNB";
  42. function SlashCmdList.MMBSHOW(msg, editbox)
  43.   MBicon:Show("MNMiniMapButton")
  44.   db.show["HideMMB"] = false
  45.   print("|cffff0000Map|r|cff00ccffNotes|r" .. "|cffffff00 • " .. L["-> MiniMapButton <-"], "|cff00ff00" .. L["is activated"])
  46. end
  47.  
  48. SLASH_MMBHIDE1, SLASH_MMBHIDE2 = "/mnbh", "/MNBH";
  49. function SlashCmdList.MMBHIDE(msg, editbox)
  50.   MBicon:Hide("MNMiniMapButton")
  51.   db.show["HideMMB"] = true
  52.   print("|cffff0000Map|r|cff00ccffNotes|r" .. "|cffffff00 • " .. L["-> MiniMapButton <-"], "|cffff0000" .. L["is deactivated"])
  53. end
  54.  
  55. local miniButton = {
  56.   text = "MapNotes",
  57.   type = "data source",
  58.   icon = "Interface\\AddOns\\HandyNotes_MapNotes\\Images\\MN_Logo",
  59.   OnTooltipShow = function(tooltip)
  60.   if not tooltip or not tooltip.AddLine then return end
  61.   tooltip:AddLine("|cffff0000Map|r|cff00ccffNotes|r")
  62.   end,
  63.   OnClick = function(self, button)
  64.     if button == "RightButton" then
  65.       LibStub("AceConfigDialog-3.0"):Close("MNMiniMapButton")
  66.     end
  67.     if IsShiftKeyDown() and button == "RightButton" then
  68.       MBicon:Hide("MNMiniMapButton")
  69.       db.show["HideMMB"] = true
  70.       print("|cffff0000Map|r|cff00ccffNotes|r" .. "|cffffff00 • " .. L["-> MiniMapButton <-"], "|cffff0000" .. L["is deactivated"])
  71.     end
  72.     if button == "LeftButton" then
  73.       LibStub("AceConfigDialog-3.0"):Open("MNMiniMapButton")
  74.       print("|cffff0000Map|r|cff00ccffNotes|r".."|cffffff00 • ".. L["MapNotes menu window"], "|cff00ff00" .. L["is activated"])
  75.     end
  76. end}
  77.  
  78. function MapNotesMiniButton:OnInitialize()
  79.   self.db = LibStub("AceDB-3.0"):New("MNMiniMapButtonDB", { profile = { minimap = { hide = false, }, }, })
  80.   MBicon:Register("MNMiniMapButton", miniButton, self.db.profile.minimap)
  81. end
  82.  
  83. local function updateAssignedID()
  84.     table.wipe(assignedIDs)
  85.     for i=1,GetNumSavedInstances() do
  86.         local name, _, _, _, locked, _, _, _, _, difficultyName, numEncounters, encounterProgress = GetSavedInstanceInfo(i)
  87.         if (locked) then
  88.             if (not assignedIDs[name]) then
  89.             assignedIDs[name] = { }
  90.             end
  91.             assignedIDs[name][difficultyName] = encounterProgress .. "/" .. numEncounters
  92.         end
  93.     end
  94. end
  95.  
  96. local pluginHandler = { }
  97. function pluginHandler:OnEnter(uiMapId, coord)
  98.   local nodeData = nil
  99.  
  100.     if (minimap[uiMapId] and minimap[uiMapId][coord]) then
  101.       nodeData = minimap[uiMapId][coord]
  102.     end
  103.     if (nodes[uiMapId] and nodes[uiMapId][coord]) then
  104.       nodeData = nodes[uiMapId][coord]
  105.     end
  106.    
  107.     if (not nodeData) then return end
  108.    
  109.     local tooltip = self:GetParent() == WorldMapButton and WorldMapTooltip or GameTooltip
  110.     if ( self:GetCenter() > UIParent:GetCenter() ) then
  111.       tooltip:SetOwner(self, "ANCHOR_LEFT")
  112.     else
  113.         tooltip:SetOwner(self, "ANCHOR_RIGHT")
  114.     end
  115.  
  116.     if (not nodeData.name) then return end
  117.  
  118.     local instances = { strsplit("\n", nodeData.name) }
  119.    
  120.  
  121.     updateAssignedID()
  122.    
  123.     for i, v in pairs(instances) do
  124.       if (db.assignedID and (assignedIDs[v] or (lfgIDs[v] and assignedIDs[lfgIDs[v]]))) then
  125.         if (assignedIDs[v]) then
  126.           for a,b in pairs(assignedIDs[v]) do
  127.             tooltip:AddDoubleLine(v, a .. " " .. b, 1, 1, 1, 1, 1, 1)
  128.           end
  129.         end
  130.       if (lfgIDs[v] and assignedIDs[lfgIDs[v]]) then
  131.         for a,b in pairs(assignedIDs[lfgIDs[v]]) do
  132.           tooltip:AddDoubleLine(v, a .. " " .. b, 1, 1, 1, 1, 1, 1)
  133.         end
  134.       end
  135.       else
  136.         tooltip:AddLine(v, nil, nil, nil, false)
  137.       end
  138.     end
  139.     tooltip:Show()
  140. end
  141.  
  142. function pluginHandler:OnLeave(uiMapID, coord)
  143.     if self:GetParent() == WorldMapButton then
  144.       WorldMapTooltip:Hide()
  145.     else
  146.       GameTooltip:Hide()
  147.     end
  148. end
  149.  
  150. do
  151.     local tablepool = setmetatable({}, {__mode = 'k'})
  152.    
  153.     local function deepCopy(object)
  154.         local lookup_table = {}
  155.         local function _copy(object)
  156.             if type(object) ~= "table" then
  157.                 return object
  158.             elseif lookup_table[object] then
  159.                 return lookup_table[object]
  160.             end
  161.  
  162.             local new_table = {}
  163.               lookup_table[object] = new_table
  164.             for index, value in pairs(object) do
  165.                 new_table[_copy(index)] = _copy(value)
  166.             end
  167.  
  168.             return setmetatable(new_table, getmetatable(object))
  169.         end
  170.             return _copy(object)
  171.     end
  172.  
  173.     local function iter(t, prestate)
  174.         if not t then return end
  175.         local data = t.data
  176.  
  177.         local state, value = next(data, prestate)
  178.  
  179.         while value do
  180.             local alpha
  181.            
  182.             local allLocked = true
  183.             local anyLocked = false
  184.             if value.name == nil then value.name = value.id end
  185.             local instances = { strsplit("\n", value.name) }
  186.             for i, v in pairs(instances) do
  187.                 if (not assignedIDs[v] and not assignedIDs[lfgIDs[v]]) then
  188.                     allLocked = false
  189.                 else
  190.                     anyLocked = true
  191.                 end
  192.             end
  193.  
  194.             local icon = icons[value.type]
  195.             if ((anyLocked and db.graymultipleID) or (allLocked and not db.graymultipleID)) then
  196.                 icon = icons["Locked"]
  197.             end
  198.  
  199.             if t.minimapUpdate or value.showInZone then
  200.               return state, nil, icon, db.azerothScale, alpha
  201.             end
  202.      
  203.             state, value = next(data, state)
  204.         end
  205.         wipe(t)
  206.         tablepool[t] = true
  207.     end
  208.  
  209.  
  210.     local function iterCont(t, prestate)
  211.         if not t then return end
  212.     if not db.showContinent then return end
  213.         local zone = t.C[t.Z]
  214.         local data = nodes[zone]
  215.         local state, value
  216.         while zone do
  217.             if data then
  218.                 state, value = next(data, prestate)
  219.                 while state do
  220.                     local icon, alpha
  221.  
  222.                     icon = icons[value.type]
  223.                     local allLocked = true
  224.                     local anyLocked = false
  225.                     local instances = { strsplit("\n", value.name) }
  226.                     for i, v in pairs(instances) do
  227.                         if (not assignedIDs[v] and not assignedIDs[lfgIDs[v]]) then
  228.                             allLocked = false
  229.                         else
  230.                             anyLocked = true
  231.                         end
  232.                     end
  233.      
  234.                     if ((anyLocked and db.assignedgray) or (allLocked and db.assignedgray)) then  
  235.                         icon = icons["Locked"]
  236.                     end
  237.  
  238.                     if not value.hideOnContinent and db.showContinent then
  239.                         return state, zone, icon, db.continentScale, alpha
  240.           end
  241.                     state, value = next(data, state)
  242.                 end
  243.             end
  244.             t.Z = next(t.C, t.Z)
  245.             zone = t.C[t.Z]
  246.             data = nodes[zone]
  247.             prestate = nil
  248.         end
  249.         wipe(t)
  250.         tablepool[t] = true
  251.     end
  252.  
  253.     function pluginHandler:GetNodes2(uiMapId, isMinimapUpdate, coord)
  254.         local C = deepCopy(HandyNotes:GetContinentZoneList(uiMapId))
  255.         if C then
  256.             table.insert(C, uiMapId)
  257.             local tbl = next(tablepool) or {}
  258.             tablepool[tbl] = nil
  259.             tbl.C = C
  260.             tbl.Z = next(C)
  261.             tbl.contId = uiMapId
  262.             return iterCont, tbl, nil
  263.         else
  264.             if (nodes[uiMapId] == nil) then return iter end
  265.             local tbl = next(tablepool) or {}
  266.             tablepool[tbl] = nil
  267.             tbl.minimapUpdate = isMinimapUpdate
  268.             if (isMinimapUpdate and minimap[uiMapId]) then
  269.                 tbl.data = minimap[uiMapId]
  270.             else
  271.                 tbl.data = nodes[uiMapId]
  272.             end
  273.             return iter, tbl, nil
  274.         end
  275.     end
  276. end
  277.  
  278. local waypoints = {}
  279. local function setWaypoint(uiMapID, coord)
  280.     local dungeon = nodes[uiMapID][coord]
  281.  
  282.     local waypoint = nodes[dungeon]
  283.     if waypoint and TomTom:IsValidWaypoint(waypoint) then
  284.         return
  285.     end
  286.  
  287.     local title = dungeon.name
  288.     local x, y = HandyNotes:getXY(coord)
  289.     waypoints[dungeon] = TomTom:AddWaypoint(uiMapID, x, y, {
  290.         title = dungeon.name,
  291.         persistent = nil,
  292.         minimap = true,
  293.         world = true
  294.     })
  295. end
  296.  
  297. function pluginHandler:OnClick(button, pressed, uiMapId, coord)
  298.     if (not pressed) then return end
  299.     if IsShiftKeyDown() and (button == "RightButton" and db.tomtom and TomTom) then
  300.         setWaypoint(uiMapId, coord)
  301.         return
  302.         end
  303.     if (button == "LeftButton" and db.journal) then
  304.         if (not EncounterJournal_OpenJournal) then
  305.         UIParentLoadAddOn('Blizzard_EncounterJournal')
  306.         end
  307.         local dungeonID
  308.         if (type(nodes[uiMapId][coord].id) == "table") then
  309.             dungeonID = nodes[uiMapId][coord].id[1]
  310.         else
  311.             dungeonID = nodes[uiMapId][coord].id
  312.         end
  313.  
  314.         if (not dungeonID) then return end
  315.  
  316.         local name, _, _, _, _, _, _, link = EJ_GetInstanceInfo(dungeonID)
  317.         if not link then return end
  318.         local difficulty = string.match(link, 'journal:.-:.-:(.-)|h')
  319.         if (not dungeonID or not difficulty) then return end
  320.         EncounterJournal_OpenJournal(difficulty, dungeonID)
  321.         _G.EncounterJournal:SetScript("OnShow", BBBEncounterJournal_OnShow)
  322.     end
  323. end
  324.  
  325. local defaults = {
  326.   profile = {
  327.       show = {
  328.       },
  329.  
  330.     --1
  331.       hideAddon = false,
  332.       hideMMB = false,
  333.   },
  334. }
  335.  
  336. local Addon = CreateFrame("Frame")
  337. Addon:RegisterEvent("PLAYER_LOGIN")
  338. Addon:SetScript("OnEvent", function(self, event, ...) return self[event](self, ...)end)
  339.  
  340. local function updateStuff()
  341.   updateAssignedID()
  342.   HandyNotes:SendMessage("HandyNotes_NotifyUpdate", "MapNotes")
  343. end
  344.  
  345. function Addon:PLAYER_ENTERING_WORLD()
  346.   if (not self.faction) then
  347.       self.faction = UnitFactionGroup("player")
  348.       self:PopulateTable()
  349.       self:PopulateMinimap()
  350.       self:ProcessTable()
  351.   end
  352.  
  353.     updateAssignedID()
  354.     updateStuff()
  355. end
  356.  
  357. function Addon:PLAYER_LOGIN()
  358.   local options = {
  359.     type = "group",
  360.     name = "|cffff0000Map|r|cff00ccffNotes|r",
  361.     childGroups = "tab",
  362.     desc = L["Shows locations of raids, dungeons, portals ,ship and zeppelins symbols on different maps"],
  363.     get = function(info) return db[info[#info]] end,
  364.     set = function(info, v) db[info[#info]] = v HandyNotes:SendMessage("HandyNotes_NotifyUpdate", "MapNotes") end,
  365.     args = {  
  366.       GeneralTab = {
  367.         type = "group",
  368.         name = L["General"],
  369.         desc = L["General settings that apply to Azeroth / Continent / Dungeon map at the same time"],
  370.         order = 0,
  371.         args = {
  372.           hideMapNotesMMB = {
  373.             type = "header",
  374.             name = L["Hide MapNotes / Hide Minimap Button"],
  375.             order = 1,
  376.             },
  377.           hideAddon = {
  378.             type = "toggle",
  379.             name = "|cffff0000" .. L["hide MapNotes!"] .."\n",
  380.             desc = L["Disable MapNotes, all icons will be hidden on each map and all categories will be disabled"],
  381.             order = 1.2,
  382.             get = function() return db.show["HideMapNote"] end,
  383.             set = function(info, v) db.show["HideMapNote"] = v self:FullUpdate() HandyNotes:SendMessage("HandyNotes_NotifyUpdate", "MapNotes")
  384.                   if db.show["HideMapNote"] then print("|cffff0000Map|r|cff00ccffNotes|r".."|cffff0000 • ".. L["All MapNotes symbols have been hidden"]) else
  385.                   if not db.show["HideMapNote"] then print("|cffff0000Map|r|cff00ccffNotes|r".."|cff00ff00 • ".. L["All set symbols have been restored"]) end end end,
  386.             },  
  387.           hideMMB = {
  388.             type = "toggle",
  389.             name = "|cffff0000" .. "" .. L["hide minimap button"],
  390.             desc = L["Hide the minimap button on the minimap"],
  391.             order = 1.3,
  392.             width = 1.89,
  393.             get = function() return db.show["HideMMB"] end,
  394.             set = function(info, v) db.show["HideMMB"] = v self:FullUpdate() HandyNotes:SendMessage("HandyNotes_NotifyUpdate", "MapNotes")
  395.               if not db.show["HideMMB"] then MBicon:Show("MNMiniMapButton") print("|cffff0000Map|r|cff00ccffNotes|r" .. "|cffffff00 • " .. L["-> MiniMapButton <-"], "|cff00ff00" .. L["is activated"]) else
  396.               if db.show["HideMMB"] then MBicon:Hide("MNMiniMapButton") print("|cffff0000Map|r|cff00ccffNotes|r" .. "|cffffff00 • " .. L["-> MiniMapButton <-"], "|cffff0000" .. L["is deactivated"]) end end end,
  397.             },
  398.         }
  399.       }
  400.     }
  401.   }
  402.  
  403.   HandyNotes:RegisterPluginDB("MapNotes", pluginHandler, options)
  404.   self.db = LibStub("AceDB-3.0"):New("HandyNotes_MapNotesDB", defaults, true)
  405.   db = self.db.profile
  406.   LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("MNMiniMapButton", options)
  407.   Addon:RegisterEvent("PLAYER_ENTERING_WORLD")
  408.   if db.show["HideMMB"] then
  409.     MBicon:Hide("MNMiniMapButton")
  410.   end
  411. end
  412.  
  413. function Addon:PopulateMinimap()
  414.     local temp = { }
  415.     for k,v in pairs(nodes) do
  416.         if (minimap[k]) then
  417.             for a,b in pairs(minimap[k]) do
  418.                 temp[a] = true
  419.             end
  420.             for c,d in pairs(v) do
  421.                 if (not temp[c] and not d.hideOnMinimap) then
  422.                     minimap[k][c] = d
  423.                 end
  424.             end
  425.         end
  426.     end
  427. end
  428.  
  429. function Addon:PopulateTable()
  430.   table.wipe(nodes)
  431.   table.wipe(minimap)
  432. end
  433.  
  434. function Addon:UpdateInstanceNames(node)
  435.   local dungeonInfo = EJ_GetInstanceInfo
  436.     local id = node.id
  437.  
  438.       if (node.lfgid) then
  439.         dungeonInfo = GetLFGDungeonInfo
  440.         id = node.lfgid
  441.       end
  442.  
  443.       if (type(id) == "table") then
  444.         for i,v in pairs(node.id) do
  445.           local name = dungeonInfo(v)
  446.             self:UpdateAlter(v, name)
  447.           if (node.name) then
  448.             node.name = node.name .. "\n" .. name
  449.           else
  450.             node.name = name
  451.           end
  452.         end
  453.       elseif (id) then
  454.         node.name = dungeonInfo(id)
  455.         self:UpdateAlter(id, node.name)
  456.       end
  457. end
  458.  
  459. function Addon:ProcessTable()
  460.   table.wipe(lfgIDs)
  461.  
  462.  
  463.   function Addon:UpdateAlter(id, name)
  464.     if (lfgIDs[id]) then
  465.       local lfgIDs1, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, lfgIDs2 = GetLFGDungeonInfo(lfgIDs[id])
  466.         if (lfgIDs2 and lfgIDs1 == name) then
  467.           lfgIDs1 = lfgIDs2
  468.         end
  469.  
  470.       if (lfgIDs1) then
  471.         if (lfgIDs1 == name) then
  472.         else
  473.         lfgIDs[id] = nil
  474.         lfgIDs[name] = lfgIDs1
  475.         end
  476.       end
  477.     end
  478.   end
  479.  
  480.   for i,v in pairs(nodes) do
  481.     for j,u in pairs(v) do
  482.       self:UpdateInstanceNames(u)
  483.     end
  484.   end
  485.  
  486.   for i,v in pairs(minimap) do
  487.     for j,u in pairs(v) do
  488.       if (not u.name) then
  489.         self:UpdateInstanceNames(u)
  490.       end
  491.     end
  492.   end
  493. end
  494.  
  495. function Addon:FullUpdate()
  496.   self:PopulateTable()
  497.   self:PopulateMinimap()
  498.   self:ProcessTable()
  499. end
  Reply With Quote