TrinityCore
Loading...
Searching...
No Matches
ChatTextBuilder.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 "ChatTextBuilder.h"
19#include "CreatureTextMgr.h"
20#include "DB2Stores.h"
21#include "LanguageMgr.h"
22#include "ObjectMgr.h"
23#include "Player.h"
24#include "WorldSession.h"
25#include <cstdarg>
26
27namespace Trinity
28{
29ChatPacketSender::ChatPacketSender(ChatMsg chatType, ::Language language, WorldObject const* sender, WorldObject const* receiver,
30 std::string message, uint32 achievementId /*= 0*/, LocaleConstant locale /*= LOCALE_enUS*/, uint32 broadcastTextId /*= 0*/, uint16 emoteId /*= 0*/, uint32 soundKitId /*= 0*/, SoundKitPlayType soundKitPlayType /*= SoundKitPlayType::Normal*/, uint32 playerConditionId /*= 0*/)
31 : Type(chatType), Language(language), Sender(sender), Receiver(receiver), Text(std::move(message)), AchievementId(achievementId), Locale(locale), PlayerConditionID(playerConditionId)
32{
35
36 if (sender && sender->IsUnit() && emoteId)
37 {
38 EmotePacket.emplace();
39 EmotePacket->Guid = sender->GetGUID();
40 EmotePacket->EmoteID = emoteId;
41 EmotePacket->Write();
42 }
43
44 SoundPacket = nullptr;
45 if (soundKitId)
46 {
47 if (soundKitPlayType == SoundKitPlayType::Normal)
48 {
49 SoundPacket = std::make_unique<WorldPackets::Misc::PlaySound>(
50 sender ? sender->GetGUID() : ObjectGuid::Empty,
51 soundKitId,
52 broadcastTextId
53 );
54 }
55 else if (soundKitPlayType == SoundKitPlayType::ObjectSound)
56 {
57 SoundPacket = std::make_unique<WorldPackets::Misc::PlayObjectSound>(
58 receiver ? receiver->GetGUID() : ObjectGuid::Empty,
59 sender ? sender->GetGUID() : ObjectGuid::Empty,
60 soundKitId,
61 receiver ? receiver->GetPosition() : Position(),
62 broadcastTextId
63 );
64 }
65 SoundPacket->Write();
66 }
67}
68
69void ChatPacketSender::operator()(Player const* player) const
70{
72 return;
73
74 if (SoundPacket)
75 player->SendDirectMessage(SoundPacket->GetRawPacket());
76
77 if (EmotePacket)
78 player->SendDirectMessage(EmotePacket->GetRawPacket());
79
81 {
83 return;
84 }
85
87 {
88 TranslatedPacket.emplace();
91 TranslatedPacket->Write();
92 }
93
94 player->SendDirectMessage(TranslatedPacket->GetRawPacket());
95}
96
98{
99 BroadcastTextEntry const* bct = sBroadcastTextStore.LookupEntry(_textId);
100 Unit const* unitSender = Object::ToUnit(_source);
101 uint8 const gender = unitSender ? unitSender->GetGender() : GENDER_UNKNOWN;
102 uint32 soundKitId = bct ? bct->SoundKitID[gender == GENDER_FEMALE ? 1 : 0] : 0;
103
104 return new ChatPacketSender(_msgType,
105 bct ? Language(bct->LanguageID) : LANG_UNIVERSAL,
106 _source,
107 _target,
108 bct ? DB2Manager::GetBroadcastTextValue(bct, locale, _gender) : "",
110 locale,
111 bct ? bct->ID : 0,
112 bct ? bct->EmotesID : 0,
113 soundKitId,
115 bct ? bct->ConditionID : 0
116 );
117}
118
123
125{
126 char const* text = sObjectMgr->GetTrinityString(_textId, locale);
127
128 if (_args)
129 {
130 // we need copy va_list before use or original va_list will corrupted
131 va_list ap;
132 va_copy(ap, *_args);
133
134 static size_t const BufferSize = 2048;
135 char strBuffer[BufferSize];
136 vsnprintf(strBuffer, BufferSize, text, ap);
137 va_end(ap);
138
139 return new ChatPacketSender(_msgType, LANG_UNIVERSAL, _source, _target, strBuffer, 0, locale);
140 }
141
142 return new ChatPacketSender(_msgType, LANG_UNIVERSAL, _source, _target, text, 0, locale);
143}
144
150}
LocaleConstant
Definition Common.h:51
#define sCreatureTextMgr
DB2Storage< BroadcastTextEntry > sBroadcastTextStore("BroadcastText.db2", &BroadcastTextLoadInfo::Instance)
uint8_t uint8
Definition Define.h:156
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
#define sLanguageMgr
Definition LanguageMgr.h:97
#define sObjectMgr
Definition ObjectMgr.h:1885
Language
@ LANG_ADDON_LOGGED
@ LANG_UNIVERSAL
@ LANG_ADDON
@ GENDER_UNKNOWN
@ GENDER_FEMALE
SoundKitPlayType
ChatMsg
bool IsUnit() const
Definition BaseEntity.h:171
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
static char const * GetBroadcastTextValue(BroadcastTextEntry const *broadcastText, LocaleConstant locale=DEFAULT_LOCALE, uint8 gender=GENDER_MALE, bool forceGender=false)
static ObjectGuid const Empty
Definition ObjectGuid.h:314
uint32 GetEntry() const
Definition Object.h:89
Unit * ToUnit()
Definition Object.h:116
void SendDirectMessage(WorldPacket const *data) const
Definition Player.cpp:6283
bool MeetPlayerCondition(uint32 conditionId) const
Definition Player.cpp:30309
WorldSession * GetSession() const
Definition Player.h:2272
bool CanUnderstandLanguage(Language language) const
Definition Player.cpp:22433
ChatPacketSender * operator()(LocaleConstant locale) const
WorldPackets::Chat::Chat UntranslatedPacket
WorldObject const * Sender
WorldObject const * Receiver
Optional< WorldPackets::Chat::Chat > TranslatedPacket
std::unique_ptr< WorldPackets::ServerPacket > SoundPacket
Optional< WorldPackets::Chat::Emote > EmotePacket
void operator()(Player const *player) const
ChatPacketSender(ChatMsg chatType, ::Language language, WorldObject const *sender, WorldObject const *receiver, std::string message, uint32 achievementId=0, LocaleConstant locale=LOCALE_enUS, uint32 broadcastTextId=0, uint16 emoteId=0, uint32 soundKitId=0, SoundKitPlayType soundKitPlayType=SoundKitPlayType::Normal, uint32 playerConditionId=0)
ChatPacketSender * operator()(LocaleConstant locale) const
ChatPacketSender * operator()(LocaleConstant locale) const
ChatPacketSender * operator()(LocaleConstant locale) const
Definition Unit.h:635
Gender GetGender() const
Definition Unit.h:767
void Initialize(ChatMsg chatType, Language language, WorldObject const *sender, WorldObject const *receiver, std::string_view message, uint32 achievementId=0, std::string_view channelName="", LocaleConstant locale=DEFAULT_LOCALE, std::string_view addonPrefix="")
WorldPacket const * Write() override
WorldPacket const * GetRawPacket() const
Definition Packet.h:38
LocaleConstant GetSessionDbcLocale() const
STL namespace.
std::array< uint32, 2 > SoundKitID
constexpr void GetPosition(float &x, float &y) const
Definition Position.h:92