View Single Post
02-24-24, 11:32 AM   #33
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,894
Probably best to make separate threads for each question as the code is getting pretty messy even for just one (hopefully Xrystal has answered your timer question).

Having multiple item links and locales in your addon.db texts complicates things because the order you want the links to display might be different in german to the order displayed in french and might be different again for english.

This requires identifying the order each link needs to be in for every locale text. With that in mind, the structure of addon.db needs to change so this is one way you could do it (my german in non-existant so it didn't even try):

NOTE: the number if items in each itemOrder should be the same as the number of %s tokens that are in the corresponding text field.
(This is probably overkill as it's replacing all the locale texts and you probably only need the ones that will be actually be used but that depends on what the end addon requires so...)

Lua Code:
  1. local addonName, addon = ...
  2. addon.db = {
  3.     {
  4.         name = {
  5.             enUS = "Herb-Infused Water",
  6.             deDE = "Mit Kräutern aromatisiertes Wasser"
  7.         },
  8.         questID = 75612,
  9.         icon = "interface/icons/inv_mushroom_11",
  10.         announce = {
  11.              enUS = { -- the text to display for english
  12.                  text = "Awarded for %s outstanding service %s to Dragonkind.\n%s\nBring it to Theozhaklos the Curious at the Wellspring \nOverlook in the Emerald %s Dream to receive powerful \nequipment for your efforts",
  13.                  itemOrder = { -- the order to display the items in english
  14.                      210399,
  15.                      210400,
  16.                      210401,
  17.                      210402,
  18.                  },
  19.              },
  20.              deDE = { -- the text to display for german
  21.                  text = "The items %s might be %s in a different %s order of items %s in german",
  22.                  itemOrder = { -- the order to display the items in german
  23.                      210401,
  24.                      210402,
  25.                      210399,
  26.                      210400,
  27.                  },
  28.              },
  29.         },
  30.     },
  31.     {
  32.         name = "Emerald Mark of Mastery",
  33.         questID = 75624,
  34.         icon = "interface/icons/inv_mushroom_11",
  35.         announce = {
  36.             enUS = { -- the text to display for english
  37.                 text = "Awarded for outstanding service to Dragonkind.\n%s\nBring it to Theozhaklos the Curious at the Wellspring \nOverlook in the Emerald Dream to receive powerful \nequipment for your efforts",
  38.                 itemOrder = { -- the order to display the items in english
  39.                     20897,
  40.                 },
  41.             },
  42.             deDE = { -- the text to display for german
  43.                 text = "Only one %s in this entry",
  44.                 itemOrder = { -- the order to display the items in german
  45.                     20897,
  46.                 },
  47.             },
  48.         }
  49.     },
  50. }
  51. ---------------------------------------------------------------------------------------------------
  52. -- Code to replace %s in announce texts with item links. Replaces the GetItemLinkById(...) function
  53.  
  54. local itemList = {} -- table to save the itsmID to
  55.  
  56. for index, v in ipairs(addon.db) do -- get/save every itmeID in addon.db announce
  57.    for k, itemID in pairs(v.announce.enUS.itemOrder) do -- assumes all announce entries will use all the same itemIDs
  58.       if not itemList[itemID] then
  59.          itemList[itemID] = true
  60.       end
  61.    end
  62. end
  63.  
  64. local function FormatTexts() -- fill addon.db with the item links
  65.    for index, v in ipairs(addon.db) do
  66.       for locale, settings in pairs(v.announce) do
  67.          local order = {}  
  68.          for i, link in ipairs(settings.itemOrder) do -- get the links in order from addon.db
  69.             order[i] = itemList[link] -- and save them into a tmporary table
  70.          end
  71.          settings.text = format(settings.text, unpack(order)) -- replace %s with the ordered item links (unpack(table) returns a number keyed tables entries in order eg. order[1], order[2], order[3] etc.)
  72.       end
  73.    end
  74.  
  75. end
  76.  
  77. local function LoadItem(item) -- Get the links for all the saved itemIDs
  78.    if item then
  79.       itemList[item:GetItemID()] = item:GetItemLink()
  80.    end
  81.    local key = next(itemList, item and item.Next or nil)
  82.    if not key then -- when we have all the links for all the items
  83.       FormatTexts() -- Run FormatTexts() to do the text replacements
  84.       return
  85.    end
  86.    local nextItem = Item:CreateFromItemID(key)
  87.    nextItem.Next = key
  88.    nextItem:ContinueOnItemLoad(function() LoadItem(nextItem) end)
  89. end
  90. LoadItem()
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-24-24 at 02:21 PM.
  Reply With Quote