Core/PacketIO: Create BankHandler & update bank opcodes to new packet class (#24695)

* Core/PacketIO: Create BankHandler & update bank opcodes to new packet class

(cherry picked from commit 478e86c074)

* fix nopch build

* Update SMSG_BUY_BANK_SLOT_RESULT

* fix naming

* fix copypaste error

Co-authored-by: Luzifix <luzifix19@gmail.com>
This commit is contained in:
ForesterDev
2020-05-29 19:26:53 +03:00
committed by GitHub
parent f0060496f9
commit f7ac6296b6
7 changed files with 333 additions and 185 deletions

View File

@@ -0,0 +1,189 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* 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 "BankPackets.h"
#include "Item.h"
#include "DBCStores.h"
#include "Log.h"
#include "NPCPackets.h"
#include "Opcodes.h"
#include "Player.h"
#include "WorldPacket.h"
#include "WorldSession.h"
bool WorldSession::CanUseBank(ObjectGuid bankerGUID) const
{
// bankerGUID parameter is optional, set to 0 by default.
if (!bankerGUID)
bankerGUID = m_currentBankerGUID;
bool isUsingBankCommand = (bankerGUID == GetPlayer()->GetGUID() && bankerGUID == m_currentBankerGUID);
if (!isUsingBankCommand)
{
Creature* creature = GetPlayer()->GetNPCIfCanInteractWith(bankerGUID, UNIT_NPC_FLAG_BANKER);
if (!creature)
return false;
}
return true;
}
void WorldSession::HandleBankerActivateOpcode(WorldPackets::NPC::Hello& packet)
{
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(packet.Unit, UNIT_NPC_FLAG_BANKER);
if (!unit)
{
TC_LOG_DEBUG("network", "WORLD: HandleBankerActivateOpcode - %s not found or you can not interact with him.", packet.Unit.ToString().c_str());
return;
}
// remove fake death
if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
SendShowBank(packet.Unit);
}
void WorldSession::HandleAutoBankItemOpcode(WorldPackets::Bank::AutoBankItem& packet)
{
TC_LOG_DEBUG("network", "STORAGE: receive bag = %u, slot = %u", packet.Bag, packet.Slot);
if (!CanUseBank())
{
TC_LOG_DEBUG("network", "WORLD: HandleAutoBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
Item* item = _player->GetItemByPos(packet.Bag, packet.Slot);
if (!item)
return;
ItemPosCountVec dest;
InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, item, nullptr);
return;
}
if (dest.size() == 1 && dest[0].pos == item->GetPos())
{
_player->SendEquipError(EQUIP_ERR_NONE, item, nullptr);
return;
}
_player->RemoveItem(packet.Bag, packet.Slot, true);
_player->ItemRemovedQuestCheck(item->GetEntry(), item->GetCount());
_player->BankItem(dest, item, true);
}
void WorldSession::HandleAutoStoreBankItemOpcode(WorldPackets::Bank::AutoStoreBankItem& packet)
{
TC_LOG_DEBUG("network", "STORAGE: receive bag = %u, slot = %u", packet.Bag, packet.Slot);
if (!CanUseBank())
{
TC_LOG_DEBUG("network", "WORLD: HandleAutoStoreBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
Item* item = _player->GetItemByPos(packet.Bag, packet.Slot);
if (!item)
return;
if (_player->IsBankPos(packet.Bag, packet.Slot)) // moving from bank to inventory
{
ItemPosCountVec dest;
InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, item, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, item, nullptr);
return;
}
_player->RemoveItem(packet.Bag, packet.Slot, true);
if (Item const* storedItem = _player->StoreItem(dest, item, true))
_player->ItemAddedQuestCheck(storedItem->GetEntry(), storedItem->GetCount());
}
else // moving from inventory to bank
{
ItemPosCountVec dest;
InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, item, nullptr);
return;
}
_player->RemoveItem(packet.Bag, packet.Slot, true);
_player->BankItem(dest, item, true);
}
}
void WorldSession::HandleBuyBankSlotOpcode(WorldPackets::Bank::BuyBankSlot& buyBankSlot)
{
WorldPackets::Bank::BuyBankSlotResult packet;
if (!CanUseBank(buyBankSlot.Banker))
{
packet.Result = ERR_BANKSLOT_NOTBANKER;
SendPacket(packet.Write());
TC_LOG_DEBUG("network", "WORLD: HandleBuyBankSlotOpcode - %s not found or you can't interact with him.", buyBankSlot.Banker.ToString().c_str());
return;
}
uint32 slot = _player->GetBankBagSlotCount();
// next slot
++slot;
TC_LOG_INFO("network", "PLAYER: Buy bank bag slot, slot number = %u", slot);
BankBagSlotPricesEntry const* slotEntry = sBankBagSlotPricesStore.LookupEntry(slot);
if (!slotEntry)
{
packet.Result = ERR_BANKSLOT_FAILED_TOO_MANY;
SendPacket(packet.Write());
return;
}
uint32 price = slotEntry->price;
if (!_player->HasEnoughMoney(price))
{
packet.Result = ERR_BANKSLOT_INSUFFICIENT_FUNDS;
SendPacket(packet.Write());
return;
}
_player->SetBankBagSlotCount(slot);
_player->ModifyMoney(-int32(price));
packet.Result = ERR_BANKSLOT_OK;
SendPacket(packet.Write());
_player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BUY_BANK_SLOT);
}
void WorldSession::SendShowBank(ObjectGuid guid)
{
m_currentBankerGUID = guid;
WorldPackets::Bank::ShowBank packet;
packet.Banker = guid;
SendPacket(packet.Write());
}

