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

Port From (https://github.com/TrinityCore/TrinityCore/commit/59da957165ef7d7529db30f90a30dc401d682052)
This commit is contained in:
hondacrx
2021-12-17 19:26:43 -05:00
parent 112803c149
commit b349f922b2
3 changed files with 99 additions and 71 deletions
+24 -1
View File
@@ -17,6 +17,7 @@
using Framework.Constants; using Framework.Constants;
using Framework.Database; using Framework.Database;
using Game.BattleFields;
using Game.BattleGrounds; using Game.BattleGrounds;
using Game.DataStorage; using Game.DataStorage;
using Game.Groups; using Game.Groups;
@@ -30,7 +31,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Game.BattleFields;
namespace Game.Entities namespace Game.Entities
{ {
@@ -4515,6 +4515,29 @@ namespace Game.Entities
return freeSlotCount; return freeSlotCount;
} }
public uint GetFreeInventorySpace()
{
uint freeSpace = 0;
// Check backpack
for (byte slot = InventorySlots.ItemStart; slot < InventorySlots.ItemEnd; ++slot)
{
Item item = GetItemByPos(InventorySlots.Bag0, slot);
if (item == null)
freeSpace += 1;
}
// Check bags
for (byte i = InventorySlots.BagStart; i < InventorySlots.BagEnd; i++)
{
Bag bag = GetBagByPos(i);
if (bag != null)
freeSpace += bag.GetFreeSlots();
}
return freeSpace;
}
//Bags //Bags
public Bag GetBagByPos(byte bag) public Bag GetBagByPos(byte bag)
{ {
+22 -6
View File
@@ -6094,15 +6094,29 @@ namespace Game.Spells
case SpellEffectName.CreateLoot: case SpellEffectName.CreateLoot:
{ {
// 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() : player; Unit target = m_targets.GetUnitTarget() ?? player;
if (target.IsPlayer() && !IsTriggered() && spellEffectInfo.ItemType != 0) if (target.IsPlayer() && !IsTriggered())
{
// 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 == SpellEffectName.CreateLoot)
{
if (target.ToPlayer().GetFreeInventorySpace() == 0)
{
player.SendEquipError(InventoryResult.InvFull, null, null, spellEffectInfo.ItemType);
return SpellCastResult.DontReport;
}
}
if (spellEffectInfo.ItemType != 0)
{ {
List<ItemPosCount> dest = new(); List<ItemPosCount> dest = new();
InventoryResult msg = target.ToPlayer().CanStoreNewItem(ItemConst.NullBag, ItemConst.NullSlot, dest, spellEffectInfo.ItemType, 1); InventoryResult msg = target.ToPlayer().CanStoreNewItem(ItemConst.NullBag, ItemConst.NullSlot, dest, spellEffectInfo.ItemType, 1);
if (msg != InventoryResult.Ok) if (msg != InventoryResult.Ok)
{ {
ItemTemplate itemTemplate = Global.ObjectMgr.GetItemTemplate(spellEffectInfo.ItemType); ItemTemplate itemTemplate = Global.ObjectMgr.GetItemTemplate(spellEffectInfo.ItemType);
// @todo Needs review /// @todo Needs review
if (itemTemplate != null && itemTemplate.GetItemLimitCategory() == 0) if (itemTemplate != null && itemTemplate.GetItemLimitCategory() == 0)
{ {
player.SendEquipError(msg, null, null, spellEffectInfo.ItemType); player.SendEquipError(msg, null, null, spellEffectInfo.ItemType);
@@ -6111,19 +6125,21 @@ namespace Game.Spells
else else
{ {
// Conjure Food/Water/Refreshment spells // Conjure Food/Water/Refreshment spells
if (!(m_spellInfo.SpellFamilyName == SpellFamilyNames.Mage && m_spellInfo.SpellFamilyFlags[0].HasAnyFlag(0x40000000u))) if (m_spellInfo.SpellFamilyName != SpellFamilyNames.Mage || (!m_spellInfo.SpellFamilyFlags[0].HasAnyFlag(0x40000000u)))
return SpellCastResult.TooManyOfItem; return SpellCastResult.TooManyOfItem;
else if (!target.ToPlayer().HasItemCount(spellEffectInfo.ItemType)) else if (!(target.ToPlayer().HasItemCount(spellEffectInfo.ItemType)))
{ {
player.SendEquipError(msg, null, null, spellEffectInfo.ItemType); player.SendEquipError(msg, null, null, spellEffectInfo.ItemType);
return SpellCastResult.DontReport; return SpellCastResult.DontReport;
} }
else if (m_spellInfo.GetEffects().Count > 1) else if (m_spellInfo.GetEffects().Count > 1)
player.CastSpell(m_caster, (uint)m_spellInfo.GetEffect(1).CalcValue(), new CastSpellExtraArgs().SetOriginalCastId(m_castId)); // move this to anywhere player.CastSpell(player, (uint)m_spellInfo.GetEffect(1).CalcValue(), new CastSpellExtraArgs()
.SetOriginalCastId(m_castId)); // move this to anywhere
return SpellCastResult.DontReport; return SpellCastResult.DontReport;
} }
} }
} }
}
break; break;
} }
case SpellEffectName.EnchantItem: case SpellEffectName.EnchantItem:
+9 -20
View File
@@ -1098,32 +1098,21 @@ namespace Game.Spells
Player player = unitTarget.ToPlayer(); Player player = unitTarget.ToPlayer();
uint item_id = effectInfo.ItemType;
ItemContext context = m_spellInfo.HasAttribute(SpellAttr0.Tradespell) ? ItemContext.TradeSkill : ItemContext.None; ItemContext context = m_spellInfo.HasAttribute(SpellAttr0.Tradespell) ? ItemContext.TradeSkill : ItemContext.None;
if (item_id != 0) // Pick a random item from spell_loot_template
DoCreateItem(item_id, context);
// special case: fake item replaced by generate using spell_loot_template
if (m_spellInfo.IsLootCrafting()) if (m_spellInfo.IsLootCrafting())
{ {
if (item_id != 0) player.AutoStoreLoot(m_spellInfo.Id, LootStorage.Spell, context, false, true);
{
if (!player.HasItemCount(item_id))
return;
// remove reagent
uint count = 1;
player.DestroyItemCount(item_id, count, true);
// create some random items
player.AutoStoreLoot(m_spellInfo.Id, LootStorage.Spell, context);
}
else
player.AutoStoreLoot(m_spellInfo.Id, LootStorage.Spell, context); // create some random items
player.UpdateCraftSkill(m_spellInfo.Id); player.UpdateCraftSkill(m_spellInfo.Id);
} }
else // If there's no random loot entries for this spell, pick the item associated with this spell
{
uint itemId = effectInfo.ItemType;
if (itemId != 0)
DoCreateItem(itemId, context);
}
// @todo ExecuteLogEffectCreateItem(i, GetEffect(i].ItemType); // @todo ExecuteLogEffectCreateItem(i, GetEffect(i].ItemType);
} }