TrinityCore
ToyHandler.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "WorldSession.h"
19#include "DB2Stores.h"
20#include "Item.h"
21#include "Log.h"
22#include "ObjectMgr.h"
23#include "Player.h"
24#include "Spell.h"
25#include "SpellMgr.h"
26#include "ToyPackets.h"
27
29{
30 if (!packet.Guid)
31 return;
32
33 Item* item = _player->GetItemByGuid(packet.Guid);
34 if (!item)
35 {
37 return;
38 }
39
40 if (!sDB2Manager.IsToyItem(item->GetEntry()))
41 return;
42
44 if (msg != EQUIP_ERR_OK)
45 {
46 _player->SendEquipError(msg, item, nullptr);
47 return;
48 }
49
50 if (_collectionMgr->AddToy(item->GetEntry(), false, false))
51 _player->DestroyItem(item->GetBagSlot(), item->GetSlot(), true);
52}
53
55{
56 uint32 itemId = packet.Cast.Misc[0];
57 ItemTemplate const* item = sObjectMgr->GetItemTemplate(itemId);
58 if (!item)
59 return;
60
61 if (!_collectionMgr->HasToy(itemId))
62 return;
63
64 auto effect = std::find_if(item->Effects.begin(), item->Effects.end(), [&packet](ItemEffectEntry const* effect)
65 {
66 return packet.Cast.SpellID == effect->SpellID;
67 });
68
69 if (effect == item->Effects.end())
70 return;
71
72 SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(packet.Cast.SpellID, DIFFICULTY_NONE);
73 if (!spellInfo)
74 {
75 TC_LOG_ERROR("network", "HandleUseToy: unknown spell id: {} used by Toy Item entry {}", packet.Cast.SpellID, itemId);
76 return;
77 }
78
79 if (_player->isPossessing())
80 return;
81
82 SpellCastTargets targets(_player, packet.Cast);
83
84 Spell* spell = new Spell(_player, spellInfo, TRIGGERED_NONE);
85
87 spellPrepare.ClientCastID = packet.Cast.CastID;
88 spellPrepare.ServerCastID = spell->m_castId;
89 SendPacket(spellPrepare.Write());
90
91 spell->m_fromClient = true;
92 spell->m_castItemEntry = itemId;
93 spell->m_misc.Raw.Data[0] = packet.Cast.Misc[0];
94 spell->m_misc.Raw.Data[1] = packet.Cast.Misc[1];
96 spell->prepare(targets);
97}
98
100{
101 _collectionMgr->ToyClearFanfare(toyClearFanfare.ItemID);
102}
#define sDB2Manager
Definition: DB2Stores.h:538
@ DIFFICULTY_NONE
Definition: DBCEnums.h:874
uint32_t uint32
Definition: Define.h:142
InventoryResult
Definition: ItemDefines.h:25
@ EQUIP_ERR_ITEM_NOT_FOUND
Definition: ItemDefines.h:49
@ EQUIP_ERR_OK
Definition: ItemDefines.h:26
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define sObjectMgr
Definition: ObjectMgr.h:1946
@ TRIGGERED_NONE
Not triggered.
Definition: SpellDefines.h:246
#define sSpellMgr
Definition: SpellMgr.h:849
@ CAST_FLAG_EX_USE_TOY_SPELL
Definition: Spell.h:136
Definition: Item.h:170
uint8 GetSlot() const
Definition: Item.h:280
uint8 GetBagSlot() const
Definition: Item.cpp:1239
uint32 GetEntry() const
Definition: Object.h:161
void SendEquipError(InventoryResult msg, Item const *item1=nullptr, Item const *item2=nullptr, uint32 itemId=0) const
Definition: Player.cpp:13254
void DestroyItem(uint8 bag, uint8 slot, bool update)
Definition: Player.cpp:12180
Item * GetItemByGuid(ObjectGuid guid) const
Definition: Player.cpp:9566
InventoryResult CanUseItem(Item *pItem, bool not_loading=true) const
Definition: Player.cpp:11381
Definition: Spell.h:255
bool m_fromClient
Definition: Spell.h:570
uint32 m_castFlagsEx
Definition: Spell.h:571
struct Spell::@333::@335 Raw
uint32 Data[2]
Definition: Spell.h:602
SpellCastResult prepare(SpellCastTargets const &targets, AuraEffect const *triggeredByAura=nullptr)
Definition: Spell.cpp:3426
union Spell::@333 m_misc
uint32 m_castItemEntry
Definition: Spell.h:566
ObjectGuid m_castId
Definition: Spell.h:568
bool isPossessing() const
Definition: Unit.cpp:6465
WorldPacket const * Write() override
WorldPackets::Spells::SpellCastRequest Cast
Definition: ToyPackets.h:45
void HandleToyClearFanfare(WorldPackets::Toy::ToyClearFanfare &toyClearFanfare)
Definition: ToyHandler.cpp:99
void HandleAddToy(WorldPackets::Toy::AddToy &packet)
Definition: ToyHandler.cpp:28
std::unique_ptr< CollectionMgr > _collectionMgr
void HandleUseToy(WorldPackets::Toy::UseToy &packet)
Definition: ToyHandler.cpp:54
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
Player * _player
std::vector< ItemEffectEntry const * > Effects
Definition: ItemTemplate.h:828