View Single Post
12-22-23, 06:42 AM   #9
Ssesmar
A Deviate Faerie Dragon
Join Date: Oct 2023
Posts: 15
I'm trying to split my code into multiple files, this is the first time I've ever done this. If I move it to another file, the open/close chat commands work fine without any errors. There is an error in the MBSHOW/MBHIDE commands. Although the commands are executed correctly, the minimap button is displayed or hidden. But these chat commands are not saved permanently (the function) and I also get this
Code:
error:modules/chatcommands.lua:37: attempt to index field 'show' (a nil value)
here are my files:

toc:
Code:
modules\defaults.lua 
modules\mmb.lua
modules\chatcommands.lua
defaults.lua and mmb.lua working fine without errors.

chatcommands.lua:
Lua Code:
  1. local ADDON_NAME, ns = ...
  2.  
  3. local L = LibStub("AceLocale-3.0"):GetLocale(ADDON_NAME)
  4. local COLORED_ADDON_NAME = "|cffff0000Map|r|cff00ccffNotes|r"
  5. local MNMMBIcon = LibStub("LibDBIcon-1.0", true)
  6.  
  7. local db = { }
  8.  
  9. 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";
  10. function SlashCmdList.INFO(msg, editbox)
  11.   print("|cff00ccff".."------------------------------------------------------------------------------------------")
  12.   print("|cffffff00~~"..COLORED_ADDON_NAME .. "|cffffff00~~")
  13.   print("|cffffff00".. L["Chat commands:"])
  14.   print("|cffffff00                      • ".. L["to open MapNotes menu: /mno, /MNO"])
  15.   print("|cffffff00                      • ".. L["to close MapNotes menu: /mnc, /MNC"])
  16.   print("|cffffff00                      • ".. L["to show minimap button: /mnb or /MNB"])
  17.   print("|cffffff00                      • ".. L["to hide minimap button: /mnbh or /MNBH"])
  18.   print("|cffffff00~~"..COLORED_ADDON_NAME .. "|cffffff00~~")
  19.   print("|cff00ccff".."------------------------------------------------------------------------------------------")
  20. end
  21.  
  22. SLASH_OPEN1, SLASH_OPEN2 = "/mno", "/MNO";
  23. function SlashCmdList.OPEN(msg, editbox)
  24.   LibStub("AceConfigDialog-3.0"):Open("MNMiniMapButton")
  25.   print(COLORED_ADDON_NAME.."|cffffff00  ".. L["MapNotes menu window"], "|cff00ff00" .. L["is activated"])
  26. end
  27.  
  28. SLASH_CLOSE1, SLASH_CLOSE2 = "/mnc", "/MNC";
  29. function SlashCmdList.CLOSE(msg, editbox)
  30.   LibStub("AceConfigDialog-3.0"):Close("MNMiniMapButton")
  31.   print(COLORED_ADDON_NAME.."|cffffff00  ".. L["MapNotes menu window"], "|cffff0000" .. L["is deactivated"])
  32. end
  33.  
  34. SLASH_MMBSHOW1, SLASH_MMBSHOW2 = "/mnb", "/MNB";
  35. function SlashCmdList.MMBSHOW(msg, editbox)
  36.   MNMMBIcon:Show("MNMiniMapButton")
  37.   db.show.HideMMB = false
  38.   print(COLORED_ADDON_NAME .. "|cffffff00  " .. L["-> MiniMapButton <-"], "|cff00ff00" .. L["is activated"])
  39. end
  40.  
  41. SLASH_MMBHIDE1, SLASH_MMBHIDE2 = "/mnbh", "/MNBH";
  42. function SlashCmdList.MMBHIDE(msg, editbox)
  43.   MNMMBIcon:Hide("MNMiniMapButton")
  44.   db.show.HideMMB = true
  45.   print(COLORED_ADDON_NAME .. "|cffffff00  " .. L["-> MiniMapButton <-"], "|cffff0000" .. L["is deactivated"])
  46. end

and the main file:

