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
166143uint32 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+ }
0 commit comments