Thread Tools Display Modes
12-30-17, 05:29 PM   #1
Marsgames
An Aku'mai Servant
 
Marsgames's Avatar
Join Date: Jul 2016
Posts: 33
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.
  Reply With Quote
12-30-17, 05:54 PM   #2
Tim
A Rage Talon Dragon Guard
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 308
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.
  Reply With Quote
12-30-17, 06:10 PM   #3
Marsgames
An Aku'mai Servant
 
Marsgames's Avatar
Join Date: Jul 2016
Posts: 33
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")"
  Reply With Quote
12-30-17, 06:18 PM   #4
Tim
A Rage Talon Dragon Guard
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 308
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.
__________________
AddOns: Tim @ WoWInterface
Characters: Mage, Priest, Devoker, Pally
Battle Tag: Mysterio#11164
Current PC Setup: PCPartPicker List
  Reply With Quote
12-30-17, 06:19 PM   #5
Marsgames
An Aku'mai Servant
 
Marsgames's Avatar
Join Date: Jul 2016
Posts: 33
I will try this. Thank you for your help
  Reply With Quote
01-01-18, 12:30 PM   #6
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Originally Posted by Marsgames View Post
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.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
01-01-18, 12:57 PM   #7
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Originally Posted by Lombra View Post
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
  Reply With Quote
02-06-18, 05:25 AM   #8
Marsgames
An Aku'mai Servant
 
Marsgames's Avatar
Join Date: Jul 2016
Posts: 33
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
  Reply With Quote
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

WoWInterface » Developer Discussions » General Authoring Discussion » Check if itemString or itemLink

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off