View File

@@ -748,139 +748,6 @@ void WorldSession::HandleAutoStoreBagItemOpcode(WorldPacket& recvData)
_player->StoreItem(dest, pItem, true);
}
void WorldSession::HandleBuyBankSlotOpcode(WorldPacket& recvPacket)
{
TC_LOG_DEBUG("network", "WORLD: CMSG_BUY_BANK_SLOT");
ObjectGuid guid;
recvPacket >> guid;
if (!CanUseBank(guid))
{
TC_LOG_DEBUG("network", "WORLD: HandleBuyBankSlotOpcode - %s not found or you can't interact with him.", guid.ToString().c_str());
return;
}
uint32 slot = _player->GetBankBagSlotCount();
// next slot
++slot;
TC_LOG_INFO("network", "PLAYER: Buy bank bag slot, slot number = %u", slot);
BankBagSlotPricesEntry const* slotEntry = sBankBagSlotPricesStore.LookupEntry(slot);
WorldPacket data(SMSG_BUY_BANK_SLOT_RESULT, 4);
if (!slotEntry)
{
data << uint32(ERR_BANKSLOT_FAILED_TOO_MANY);
SendPacket(&data);
return;
}
uint32 price = slotEntry->price;
if (!_player->HasEnoughMoney(price))
{
data << uint32(ERR_BANKSLOT_INSUFFICIENT_FUNDS);
SendPacket(&data);
return;
}
_player->SetBankBagSlotCount(slot);
_player->ModifyMoney(-int32(price));
data << uint32(ERR_BANKSLOT_OK);
SendPacket(&data);
_player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BUY_BANK_SLOT);
}
void WorldSession::HandleAutoBankItemOpcode(WorldPacket& recvPacket)
{
TC_LOG_DEBUG("network", "WORLD: CMSG_AUTOBANK_ITEM");
uint8 srcbag, srcslot;
recvPacket >> srcbag >> srcslot;
TC_LOG_DEBUG("network", "STORAGE: receive srcbag = %u, srcslot = %u", srcbag, srcslot);
if (!CanUseBank())
{
TC_LOG_DEBUG("network", "WORLD: HandleAutoBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
Item* pItem = _player->GetItemByPos(srcbag, srcslot);
if (!pItem)
return;
ItemPosCountVec dest;
InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, pItem, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, pItem, nullptr);
return;
}
if (dest.size() == 1 && dest[0].pos == pItem->GetPos())
{
_player->SendEquipError(EQUIP_ERR_NONE, pItem, nullptr);
return;
}
_player->RemoveItem(srcbag, srcslot, true);
_player->ItemRemovedQuestCheck(pItem->GetEntry(), pItem->GetCount());
_player->BankItem(dest, pItem, true);
}
void WorldSession::HandleAutoStoreBankItemOpcode(WorldPacket& recvPacket)
{
TC_LOG_DEBUG("network", "WORLD: CMSG_AUTOSTORE_BANK_ITEM");
uint8 srcbag, srcslot;
recvPacket >> srcbag >> srcslot;
TC_LOG_DEBUG("network", "STORAGE: receive srcbag = %u, srcslot = %u", srcbag, srcslot);
if (!CanUseBank())
{
TC_LOG_DEBUG("network", "WORLD: HandleAutoStoreBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
Item* pItem = _player->GetItemByPos(srcbag, srcslot);
if (!pItem)
return;
if (_player->IsBankPos(srcbag, srcslot)) // moving from bank to inventory
{
ItemPosCountVec dest;
InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, pItem, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, pItem, nullptr);
return;
}
_player->RemoveItem(srcbag, srcslot, true);
if (Item const* storedItem = _player->StoreItem(dest, pItem, true))
_player->ItemAddedQuestCheck(storedItem->GetEntry(), storedItem->GetCount());
}
else // moving from inventory to bank
{
ItemPosCountVec dest;
InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, pItem, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, pItem, nullptr);
return;
}
_player->RemoveItem(srcbag, srcslot, true);
_player->BankItem(dest, pItem, true);
}
}
void WorldSession::HandleSetAmmoOpcode(WorldPacket& recvData)
{
if (!_player->IsAlive())
@@ -1350,21 +1217,3 @@ void WorldSession::HandleItemTextQuery(WorldPacket& recvData )
SendPacket(&data);
}
bool WorldSession::CanUseBank(ObjectGuid bankerGUID) const
{
// bankerGUID parameter is optional, set to 0 by default.
if (!bankerGUID)
bankerGUID = m_currentBankerGUID;
bool isUsingBankCommand = (bankerGUID == GetPlayer()->GetGUID() && bankerGUID == m_currentBankerGUID);
if (!isUsingBankCommand)
{
Creature* creature = GetPlayer()->GetNPCIfCanInteractWith(bankerGUID, UNIT_NPC_FLAG_BANKER);
if (!creature)
return false;
}
return true;
}

