From c5ce3e4460ec845905a16e7f43755371a6afbf1e Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 31 Jan 2021 02:41:49 +0100 Subject: [PATCH] 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 --- src/server/game/Spells/Spell.cpp | 42 ++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/server/game/Spells/Spell.cpp b/src/server/game/Spells/Spell.cpp index 0ea029fb9..4e9c4fb73 100644 --- a/src/server/game/Spells/Spell.cpp +++ b/src/server/game/Spells/Spell.cpp @@ -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(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)