TrinityCore
Loading...
Searching...
No Matches
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:624
@ ERR_BANKSLOT_NOTBANKER
Definition Player.h:142
@ ERR_BANKSLOT_FAILED_TOO_MANY
Definition Player.h:140
@ ERR_BANKSLOT_OK
Definition Player.h:143
@ ERR_BANKSLOT_INSUFFICIENT_FUNDS
Definition Player.h:141
@ SPELL_AURA_FEIGN_DEATH
@ UNIT_NPC_FLAG_BANKER
@ 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
uint32 GetEntry() const
Definition Object.h:81
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
void ItemRemovedQuestCheck(uint32 entry, uint32 count)
Definition Player.cpp:16086
Item * BankItem(ItemPosCountVec const &dest, Item *pItem, bool update)
Definition Player.cpp:11978
InventoryResult CanStoreItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, Item *pItem, bool swap=false) const
Definition Player.cpp:9994
void UpdateAchievementCriteria(AchievementCriteriaTypes type, uint32 miscValue1=0, uint32 miscValue2=0, WorldObject *ref=nullptr)
Definition Player.cpp:24940
uint8 GetBankBagSlotCount() const
Definition Player.h:1124
bool ModifyMoney(int32 amount, bool sendError=true)
Definition Player.cpp:22339
Item * StoreItem(ItemPosCountVec const &pos, Item *pItem, bool update)
Definition Player.cpp:11661
Item * GetItemByPos(uint16 pos) const
Definition Player.cpp:9552
bool HasEnoughMoney(uint32 amount) const
Definition Player.h:1410
Creature * GetNPCIfCanInteractWith(ObjectGuid const &guid, NPCFlags npcFlags) const
Definition Player.cpp:2094
void SendEquipError(InventoryResult msg, Item *pItem, Item *pItem2=nullptr, uint32 itemid=0) const
Definition Player.cpp:13075
InventoryResult CanBankItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, Item *pItem, bool swap, bool not_loading=true) const
Definition Player.cpp:11183
void ItemAddedQuestCheck(uint32 entry, uint32 count)
Definition Player.cpp:16049
static bool IsBankPos(uint16 pos)
Definition Player.h:1120
void RemoveItem(uint8 bag, uint8 slot, bool update)
Definition Player.cpp:11983
void SetBankBagSlotCount(uint8 count)
Definition Player.h:1125
void RemoveAurasByType(AuraType auraType, std::function< bool(AuraApplication const *)> const &check, AuraRemoveMode removeMode=AURA_REMOVE_BY_DEFAULT)
Definition Unit.cpp:3765
WorldPacket const * Write() override
WorldPacket const * Write() override
void SendPacket(WorldPacket const *packet)
Send a packet to the client.
Player * GetPlayer() const
ObjectGuid m_currentBankerGUID
void HandleAutoBankItemOpcode(WorldPackets::Bank::AutoBankItem &packet)
Player * _player
void SendShowBank(ObjectGuid guid)
void HandleBankerActivateOpcode(WorldPackets::NPC::Hello &packet)
bool CanUseBank(ObjectGuid bankerGUID=ObjectGuid::Empty) const
void HandleAutoStoreBankItemOpcode(WorldPackets::Bank::AutoStoreBankItem &packet)
void HandleBuyBankSlotOpcode(WorldPackets::Bank::BuyBankSlot &buyBankSlot)