fix(Core/Spell): improve check in SPELL_EFFECT_CREATE_ITEM(_2) (#4296)

* fix(Core/Spell): items disappearing in Spell::CheckItems()

* 18b36734f6

* remove GetFreeInventorySpace

* restore p_caster to make other PR needing to merge before this

* fix build

* fuck github conflicts
This commit is contained in:
Kitzunu
2021-01-31 02:41:49 +01:00
committed by GitHub
parent 422ebca567
commit c5ce3e4460

View File

@@ -6881,27 +6881,41 @@ SpellCastResult Spell::CheckItems()
{
case SPELL_EFFECT_CREATE_ITEM:
case SPELL_EFFECT_CREATE_ITEM_2:
{
// m_targets.GetUnitTarget() means explicit cast, otherwise we dont check for possible equip error
Unit* target = m_targets.GetUnitTarget() ? m_targets.GetUnitTarget() : player;
if (target->GetTypeId() == TYPEID_PLAYER && !IsTriggered())
{
// xinef: m_targets.GetUnitTarget() means explicit cast, otherwise we dont check for possible equip error
Unit* target = m_targets.GetUnitTarget() ? m_targets.GetUnitTarget() : m_caster;
if (target && target->GetTypeId() == TYPEID_PLAYER && !IsTriggered() && m_spellInfo->Effects[i].ItemType)
{
ItemPosCountVec dest;
// SPELL_EFFECT_CREATE_ITEM_2 differs from SPELL_EFFECT_CREATE_ITEM in that it picks the random item to create from a pool of potential items,
// so we need to make sure there is at least one free space in the player's inventory
if (m_spellInfo->Effects[i].Effect == SPELL_EFFECT_CREATE_ITEM_2)
if (target->ToPlayer()->GetFreeInventorySpace() == 0)
{
player->SendEquipError(EQUIP_ERR_INVENTORY_FULL, nullptr, nullptr, m_spellInfo->Effects[i].ItemType);
return SPELL_FAILED_DONT_REPORT;
}
// xinef: why do we check p_caster? retards p_caster->CanStoreNewItem(...)
InventoryResult msg = target->ToPlayer()->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, m_spellInfo->Effects[i].ItemType, 1);
if (m_spellInfo->Effects[i].ItemType)
{
ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(m_spellInfo->Effects[i].ItemType);
if (!itemTemplate)
return SPELL_FAILED_ITEM_NOT_FOUND;
uint32 createCount = std::clamp<uint32>(m_spellInfo->Effects[i].CalcValue(), 1u, itemTemplate->GetMaxStackSize());
ItemPosCountVec dest;
InventoryResult msg = target->ToPlayer()->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, m_spellInfo->Effects[i].ItemType, createCount);
if (msg != EQUIP_ERR_OK)
{
ItemTemplate const* pProto = sObjectMgr->GetItemTemplate(m_spellInfo->Effects[i].ItemType);
// TODO: Needs review
if (pProto && !(pProto->ItemLimitCategory))
/// @todo Needs review
if (!itemTemplate->ItemLimitCategory)
{
player->SendEquipError(msg, nullptr, nullptr, m_spellInfo->Effects[i].ItemType);
return SPELL_FAILED_DONT_REPORT;
}
else
{
if (!(m_spellInfo->SpellFamilyName == SPELLFAMILY_MAGE && (m_spellInfo->SpellFamilyFlags[0] & 0x40000000)))
// Conjure Food/Water/Refreshment spells
if (m_spellInfo->SpellFamilyName != SPELLFAMILY_MAGE || (!(m_spellInfo->SpellFamilyFlags[0] & 0x40000000)))
return SPELL_FAILED_TOO_MANY_OF_ITEM;
else if (!(target->ToPlayer()->HasItemCount(m_spellInfo->Effects[i].ItemType)))
{
@@ -6909,13 +6923,15 @@ SpellCastResult Spell::CheckItems()
return SPELL_FAILED_DONT_REPORT;
}
else
player->CastSpell(m_caster, m_spellInfo->Effects[EFFECT_1].CalcValue(), false); // move this to anywhere
player->CastSpell(player, m_spellInfo->Effects[EFFECT_1].CalcValue(), false); // move this to anywhere
return SPELL_FAILED_DONT_REPORT;
}
}
}
break;
}
break;
}
case SPELL_EFFECT_CREATE_RANDOM_ITEM:
{
if (player->GetFreeInventorySpace() == 0)