View Single Post
02-06-18, 07:11 AM   #9
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by Marsgames View Post
Hey,

I have a new question. Is it possible to convert an itemString into an itemLink without using GetItemInfo() to avoid the possible nil due to item not in cache
They're pretty much interchangeable.

Just store item ID of an item that isn't cached in some table, then compare it to an item ID that comes w/ "GET_ITEM_INFO_RECEIVED", then re-request item's info and save it.

And, IMHO, it's better to use item strings for storing item's info in SVs.

Lua Code:
  1. local frame = CreateFrame("Frame")
  2. local notCached = {}
  3.  
  4. frame:SetScript("OnEvent", function(_, event, arg1)
  5.     if event == "PLAYER_LOGIN" then
  6.         local slotID = GetInventorySlotInfo("Trinket0Slot")
  7.         local itemLink = GetInventoryItemLink("player", slotID)
  8.         local itemID = GetInventoryItemID("player", slotID)
  9.         local itemString, itemName
  10.  
  11.         if itemLink then
  12.             itemString, itemName = itemLink:match("|H(.*)|h%[(.*)%]|h")
  13.         end
  14.  
  15.         -- if there's no itemID, then there's no item :p
  16.         if itemID then
  17.             if not itemName or itemName == "" then
  18.                 GetItemInfo(itemID) -- for GET_ITEM_INFO_RECEIVED
  19.                 notCached[itemID] = slotID
  20.             else
  21.                 -- do stuff here
  22.             end
  23.         end
  24.     elseif event == "GET_ITEM_INFO_RECEIVED" then
  25.         if notCached[arg1] then
  26.             -- do stuff here
  27.             -- remove item's ID from the table
  28.             notCached[arg1] = nil
  29.         end
  30.     end
  31.  
  32. end)
  33.  
  34. frame:RegisterEvent("GET_ITEM_INFO_RECEIVED")
  35. frame:RegisterEvent("PLAYER_LOGIN")

-- edit #1

Something like this

Lua Code:
  1. local frame = CreateFrame("Frame")
  2. local notCached = {}
  3. local equippedItems = {
  4.     [INVSLOT_HEAD] = false,     [INVSLOT_HAND] = false,
  5.     [INVSLOT_NECK] = false,     [INVSLOT_WAIST] = false,
  6.     [INVSLOT_SHOULDER] = false, [INVSLOT_LEGS] = false,
  7.     [INVSLOT_BACK] = false,     [INVSLOT_FEET] = false,
  8.     [INVSLOT_CHEST] = false,    [INVSLOT_FINGER1] = false,
  9.     [INVSLOT_BODY] = false,     [INVSLOT_FINGER2] = false,
  10.     [INVSLOT_TABARD] = false,   [INVSLOT_TRINKET1] = false,
  11.     [INVSLOT_WRIST] = false,    [INVSLOT_TRINKET2] = false,
  12.     [INVSLOT_MAINHAND] = false, [INVSLOT_OFFHAND] = false,
  13. }
  14.  
  15. local function getEquippedItemString(slotID)
  16.     local itemLink = GetInventoryItemLink("player", slotID)
  17.     local itemID = GetInventoryItemID("player", slotID)
  18.     local itemString, itemName
  19.  
  20.     if itemLink then
  21.         itemString, itemName = itemLink:match("|H(.*)|h%[(.*)%]|h")
  22.     end
  23.  
  24.     -- if there's no itemID, then there's no item :p
  25.     if itemID then
  26.         if not itemName or itemName == "" then
  27.             GetItemInfo(itemID) -- for GET_ITEM_INFO_RECEIVED
  28.             notCached[itemID] = slotID
  29.         else
  30.             return itemString
  31.         end
  32.     else
  33.         return false
  34.     end
  35. end
  36.  
  37. frame:SetScript("OnEvent", function(_, event, arg1)
  38.     if event == "PLAYER_LOGIN" then
  39.         for slotID in next, equippedItems do
  40.             equippedItems[slotID] = getEquippedItemString(slotID)
  41.         end
  42.     elseif event == "GET_ITEM_INFO_RECEIVED" then
  43.         if notCached[arg1] then
  44.             local slotID = notCached[arg1]
  45.             notCached[arg1] = nil
  46.  
  47.             equippedItems[slotID] = getEquippedItemString(slotID)
  48.         end
  49.     end
  50. end)
  51.  
  52. frame:RegisterEvent("GET_ITEM_INFO_RECEIVED")
  53. frame:RegisterEvent("PLAYER_LOGIN")

equippedItems is a table where indices are slot IDs, I organised them in a way that resembles their placement on char info panel.
__________________

Last edited by lightspark : 02-06-18 at 07:52 AM.
  Reply With Quote