Core/Spells: don't allow spells with SPELL_EFFECT_CREATE_ITEM_2 to be cast if there isn't enough space in inventory

This commit is contained in:
Wyrserth authored and Ovahlord committed 2022-09-12 09:15:24 +02:00
1 parent 85893e9a44
commit 2f5fd3f227
4 files changed
+61 -42

No files matched your search

@@ -9899,6 +9899,28 @@ Bag* Player::GetBagByPos(uint8 bag) const
return nullptr;
}
uint32 Player::GetFreeInventorySpace() const
{
uint32 freeSpace = 0;
// Check backpack
for (uint8 slot = INVENTORY_SLOT_ITEM_START; slot < INVENTORY_SLOT_ITEM_END; ++slot)
{
Item* item = GetItemByPos(INVENTORY_SLOT_BAG_0, slot);
if (!item)
freeSpace += 1;
}
// Check bags
for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++)
{
if (Bag* bag = GetBagByPos(i))
freeSpace += bag->GetFreeSlots();
}
return freeSpace;
}
Item* Player::GetWeaponForAttack(WeaponAttackType attackType, bool useable /*= false*/) const
{
uint8 slot;
+1
View File
@@ -1150,6 +1150,7 @@ class TC_GAME_API Player : public Unit, public GridObject<Player>
Item* GetItemByPos(uint8 bag, uint8 slot) const;
Item* GetUseableItemByPos(uint8 bag, uint8 slot) const;
Bag* GetBagByPos(uint8 slot) const;
uint32 GetFreeInventorySpace() const;
Item* GetWeaponForAttack(WeaponAttackType attackType, bool useable = false) const;
Item* GetShield(bool useable = false) const;
static WeaponAttackType GetAttackBySlot(uint8 slot); // MAX_ATTACK if not weapon slot
+28 -21
View File
@@ -7106,37 +7106,44 @@ SpellCastResult Spell::CheckItems(uint32* param1 /*= nullptr*/, uint32* param2 /
{
// 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 && target->GetTypeId() == TYPEID_PLAYER && !IsTriggered() && m_spellInfo->Effects[i].ItemType)
if (target->GetTypeId() == TYPEID_PLAYER && !IsTriggered())
{
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)
{
/// @todo Needs review
if (itemTemplate->GetItemLimitCategory())
// 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(msg, nullptr, nullptr, m_spellInfo->Effects[i].ItemType);
player->SendEquipError(EQUIP_ERR_INV_FULL, nullptr, nullptr, m_spellInfo->Effects[i].ItemType);
return SPELL_FAILED_DONT_REPORT;
}
else
if (m_spellInfo->Effects[i].ItemType)
{
ItemPosCountVec dest;
InventoryResult msg = target->ToPlayer()->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, m_spellInfo->Effects[i].ItemType, 1);
if (msg != EQUIP_ERR_OK)
{
// 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)))
ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(m_spellInfo->Effects[i].ItemType);
/// @todo Needs review
if (itemTemplate && !itemTemplate->GetItemLimitCategory())
{
player->SendEquipError(msg, nullptr, nullptr, m_spellInfo->Effects[i].ItemType);
return SPELL_FAILED_DONT_REPORT;
}
else
player->CastSpell(player, m_spellInfo->Effects[EFFECT_1].CalcValue(), false); // move this to anywhere
return SPELL_FAILED_DONT_REPORT;
{
// 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)))
{
player->SendEquipError(msg, nullptr, nullptr, m_spellInfo->Effects[i].ItemType);
return SPELL_FAILED_DONT_REPORT;
}
else
player->CastSpell(player, m_spellInfo->Effects[EFFECT_1].CalcValue(), false); // move this to anywhere
return SPELL_FAILED_DONT_REPORT;
}
}
}
}
+10 -21
View File
@@ -1402,31 +1402,20 @@ void Spell::EffectCreateItem2(SpellEffIndex effIndex)
Player* player = unitTarget->ToPlayer();
uint32 item_id = m_spellInfo->Effects[effIndex].ItemType;
if (item_id)
DoCreateItem(effIndex, item_id);
// special case: fake item replaced by generate using spell_loot_template
// Pick a random item from spell_loot_template
if (m_spellInfo->IsLootCrafting())
{
if (item_id)
{
if (!player->HasItemCount(item_id))
return;
// remove reagent
uint32 count = 1;
player->DestroyItemCount(item_id, count, true);
// create some random items
player->AutoStoreLoot(m_spellInfo->Id, LootTemplates_Spell);
}
else
player->AutoStoreLoot(m_spellInfo->Id, LootTemplates_Spell); // create some random items
player->AutoStoreLoot(m_spellInfo->Id, LootTemplates_Spell, false);
player->UpdateCraftSkill(m_spellInfo->Id);
}
else // If there's no random loot entries for this spell, pick the item associated with this spell
{
uint32 item_id = m_spellInfo->Effects[effIndex].ItemType;
if (item_id)
DoCreateItem(effIndex, item_id);
}
/// @todo ExecuteLogEffectCreateItem(i, m_spellInfo->Effects[i].ItemType);
}