TrinityCore
VoidStorageHandler.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 "Bag.h"
20#include "CollectionMgr.h"
21#include "Common.h"
22#include "Log.h"
23#include "ObjectAccessor.h"
24#include "ObjectMgr.h"
25#include "Player.h"
26#include "VoidStoragePackets.h"
27
29{
31}
32
34{
36 if (!unit)
37 {
38 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageUnlock - {} not found or player can't interact with it.", unlockVoidStorage.Npc.ToString());
39 return;
40 }
41
43 {
44 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageUnlock - Player ({}, name: {}) tried to unlock void storage a 2nd time.", _player->GetGUID().ToString(), _player->GetName());
45 return;
46 }
47
50}
51
53{
55 if (!unit)
56 {
57 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageQuery - {} not found or player can't interact with it.", queryVoidStorage.Npc.ToString());
59 return;
60 }
61
63 {
64 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageQuery - Player ({}, name: {}) queried void storage without unlocking it.", _player->GetGUID().ToString(), _player->GetName());
66 return;
67 }
68
70 voidStorageContents.Items.reserve(VOID_STORAGE_MAX_SLOT);
71
72 for (uint8 i = 0; i < VOID_STORAGE_MAX_SLOT; ++i)
73 {
75 if (!item)
76 continue;
77
79 voidItem.Guid = ObjectGuid::Create<HighGuid::Item>(item->ItemId);
80 voidItem.Creator = item->CreatorGuid;
81 voidItem.Slot = i;
82 voidItem.Item.Initialize(item);
83
84 voidStorageContents.Items.push_back(voidItem);
85 }
86
87 SendPacket(voidStorageContents.Write());
88}
89
91{
93 if (!unit)
94 {
95 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - {} not found or player can't interact with it.", voidStorageTransfer.Npc.ToString());
96 return;
97 }
98
100 {
101 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - Player ({}, name: {}) queried void storage without unlocking it.", _player->GetGUID().ToString(), _player->GetName());
102 return;
103 }
104
105 if (voidStorageTransfer.Deposits.size() > _player->GetNumOfVoidStorageFreeSlots())
106 {
108 return;
109 }
110
111 uint32 freeBagSlots = 0;
112 if (!voidStorageTransfer.Withdrawals.empty())
113 {
114 // make this a Player function
116 if (Bag* bag = _player->GetBagByPos(i))
117 freeBagSlots += bag->GetFreeSlots();
118
120 for (uint8 i = INVENTORY_SLOT_ITEM_START; i < inventoryEnd; i++)
122 ++freeBagSlots;
123 }
124
125 if (voidStorageTransfer.Withdrawals.size() > freeBagSlots)
126 {
128 return;
129 }
130
132 {
134 return;
135 }
136
138 voidStorageTransferChanges.AddedItems.reserve(VOID_STORAGE_MAX_DEPOSIT);
139 voidStorageTransferChanges.RemovedItems.reserve(VOID_STORAGE_MAX_DEPOSIT);
140
141 uint8 depositCount = 0;
142 for (uint32 i = 0; i < voidStorageTransfer.Deposits.size(); ++i)
143 {
144 Item* item = _player->GetItemByGuid(voidStorageTransfer.Deposits[i]);
145 if (!item)
146 {
147 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - {} {} wants to deposit an invalid item ({}).", _player->GetGUID().ToString(), _player->GetName(), voidStorageTransfer.Deposits[i].ToString());
148 continue;
149 }
150
151 VoidStorageItem itemVS(sObjectMgr->GenerateVoidStorageItemId(), item->GetEntry(), item->GetCreator(),
153 item->GetContext(), item->GetBonusListIDs());
154
156 voidItem.Guid = ObjectGuid::Create<HighGuid::Item>(itemVS.ItemId);
157 voidItem.Creator = item->GetCreator();
158 voidItem.Item.Initialize(&itemVS);
159 voidItem.Slot = _player->AddVoidStorageItem(std::move(itemVS));
160
161 voidStorageTransferChanges.AddedItems.push_back(voidItem);
162
163 _player->DestroyItem(item->GetBagSlot(), item->GetSlot(), true);
164 ++depositCount;
165 }
166
167 int64 cost = depositCount * VOID_STORAGE_STORE_ITEM_COST;
168
169 _player->ModifyMoney(-cost);
170
171 for (uint32 i = 0; i < voidStorageTransfer.Withdrawals.size(); ++i)
172 {
173 uint8 slot = 0;
174 VoidStorageItem* itemVS = _player->GetVoidStorageItem(voidStorageTransfer.Withdrawals[i].GetCounter(), slot);
175 if (!itemVS)
176 {
177 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - {} {} tried to withdraw an invalid item (id: {})", _player->GetGUID().ToString(), _player->GetName(), voidStorageTransfer.Withdrawals[i].ToString());
178 continue;
179 }
180
181 ItemPosCountVec dest;
183 if (msg != EQUIP_ERR_OK)
184 {
186 TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - {} {} couldn't withdraw item id {} because inventory was full.", _player->GetGUID().ToString(), _player->GetName(), voidStorageTransfer.Withdrawals[i].ToString());
187 return;
188 }
189
190 if (Item* item = _player->StoreNewItem(dest, itemVS->ItemEntry, true, itemVS->RandomBonusListId, GuidSet(), itemVS->Context, &itemVS->BonusListIDs))
191 {
192 item->SetCreator(itemVS->CreatorGuid);
193 item->SetBinding(true);
195 }
196
197 voidStorageTransferChanges.RemovedItems.push_back(ObjectGuid::Create<HighGuid::Item>(itemVS->ItemId));
198
200 }
201
202 SendPacket(voidStorageTransferChanges.Write());
203
205}
206
208{
210 if (!unit)
211 {
212 TC_LOG_DEBUG("network", "WORLD: HandleVoidSwapItem - {} not found or player can't interact with it.", swapVoidItem.Npc.ToString());
213 return;
214 }
215
217 {
218 TC_LOG_DEBUG("network", "WORLD: HandleVoidSwapItem - Player ({}, name: {}) queried void storage without unlocking it.", _player->GetGUID().ToString(), _player->GetName());
219 return;
220 }
221
222 uint8 oldSlot;
223 if (!_player->GetVoidStorageItem(swapVoidItem.VoidItemGuid.GetCounter(), oldSlot))
224 {
225 TC_LOG_DEBUG("network", "WORLD: HandleVoidSwapItem - {} {} requested swapping an invalid item (slot: {}, itemid: {}).", _player->GetGUID().ToString(), _player->GetName(), swapVoidItem.DstSlot, swapVoidItem.VoidItemGuid.ToString());
226 return;
227 }
228
229 bool usedDestSlot = _player->GetVoidStorageItem(swapVoidItem.DstSlot) != nullptr;
230 ObjectGuid itemIdDest;
231 if (usedDestSlot)
232 itemIdDest = ObjectGuid::Create<HighGuid::Item>(_player->GetVoidStorageItem(swapVoidItem.DstSlot)->ItemId);
233
234 if (!_player->SwapVoidStorageItem(oldSlot, swapVoidItem.DstSlot))
235 {
237 return;
238 }
239
241 voidItemSwapResponse.VoidItemA = swapVoidItem.VoidItemGuid;
242 voidItemSwapResponse.VoidItemSlotA = swapVoidItem.DstSlot;
243 if (usedDestSlot)
244 {
245 voidItemSwapResponse.VoidItemB = itemIdDest;
246 voidItemSwapResponse.VoidItemSlotB = oldSlot;
247 }
248
249 SendPacket(voidItemSwapResponse.Write());
250}
uint8_t uint8
Definition: Define.h:144
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_OK
Definition: ItemDefines.h:26
@ ITEM_MODIFIER_TIMEWALKER_LEVEL
Definition: ItemDefines.h:220
@ ITEM_MODIFIER_ARTIFACT_KNOWLEDGE_LEVEL
Definition: ItemDefines.h:234
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
std::set< ObjectGuid > GuidSet
Definition: ObjectGuid.h:393
#define sObjectMgr
Definition: ObjectMgr.h:1946
std::vector< ItemPosCount > ItemPosCountVec
Definition: Player.h:750
@ INVENTORY_SLOT_BAG_START
Definition: Player.h:675
@ INVENTORY_SLOT_BAG_END
Definition: Player.h:676
@ INVENTORY_SLOT_ITEM_START
Definition: Player.h:687
#define INVENTORY_SLOT_BAG_0
Definition: Player.h:625
@ VOID_STORAGE_MAX_SLOT
@ VOID_STORAGE_STORE_ITEM_COST
@ VOID_STORAGE_MAX_DEPOSIT
@ VOID_STORAGE_UNLOCK_COST
VoidTransferError
@ VOID_TRANSFER_ERROR_NO_ERROR
@ VOID_TRANSFER_ERROR_FULL
@ VOID_TRANSFER_ERROR_NOT_ENOUGH_MONEY
@ VOID_TRANSFER_ERROR_INVENTORY_FULL
@ VOID_TRANSFER_ERROR_INTERNAL_ERROR_1
@ UNIT_NPC_FLAG_TRANSMOGRIFIER
Definition: UnitDefines.h:325
@ UNIT_NPC_FLAG_VAULTKEEPER
Definition: UnitDefines.h:326
@ UNIT_NPC_FLAG_2_NONE
Definition: UnitDefines.h:336
@ NULL_BAG
Definition: Unit.h:62
@ NULL_SLOT
Definition: Unit.h:63
Definition: Bag.h:27
void AddItemAppearance(Item *item)
Definition: Item.h:170
uint8 GetSlot() const
Definition: Item.h:280
ObjectGuid GetCreator() const
Definition: Item.h:192
std::vector< int32 > const & GetBonusListIDs() const
Definition: Item.h:229
ItemRandomBonusListId GetItemRandomBonusListId() const
Definition: Item.h:292
ItemContext GetContext() const
Definition: Item.h:441
uint8 GetBagSlot() const
Definition: Item.cpp:1239
uint32 GetModifier(ItemModifier modifier) const
Definition: Item.cpp:2423
LowType GetCounter() const
Definition: ObjectGuid.h:293
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
Item * StoreNewItem(ItemPosCountVec const &pos, uint32 itemId, bool update, ItemRandomBonusListId randomBonusListId=0, GuidSet const &allowedLooters=GuidSet(), ItemContext context=ItemContext::NONE, std::vector< int32 > const *bonusListIDs=nullptr, bool addToCollection=true)
Definition: Player.cpp:11537
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
Bag * GetBagByPos(uint8 slot) const
Definition: Player.cpp:9611
uint8 GetInventorySlotCount() const
Definition: Player.h:1376
uint8 AddVoidStorageItem(VoidStorageItem &&item)
Definition: Player.cpp:29156
Item * GetItemByPos(uint16 pos) const
Definition: Player.cpp:9582
void DestroyItem(uint8 bag, uint8 slot, bool update)
Definition: Player.cpp:12180
void DeleteVoidStorageItem(uint8 slot)
Definition: Player.cpp:29170
uint8 GetNumOfVoidStorageFreeSlots() const
Definition: Player.cpp:29145
void UnlockVoidStorage()
Definition: Player.h:2694
VoidStorageItem * GetVoidStorageItem(uint8 slot) const
Definition: Player.cpp:29191
bool IsVoidStorageUnlocked() const
Definition: Player.h:2693
Item * GetItemByGuid(ObjectGuid guid) const
Definition: Player.cpp:9566
InventoryResult CanStoreNewItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, uint32 item, uint32 count, uint32 *no_space_count=nullptr) const
Definition: Player.cpp:10032
bool SwapVoidStorageItem(uint8 oldSlot, uint8 newSlot)
Definition: Player.cpp:29182
bool HasEnoughMoney(uint64 amount) const
Definition: Player.h:1740
std::string const & GetName() const
Definition: Object.h:555
size_type size() const
Array< ObjectGuid, VOID_STORAGE_MAX_DEPOSIT > Deposits
Array< ObjectGuid, VOID_STORAGE_MAX_WITHDRAW > Withdrawals
void HandleVoidStorageQuery(WorldPackets::VoidStorage::QueryVoidStorage &queryVoidStorage)
void HandleVoidStorageUnlock(WorldPackets::VoidStorage::UnlockVoidStorage &unlockVoidStorage)
void HandleVoidStorageTransfer(WorldPackets::VoidStorage::VoidStorageTransfer &voidStorageTransfer)
void HandleVoidSwapItem(WorldPackets::VoidStorage::SwapVoidItem &swapVoidItem)
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
void SendVoidStorageTransferResult(VoidTransferError result)
Player * _player
CollectionMgr * GetCollectionMgr() const
uint64 ItemId
Definition: Player.h:1034
ObjectGuid CreatorGuid
Definition: Player.h:1036
ItemRandomBonusListId RandomBonusListId
Definition: Player.h:1037
ItemContext Context
Definition: Player.h:1040
std::vector< int32 > BonusListIDs
Definition: Player.h:1041
uint32 ItemEntry
Definition: Player.h:1035
void Initialize(::Item const *item)
WorldPackets::Item::ItemInstance Item