From b0f7f4a2f0251aa900ce7dbfc19a06fe05f8acfa Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sat, 16 May 2026 14:44:57 -0500 Subject: [PATCH] `GameTooltip:HasItem()` / `GameTooltip:HasSpell()` --- README.md | 2 +- docs/API.md | 19 +++++++++++++++++++ src/item/Tooltip.cpp | 22 ++++++++++++++++++++++ src/spell/Tooltip.cpp | 26 ++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2887b961..bd49c118 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | Faction | `C_Reputation.GetFactionStandings`, `C_Reputation.GetLastStandingChange`, `C_Reputation.GetWatchedFactionData`, `C_Reputation.SetWatchedFactionByID`, `GetFactionIDByIndex`, `GetFactionInfoByID`, `GetFactionParentID` | | FriendList | `C_FriendList.IsWhoQueryPending`, `C_FriendList.SendWhoQueryByName` | | Gossip | `C_GossipInfo.CloseGossip`, `C_GossipInfo.GetActiveQuests`, `C_GossipInfo.GetAvailableQuests`, `C_GossipInfo.GetNumActiveQuests`, `C_GossipInfo.GetNumAvailableQuests`, `C_GossipInfo.GetNumOptions`, `C_GossipInfo.GetOptions`, `C_GossipInfo.GetText`, `C_GossipInfo.SelectActiveQuest`, `C_GossipInfo.SelectAvailableQuest`, `C_GossipInfo.SelectOption`, `C_GossipInfo.SelectOptionByIndex` | -| GameTooltip | `GameTooltip:GetItem`, `GameTooltip:GetSpell`, `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetUnitAura` | +| GameTooltip | `GameTooltip:GetItem`, `GameTooltip:GetSpell`, `GameTooltip:HasItem`, `GameTooltip:HasSpell`, `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetUnitAura` | | Hooks | `hooksecurefunc` | | Input | `IsLeftAltKeyDown`, `IsLeftControlKeyDown`, `IsLeftShiftKeyDown`, `IsModifierKeyDown`, `IsRightAltKeyDown`, `IsRightControlKeyDown`, `IsRightShiftKeyDown` | | Item | `C_Item.DoesItemExist`, `C_Item.DoesItemExistByID`, `C_Item.EquipItemByName`, `C_Item.GetCurrentItemLevel`, `C_Item.GetDetailedItemLevelInfo`, `C_Item.GetItemCount`, `C_Item.GetItemFamily`, `C_Item.GetItemGUID`, `C_Item.GetItemIcon`, `C_Item.GetItemIconByID`, `C_Item.GetItemID`, `C_Item.GetItemInfoInstant`, `C_Item.GetItemInventoryType`, `C_Item.GetItemInventoryTypeByID`, `C_Item.GetItemLink`, `C_Item.GetItemLocation`, `C_Item.GetItemMaxStackSize`, `C_Item.GetItemMaxStackSizeByID`, `C_Item.GetItemName`, `C_Item.GetItemNameByID`, `C_Item.GetItemQuality`, `C_Item.GetItemQualityByID`, `C_Item.GetItemSellPrice`, `C_Item.GetItemSellPriceByID`, `C_Item.GetItemSpell`, `C_Item.GetWeaponEnchantInfo`, `C_Item.IsBound`, `C_Item.IsEquippableItem`, `C_Item.IsEquippedItem`, `C_Item.IsItemDataCached`, `C_Item.IsItemDataCachedByID`, `C_Item.IsLocked`, `C_Item.RequestLoadItemData`, `C_Item.RequestLoadItemDataByID`, `GetAuctionItemID`, `GetAuctionSellItemID`, `GetCraftReagentItemID`, `GetInboxItemID`, `GetInventoryItemDurability`, `GetInventoryItemID`, `GetInventoryItemRepairCost`, `GetItemIcon`, `GetLootRollItemID`, `GetLootSlotItemID`, `GetMerchantItemID`, `GetQuestItemID`, `GetQuestLogItemID`, `GetTradePlayerItemID`, `GetTradeSkillItemID`, `GetTradeSkillReagentItemID`, `GetTradeTargetItemID`, `OffhandHasWeapon` | diff --git a/docs/API.md b/docs/API.md index 6447c31d..00c5359d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -37,6 +37,7 @@ build instructions. - [`GameTooltip:SetInventoryItemByID(itemID)`](#gametooltipsetinventoryitembyiditemid) - [`GameTooltip:GetItem()`](#gametooltipgetitem) - [`GameTooltip:GetSpell()`](#gametooltipgetspell) + - [`GameTooltip:HasItem()` / `GameTooltip:HasSpell()`](#gametooltiphasitem--gametooltiphasspell) - [Quest](#quest) - [`GetQuestIDFromLogIndex(index)`](#getquestidfromlogindexindex) - [`C_QuestLog.RequestLoadQuestByID(questID)`](#c_questlogrequestloadquestbyidquestid) @@ -1115,6 +1116,24 @@ always populated. Equivalent to the function of the same name introduced in 3.0. +### `GameTooltip:HasItem()` / `GameTooltip:HasSpell()` + +Boolean companions to `GetItem` / `GetSpell`. Return `true` if the +tooltip is currently displaying an item / spell, `false` otherwise. +Cheaper than `GetItem` / `GetSpell` when all you need is the +predicate — single `uint32` read against `OFF_TOOLTIP_ITEM_ID` / +`OFF_TOOLTIP_SPELL_ID`, no DBC lookup or link-build. + +```lua +if GameTooltip:HasItem() then + local _, link = GameTooltip:GetItem() + -- only do the GetItem work when we actually need it +end +``` + +Equivalent to the functions of the same name introduced in 3.0 +(`HasItem`) and 6.0 (`HasSpell`). + ### `GameTooltip:SetTalentByID(talentID)` Renders a tooltip for the talent identified by `Talent.dbc` primary diff --git a/src/item/Tooltip.cpp b/src/item/Tooltip.cpp index 46d5595d..5432234b 100644 --- a/src/item/Tooltip.cpp +++ b/src/item/Tooltip.cpp @@ -332,10 +332,32 @@ static int __fastcall Script_GameTooltipGetItem(void *L) { return 3; } +// `GameTooltip:HasItem()` — boolean companion to `GetItem`. Returns +// true iff the tooltip is currently showing an item (any path: +// SetBagItem, SetHyperlink, SetItemByID, SetMerchantItem, etc.). Same +// `[tooltip + OFF_TOOLTIP_ITEM_ID]` check `GetItem` does — that field +// is non-zero only while an item tooltip is live. +static int __fastcall Script_GameTooltipHasItem(void *L) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { + Game::Lua::Error(L, "Usage: GameTooltip:HasItem()"); + return 0; + } + void *tooltipObj = ResolveTooltipObject(L); + if (tooltipObj == nullptr) { + Game::Lua::PushBoolean(L, 0); + return 1; + } + const int itemID = *reinterpret_cast( + static_cast(tooltipObj) + Offsets::OFF_TOOLTIP_ITEM_ID); + Game::Lua::PushBoolean(L, itemID > 0 ? 1 : 0); + return 1; +} + static const Game::Lua::FrameMethodEntry g_methods[] = { {"SetItemByID", &Script_GameTooltipSetItemByID}, {"SetInventoryItemByID", &Script_GameTooltipSetInventoryItemByID}, {"GetItem", &Script_GameTooltipGetItem}, + {"HasItem", &Script_GameTooltipHasItem}, }; static void RegisterLuaFunctions() { diff --git a/src/spell/Tooltip.cpp b/src/spell/Tooltip.cpp index 8dd4db27..f0c21438 100644 --- a/src/spell/Tooltip.cpp +++ b/src/spell/Tooltip.cpp @@ -73,7 +73,7 @@ void ShowByID(void *L, int spellID) { } static int __fastcall Script_GameTooltipSetSpellByID(void *L) { - if (Game::Lua::Type(L, 1) != 5) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { Game::Lua::Error(L, "Usage: GameTooltipSetSpellByID(self, spellID)"); return 0; } @@ -94,7 +94,7 @@ static int __fastcall Script_GameTooltipSetSpellByID(void *L) { // live. Name + rank come from the Spell.dbc record's locale-string // arrays at +0x1E0 and +0x204 (locale index at VAR_LOCALE_INDEX). static int __fastcall Script_GameTooltipGetSpell(void *L) { - if (Game::Lua::Type(L, 1) != 5) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { Game::Lua::Error(L, "Usage: GameTooltip:GetSpell()"); return 0; } @@ -128,9 +128,31 @@ static int __fastcall Script_GameTooltipGetSpell(void *L) { return 3; } +// `GameTooltip:HasSpell()` — boolean companion to `GetSpell`. Returns +// true iff the tooltip is currently showing a spell (any path: +// SetSpell, SetSpellByID, SetUnitBuff/Debuff, etc.). Same +// `[tooltip + OFF_TOOLTIP_SPELL_ID]` check `GetSpell` does — that +// field is non-zero only while a spell tooltip is live. +static int __fastcall Script_GameTooltipHasSpell(void *L) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { + Game::Lua::Error(L, "Usage: GameTooltip:HasSpell()"); + return 0; + } + void *tooltipObj = ResolveTooltipObject(L); + if (tooltipObj == nullptr) { + Game::Lua::PushBoolean(L, 0); + return 1; + } + const int spellID = *reinterpret_cast( + static_cast(tooltipObj) + Offsets::OFF_TOOLTIP_SPELL_ID); + Game::Lua::PushBoolean(L, spellID > 0 ? 1 : 0); + return 1; +} + static const Game::Lua::FrameMethodEntry g_methods[] = { {"SetSpellByID", &Script_GameTooltipSetSpellByID}, {"GetSpell", &Script_GameTooltipGetSpell}, + {"HasSpell", &Script_GameTooltipHasSpell}, }; static void RegisterLuaFunctions() {