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

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

* https://github.com/TrinityCore/TrinityCore/commit/18b36734f6ac235dbb7a37ba46d1c8d09d8171ab

* 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
+27 -11
View File
@@ -6882,26 +6882,40 @@ SpellCastResult Spell::CheckItems()
case SPELL_EFFECT_CREATE_ITEM: case SPELL_EFFECT_CREATE_ITEM:
case SPELL_EFFECT_CREATE_ITEM_2: case SPELL_EFFECT_CREATE_ITEM_2:
{ {
// xinef: m_targets.GetUnitTarget() means explicit cast, otherwise we dont check for possible equip error // m_targets.GetUnitTarget() means explicit cast, otherwise we dont check for possible equip error
Unit* target = m_targets.GetUnitTarget() ? m_targets.GetUnitTarget() : m_caster; Unit* target = m_targets.GetUnitTarget() ? m_targets.GetUnitTarget() : player;
if (target && target->GetTypeId() == TYPEID_PLAYER && !IsTriggered() && m_spellInfo->Effects[i].ItemType) if (target->GetTypeId() == TYPEID_PLAYER && !IsTriggered())
{ {
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(...) if (m_spellInfo->Effects[i].ItemType)
InventoryResult msg = target->ToPlayer()->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, m_spellInfo->Effects[i].ItemType, 1); {
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) if (msg != EQUIP_ERR_OK)
{ {
ItemTemplate const* pProto = sObjectMgr->GetItemTemplate(m_spellInfo->Effects[i].ItemType); /// @todo Needs review
// TODO: Needs review if (!itemTemplate->ItemLimitCategory)
if (pProto && !(pProto->ItemLimitCategory))
{ {
player->SendEquipError(msg, nullptr, nullptr, m_spellInfo->Effects[i].ItemType); player->SendEquipError(msg, nullptr, nullptr, m_spellInfo->Effects[i].ItemType);
return SPELL_FAILED_DONT_REPORT; return SPELL_FAILED_DONT_REPORT;
} }
else 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; return SPELL_FAILED_TOO_MANY_OF_ITEM;
else if (!(target->ToPlayer()->HasItemCount(m_spellInfo->Effects[i].ItemType))) else if (!(target->ToPlayer()->HasItemCount(m_spellInfo->Effects[i].ItemType)))
{ {
@@ -6909,11 +6923,13 @@ SpellCastResult Spell::CheckItems()
return SPELL_FAILED_DONT_REPORT; return SPELL_FAILED_DONT_REPORT;
} }
else 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; return SPELL_FAILED_DONT_REPORT;
} }
} }
} }
}
break; break;
} }
case SPELL_EFFECT_CREATE_RANDOM_ITEM: case SPELL_EFFECT_CREATE_RANDOM_ITEM: