How to find item equip slot

How to find the item’s equip slot?

I’m trying to auto equip a new item to the player which doesn’t exist in the player’s bag, but world crash because of uint16 equipslot

I used:

// item template for warglaive off azzinoth off hand
uint32 warglaive_of_azzinoth = 32838;
ItemTemplate const* pProto = sObjectMgr->GetItemTemplate(warglaive_of_azzinoth);

int32 count = 1;
uint32 noSpaceForCount = 0;
ItemPosCountVec dest;

InventoryResult msg1 = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, warglaive_of_azzinoth, 1, &noSpaceForCount);

uint16 equipslot = player->FindEquipSlot(pProto, pProto->InventoryType, true);

if (msg1 != EQUIP_ERR_OK)
count -= noSpaceForCount;

if (count == 0 || dest.empty())
return;

player->EquipNewItem(equipslot, warglaive_of_azzinoth, true);

Shouldn’t this player->FindEquipSlot(pProto, pProto->InventoryType, true) convert item’s inventory id to equip slot id?

Well for starters, you’re passing the wrong value for slot into FindEquipSlot()

pProto->InventoryType maps to values such as INVTYPE_SHIELD, INVTYPE_RANGED, etc. so when the value for slot gets used later in the function it has the wrong data in it and the function ultimately fails and returns NULL_SLOT. Then you don’t check for this value in equipslot before calling EquipNewItem().

In the snippet below, your call would execute the first part of the if…then because your slot value does not equal NULL_SLOT. In turn, you have swap = true so it will iterate over slots but none of them will fit so you fall out the bottom of the function. **


if (slot != NULL_SLOT)
{
if (swap || !GetItemByPos(INVENTORY_SLOT_BAG_0, slot))
for (uint8 i = 0; i < 4; ++i)
if (slots[i] == slot)
return slot;
}
else
{
// search free slot at first
for (uint8 i = 0; i < 4; ++i)
if (slots[i] != NULL_SLOT && !GetItemByPos(INVENTORY_SLOT_BAG_0, slots[i]))
// in case 2hand equipped weapon (without titan grip) offhand slot empty but not free
if (slots[i] != EQUIPMENT_SLOT_OFFHAND || !IsTwoHandUsed())
return slots[i];

    // if not found free and can swap return first appropriate from used
    for (uint8 i = 0; i < 4; ++i)
        if (slots[i] != NULL_SLOT && swap)
            return slots[i];
}

// no free position
return NULL_SLOT;


** Actually, there’s an off chance you might find a valid match where slots == slot (because the values for the enums overlap) but because your passing an INVTYPE_* value into the function, it might actually be the wrong slot. For Example:



EQUIPMENT_SLOT_OFFHAND = 16


INVTYPE_CLOAK = 16



In this case if slots is EQUIPMENT_SLOT_OFFHAND but you passed INVTYPE_CLOAK for slot, they will match but they aren’t exactly the same thing.