TrinityCore
Loading...
Searching...
No Matches
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 std::ranges::copy(packet.Cast.Misc, std::ranges::begin(spell->m_misc.Raw.Data));
95 spell->prepare(targets);
96}
97
99{
100 _collectionMgr->ToyClearFanfare(toyClearFanfare.ItemID);
101}
#define sDB2Manager
Definition DB2Stores.h:569
@ DIFFICULTY_NONE
Definition DBCEnums.h:933
uint32_t uint32
Definition Define.h:154
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__, message__,...)
Definition Log.h:190
#define sObjectMgr
Definition ObjectMgr.h:1885
@ TRIGGERED_NONE
Not triggered.
#define sSpellMgr
Definition SpellMgr.h:812
@ CAST_FLAG_EX_USE_TOY_SPELL
Definition Spell.h:146
Definition Item.h:179
uint8 GetSlot() const
Definition Item.h:290
uint8 GetBagSlot() const
Definition Item.cpp:1331
uint32 GetEntry() const
Definition Object.h:89
void SendEquipError(InventoryResult msg, Item const *item1=nullptr, Item const *item2=nullptr, uint32 itemId=0) const
Definition Player.cpp:13130
void DestroyItem(uint8 bag, uint8 slot, bool update)
Definition Player.cpp:12121
Item * GetItemByGuid(ObjectGuid guid) const
Definition Player.cpp:9614
InventoryResult CanUseItem(Item *pItem, bool not_loading=true) const
Definition Player.cpp:11214
Definition Spell.h:277
bool m_fromClient
Definition Spell.h:606
uint32 m_castFlagsEx
Definition Spell.h:607
union Spell::@321 m_misc
SpellCastResult prepare(SpellCastTargets const &targets, AuraEffect const *triggeredByAura=nullptr)
Definition Spell.cpp:3419
uint32 Data[3]
Definition Spell.h:646
uint32 m_castItemEntry
Definition Spell.h:602
struct Spell::@321::@324 Raw
ObjectGuid m_castId
Definition Spell.h:604
bool isPossessing() const
Definition Unit.cpp:6650
WorldPacket const * Write() override
WorldPackets::Spells::SpellCastRequest Cast
Definition ToyPackets.h:45
void HandleToyClearFanfare(WorldPackets::Toy::ToyClearFanfare &toyClearFanfare)
void HandleAddToy(WorldPackets::Toy::AddToy &packet)
std::unique_ptr< CollectionMgr > _collectionMgr
void HandleUseToy(WorldPackets::Toy::UseToy &packet)
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
Player * _player
std::vector< ItemEffectEntry const * > Effects