diff options
author | Wyrserth <43747507+Wyrserth@users.noreply.github.com> | 2019-06-15 15:11:49 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2021-12-11 13:29:39 +0100 |
commit | 59da957165ef7d7529db30f90a30dc401d682052 (patch) | |
tree | dd9985c6f699edae30b123e7b965e43db1cd229c | |
parent | 264373bb656352a0cdf9b18bad4f3929c7376a9c (diff) |
Core/Spells: don't allow spells with SPELL_EFFECT_CREATE_LOOT to be cast if there isn't enough space in inventory (#23404)
* Core/Spells: don't allow spells with SPELL_EFFECT_CREATE_LOOT to be cast if there isn't enough space in inventory,
* Move GetFreeInventorySpace() to a better place, thanks ccrs!
(cherry picked from commit 218055280af4563f28643e2683eafe40808f90db)
-rw-r--r-- | src/server/game/Entities/Player/Player.cpp | 22 | ||||
-rw-r--r-- | src/server/game/Entities/Player/Player.h | 1 | ||||
-rw-r--r-- | src/server/game/Spells/Spell.cpp | 48 | ||||
-rw-r--r-- | src/server/game/Spells/SpellEffects.cpp | 30 |
4 files changed, 63 insertions, 38 deletions
diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index 2e4672d1291..8c3f0e786c3 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -10156,6 +10156,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; diff --git a/src/server/game/Entities/Player/Player.h b/src/server/game/Entities/Player/Player.h index fc13fc16798..37c89b30800 100644 --- a/src/server/game/Entities/Player/Player.h +++ b/src/server/game/Entities/Player/Player.h @@ -1304,6 +1304,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; Item* GetChildItemByGuid(ObjectGuid guid) const; diff --git a/src/server/game/Spells/Spell.cpp b/src/server/game/Spells/Spell.cpp index 0526c512dc1..74baf6338c8 100644 --- a/src/server/game/Spells/Spell.cpp +++ b/src/server/game/Spells/Spell.cpp @@ -6847,33 +6847,45 @@ SpellCastResult Spell::CheckItems(int32* param1 /*= nullptr*/, int32* 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->GetTypeId() == TYPEID_PLAYER && !IsTriggered() && spellEffectInfo.ItemType) + if (target->GetTypeId() == TYPEID_PLAYER && !IsTriggered()) { - ItemPosCountVec dest; - InventoryResult msg = target->ToPlayer()->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, spellEffectInfo.ItemType, 1); - if (msg != EQUIP_ERR_OK) - { - ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(spellEffectInfo.ItemType); - /// @todo Needs review - if (itemTemplate && !(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 (spellEffectInfo.Effect == SPELL_EFFECT_CREATE_LOOT) + if (target->ToPlayer()->GetFreeInventorySpace() == 0) { - player->SendEquipError(msg, nullptr, nullptr, spellEffectInfo.ItemType); + player->SendEquipError(EQUIP_ERR_INV_FULL, nullptr, nullptr, spellEffectInfo.ItemType); return SPELL_FAILED_DONT_REPORT; } - else + + if (spellEffectInfo.ItemType) + { + ItemPosCountVec dest; + InventoryResult msg = target->ToPlayer()->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, spellEffectInfo.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(spellEffectInfo.ItemType))) + ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(spellEffectInfo.ItemType); + /// @todo Needs review + if (itemTemplate && !itemTemplate->GetItemLimitCategory()) { player->SendEquipError(msg, nullptr, nullptr, spellEffectInfo.ItemType); return SPELL_FAILED_DONT_REPORT; } - else if (m_spellInfo->GetEffects().size() > EFFECT_1) - player->CastSpell(player, m_spellInfo->GetEffect(EFFECT_1).CalcValue(), CastSpellExtraArgs() - .SetOriginalCastId(m_castId)); // move this to anywhere - return SPELL_FAILED_DONT_REPORT; + else + { + // 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(spellEffectInfo.ItemType))) + { + player->SendEquipError(msg, nullptr, nullptr, spellEffectInfo.ItemType); + return SPELL_FAILED_DONT_REPORT; + } + else if (m_spellInfo->GetEffects().size() > EFFECT_1) + player->CastSpell(player, m_spellInfo->GetEffect(EFFECT_1).CalcValue(), CastSpellExtraArgs() + .SetOriginalCastId(m_castId)); // move this to anywhere + return SPELL_FAILED_DONT_REPORT; + } } } } diff --git a/src/server/game/Spells/SpellEffects.cpp b/src/server/game/Spells/SpellEffects.cpp index a7bd4d21cb5..d5f4cac251d 100644 --- a/src/server/game/Spells/SpellEffects.cpp +++ b/src/server/game/Spells/SpellEffects.cpp @@ -1437,32 +1437,22 @@ void Spell::EffectCreateItem2() Player* player = unitTarget->ToPlayer(); - uint32 item_id = effectInfo->ItemType; ItemContext context = m_spellInfo->HasAttribute(SPELL_ATTR0_TRADESPELL) ? ItemContext::Trade_Skill : ItemContext::NONE; - if (item_id) - DoCreateItem(item_id, context); - - // 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, context); - } - else - player->AutoStoreLoot(m_spellInfo->Id, LootTemplates_Spell, context); // create some random items - + player->AutoStoreLoot(m_spellInfo->Id, LootTemplates_Spell, context, false, true); 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 = effectInfo->ItemType; + + if (item_id) + DoCreateItem(item_id, context); + } + /// @todo ExecuteLogEffectCreateItem(i, m_spellInfo->Effects[i].ItemType); } |