Skip to content

Commit b7f0070

Browse files
committed
Minor refact
1 parent 25bf1d0 commit b7f0070

3 files changed

Lines changed: 34 additions & 33 deletions

File tree

src/server/game/Craft/Crafting.cpp

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@
2727
#include "SpellPackets.h"
2828
#include "Player.h"
2929

30-
void Crafting::DoCraft(uint32 craftingDataId)
30+
SpellCastResult Crafting::DoCraft(uint32 craftingDataId)
3131
{
3232
std::vector<ModifiedCraftingSpellSlotEntry const*> spellSlots = sDB2Manager.GetMCRSpellSlotBySpell(_spell->GetSpellInfo()->Id);
3333
_craftingData = sCraftingDataStore.LookupEntry(craftingDataId);
3434

3535
if (!_craftingData || spellSlots.empty())
36-
{
37-
_spell->SendCastResult(SpellCastResult::SPELL_FAILED_ERROR);
38-
return;
39-
}
36+
return SpellCastResult::SPELL_FAILED_ERROR;
4037

4138
std::unordered_map<uint32 /*itemId*/, uint32 /*quantity*/> usedReagentsToDestroy;
4239
std::vector<uint32> bonusTreeIds;
@@ -76,10 +73,7 @@ void Crafting::DoCraft(uint32 craftingDataId)
7673
{
7774
// If we don't have enough Reagent for this slot and it is required, craft failed
7875
if (reagentSlot->ReagentType == MCR_REAGENT_TYPE_REQUIRED)
79-
{
80-
_spell->SendCastResult(SpellCastResult::SPELL_FAILED_NEED_MORE_ITEMS);
81-
return;
82-
}
76+
return SpellCastResult::SPELL_FAILED_NEED_MORE_ITEMS;
8377

8478
// Still not enough but the reagent is optional, just skip
8579
continue;
@@ -112,34 +106,15 @@ void Crafting::DoCraft(uint32 craftingDataId)
112106

113107
// If any item provided do not match, probably a cheater, return
114108
if (!hasOneMatchingItem)
115-
{
116-
_spell->SendCastResult(SpellCastResult::SPELL_FAILED_NEED_MORE_ITEMS);
117-
return;
118-
}
109+
return SpellCastResult::SPELL_FAILED_NEED_MORE_ITEMS;
119110
}
120111

121112
uint32 skillLevel = GetSkillLevelForCraft();
122113
uint32 difficultyPercentMatched = round(float(skillLevel) / float(craftingDifficulty) * 100.f);
123114

124115
uint32 qualityTier = sDB2Manager.GetCraftingQualityTierByDifficultyPercent(_craftingData->CraftingDifficultyID, difficultyPercentMatched);
125116

126-
// If this is a simple item (no quality), we have the id
127-
uint32 newItemId = _craftingData->CraftedItemID;
128-
129-
// If it's a reagent, get id from CraftingDataItem
130-
if (!newItemId)
131-
{
132-
std::vector<uint32> craftedItemList = sDB2Manager.GetCraftingDataItemIDByCraftingData(_craftingData->ID);
133-
134-
// If we didn't found anything, we have a problem. Return and don't take reagent from player.
135-
if (craftedItemList.size() < qualityTier)
136-
{
137-
_spell->SendCastResult(SpellCastResult::SPELL_FAILED_ERROR);
138-
return;
139-
}
140-
141-
newItemId = craftedItemList[qualityTier - 1];
142-
}
117+
uint32 newItemId = GetItemIdForQuality(qualityTier);
143118

144119
ItemContext context = _spell->GetSpellInfo()->HasAttribute(SPELL_ATTR0_IS_TRADESKILL) ? ItemContext::Trade_Skill : ItemContext::NONE;
145120

@@ -155,12 +130,14 @@ void Crafting::DoCraft(uint32 craftingDataId)
155130
InitCraftingStatModifier(createdItem);
156131

157132
createdItem->AddToWorld();
158-
createdItem->SendUpdateToPlayer(createdItem->GetOwner());
133+
createdItem->SendUpdateToPlayer(_player);
159134
}
160135

161136
// Item has been crafted, remove reagent from player
162137
for (auto const& [itemId, itemCount] : usedReagentsToDestroy)
163138
_player->DestroyItemCount(itemId, itemCount, true);
139+
140+
return SpellCastResult::SPELL_CAST_OK;
164141
}
165142

166143
uint32 Crafting::GetSkillLevelForCraft()
@@ -241,3 +218,23 @@ void Crafting::InitCraftingStatModifier(Item* item)
241218
item->SetModifier(ITEM_MODIFIER_CHANGE_MODIFIED_CRAFTING_STAT_2, allowedStats[1]);
242219
}
243220
}
221+
222+
uint32 Crafting::GetItemIdForQuality(uint32 qualityTier)
223+
{
224+
// If this is a simple item (no quality), we have the id
225+
uint32 newItemId = _craftingData->CraftedItemID;
226+
227+
// If it's a reagent, get id from CraftingDataItem
228+
if (!newItemId)
229+
{
230+
std::vector<uint32> craftedItemList = sDB2Manager.GetCraftingDataItemIDByCraftingData(_craftingData->ID);
231+
232+
// If we didn't found anything, we have a problem. Return and don't take reagent from player.
233+
if (craftedItemList.size() < qualityTier)
234+
return SpellCastResult::SPELL_FAILED_ERROR;
235+
236+
newItemId = craftedItemList[qualityTier - 1];
237+
}
238+
239+
return newItemId;
240+
}

src/server/game/Craft/Crafting.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ class TC_GAME_API Crafting
3131
Crafting(Player* player, Spell* spell) :
3232
_player(player), _spell(spell), _craftingData(nullptr) { }
3333

34-
void DoCraft(uint32 craftingDataId);
34+
SpellCastResult DoCraft(uint32 craftingDataId);
3535

3636
private:
3737
uint32 GetSkillLevelForCraft();
3838
uint32 GetSkillIdForSpell(uint32 spellId);
3939
uint32 CalculateTotalReagentWeights();
4040
void InitCraftingStatModifier(Item* item);
41+
uint32 GetItemIdForQuality(uint32 qualityTier);
4142

4243
Player* _player;
4344
Spell* _spell;

src/server/game/Spells/SpellEffects.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5995,7 +5995,10 @@ void Spell::EffectCraftItem()
59955995
}
59965996

59975997
Crafting craftingInstance = Crafting(playerCaster, this);
5998-
craftingInstance.DoCraft(effectInfo->MiscValue);
5998+
SpellCastResult result = craftingInstance.DoCraft(effectInfo->MiscValue);
5999+
6000+
if (result != SpellCastResult::SPELL_CAST_OK)
6001+
SendCastResult(result);
59996002
}
60006003

60016004
void Spell::EffectModifyAuraStacks()

0 commit comments

Comments
 (0)