WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Check if itemString or itemLink (https://www.wowinterface.com/forums/showthread.php?t=55945)

Marsgames 12-30-17 05:29 PM

Check if itemString or itemLink
 
Hey, I would like to know if there’s a way to check if I have an itemLink or an itemString.

I’m using
Lua Code:
  1. GetInventoryItemLink("player", GetInventorySlotInfo("HeadSlot"))
to get all my equipped items, but because of the item is not always cached, sometimes I get something like this : "[Tome de la raison déclinante]" that is an itemLink, but sometimes I only get "[]". At first I thought it was nothing but after some research, I found that it was an itemString, and if I try to parse it with
Lua Code:
  1. local aze = GetInventoryItemLink("player",GetInventorySlotInfo("Trinket0Slot"))
  2. local itemString = select(3, strfind(aze, "|H(.+)|h"))
  3. print(itemString)
It returns "item:147019::::::::110:265::3:3:3561:1492:3528:::[]"

So, as I want to have an itemLink, is there a way to check if it returns an itemLink or an itemString, like that I can wait for ITEM_INFO_RECEIVED to add the itemLink in my table.

Tim 12-30-17 05:54 PM

What is the main goal you're trying to accomplish?

I would use GetInventoryItemID to get the ID of the item and then pass it through GetItemInfo to get the itemLink or itemString.

Marsgames 12-30-17 06:10 PM

I'm trying to have a table of itemLinks of all items that are equipped. My addon check if an item looted is better than an item equipped, but because of items are not cached, when I do GetInventoryItemLink it adds an itemString into my table, and when I do a GetItemInfo on this item to compare the looted one with the equipped one, I got a an error "Usage: GetItemInfo(itemID | "name" | "itemlink")"

Tim 12-30-17 06:18 PM

I would recommend downloading Personal Loot Helper and see how it retrieves the information it uses to determine upgrades for you and/or party/raid members.

Marsgames 12-30-17 06:19 PM

I will try this. Thank you for your help

Lombra 01-01-18 12:30 PM

Quote:

Originally Posted by Marsgames (Post 326316)
Lua Code:
  1. local itemString = select(3, strfind(aze, "|H(.+)|h"))
  2. print(itemString)

There are two occurences of |h in an item link. The first one comes immediately after the item string portion, and the second comes after the link text portion. By using + in your pattern, the result will include the link text. You should use - instead to get only the item string. I don't know if that helps, but I think you should be able to pass proper item strings to GetItemInfo.

Kanegasi 01-01-18 12:57 PM

Quote:

Originally Posted by Lombra (Post 326329)
There are two occurences of |h in an item link. The first one comes immediately after the item string portion, and the second comes after the link text portion. By using + in your pattern, the result will include the link text. You should use - instead to get only the item string. I don't know if that helps, but I think you should be able to pass proper item strings to GetItemInfo.

It would actually be better to include the second |h in the pattern: |H(.*)|h.*|h

Marsgames 02-06-18 05:25 AM

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

lightspark 02-06-18 07:11 AM

Quote:

Originally Posted by Marsgames (Post 326749)
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.


All times are GMT -6. The time now is 01:27 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI