TrinityCore
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "DBCStores.h"
21#include "Log.h"
22#include "NPCPackets.h"
23#include "Opcodes.h"
24#include "Player.h"
25#include "WorldPacket.h"
26#include "WorldSession.h"
27
29{
30 // bankerGUID parameter is optional, set to 0 by default.
31 if (!bankerGUID)
32 bankerGUID = m_currentBankerGUID;
33
34 bool isUsingBankCommand = (bankerGUID == GetPlayer()->GetGUID() && bankerGUID == m_currentBankerGUID);
35
36 if (!isUsingBankCommand)
37 {
39 if (!creature)
40 return false;
41 }
42
43 return true;
44}
45
47{
49 if (!unit)
50 {
51 TC_LOG_DEBUG("network", "WORLD: HandleBankerActivateOpcode - {} not found or you can not interact with him.", packet.Unit.ToString());
52 return;
53 }
54
55 // remove fake death
56 if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
58
59 SendShowBank(packet.Unit);
60}
61
63{
64 TC_LOG_DEBUG("network", "STORAGE: receive bag = {}, slot = {}", packet.Bag, packet.Slot);
65
66 if (!CanUseBank())
67 {
68 TC_LOG_DEBUG("network", "WORLD: HandleAutoBankItemOpcode - Unit ({}) not found or you can't interact with him.", m_currentBankerGUID.ToString());
69 return;
70 }
71
72 Item* item = _player->GetItemByPos(packet.Bag, packet.Slot);
73 if (!item)
74 return;
75
76 ItemPosCountVec dest;
77 InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false);
78 if (msg != EQUIP_ERR_OK)
79 {
80 _player->SendEquipError(msg, item, nullptr);
81 return;
82 }
83
84 if (dest.size() == 1 && dest[0].pos == item->GetPos())
85 {
87 return;
88 }
89
90 _player->RemoveItem(packet.Bag, packet.Slot, true);
92 _player->BankItem(dest, item, true);
93}
94
96{
97 TC_LOG_DEBUG("network", "STORAGE: receive bag = {}, slot = {}", packet.Bag, packet.Slot);
98
99 if (!CanUseBank())
100 {
101 TC_LOG_DEBUG("network", "WORLD: HandleAutoStoreBankItemOpcode - Unit ({}) not found or you can't interact with him.", m_currentBankerGUID.ToString());
102 return;
103 }
104
105 Item* item = _player->GetItemByPos(packet.Bag, packet.Slot);
106 if (!item)
107 return;
108
109 if (_player->IsBankPos(packet.Bag, packet.Slot)) // moving from bank to inventory
110 {
111 ItemPosCountVec dest;
112 InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, item, false);
113 if (msg != EQUIP_ERR_OK)
114 {
115 _player->SendEquipError(msg, item, nullptr);
116 return;
117 }
118
119 _player->RemoveItem(packet.Bag, packet.Slot, true);
120 if (Item const* storedItem = _player->StoreItem(dest, item, true))
121 _player->ItemAddedQuestCheck(storedItem->GetEntry(), storedItem->GetCount());
122 }
123 else // moving from inventory to bank
124 {
125 ItemPosCountVec dest;
126 InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false);
127 if (msg != EQUIP_ERR_OK)
128 {
129 _player->SendEquipError(msg, item, nullptr);
130 return;
131 }
132
133 _player->RemoveItem(packet.Bag, packet.Slot, true);
134 _player->BankItem(dest, item, true);
135 }
136}
137
139{
141 if (!CanUseBank(buyBankSlot.Banker))
142 {
144 SendPacket(packet.Write());
145 TC_LOG_DEBUG("network", "WORLD: HandleBuyBankSlotOpcode - {} not found or you can't interact with him.", buyBankSlot.Banker.ToString());
146 return;
147 }
148
150
151 // next slot
152 ++slot;
153
154 TC_LOG_INFO("network", "PLAYER: Buy bank bag slot, slot number = {}", slot);
155
156 BankBagSlotPricesEntry const* slotEntry = sBankBagSlotPricesStore.LookupEntry(slot);
157
158 if (!slotEntry)
159 {
161 SendPacket(packet.Write());
162 return;
163 }
164
165 uint32 price = slotEntry->Cost;
166
167 if (!_player->HasEnoughMoney(price))
168 {
170 SendPacket(packet.Write());
171 return;
172 }
173
175 _player->ModifyMoney(-int32(price));
176
177 packet.Result = ERR_BANKSLOT_OK;
178 SendPacket(packet.Write());
179
181}
182
184{
185 m_currentBankerGUID = guid;
187 packet.Banker = guid;
188 SendPacket(packet.Write());
189}
@ ACHIEVEMENT_CRITERIA_TYPE_BUY_BANK_SLOT
Definition: DBCEnums.h:171
DBCStorage< BankBagSlotPricesEntry > sBankBagSlotPricesStore(BankBagSlotPricesEntryfmt)
int32_t int32
Definition: Define.h:129
uint32_t uint32
Definition: Define.h:133
InventoryResult
Definition: ItemDefines.h:25
@ 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_INFO(filterType__,...)
Definition: Log.h:159
std::vector< ItemPosCount > ItemPosCountVec
Definition: Player.h:602
@ ERR_BANKSLOT_NOTBANKER
Definition: Player.h:114
@ ERR_BANKSLOT_FAILED_TOO_MANY
Definition: Player.h:112
@ ERR_BANKSLOT_OK
Definition: Player.h:115
@ ERR_BANKSLOT_INSUFFICIENT_FUNDS
Definition: Player.h:113
@ SPELL_AURA_FEIGN_DEATH
@ UNIT_NPC_FLAG_BANKER
Definition: UnitDefines.h:254
@ UNIT_STATE_DIED
Definition: Unit.h:220
@ NULL_BAG
Definition: Unit.h:61
@ NULL_SLOT
Definition: Unit.h:62
Definition: Item.h:62
uint16 GetPos() const
Definition: Item.h:130
uint32 GetCount() const
Definition: Item.h:119
std::string ToString() const
Definition: ObjectGuid.cpp:110
uint32 GetEntry() const
Definition: Object.h:80
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:77
void ItemRemovedQuestCheck(uint32 entry, uint32 count)
Definition: Player.cpp:16219
Item * BankItem(ItemPosCountVec const &dest, Item *pItem, bool update)
Definition: Player.cpp:12112
InventoryResult CanStoreItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, Item *pItem, bool swap=false) const
Definition: Player.cpp:10135
void UpdateAchievementCriteria(AchievementCriteriaTypes type, uint32 miscValue1=0, uint32 miscValue2=0, WorldObject *ref=nullptr)
Definition: Player.cpp:25092
uint8 GetBankBagSlotCount() const
Definition: Player.h:1114
bool ModifyMoney(int32 amount, bool sendError=true)
Definition: Player.cpp:22493
Item * StoreItem(ItemPosCountVec const &pos, Item *pItem, bool update)
Definition: Player.cpp:11795
Item * GetItemByPos(uint16 pos) const
Definition: Player.cpp:9693
bool HasEnoughMoney(uint32 amount) const
Definition: Player.h:1403
Creature * GetNPCIfCanInteractWith(ObjectGuid const &guid, NPCFlags npcFlags) const
Definition: Player.cpp:2146
void SendEquipError(InventoryResult msg, Item *pItem, Item *pItem2=nullptr, uint32 itemid=0) const
Definition: Player.cpp:13208
InventoryResult CanBankItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, Item *pItem, bool swap, bool not_loading=true) const
Definition: Player.cpp:11327
void ItemAddedQuestCheck(uint32 entry, uint32 count)
Definition: Player.cpp:16182
static bool IsBankPos(uint16 pos)
Definition: Player.h:1110
void RemoveItem(uint8 bag, uint8 slot, bool update)
Definition: Player.cpp:12117
void SetBankBagSlotCount(uint8 count)
Definition: Player.h:1115
void RemoveAurasByType(AuraType auraType, std::function< bool(AuraApplication const *)> const &check, AuraRemoveMode removeMode=AURA_REMOVE_BY_DEFAULT)
Definition: Unit.cpp:3761
WorldPacket const * Write() override
Definition: BankPackets.cpp:37
WorldPacket const * Write() override
Definition: BankPackets.cpp:44
void SendPacket(WorldPacket const *packet)
Send a packet to the client.
Player * GetPlayer() const
Definition: WorldSession.h:500
ObjectGuid m_currentBankerGUID
void HandleAutoBankItemOpcode(WorldPackets::Bank::AutoBankItem &packet)
Definition: BankHandler.cpp:62
Player * _player
void SendShowBank(ObjectGuid guid)
void HandleBankerActivateOpcode(WorldPackets::NPC::Hello &packet)
Definition: BankHandler.cpp:46
bool CanUseBank(ObjectGuid bankerGUID=ObjectGuid::Empty) const
Definition: BankHandler.cpp:28
void HandleAutoStoreBankItemOpcode(WorldPackets::Bank::AutoStoreBankItem &packet)
Definition: BankHandler.cpp:95
void HandleBuyBankSlotOpcode(WorldPackets::Bank::BuyBankSlot &buyBankSlot)