2012-07-30 16:39:46 +01:00
/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2012-07-30 16:39:46 +01:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WorldSession.h"
2017-06-04 01:00:45 +02:00
#include "Bag.h"
#include "CollectionMgr.h"
#include "Common.h"
#include "Log.h"
2012-07-30 16:39:46 +01:00
#include "ObjectAccessor.h"
2012-08-01 23:36:41 +01:00
#include "ObjectMgr.h"
2012-07-30 16:39:46 +01:00
#include "Player.h"
2015-04-13 20:35:57 +02:00
#include "VoidStoragePackets.h"
2017-06-04 01:00:45 +02:00
#include "World.h"
2012-07-30 16:39:46 +01:00
void WorldSession :: SendVoidStorageTransferResult ( VoidTransferError result )
{
2015-04-13 20:35:57 +02:00
SendPacket ( WorldPackets :: VoidStorage :: VoidTransferResult ( result ). Write ());
2012-07-30 16:39:46 +01:00
}
2015-04-13 20:35:57 +02:00
void WorldSession :: HandleVoidStorageUnlock ( WorldPackets :: VoidStorage :: UnlockVoidStorage & unlockVoidStorage )
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature * unit = _player -> GetNPCIfCanInteractWith ( unlockVoidStorage . Npc , UNIT_NPC_FLAG_VAULTKEEPER , UNIT_NPC_FLAG_2_NONE );
2012-07-30 16:39:46 +01:00
if ( ! unit )
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageUnlock - %s not found or player can't interact with it." , unlockVoidStorage . Npc . ToString (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
2015-04-13 20:35:57 +02:00
if ( _player -> IsVoidStorageUnlocked ())
2012-07-30 16:39:46 +01:00
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageUnlock - Player (%s, name: %s) tried to unlock void storage a 2nd time." , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
2015-04-13 20:35:57 +02:00
_player -> ModifyMoney ( - int64 ( VOID_STORAGE_UNLOCK_COST ));
_player -> UnlockVoidStorage ();
2012-07-30 16:39:46 +01:00
}
2015-04-13 20:35:57 +02:00
void WorldSession :: HandleVoidStorageQuery ( WorldPackets :: VoidStorage :: QueryVoidStorage & queryVoidStorage )
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature * unit = _player -> GetNPCIfCanInteractWith ( queryVoidStorage . Npc , NPCFlags ( UNIT_NPC_FLAG_TRANSMOGRIFIER | UNIT_NPC_FLAG_VAULTKEEPER ), UNIT_NPC_FLAG_2_NONE );
2012-07-30 16:39:46 +01:00
if ( ! unit )
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageQuery - %s not found or player can't interact with it." , queryVoidStorage . Npc . ToString (). c_str ());
SendPacket ( WorldPackets :: VoidStorage :: VoidStorageFailed (). Write ());
2012-07-30 16:39:46 +01:00
return ;
}
2015-04-13 20:35:57 +02:00
if ( ! _player -> IsVoidStorageUnlocked ())
2012-07-30 16:39:46 +01:00
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageQuery - Player (%s, name: %s) queried void storage without unlocking it." , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str ());
SendPacket ( WorldPackets :: VoidStorage :: VoidStorageFailed (). Write ());
2012-07-30 16:39:46 +01:00
return ;
}
uint8 count = 0 ;
for ( uint8 i = 0 ; i < VOID_STORAGE_MAX_SLOT ; ++ i )
2015-04-13 20:35:57 +02:00
if ( _player -> GetVoidStorageItem ( i ))
2012-07-30 16:39:46 +01:00
++ count ;
2015-04-13 20:35:57 +02:00
WorldPackets :: VoidStorage :: VoidStorageContents voidStorageContents ;
voidStorageContents . Items . reserve ( VOID_STORAGE_MAX_SLOT );
2012-07-30 16:39:46 +01:00
for ( uint8 i = 0 ; i < VOID_STORAGE_MAX_SLOT ; ++ i )
{
2015-04-13 20:35:57 +02:00
VoidStorageItem * item = _player -> GetVoidStorageItem ( i );
2012-07-30 16:39:46 +01:00
if ( ! item )
continue ;
2015-04-13 20:35:57 +02:00
WorldPackets :: VoidStorage :: VoidItem voidItem ;
voidItem . Guid = ObjectGuid :: Create < HighGuid :: Item > ( item -> ItemId );
voidItem . Creator = item -> CreatorGuid ;
voidItem . Slot = i ;
voidItem . Item . Initialize ( item );
2012-07-30 16:39:46 +01:00
2015-04-13 20:35:57 +02:00
voidStorageContents . Items . push_back ( voidItem );
}
2012-07-30 16:39:46 +01:00
2015-04-13 20:35:57 +02:00
SendPacket ( voidStorageContents . Write ());
2012-07-30 16:39:46 +01:00
}
2015-04-13 20:35:57 +02:00
void WorldSession :: HandleVoidStorageTransfer ( WorldPackets :: VoidStorage :: VoidStorageTransfer & voidStorageTransfer )
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature * unit = _player -> GetNPCIfCanInteractWith ( voidStorageTransfer . Npc , UNIT_NPC_FLAG_VAULTKEEPER , UNIT_NPC_FLAG_2_NONE );
2012-07-30 16:39:46 +01:00
if ( ! unit )
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageTransfer - %s not found or player can't interact with it." , voidStorageTransfer . Npc . ToString (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
2015-04-13 20:35:57 +02:00
if ( ! _player -> IsVoidStorageUnlocked ())
2012-07-30 16:39:46 +01:00
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageTransfer - Player (%s, name: %s) queried void storage without unlocking it." , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
2015-07-29 00:04:37 +02:00
if ( voidStorageTransfer . Deposits . size () > _player -> GetNumOfVoidStorageFreeSlots ())
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult ( VOID_TRANSFER_ERROR_FULL );
return ;
}
uint32 freeBagSlots = 0 ;
2015-07-29 00:04:37 +02:00
if ( ! voidStorageTransfer . Withdrawals . empty ())
2012-07-30 16:39:46 +01:00
{
// make this a Player function
for ( uint8 i = INVENTORY_SLOT_BAG_START ; i < INVENTORY_SLOT_BAG_END ; i ++ )
2015-04-13 20:35:57 +02:00
if ( Bag * bag = _player -> GetBagByPos ( i ))
2012-07-30 16:39:46 +01:00
freeBagSlots += bag -> GetFreeSlots ();
2015-04-13 20:35:57 +02:00
2018-02-04 00:11:10 +01:00
uint8 inventoryEnd = INVENTORY_SLOT_ITEM_START + _player -> GetInventorySlotCount ();
for ( uint8 i = INVENTORY_SLOT_ITEM_START ; i < inventoryEnd ; i ++ )
2015-04-13 20:35:57 +02:00
if ( ! _player -> GetItemByPos ( INVENTORY_SLOT_BAG_0 , i ))
2012-07-30 16:39:46 +01:00
++ freeBagSlots ;
}
2015-07-29 00:04:37 +02:00
if ( voidStorageTransfer . Withdrawals . size () > freeBagSlots )
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult ( VOID_TRANSFER_ERROR_INVENTORY_FULL );
return ;
}
2015-07-29 00:04:37 +02:00
if ( ! _player -> HasEnoughMoney ( uint64 ( voidStorageTransfer . Deposits . size () * VOID_STORAGE_STORE_ITEM_COST )))
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult ( VOID_TRANSFER_ERROR_NOT_ENOUGH_MONEY );
return ;
}
2015-08-12 23:05:36 +02:00
WorldPackets :: VoidStorage :: VoidStorageTransferChanges voidStorageTransferChanges ;
voidStorageTransferChanges . AddedItems . reserve ( VOID_STORAGE_MAX_DEPOSIT );
voidStorageTransferChanges . RemovedItems . reserve ( VOID_STORAGE_MAX_DEPOSIT );
2012-07-30 16:39:46 +01:00
uint8 depositCount = 0 ;
2015-07-29 00:04:37 +02:00
for ( uint32 i = 0 ; i < voidStorageTransfer . Deposits . size (); ++ i )
2012-07-30 16:39:46 +01:00
{
2015-04-13 20:35:57 +02:00
Item * item = _player -> GetItemByGuid ( voidStorageTransfer . Deposits [ i ]);
2012-07-30 16:39:46 +01:00
if ( ! item )
{
2015-04-29 20:53:47 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageTransfer - %s %s wants to deposit an invalid item (%s)." , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str (), voidStorageTransfer . Deposits [ i ]. ToString (). c_str ());
2012-07-30 16:39:46 +01:00
continue ;
}
2019-06-03 20:40:34 +02:00
VoidStorageItem itemVS ( sObjectMgr -> GenerateVoidStorageItemId (), item -> GetEntry (), item -> GetCreator (),
2019-10-26 23:58:29 +02:00
item -> GetItemRandomBonusListId (), item -> GetModifier ( ITEM_MODIFIER_TIMEWALKER_LEVEL ), item -> GetModifier ( ITEM_MODIFIER_ARTIFACT_KNOWLEDGE_LEVEL ),
2019-11-23 00:03:42 +01:00
item -> GetContext (), item -> m_itemData -> BonusListIDs );
2012-07-30 16:39:46 +01:00
2015-08-12 23:05:36 +02:00
WorldPackets :: VoidStorage :: VoidItem voidItem ;
voidItem . Guid = ObjectGuid :: Create < HighGuid :: Item > ( itemVS . ItemId );
2019-06-03 20:40:34 +02:00
voidItem . Creator = item -> GetCreator ();
2015-08-12 23:05:36 +02:00
voidItem . Item . Initialize ( & itemVS );
voidItem . Slot = _player -> AddVoidStorageItem ( std :: move ( itemVS ));
2012-07-30 16:39:46 +01:00
2015-08-12 23:05:36 +02:00
voidStorageTransferChanges . AddedItems . push_back ( voidItem );
2012-07-30 16:39:46 +01:00
2015-04-13 20:35:57 +02:00
_player -> DestroyItem ( item -> GetBagSlot (), item -> GetSlot (), true );
2015-08-12 23:05:36 +02:00
++ depositCount ;
2012-07-30 16:39:46 +01:00
}
2015-04-13 20:35:57 +02:00
int64 cost = depositCount * VOID_STORAGE_STORE_ITEM_COST ;
2012-07-30 16:39:46 +01:00
2015-04-13 20:35:57 +02:00
_player -> ModifyMoney ( - cost );
2012-07-30 16:39:46 +01:00
2015-07-29 00:04:37 +02:00
for ( uint32 i = 0 ; i < voidStorageTransfer . Withdrawals . size (); ++ i )
2012-07-30 16:39:46 +01:00
{
2015-04-13 20:35:57 +02:00
uint8 slot = 0 ;
VoidStorageItem * itemVS = _player -> GetVoidStorageItem ( voidStorageTransfer . Withdrawals [ i ]. GetCounter (), slot );
2012-07-30 16:39:46 +01:00
if ( ! itemVS )
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageTransfer - %s %s tried to withdraw an invalid item (id: %s)" , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str (), voidStorageTransfer . Withdrawals [ i ]. ToString (). c_str ());
2012-07-30 16:39:46 +01:00
continue ;
}
ItemPosCountVec dest ;
2015-04-13 20:35:57 +02:00
InventoryResult msg = _player -> CanStoreNewItem ( NULL_BAG , NULL_SLOT , dest , itemVS -> ItemEntry , 1 );
2012-07-30 16:39:46 +01:00
if ( msg != EQUIP_ERR_OK )
{
SendVoidStorageTransferResult ( VOID_TRANSFER_ERROR_INVENTORY_FULL );
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidStorageTransfer - %s %s couldn't withdraw item id %s because inventory was full." , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str (), voidStorageTransfer . Withdrawals [ i ]. ToString (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
2019-06-04 22:36:17 +02:00
Item * item = _player -> StoreNewItem ( dest , itemVS -> ItemEntry , true , itemVS -> RandomBonusListId , GuidSet (), itemVS -> Context , itemVS -> BonusListIDs );
2019-06-03 20:40:34 +02:00
item -> SetCreator ( itemVS -> CreatorGuid );
2012-07-30 18:34:39 +01:00
item -> SetBinding ( true );
2016-07-05 22:07:35 +02:00
GetCollectionMgr () -> AddItemAppearance ( item );
2012-07-30 16:39:46 +01:00
2015-08-12 23:05:36 +02:00
voidStorageTransferChanges . RemovedItems . push_back ( ObjectGuid :: Create < HighGuid :: Item > ( itemVS -> ItemId ));
2012-07-30 16:39:46 +01:00
2015-04-13 20:35:57 +02:00
_player -> DeleteVoidStorageItem ( slot );
2012-07-30 16:39:46 +01:00
}
2015-04-13 20:35:57 +02:00
SendPacket ( voidStorageTransferChanges . Write ());
2012-07-30 16:39:46 +01:00
SendVoidStorageTransferResult ( VOID_TRANSFER_ERROR_NO_ERROR );
}
2015-04-13 20:35:57 +02:00
void WorldSession :: HandleVoidSwapItem ( WorldPackets :: VoidStorage :: SwapVoidItem & swapVoidItem )
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature * unit = _player -> GetNPCIfCanInteractWith ( swapVoidItem . Npc , UNIT_NPC_FLAG_VAULTKEEPER , UNIT_NPC_FLAG_2_NONE );
2012-07-30 16:39:46 +01:00
if ( ! unit )
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidSwapItem - %s not found or player can't interact with it." , swapVoidItem . Npc . ToString (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
2015-04-13 20:35:57 +02:00
if ( ! _player -> IsVoidStorageUnlocked ())
2012-07-30 16:39:46 +01:00
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidSwapItem - Player (%s, name: %s) queried void storage without unlocking it." , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
uint8 oldSlot ;
2015-04-13 20:35:57 +02:00
if ( ! _player -> GetVoidStorageItem ( swapVoidItem . VoidItemGuid . GetCounter (), oldSlot ))
2012-07-30 16:39:46 +01:00
{
2015-04-13 20:35:57 +02:00
TC_LOG_DEBUG ( "network" , "WORLD: HandleVoidSwapItem - %s %s requested swapping an invalid item (slot: %u, itemid: %s)." , _player -> GetGUID (). ToString (). c_str (), _player -> GetName (). c_str (), swapVoidItem . DstSlot , swapVoidItem . VoidItemGuid . ToString (). c_str ());
2012-07-30 16:39:46 +01:00
return ;
}
2020-08-14 17:06:03 +02:00
bool usedDestSlot = _player -> GetVoidStorageItem ( swapVoidItem . DstSlot ) != nullptr ;
2012-07-30 16:39:46 +01:00
ObjectGuid itemIdDest ;
if ( usedDestSlot )
2015-04-13 20:35:57 +02:00
itemIdDest = ObjectGuid :: Create < HighGuid :: Item > ( _player -> GetVoidStorageItem ( swapVoidItem . DstSlot ) -> ItemId );
2012-07-30 16:39:46 +01:00
2015-04-13 20:35:57 +02:00
if ( ! _player -> SwapVoidStorageItem ( oldSlot , swapVoidItem . DstSlot ))
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult ( VOID_TRANSFER_ERROR_INTERNAL_ERROR_1 );
return ;
}
2015-04-13 20:35:57 +02:00
WorldPackets :: VoidStorage :: VoidItemSwapResponse voidItemSwapResponse ;
voidItemSwapResponse . VoidItemA = swapVoidItem . VoidItemGuid ;
voidItemSwapResponse . VoidItemSlotA = swapVoidItem . DstSlot ;
2012-07-30 16:39:46 +01:00
if ( usedDestSlot )
{
2015-04-13 20:35:57 +02:00
voidItemSwapResponse . VoidItemB = itemIdDest ;
voidItemSwapResponse . VoidItemSlotB = oldSlot ;
2012-07-30 16:39:46 +01:00
}
2015-04-13 20:35:57 +02:00
SendPacket ( voidItemSwapResponse . Write ());
2012-07-30 16:39:46 +01:00
}