Lua Code:
  1. local ADDON_NAME, ns = ...
  2.  
  3. local HandyNotes = LibStub("AceAddon-3.0"):GetAddon("HandyNotes", true)
  4. if not HandyNotes then return end
  5.  
  6. local ADDON_NAME = "HandyNotes_MapNotes"
  7. local MapNotesMiniButton = LibStub("AceAddon-3.0"):NewAddon("MNMiniMapButton", "AceConsole-3.0")  
  8. local MNMMBIcon = LibStub("LibDBIcon-1.0", true)
  9.  
  10.  
  11. local db = { }
  12. local nodes = { }
  13. local minimap = { }
  14. local lfgIDs = { }
  15. local assignedIDs = { }
  16.  
  17.  
  18. function MapNotesMiniButton:OnInitialize()
  19.   self.db = LibStub("AceDB-3.0"):New("MNMiniMapButtonDB", { profile = { minimap = { hide = false, }, }, })
  20.   MNMMBIcon:Register("MNMiniMapButton", ns.miniButton, self.db.profile.minimap)
  21. end
  22.  
  23. local function updateAssignedID()
  24.     table.wipe(assignedIDs)
  25.     for i=1,GetNumSavedInstances() do
  26.         local name, _, _, _, locked, _, _, _, _, difficultyName, numEncounters, encounterProgress = GetSavedInstanceInfo(i)
  27.         if (locked) then
  28.             if (not assignedIDs[name]) then
  29.             assignedIDs[name] = { }
  30.             end
  31.             assignedIDs[name][difficultyName] = encounterProgress .. "/" .. numEncounters
  32.         end
  33.     end
  34. end
  35.  
  36. local pluginHandler = { }
  37. function pluginHandler:OnEnter(uiMapId, coord)
  38.   local nodeData = nil
  39.  
  40.     if (minimap[uiMapId] and minimap[uiMapId][coord]) then
  41.       nodeData = minimap[uiMapId][coord]
  42.     end
  43.     if (nodes[uiMapId] and nodes[uiMapId][coord]) then
  44.       nodeData = nodes[uiMapId][coord]
  45.     end
  46.    
  47.     if (not nodeData) then return end
  48.    
  49.     local tooltip = self:GetParent() == WorldMapButton and WorldMapTooltip or GameTooltip
  50.     if ( self:GetCenter() > UIParent:GetCenter() ) then
  51.       tooltip:SetOwner(self, "ANCHOR_LEFT")
  52.     else
  53.         tooltip:SetOwner(self, "ANCHOR_RIGHT")
  54.     end
  55.  
  56.     if (not nodeData.name) then return end
  57.  
  58.     local instances = { strsplit("\n", nodeData.name) }
  59.    
  60.  
  61.     updateAssignedID()
  62.    
  63.     for i, v in pairs(instances) do
  64.       if (db.assignedID and (assignedIDs[v] or (lfgIDs[v] and assignedIDs[lfgIDs[v]]))) then
  65.         if (assignedIDs[v]) then
  66.           for a,b in pairs(assignedIDs[v]) do
  67.             tooltip:AddDoubleLine(v, a .. " " .. b, 1, 1, 1, 1, 1, 1)
  68.           end
  69.         end
  70.       if (lfgIDs[v] and assignedIDs[lfgIDs[v]]) then
  71.         for a,b in pairs(assignedIDs[lfgIDs[v]]) do
  72.           tooltip:AddDoubleLine(v, a .. " " .. b, 1, 1, 1, 1, 1, 1)
  73.         end
  74.       end
  75.       else
  76.         tooltip:AddLine(v, nil, nil, nil, false)
  77.       end
  78.     end
  79.     tooltip:Show()
  80. end
  81.  
  82. function pluginHandler:OnLeave(uiMapID, coord)
  83.     if self:GetParent() == WorldMapButton then
  84.       WorldMapTooltip:Hide()
  85.     else
  86.       GameTooltip:Hide()
  87.     end
  88. end
  89.  
  90. do
  91.     local tablepool = setmetatable({}, {__mode = 'k'})
  92.    
  93.     local function deepCopy(object)
  94.         local lookup_table = {}
  95.         local function _copy(object)
  96.             if type(object) ~= "table" then
  97.                 return object
  98.             elseif lookup_table[object] then
  99.                 return lookup_table[object]
  100.             end
  101.  
  102.             local new_table = {}
  103.               lookup_table[object] = new_table
  104.             for index, value in pairs(object) do
  105.                 new_table[_copy(index)] = _copy(value)
  106.             end
  107.  
  108.             return setmetatable(new_table, getmetatable(object))
  109.         end
  110.             return _copy(object)
  111.     end
  112.  
  113.     local function iter(t, prestate)
  114.         if not t then return end
  115.         local data = t.data
  116.  
  117.         local state, value = next(data, prestate)
  118.  
  119.         while value do
  120.  
  121.       local alpha
  122.            
  123.             local allLocked = true
  124.             local anyLocked = false
  125.             if value.name == nil then value.name = value.id end
  126.             local instances = { strsplit("\n", value.name) }
  127.             for i, v in pairs(instances) do
  128.                 if (not assignedIDs[v] and not assignedIDs[lfgIDs[v]]) then
  129.                     allLocked = false
  130.                 else
  131.                     anyLocked = true
  132.                 end
  133.             end
  134.  
  135.             local icon = icons[value.type]
  136.             if ((anyLocked and db.graymultipleID) or (allLocked and not db.graymultipleID)) then
  137.                 icon = icons["Locked"]
  138.             end
  139.  
  140.       if ((anyLocked and db.invertlockout) or (allLocked and not db.invertlockout) and db.uselockoutalpha) then
  141.                 alpha = db.mapnoteAlpha
  142.             else
  143.                 alpha = db.mapnoteAlpha
  144.             end
  145.  
  146.       if db.show.Azeroth then
  147.               return state, nil, icon, db.mapnoteScale, alpha
  148.             end
  149.             state, value = next(data, state)
  150.         end
  151.         wipe(t)
  152.         tablepool[t] = true
  153.     end
  154.  
  155.  
  156.     local function iterCont(t, prestate)
  157.         if not t then return end
  158.  
  159.     local zone = t.C[t.Z]
  160.         local data = nodes[zone]
  161.         local state, value
  162.  
  163.         while zone do
  164.             if data then
  165.                 state, value = next(data, prestate)
  166.  
  167.                 while state do
  168.           local icon, alpha
  169.  
  170.                     local allLocked = true
  171.                     local anyLocked = false
  172.           --
  173.                     local instances = { strsplit("\n", value.name) }
  174.                     for i, v in pairs(instances) do
  175.                         if (not assignedIDs[v] and not assignedIDs[lfgIDs[v]]) then
  176.                             allLocked = false
  177.                         else
  178.                             anyLocked = true
  179.                         end
  180.                     end
  181.  
  182.           icon = icons[value.type]
  183.                     if ((anyLocked and db.assignedgray) or (allLocked and db.assignedgray)) then  
  184.                         icon = icons["Locked"]
  185.                     end
  186.           if ((anyLocked and db.invertlockout) or (allLocked and not db.invertlockout) and db.uselockoutalpha) then
  187.                         alpha = db.mapnoteAlpha
  188.                     else
  189.                         alpha = db.mapnoteAlpha
  190.                     end
  191.  
  192.                     if db.show.Continent then
  193.                         return state, zone, icon, db.mapnoteScale, alpha
  194.           end
  195.                     state, value = next(data, state)
  196.                 end
  197.             end
  198.             t.Z = next(t.C, t.Z)
  199.             zone = t.C[t.Z]
  200.             data = nodes[zone]
  201.             prestate = nil
  202.         end
  203.         wipe(t)
  204.         tablepool[t] = true
  205.     end
  206.  
  207.     function pluginHandler:GetNodes2(uiMapId, isMinimapUpdate, coord)
  208.         local C = deepCopy(HandyNotes:GetContinentZoneList(uiMapId))
  209.         if C then
  210.             table.insert(C, uiMapId)
  211.             local tbl = next(tablepool) or {}
  212.             tablepool[tbl] = nil
  213.             tbl.C = C
  214.             tbl.Z = next(C)
  215.             tbl.contId = uiMapId
  216.             return iterCont, tbl, nil
  217.         else
  218.             if (nodes[uiMapId] == nil) then return iter end
  219.             local tbl = next(tablepool) or {}
  220.             tablepool[tbl] = nil
  221.             tbl.minimapUpdate = isMinimapUpdate
  222.             if (isMinimapUpdate and minimap[uiMapId]) then
  223.                 tbl.data = minimap[uiMapId]
  224.             else
  225.                 tbl.data = nodes[uiMapId]
  226.             end
  227.             return iter, tbl, nil
  228.         end
  229.     end
  230. end
  231.  
  232. local waypoints = {}
  233. local function setWaypoint(uiMapID, coord)
  234.     local dungeon = nodes[uiMapID][coord]
  235.  
  236.     local waypoint = nodes[dungeon]
  237.     if waypoint and TomTom:IsValidWaypoint(waypoint) then
  238.         return
  239.     end
  240.  
  241.     local title = dungeon.name
  242.     local x, y = HandyNotes:getXY(coord)
  243.     waypoints[dungeon] = TomTom:AddWaypoint(uiMapID, x, y, {
  244.         title = dungeon.name,
  245.         persistent = nil,
  246.         minimap = true,
  247.         world = true
  248.     })
  249. end
  250.  
  251. function pluginHandler:OnClick(button, pressed, uiMapId, coord)
  252.     if (not pressed) then return end
  253.     if IsShiftKeyDown() and (button == "RightButton" and db.tomtom and TomTom) then
  254.         setWaypoint(uiMapId, coord)
  255.         return
  256.         end
  257.     if (button == "LeftButton" and db.journal) then
  258.         if (not EncounterJournal_OpenJournal) then
  259.         UIParentLoadAddOn('Blizzard_EncounterJournal')
  260.         end
  261.         local dungeonID
  262.         if (type(nodes[uiMapId][coord].id) == "table") then
  263.             dungeonID = nodes[uiMapId][coord].id[1]
  264.         else
  265.             dungeonID = nodes[uiMapId][coord].id
  266.         end
  267.  
  268.         if (not dungeonID) then return end
  269.  
  270.         local name, _, _, _, _, _, _, link = EJ_GetInstanceInfo(dungeonID)
  271.         if not link then return end
  272.         local difficulty = string.match(link, 'journal:.-:.-:(.-)|h')
  273.         if (not dungeonID or not difficulty) then return end
  274.         EncounterJournal_OpenJournal(difficulty, dungeonID)
  275.         _G.EncounterJournal:SetScript("OnShow", BBBEncounterJournal_OnShow)
  276.     end
  277. end
  278.  
  279. local Addon = CreateFrame("Frame")
  280. Addon:RegisterEvent("PLAYER_LOGIN")
  281. Addon:SetScript("OnEvent", function(self, event, ...) return self[event](self, ...)end)
  282.  
  283. local function updateStuff()
  284.   updateAssignedID()
  285.   HandyNotes:SendMessage("HandyNotes_NotifyUpdate", "MapNotes")
  286. end
  287.  
  288. function Addon:PLAYER_ENTERING_WORLD()
  289.   if (not self.faction) then
  290.       self.faction = UnitFactionGroup("player")
  291.       self:PopulateTable()
  292.       self:PopulateMinimap()
  293.       self:ProcessTable()
  294.   end
  295.  
  296.     updateAssignedID()
  297.     updateStuff()
  298. end
  299.  
  300. function Addon:PLAYER_LOGIN()
  301.  
  302.   HandyNotes:RegisterPluginDB("MapNotes", pluginHandler, ns.options)
  303.   self.db = LibStub("AceDB-3.0"):New(ADDON_NAME .. "DB", ns.defaults, true)
  304.   db = self.db.profile
  305.   Addon:RegisterEvent("PLAYER_ENTERING_WORLD")
  306.   LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("MNMiniMapButton", ns.options)
  307.   if db.show.HideMMB then
  308.     MNMMBIcon:Hide("MNMiniMapButton")
  309.   end
  310. end
  311.  
  312. function Addon:PopulateMinimap()
  313.     local temp = { }
  314.     for k,v in pairs(nodes) do
  315.         if (minimap[k]) then
  316.             for a,b in pairs(minimap[k]) do
  317.                 temp[a] = true
  318.             end
  319.             for c,d in pairs(v) do
  320.                 if (not temp[c] and not d.hideOnMinimap) then
  321.                     minimap[k][c] = d
  322.                 end
  323.             end
  324.         end
  325.     end
  326. end
  327.  
  328. function Addon:UpdateInstanceNames(node)
  329.   local dungeonInfo = EJ_GetInstanceInfo
  330.     local id = node.id
  331.  
  332.       if (node.lfgid) then
  333.         dungeonInfo = GetLFGDungeonInfo
  334.         id = node.lfgid
  335.       end
  336.  
  337.       if (type(id) == "table") then
  338.         for i,v in pairs(node.id) do
  339.           local name = dungeonInfo(v)
  340.             self:UpdateAlter(v, name)
  341.           if (node.name) then
  342.             node.name = node.name .. "\n" .. name
  343.           else
  344.             node.name = name
  345.           end
  346.         end
  347.       elseif (id) then
  348.         node.name = dungeonInfo(id)
  349.         self:UpdateAlter(id, node.name)
  350.       end
  351. end
  352.  
  353. function Addon:FullUpdate()
  354.   self:PopulateTable()
  355.   self:PopulateMinimap()
  356.   self:ProcessTable()
  357. end

maybe someone can help me, I'm currently stuck on how to connect
Code:
db.show.HideMMB = false
or
Code:
db.show.HideMMB = true
to the main file. So that the "show" functions work
  Reply With Quote