refactor(Core/SpellEffects): Rework some of the null checks (#6987)

This commit is contained in:
Kitzunu
2021-09-29 22:03:57 +02:00
committed by GitHub
parent 3632efc074
commit 6d83887abd

View File

@@ -1359,10 +1359,15 @@ void Spell::EffectUnlearnSpecialization(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint32 spellToUnlearn = m_spellInfo->Effects[effIndex].TriggerSpell;
player->removeSpell(spellToUnlearn, SPEC_MASK_ALL, false);
@@ -1664,10 +1669,14 @@ void Spell::EffectHealthLeech(SpellEffIndex /*effIndex*/)
void Spell::DoCreateItem(uint8 /*effIndex*/, uint32 itemId)
{
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint32 newitemid = itemId;
@@ -1800,10 +1809,14 @@ void Spell::EffectCreateItem2(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint32 itemId = m_spellInfo->Effects[effIndex].ItemType;
@@ -1836,9 +1849,14 @@ void Spell::EffectCreateRandomItem(SpellEffIndex /*effIndex*/)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
// create some random items
player->AutoStoreLoot(m_spellInfo->Id, LootTemplates_Spell);
@@ -4077,11 +4095,17 @@ void Spell::EffectScriptEffect(SpellEffIndex effIndex)
}
case 59317: // Teleporting
{
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
// return from top
if (unitTarget->ToPlayer()->GetAreaId() == 4637)
if (player->GetAreaId() == 4637)
unitTarget->CastSpell(unitTarget, 59316, true);
// teleport atop
else
@@ -4450,9 +4474,15 @@ void Spell::EffectSummonPlayer(SpellEffIndex /*effIndex*/)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
// Evil Twin (ignore player summon, but hide this for summoner)
// Xinef: Unit Target may be on other map!!!, Need workaround
if (unitTarget->HasAura(23445))
@@ -4461,13 +4491,13 @@ void Spell::EffectSummonPlayer(SpellEffIndex /*effIndex*/)
float x, y, z;
m_caster->GetPosition(x, y, z);
unitTarget->ToPlayer()->SetSummonPoint(m_caster->GetMapId(), x, y, z);
player->SetSummonPoint(m_caster->GetMapId(), x, y, z);
WorldPacket data(SMSG_SUMMON_REQUEST, 8 + 4 + 4);
data << m_caster->GetGUID(); // summoner guid
data << uint32(m_caster->GetZoneId()); // summoner zone
data << uint32(MAX_PLAYER_SUMMON_DELAY * IN_MILLISECONDS); // auto decline after msecs
unitTarget->ToPlayer()->GetSession()->SendPacket(&data);
player->GetSession()->SendPacket(&data);
}
void Spell::EffectActivateObject(SpellEffIndex /*effIndex*/)
@@ -4564,12 +4594,16 @@ void Spell::EffectEnchantHeldItem(SpellEffIndex effIndex)
return;
// this is only item spell effect applied to main-hand weapon of target player (players in area)
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* item_owner = unitTarget->ToPlayer();
Item* item = item_owner->GetItemByPos(INVENTORY_SLOT_BAG_0, EQUIPMENT_SLOT_MAINHAND);
if (!item_owner)
{
return;
}
Item* item = item_owner->GetItemByPos(INVENTORY_SLOT_BAG_0, EQUIPMENT_SLOT_MAINHAND);
if (!item)
return;
@@ -4629,10 +4663,15 @@ void Spell::EffectInebriate(SpellEffIndex /*effIndex*/)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint8 currentDrunk = player->GetDrunkValue();
uint8 drunkMod = damage;
if (currentDrunk + drunkMod > 100)
@@ -4771,13 +4810,20 @@ void Spell::EffectResurrect(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
if (unitTarget->IsAlive() || !unitTarget->IsInWorld())
if (!unitTarget)
return;
Player* target = unitTarget->ToPlayer();
if (!target)
{
return;
}
if (unitTarget->IsAlive() || !unitTarget->IsInWorld())
return;
if (target->isResurrectRequested()) // already have one active request
return;
@@ -4846,10 +4892,14 @@ void Spell::EffectReputation(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
int32 repChange = damage;
@@ -4869,9 +4919,14 @@ void Spell::EffectQuestComplete(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint32 questId = m_spellInfo->Effects[effIndex].MiscValue;
if (questId)
@@ -5204,9 +5259,14 @@ void Spell::EffectQuestClear(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint32 quest_id = m_spellInfo->Effects[effIndex].MiscValue;
@@ -5247,10 +5307,16 @@ void Spell::EffectSendTaxi(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
unitTarget->ToPlayer()->ActivateTaxiPathTo(m_spellInfo->Effects[effIndex].MiscValue, m_spellInfo->Id);
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
player->ActivateTaxiPathTo(m_spellInfo->Effects[effIndex].MiscValue, m_spellInfo->Id);
}
void Spell::EffectPullTowards(SpellEffIndex effIndex)
@@ -5406,15 +5472,21 @@ void Spell::EffectDurabilityDamage(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
int32 slot = m_spellInfo->Effects[effIndex].MiscValue;
// -1 means all player equipped items and -2 all items
if (slot < 0)
{
unitTarget->ToPlayer()->DurabilityPointsLossAll(damage, (slot < -1));
player->DurabilityPointsLossAll(damage, (slot < -1));
ExecuteLogEffectDurabilityDamage(effIndex, unitTarget, -1, -1);
return;
}
@@ -5423,9 +5495,9 @@ void Spell::EffectDurabilityDamage(SpellEffIndex effIndex)
if (slot >= INVENTORY_SLOT_BAG_END)
return;
if (Item* item = unitTarget->ToPlayer()->GetItemByPos(INVENTORY_SLOT_BAG_0, slot))
if (Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, slot))
{
unitTarget->ToPlayer()->DurabilityPointsLoss(item, damage);
player->DurabilityPointsLoss(item, damage);
ExecuteLogEffectDurabilityDamage(effIndex, unitTarget, item->GetEntry(), slot);
}
}
@@ -5435,16 +5507,22 @@ void Spell::EffectDurabilityDamagePCT(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
int32 slot = m_spellInfo->Effects[effIndex].MiscValue;
// FIXME: some spells effects have value -1/-2
// Possibly its mean -1 all player equipped items and -2 all items
if (slot < 0)
{
unitTarget->ToPlayer()->DurabilityLossAll(float(damage) / 100.0f, (slot < -1));
player->DurabilityLossAll(float(damage) / 100.0f, (slot < -1));
return;
}
@@ -5455,8 +5533,8 @@ void Spell::EffectDurabilityDamagePCT(SpellEffIndex effIndex)
if (damage <= 0)
return;
if (Item* item = unitTarget->ToPlayer()->GetItemByPos(INVENTORY_SLOT_BAG_0, slot))
unitTarget->ToPlayer()->DurabilityLoss(item, float(damage) / 100.0f);
if (Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, slot))
player->DurabilityLoss(item, float(damage) / 100.0f);
}
void Spell::EffectModifyThreatPercent(SpellEffIndex /*effIndex*/)
@@ -5815,10 +5893,16 @@ void Spell::EffectKillCreditPersonal(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
unitTarget->ToPlayer()->KilledMonsterCredit(m_spellInfo->Effects[effIndex].MiscValue);
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
player->KilledMonsterCredit(m_spellInfo->Effects[effIndex].MiscValue);
}
void Spell::EffectKillCredit(SpellEffIndex effIndex)
@@ -5826,9 +5910,15 @@ void Spell::EffectKillCredit(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
int32 creatureEntry = m_spellInfo->Effects[effIndex].MiscValue;
if (!creatureEntry)
{
@@ -5837,7 +5927,7 @@ void Spell::EffectKillCredit(SpellEffIndex effIndex)
}
if (creatureEntry)
unitTarget->ToPlayer()->RewardPlayerAndGroupAtEvent(creatureEntry, unitTarget);
player->RewardPlayerAndGroupAtEvent(creatureEntry, unitTarget);
}
void Spell::EffectQuestFail(SpellEffIndex effIndex)
@@ -5845,10 +5935,16 @@ void Spell::EffectQuestFail(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
unitTarget->ToPlayer()->FailQuest(m_spellInfo->Effects[effIndex].MiscValue);
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
player->FailQuest(m_spellInfo->Effects[effIndex].MiscValue);
}
void Spell::EffectQuestStart(SpellEffIndex effIndex)
@@ -5981,11 +6077,18 @@ void Spell::EffectDiscoverTaxi(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint32 nodeid = m_spellInfo->Effects[effIndex].MiscValue;
if (sTaxiNodesStore.LookupEntry(nodeid))
unitTarget->ToPlayer()->GetSession()->SendDiscoverNewTaxiNode(nodeid);
player->GetSession()->SendDiscoverNewTaxiNode(nodeid);
}
void Spell::EffectTitanGrip(SpellEffIndex /*effIndex*/)
@@ -6180,9 +6283,15 @@ void Spell::EffectPlayMusic(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
uint32 soundid = m_spellInfo->Effects[effIndex].MiscValue;
if (!sSoundEntriesStore.LookupEntry(soundid))
@@ -6193,7 +6302,7 @@ void Spell::EffectPlayMusic(SpellEffIndex effIndex)
WorldPacket data(SMSG_PLAY_MUSIC, 4);
data << uint32(soundid);
unitTarget->ToPlayer()->GetSession()->SendPacket(&data);
player->GetSession()->SendPacket(&data);
}
void Spell::EffectSpecCount(SpellEffIndex /*effIndex*/)
@@ -6201,10 +6310,16 @@ void Spell::EffectSpecCount(SpellEffIndex /*effIndex*/)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
unitTarget->ToPlayer()->UpdateSpecCount(damage);
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
player->UpdateSpecCount(damage);
}
void Spell::EffectActivateSpec(SpellEffIndex /*effIndex*/)
@@ -6212,10 +6327,16 @@ void Spell::EffectActivateSpec(SpellEffIndex /*effIndex*/)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
unitTarget->ToPlayer()->ActivateSpec(damage - 1); // damage is 1 or 2, spec is 0 or 1
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
player->ActivateSpec(damage - 1); // damage is 1 or 2, spec is 0 or 1
}
void Spell::EffectPlaySound(SpellEffIndex effIndex)
@@ -6223,14 +6344,20 @@ void Spell::EffectPlaySound(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
switch (m_spellInfo->Id)
{
case 58730: // Restricted Flight Area
case 58600: // Restricted Flight Area
unitTarget->ToPlayer()->GetSession()->SendNotification(LANG_ZONE_NOFLYZONE);
player->GetSession()->SendNotification(LANG_ZONE_NOFLYZONE);
break;
default:
break;
@@ -6338,10 +6465,14 @@ void Spell::EffectBind(SpellEffIndex effIndex)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (!unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
WorldLocation homeLoc;
uint32 areaId = player->GetAreaId();
@@ -6382,9 +6513,18 @@ void Spell::EffectSummonRaFFriend(SpellEffIndex /*effIndex*/)
if (effectHandleMode != SPELL_EFFECT_HANDLE_HIT_TARGET)
return;
if (m_caster->GetTypeId() != TYPEID_PLAYER || !unitTarget || unitTarget->GetTypeId() != TYPEID_PLAYER)
if (m_caster->GetTypeId() != TYPEID_PLAYER)
return;
if (!unitTarget)
return;
Player* player = unitTarget->ToPlayer();
if (!player)
{
return;
}
float x, y, z;
m_caster->GetPosition(x, y, z);
unitTarget->ToPlayer()->SetSummonPoint(m_caster->GetMapId(), x, y, z);
@@ -6392,5 +6532,5 @@ void Spell::EffectSummonRaFFriend(SpellEffIndex /*effIndex*/)
data << m_caster->GetGUID();
data << uint32(m_caster->GetZoneId());
data << uint32(MAX_PLAYER_SUMMON_DELAY * IN_MILLISECONDS); // auto decline after msecs
unitTarget->ToPlayer()->GetSession()->SendPacket(&data);
player->GetSession()->SendPacket(&data);
}