View File

@@ -78,36 +78,6 @@ void WorldSession::SendTabardVendorActivate(ObjectGuid guid)
SendPacket(&data);
}
void WorldSession::HandleBankerActivateOpcode(WorldPacket& recvData)
{
ObjectGuid guid;
TC_LOG_DEBUG("network", "WORLD: Received CMSG_BANKER_ACTIVATE");
recvData >> guid;
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_BANKER);
if (!unit)
{
TC_LOG_DEBUG("network", "WORLD: HandleBankerActivateOpcode - %s not found or you can not interact with him.", guid.ToString().c_str());
return;
}
// remove fake death
if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
SendShowBank(guid);
}
void WorldSession::SendShowBank(ObjectGuid guid)
{
WorldPacket data(SMSG_SHOW_BANK, 8);
data << guid;
m_currentBankerGUID = guid;
SendPacket(&data);
}
void WorldSession::SendShowMailBox(ObjectGuid guid)
{
WorldPacket data(SMSG_SHOW_MAILBOX, 8);

View File

@@ -18,6 +18,7 @@
#ifndef AllPackets_h__
#define AllPackets_h__
#include "BankPackets.h"
#include "CharacterPackets.h"
#include "ChatPackets.h"
#include "CombatLogPackets.h"

View File

@@ -0,0 +1,49 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* 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 "BankPackets.h"
void WorldPackets::Bank::AutoBankItem::Read()
{
_worldPacket >> Bag;
_worldPacket >> Slot;
}
void WorldPackets::Bank::AutoStoreBankItem::Read()
{
_worldPacket >> Bag;
_worldPacket >> Slot;
}
void WorldPackets::Bank::BuyBankSlot::Read()
{
_worldPacket >> Banker;
}
WorldPacket const* WorldPackets::Bank::BuyBankSlotResult::Write()
{
_worldPacket << uint32(Result);
return &_worldPacket;
}
WorldPacket const* WorldPackets::Bank::ShowBank::Write()
{
_worldPacket << Banker;
return &_worldPacket;
}

View File

@@ -0,0 +1,81 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* 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/>.
*/
#ifndef BankPackets_h__
#define BankPackets_h__
#include "Packet.h"
#include "ObjectGuid.h"
namespace WorldPackets
{
namespace Bank
{
class AutoBankItem final : public ClientPacket
{
public:
AutoBankItem(WorldPacket&& packet) : ClientPacket(CMSG_AUTOBANK_ITEM, std::move(packet)) { }
void Read() override;
uint8 Bag = 0;
uint8 Slot = 0;
};
class AutoStoreBankItem final : public ClientPacket
{
public:
AutoStoreBankItem(WorldPacket&& packet) : ClientPacket(CMSG_AUTOSTORE_BANK_ITEM, std::move(packet)) { }
void Read() override;
uint8 Bag = 0;
uint8 Slot = 0;
};
class BuyBankSlot final : public ClientPacket
{
public:
BuyBankSlot(WorldPacket&& packet) : ClientPacket(CMSG_BUY_BANK_SLOT, std::move(packet)) { }
void Read() override;
ObjectGuid Banker;
};
class BuyBankSlotResult final : public ServerPacket
{
public:
BuyBankSlotResult() : ServerPacket(SMSG_BUY_BANK_SLOT_RESULT, 4) { }
WorldPacket const* Write() override;
uint32 Result;
};
class ShowBank final : public ServerPacket
{
public:
ShowBank() : ServerPacket(SMSG_SHOW_BANK, 8) { }
WorldPacket const* Write() override;
ObjectGuid Banker;
};
}
}
#endif // BankPackets_h__

View File

@@ -78,6 +78,13 @@ class RBACData;
namespace WorldPackets
{
namespace Bank
{
class AutoBankItem;
class AutoStoreBankItem;
class BuyBankSlot;
}
namespace Character
{
class LogoutCancel;
@@ -755,8 +762,6 @@ class TC_GAME_API WorldSession
void SendActivateTaxiReply(ActivateTaxiReply reply);
void HandleTabardVendorActivateOpcode(WorldPacket& recvPacket);
void HandleBankerActivateOpcode(WorldPacket& recvPacket);
void HandleBuyBankSlotOpcode(WorldPacket& recvPacket);
void HandleTrainerListOpcode(WorldPackets::NPC::Hello& packet);
void HandleTrainerBuySpellOpcode(WorldPackets::NPC::TrainerBuySpell& packet);
void HandlePetitionShowListOpcode(WorldPacket& recvPacket);
@@ -798,6 +803,12 @@ class TC_GAME_API WorldSession
void HandleAuctionPlaceBid(WorldPacket& recvData);
void HandleAuctionListPendingSales(WorldPacket& recvData);
// Bank
void HandleBankerActivateOpcode(WorldPackets::NPC::Hello& packet);
void HandleAutoBankItemOpcode(WorldPackets::Bank::AutoBankItem& packet);
void HandleAutoStoreBankItemOpcode(WorldPackets::Bank::AutoStoreBankItem& packet);
void HandleBuyBankSlotOpcode(WorldPackets::Bank::BuyBankSlot& buyBankSlot);
void HandleGetMailList(WorldPacket& recvData);
void HandleSendMail(WorldPacket& recvData);
void HandleMailTakeMoney(WorldPacket& recvData);
@@ -823,8 +834,6 @@ class TC_GAME_API WorldSession
void HandleAutoEquipItemSlotOpcode(WorldPacket& recvPacket);
void HandleSwapItem(WorldPacket& recvPacket);
void HandleBuybackItem(WorldPacket& recvPacket);
void HandleAutoBankItemOpcode(WorldPacket& recvPacket);
void HandleAutoStoreBankItemOpcode(WorldPacket& recvPacket);
void HandleWrapItemOpcode(WorldPacket& recvPacket);
void HandleAttackSwingOpcode(WorldPackets::Combat::AttackSwing& packet);