TrinityCore
cs_titles.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/* ScriptData
19Name: titles_commandscript
20%Complete: 100
21Comment: All titles related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "Chat.h"
27#include "ChatCommand.h"
28#include "DB2Stores.h"
29#include "Language.h"
30#include "Player.h"
31#include "RBAC.h"
32
33using namespace Trinity::ChatCommands;
34
36{
37public:
38 titles_commandscript() : CommandScript("titles_commandscript") { }
39
41 {
42 static ChatCommandTable titlesSetCommandTable =
43 {
45 };
46 static ChatCommandTable titlesCommandTable =
47 {
51 { "set", titlesSetCommandTable },
52 };
53 static ChatCommandTable commandTable =
54 {
55 { "titles", titlesCommandTable },
56 };
57 return commandTable;
58 }
59
61 {
62 Player* target = handler->getSelectedPlayer();
63 if (!target)
64 {
66 handler->SetSentErrorMessage(true);
67 return false;
68 }
69
70 // check online security
71 if (handler->HasLowerSecurity(target, ObjectGuid::Empty))
72 return false;
73
74 CharTitlesEntry const* titleInfo = sCharTitlesStore.LookupEntry(titleId);
75 if (!titleInfo)
76 {
77 handler->PSendSysMessage(LANG_INVALID_TITLE_ID, titleId);
78 handler->SetSentErrorMessage(true);
79 return false;
80 }
81
82 std::string tNameLink = handler->GetNameLink(target);
83 std::string titleNameStr = fmt::sprintf(target->GetNativeGender() == GENDER_MALE ? titleInfo->Name[handler->GetSessionDbcLocale()] : titleInfo->Name1[handler->GetSessionDbcLocale()], target->GetName());
84
85 target->SetTitle(titleInfo);
86 target->SetChosenTitle(titleInfo->MaskID);
87
88 handler->PSendSysMessage(LANG_TITLE_CURRENT_RES, titleId, titleNameStr.c_str(), tNameLink.c_str());
89
90 return true;
91 }
92
94 {
95 Player* target = handler->getSelectedPlayer();
96 if (!target)
97 {
99 handler->SetSentErrorMessage(true);
100 return false;
101 }
102
103 // check online security
104 if (handler->HasLowerSecurity(target, ObjectGuid::Empty))
105 return false;
106
107 CharTitlesEntry const* titleInfo = sCharTitlesStore.LookupEntry(titleId);
108 if (!titleInfo)
109 {
110 handler->PSendSysMessage(LANG_INVALID_TITLE_ID, titleId);
111 handler->SetSentErrorMessage(true);
112 return false;
113 }
114
115 std::string tNameLink = handler->GetNameLink(target);
116 std::string titleNameStr = fmt::sprintf(target->GetNativeGender() == GENDER_MALE ? titleInfo->Name[handler->GetSessionDbcLocale()] : titleInfo->Name1[handler->GetSessionDbcLocale()], target->GetName());
117
118 target->SetTitle(titleInfo);
119 handler->PSendSysMessage(LANG_TITLE_ADD_RES, titleId, titleNameStr.c_str(), tNameLink.c_str());
120
121 return true;
122 }
123
125 {
126 Player* target = handler->getSelectedPlayer();
127 if (!target)
128 {
130 handler->SetSentErrorMessage(true);
131 return false;
132 }
133
134 // check online security
135 if (handler->HasLowerSecurity(target, ObjectGuid::Empty))
136 return false;
137
138 CharTitlesEntry const* titleInfo = sCharTitlesStore.LookupEntry(titleId);
139 if (!titleInfo)
140 {
141 handler->PSendSysMessage(LANG_INVALID_TITLE_ID, titleId);
142 handler->SetSentErrorMessage(true);
143 return false;
144 }
145
146 target->SetTitle(titleInfo, true);
147
148 std::string tNameLink = handler->GetNameLink(target);
149 std::string titleNameStr = fmt::sprintf(target->GetNativeGender() == GENDER_MALE ? titleInfo->Name[handler->GetSessionDbcLocale()] : titleInfo->Name1[handler->GetSessionDbcLocale()], target->GetName());
150
151 handler->PSendSysMessage(LANG_TITLE_REMOVE_RES, titleId, titleNameStr.c_str(), tNameLink.c_str());
152
153 if (!target->HasTitle(target->m_playerData->PlayerTitle))
154 {
155 target->SetChosenTitle(0);
156 handler->PSendSysMessage(LANG_CURRENT_TITLE_RESET, tNameLink.c_str());
157 }
158
159 return true;
160 }
161
162 //Edit Player KnownTitles
164 {
165 Player* target = handler->getSelectedPlayer();
166 if (!target)
167 {
169 handler->SetSentErrorMessage(true);
170 return false;
171 }
172
173 // check online security
174 if (handler->HasLowerSecurity(target, ObjectGuid::Empty))
175 return false;
176
177 uint64 titles2 = mask;
178
179 for (uint32 i = 1; i < sCharTitlesStore.GetNumRows() && i < 64; ++i)
180 if (CharTitlesEntry const* tEntry = sCharTitlesStore.LookupEntry(i))
181 titles2 &= ~(uint64(1) << tEntry->MaskID);
182
183 mask &= ~titles2; // remove non-existing titles
184
185 target->SetKnownTitles(0, mask);
186 handler->SendSysMessage(LANG_DONE);
187
188 if (!target->HasTitle(target->m_playerData->PlayerTitle))
189 {
190 target->SetChosenTitle(0);
191 handler->PSendSysMessage(LANG_CURRENT_TITLE_RESET, handler->GetNameLink(target).c_str());
192 }
193
194 return true;
195 }
196};
197
199{
201}
DB2Storage< CharTitlesEntry > sCharTitlesStore("CharTitles.db2", &CharTitlesLoadInfo::Instance)
uint64_t uint64
Definition: Define.h:141
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
@ LANG_TITLE_CURRENT_RES
Definition: Language.h:411
@ LANG_TITLE_REMOVE_RES
Definition: Language.h:410
@ LANG_TITLE_ADD_RES
Definition: Language.h:409
@ LANG_CURRENT_TITLE_RESET
Definition: Language.h:412
@ LANG_INVALID_TITLE_ID
Definition: Language.h:408
@ LANG_NO_CHAR_SELECTED
Definition: Language.h:150
@ LANG_DONE
Definition: Language.h:75
Role Based Access Control related classes definition.
@ GENDER_MALE
Player * getSelectedPlayer()
Definition: Chat.cpp:200
virtual LocaleConstant GetSessionDbcLocale() const
Definition: Chat.cpp:592
virtual std::string GetNameLink() const
Definition: Chat.cpp:58
void PSendSysMessage(const char *fmt, Args &&... args)
Definition: Chat.h:57
bool HasLowerSecurity(Player *target, ObjectGuid guid, bool strong=false)
Definition: Chat.cpp:63
void SetSentErrorMessage(bool val)
Definition: Chat.h:114
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition: Chat.cpp:113
static ObjectGuid const Empty
Definition: ObjectGuid.h:274
UF::UpdateField< UF::PlayerData, 0, TYPEID_PLAYER > m_playerData
Definition: Player.h:2863
Gender GetNativeGender() const override
Definition: Player.h:1217
void SetKnownTitles(uint32 index, uint64 mask)
Definition: Player.h:2664
bool HasTitle(uint32 bitIndex) const
Definition: Player.cpp:26268
void SetTitle(CharTitlesEntry const *title, bool lost=false)
Definition: Player.cpp:26283
void SetChosenTitle(int32 title)
Definition: Player.h:2663
std::string const & GetName() const
Definition: Object.h:555
static bool HandleTitlesRemoveCommand(ChatHandler *handler, Variant< Hyperlink< title >, uint16 > titleId)
Definition: cs_titles.cpp:124
static bool HandleTitlesAddCommand(ChatHandler *handler, Variant< Hyperlink< title >, uint32 > titleId)
Definition: cs_titles.cpp:93
static bool HandleTitlesSetMaskCommand(ChatHandler *handler, uint64 mask)
Definition: cs_titles.cpp:163
static bool HandleTitlesCurrentCommand(ChatHandler *handler, Variant< Hyperlink< title >, uint32 > titleId)
Definition: cs_titles.cpp:60
ChatCommandTable GetCommands() const override
Definition: cs_titles.cpp:40
void AddSC_titles_commandscript()
Definition: cs_titles.cpp:198
std::vector< ChatCommandBuilder > ChatCommandTable
Definition: ChatCommand.h:49
@ RBAC_PERM_COMMAND_TITLES_CURRENT
Definition: RBAC.h:635
@ RBAC_PERM_COMMAND_TITLES_ADD
Definition: RBAC.h:634
@ RBAC_PERM_COMMAND_TITLES_SET_MASK
Definition: RBAC.h:638
@ RBAC_PERM_COMMAND_TITLES_REMOVE
Definition: RBAC.h:636
LocalizedString Name1
Definition: DB2Structure.h:589
LocalizedString Name
Definition: DB2Structure.h:588