TrinityCore
BankHandler.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 "BankPackets.h"
19#include "Item.h"
20#include "DB2Stores.h"
21#include "GossipDef.h"
22#include "Log.h"
23#include "NPCPackets.h"
24#include "Player.h"
25#include "WorldSession.h"
26
28{
29 TC_LOG_DEBUG("network", "STORAGE: receive bag = {}, slot = {}", packet.Bag, packet.Slot);
30
31 if (!CanUseBank())
32 {
33 TC_LOG_ERROR("network", "WORLD: HandleAutoBankItemOpcode - Unit ({}) not found or you can't interact with him.", _player->PlayerTalkClass->GetInteractionData().SourceGuid.ToString());
34 return;
35 }
36
37 Item* item = _player->GetItemByPos(packet.Bag, packet.Slot);
38 if (!item)
39 return;
40
41 ItemPosCountVec dest;
42 InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false);
43 if (msg != EQUIP_ERR_OK)
44 {
45 _player->SendEquipError(msg, item, nullptr);
46 return;
47 }
48
49 if (dest.size() == 1 && dest[0].pos == item->GetPos())
50 {
52 return;
53 }
54
55 _player->RemoveItem(packet.Bag, packet.Slot, true);
57 _player->BankItem(dest, item, true);
58}
59
61{
63 if (!unit)
64 {
65 TC_LOG_ERROR("network", "WORLD: HandleBankerActivateOpcode - {} not found or you can not interact with him.", packet.Unit.ToString());
66 return;
67 }
68
69 // remove fake death
70 if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
72
73 // set currentBankerGUID for other bank action
74
75 SendShowBank(packet.Unit);
76}
77
79{
80 TC_LOG_DEBUG("network", "STORAGE: receive bag = {}, slot = {}", packet.Bag, packet.Slot);
81
82 if (!CanUseBank())
83 {
84 TC_LOG_ERROR("network", "WORLD: HandleAutoStoreBankItemOpcode - Unit ({}) not found or you can't interact with him.", _player->PlayerTalkClass->GetInteractionData().SourceGuid.ToString());
85 return;
86 }
87
88 Item* item = _player->GetItemByPos(packet.Bag, packet.Slot);
89 if (!item)
90 return;
91
92 if (_player->IsBankPos(packet.Bag, packet.Slot)) // moving from bank to inventory
93 {
94 ItemPosCountVec dest;
95 InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, item, false);
96 if (msg != EQUIP_ERR_OK)
97 {
98 _player->SendEquipError(msg, item, nullptr);
99 return;
100 }
101
102 _player->RemoveItem(packet.Bag, packet.Slot, true);
103 if (Item const* storedItem = _player->StoreItem(dest, item, true))
104 _player->ItemAddedQuestCheck(storedItem->GetEntry(), storedItem->GetCount());
105
106 }
107 else // moving from inventory to bank
108 {
109 ItemPosCountVec dest;
110 InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false);
111 if (msg != EQUIP_ERR_OK)
112 {
113 _player->SendEquipError(msg, item, nullptr);
114 return;
115 }
116
117 _player->RemoveItem(packet.Bag, packet.Slot, true);
118 _player->BankItem(dest, item, true);
119 }
120}
121
123{
124 if (!CanUseBank(packet.Guid))
125 {
126 TC_LOG_ERROR("network", "WORLD: HandleBuyBankSlotOpcode - {} not found or you can't interact with him.", packet.Guid.ToString());
127 return;
128 }
129
131
132 // next slot
133 ++slot;
134
135 TC_LOG_INFO("network", "PLAYER: Buy bank bag slot, slot number = {}", slot);
136
137 BankBagSlotPricesEntry const* slotEntry = sBankBagSlotPricesStore.LookupEntry(slot);
138 if (!slotEntry)
139 return;
140
141 uint32 price = slotEntry->Cost;
142
143 if (!_player->HasEnoughMoney(uint64(price)))
144 return;
145
147 _player->ModifyMoney(-int64(price));
148
150}
151
153{
154 if (!CanUseBank(reagentBank.Banker))
155 {
156 TC_LOG_DEBUG("network", "WORLD: HandleBuyReagentBankOpcode - {} not found or you can't interact with him.", reagentBank.Banker.ToString());
157 return;
158 }
159
161 {
162 TC_LOG_DEBUG("network", "WORLD: HandleBuyReagentBankOpcode - Player ({}, name: {}) tried to unlock reagent bank a 2nd time.", _player->GetGUID().ToString(), _player->GetName());
163 return;
164 }
165
166 constexpr int64 price = 100 * GOLD;
167
168 if (!_player->HasEnoughMoney(price))
169 {
170 TC_LOG_DEBUG("network", "WORLD: HandleBuyReagentBankOpcode - Player ({}, name: {}) without enough gold.", _player->GetGUID().ToString(), _player->GetName());
171 return;
172 }
173
174 _player->ModifyMoney(-price);
176}
177
179{
180 if (!CanUseBank(reagentBank.Banker))
181 {
182 TC_LOG_DEBUG("network", "WORLD: HandleReagentBankDepositOpcode - {} not found or you can't interact with him.", reagentBank.Banker.ToString());
183 return;
184 }
185
187 {
189 return;
190 }
191
192 // query all reagents from player's inventory
193 bool anyDeposited = false;
195 {
196 ItemPosCountVec dest;
197 InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false, true, true);
198 if (msg != EQUIP_ERR_OK)
199 {
200 if (msg != EQUIP_ERR_REAGENT_BANK_FULL || !anyDeposited)
201 _player->SendEquipError(msg, item, nullptr);
202 break;
203 }
204
205 if (dest.size() == 1 && dest[0].pos == item->GetPos())
206 {
208 continue;
209 }
210
211 // store reagent
212 _player->RemoveItem(item->GetBagSlot(), item->GetSlot(), true);
213 _player->BankItem(dest, item, true);
214 anyDeposited = true;
215 }
216}
217
219{
220 if (!CanUseBank())
221 {
222 TC_LOG_DEBUG("network", "WORLD: HandleAutoBankReagentOpcode - {} not found or you can't interact with him.", _player->PlayerTalkClass->GetInteractionData().SourceGuid.ToString());
223 return;
224 }
225
227 {
229 return;
230 }
231
232 Item* item = _player->GetItemByPos(autoBankReagent.PackSlot, autoBankReagent.Slot);
233 if (!item)
234 return;
235
236 ItemPosCountVec dest;
237 InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false, true, true);
238 if (msg != EQUIP_ERR_OK)
239 {
240 _player->SendEquipError(msg, item, nullptr);
241 return;
242 }
243
244 if (dest.size() == 1 && dest[0].pos == item->GetPos())
245 {
247 return;
248 }
249
250 _player->RemoveItem(autoBankReagent.PackSlot, autoBankReagent.Slot, true);
251 _player->BankItem(dest, item, true);
252}
253
255{
256 if (!CanUseBank())
257 {
258 TC_LOG_DEBUG("network", "WORLD: HandleAutoBankReagentOpcode - {} not found or you can't interact with him.", _player->PlayerTalkClass->GetInteractionData().SourceGuid.ToString());
259 return;
260 }
261
263 {
265 return;
266 }
267
268 Item* pItem = _player->GetItemByPos(autoStoreBankReagent.Slot, autoStoreBankReagent.PackSlot);
269 if (!pItem)
270 return;
271
272 if (_player->IsReagentBankPos(autoStoreBankReagent.Slot, autoStoreBankReagent.PackSlot))
273 {
274 ItemPosCountVec dest;
275 InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, pItem, false);
276 if (msg != EQUIP_ERR_OK)
277 {
278 _player->SendEquipError(msg, pItem, nullptr);
279 return;
280 }
281
282 _player->RemoveItem(autoStoreBankReagent.Slot, autoStoreBankReagent.PackSlot, true);
283 _player->StoreItem(dest, pItem, true);
284 }
285 else
286 {
287 ItemPosCountVec dest;
288 InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, pItem, false, true, true);
289 if (msg != EQUIP_ERR_OK)
290 {
291 _player->SendEquipError(msg, pItem, nullptr);
292 return;
293 }
294
295 _player->RemoveItem(autoStoreBankReagent.Slot, autoStoreBankReagent.PackSlot, true);
296 _player->BankItem(dest, pItem, true);
297 }
298}
299
301{
302 _player->PlayerTalkClass->GetInteractionData().Reset();
303 _player->PlayerTalkClass->GetInteractionData().SourceGuid = guid;
305 npcInteraction.Npc = guid;
307 npcInteraction.Success = true;
308 SendPacket(npcInteraction.Write());
309}
DB2Storage< BankBagSlotPricesEntry > sBankBagSlotPricesStore("BankBagSlotPrices.db2", &BankBagSlotPricesLoadInfo::Instance)
int64_t int64
Definition: Define.h:137
uint64_t uint64
Definition: Define.h:141
uint32_t uint32
Definition: Define.h:142
InventoryResult
Definition: ItemDefines.h:25
@ EQUIP_ERR_REAGENT_BANK_FULL
Definition: ItemDefines.h:122
@ EQUIP_ERR_REAGENT_BANK_LOCKED
Definition: ItemDefines.h:123
@ EQUIP_ERR_OK
Definition: ItemDefines.h:26
@ EQUIP_ERR_CANT_SWAP
Definition: ItemDefines.h:47
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
std::vector< ItemPosCount > ItemPosCountVec
Definition: Player.h:750
@ GOLD
@ SPELL_AURA_FEIGN_DEATH
@ UNIT_NPC_FLAG_BANKER
Definition: UnitDefines.h:314
@ UNIT_NPC_FLAG_2_NONE
Definition: UnitDefines.h:336
@ UNIT_STATE_DIED
Definition: Unit.h:255
@ NULL_BAG
Definition: Unit.h:62
@ NULL_SLOT
Definition: Unit.h:63
Definition: Item.h:170
uint16 GetPos() const
Definition: Item.h:284
uint32 GetCount() const
Definition: Item.h:273
std::string ToString() const
Definition: ObjectGuid.cpp:554
uint32 GetEntry() const
Definition: Object.h:161
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void SendEquipError(InventoryResult msg, Item const *item1=nullptr, Item const *item2=nullptr, uint32 itemId=0) const
Definition: Player.cpp:13254
void ItemRemovedQuestCheck(uint32 entry, uint32 count)
Definition: Player.cpp:16645
bool ModifyMoney(int64 amount, bool sendError=true)
Definition: Player.cpp:24098
Creature * GetNPCIfCanInteractWith(ObjectGuid const &guid, NPCFlags npcFlags, NPCFlags2 npcFlags2) const
Definition: Player.cpp:1947
Item * BankItem(ItemPosCountVec const &dest, Item *pItem, bool update)
Definition: Player.cpp:12043
InventoryResult CanStoreItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, Item *pItem, bool swap=false) const
Definition: Player.cpp:10037
uint8 GetBankBagSlotCount() const
Definition: Player.h:1378
InventoryResult CanBankItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, Item *pItem, bool swap, bool not_loading=true, bool reagentBankOnly=false) const
Definition: Player.cpp:11172
Item * StoreItem(ItemPosCountVec const &pos, Item *pItem, bool update)
Definition: Player.cpp:11612
Item * GetItemByPos(uint16 pos) const
Definition: Player.cpp:9582
static bool IsReagentBankPos(uint16 pos)
Definition: Player.h:1370
void UpdateCriteria(CriteriaType type, uint64 miscValue1=0, uint64 miscValue2=0, uint64 miscValue3=0, WorldObject *ref=nullptr)
Definition: Player.cpp:26767
std::vector< Item * > GetCraftingReagentItemsToDeposit()
Definition: Player.cpp:9552
bool IsReagentBankUnlocked() const
Definition: Player.h:2705
void ItemAddedQuestCheck(uint32 entry, uint32 count, Optional< bool > boundItemFlagRequirement={}, bool *hadBoundItemObjective=nullptr)
Definition: Player.cpp:16624
void UnlockReagentBank()
Definition: Player.h:2706
static bool IsBankPos(uint16 pos)
Definition: Player.h:1368
void RemoveItem(uint8 bag, uint8 slot, bool update)
Definition: Player.cpp:12048
void SetBankBagSlotCount(uint8 count)
Definition: Player.h:1379
std::unique_ptr< PlayerMenu > PlayerTalkClass
Definition: Player.h:2380
bool HasEnoughMoney(uint64 amount) const
Definition: Player.h:1740
void RemoveAurasByType(AuraType auraType, std::function< bool(AuraApplication const *)> const &check, AuraRemoveMode removeMode=AURA_REMOVE_BY_DEFAULT)
Definition: Unit.cpp:3812
std::string const & GetName() const
Definition: Object.h:555
WorldPacket const * Write() override
Definition: NPCPackets.cpp:94
void HandleAutoBankReagentOpcode(WorldPackets::Bank::AutoBankReagent &autoBankRegent)
void HandleBuyBankSlotOpcode(WorldPackets::Bank::BuyBankSlot &packet)
Player * GetPlayer() const
void HandleBuyReagentBankOpcode(WorldPackets::Bank::ReagentBank &reagentBank)
void HandleReagentBankDepositOpcode(WorldPackets::Bank::ReagentBank &reagentBank)
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
void HandleAutoBankItemOpcode(WorldPackets::Bank::AutoBankItem &packet)
Definition: BankHandler.cpp:27
Player * _player
void SendShowBank(ObjectGuid guid)
void HandleBankerActivateOpcode(WorldPackets::NPC::Hello &packet)
Definition: BankHandler.cpp:60
void HandleAutoStoreBankReagentOpcode(WorldPackets::Bank::AutoStoreBankReagent &autoStoreBankReagent)
bool CanUseBank(ObjectGuid bankerGUID=ObjectGuid::Empty) const
void HandleAutoStoreBankItemOpcode(WorldPackets::Bank::AutoStoreBankItem &packet)
Definition: BankHandler.cpp:78