Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
cs_titles.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6/* ScriptData
7Name: titles_commandscript
8%Complete: 100
9Comment: All titles related commands
10Category: commandscripts
11EndScriptData */
12
13#include "Chat.h"
14#include "Language.h"
15#include "ObjectMgr.h"
16#include "Player.h"
17#include "ScriptMgr.h"
18
20{
21public:
22 titles_commandscript() : CommandScript("titles_commandscript") { }
23
24 std::vector<ChatCommand> GetCommands() const OVERRIDE
25 {
26 static std::vector<ChatCommand> titlesSetCommandTable =
27 {
29 };
30 static std::vector<ChatCommand> titlesCommandTable =
31 {
35 { "set", rbac::RBAC_PERM_COMMAND_TITLES_SET, false, NULL, "", titlesSetCommandTable },
36 };
37 static std::vector<ChatCommand> commandTable =
38 {
39 { "titles", rbac::RBAC_PERM_COMMAND_TITLES, false, NULL, "", titlesCommandTable },
40 };
41 return commandTable;
42 }
43
44 static bool HandleTitlesCurrentCommand(ChatHandler* handler, char const* args)
45 {
46 // number or [name] Shift-click form |color|Htitle:title_id|h[name]|h|r
47 char* id_p = handler->extractKeyFromLink((char*)args, "Htitle");
48 if (!id_p)
49 return false;
50
51 int32 id = atoi(id_p);
52 if (id <= 0)
53 {
55 handler->SetSentErrorMessage(true);
56 return false;
57 }
58
59 Player* target = handler->getSelectedPlayer();
60 if (!target)
61 {
63 handler->SetSentErrorMessage(true);
64 return false;
65 }
66
67 // check online security
68 if (handler->HasLowerSecurity(target, 0))
69 return false;
70
71 CharTitlesEntry const* titleInfo = sCharTitlesStore.LookupEntry(id);
72 if (!titleInfo)
73 {
75 handler->SetSentErrorMessage(true);
76 return false;
77 }
78
79 std::string tNameLink = handler->GetNameLink(target);
80
81 target->SetTitle(titleInfo); // to be sure that title now known
83
84 handler->PSendSysMessage(LANG_TITLE_CURRENT_RES, id, titleInfo->name, tNameLink.c_str());
85
86 return true;
87 }
88
89 static bool HandleTitlesAddCommand(ChatHandler* handler, char const* args)
90 {
91 // number or [name] Shift-click form |color|Htitle:title_id|h[name]|h|r
92 char* id_p = handler->extractKeyFromLink((char*)args, "Htitle");
93 if (!id_p)
94 return false;
95
96 int32 id = atoi(id_p);
97 if (id <= 0)
98 {
100 handler->SetSentErrorMessage(true);
101 return false;
102 }
103
104 Player* target = handler->getSelectedPlayer();
105 if (!target)
106 {
108 handler->SetSentErrorMessage(true);
109 return false;
110 }
111
112 // check online security
113 if (handler->HasLowerSecurity(target, 0))
114 return false;
115
116 CharTitlesEntry const* titleInfo = sCharTitlesStore.LookupEntry(id);
117 if (!titleInfo)
118 {
120 handler->SetSentErrorMessage(true);
121 return false;
122 }
123
124 std::string tNameLink = handler->GetNameLink(target);
125
126 char titleNameStr[80];
127 snprintf(titleNameStr, 80, titleInfo->name, target->GetName().c_str());
128
129 target->SetTitle(titleInfo);
130 handler->PSendSysMessage(LANG_TITLE_ADD_RES, id, titleNameStr, tNameLink.c_str());
131
132 return true;
133 }
134
135 static bool HandleTitlesRemoveCommand(ChatHandler* handler, char const* args)
136 {
137 // number or [name] Shift-click form |color|Htitle:title_id|h[name]|h|r
138 char* id_p = handler->extractKeyFromLink((char*)args, "Htitle");
139 if (!id_p)
140 return false;
141
142 int32 id = atoi(id_p);
143 if (id <= 0)
144 {
146 handler->SetSentErrorMessage(true);
147 return false;
148 }
149
150 Player* target = handler->getSelectedPlayer();
151 if (!target)
152 {
154 handler->SetSentErrorMessage(true);
155 return false;
156 }
157
158 // check online security
159 if (handler->HasLowerSecurity(target, 0))
160 return false;
161
162 CharTitlesEntry const* titleInfo = sCharTitlesStore.LookupEntry(id);
163 if (!titleInfo)
164 {
166 handler->SetSentErrorMessage(true);
167 return false;
168 }
169
170 target->SetTitle(titleInfo, true);
171
172 std::string tNameLink = handler->GetNameLink(target);
173
174 char titleNameStr[80];
175 snprintf(titleNameStr, 80, titleInfo->name, target->GetName().c_str());
176
177 handler->PSendSysMessage(LANG_TITLE_REMOVE_RES, id, titleNameStr, tNameLink.c_str());
178
179 if (!target->HasTitle(target->GetInt32Value(PLAYER_FIELD_PLAYER_TITLE)))
180 {
182 handler->PSendSysMessage(LANG_CURRENT_TITLE_RESET, tNameLink.c_str());
183 }
184
185 return true;
186 }
187
188 //Edit Player KnownTitles
189 static bool HandleTitlesSetMaskCommand(ChatHandler* handler, char const* args)
190 {
191 if (!*args)
192 return false;
193
194 uint64 titles = 0;
195
196 sscanf((char*)args, UI64FMTD, &titles);
197
198 Player* target = handler->getSelectedPlayer();
199 if (!target)
200 {
202 handler->SetSentErrorMessage(true);
203 return false;
204 }
205
206 // check online security
207 if (handler->HasLowerSecurity(target, 0))
208 return false;
209
210 uint64 titles2 = titles;
211
212 for (uint32 i = 1; i < sCharTitlesStore.GetNumRows(); ++i)
213 if (CharTitlesEntry const* tEntry = sCharTitlesStore.LookupEntry(i))
214 titles2 &= ~(uint64(1) << tEntry->bit_index);
215
216 titles &= ~titles2; // remove not existed titles
217
219 handler->SendSysMessage(LANG_DONE);
220
221 if (!target->HasTitle(target->GetInt32Value(PLAYER_FIELD_PLAYER_TITLE)))
222 {
224 handler->PSendSysMessage(LANG_CURRENT_TITLE_RESET, handler->GetNameLink(target).c_str());
225 }
226
227 return true;
228 }
229};
230
DBCStorage< CharTitlesEntry > sCharTitlesStore(CharTitlesEntryfmt)
std::int32_t int32
Definition Define.h:73
#define UI64FMTD
Definition Define.h:64
std::uint32_t uint32
Definition Define.h:77
#define OVERRIDE
Definition Define.h:60
std::uint64_t uint64
Definition Define.h:76
@ LANG_TITLE_CURRENT_RES
Definition Language.h:362
@ LANG_TITLE_REMOVE_RES
Definition Language.h:361
@ LANG_TITLE_ADD_RES
Definition Language.h:360
@ LANG_CURRENT_TITLE_RESET
Definition Language.h:363
@ LANG_INVALID_TITLE_ID
Definition Language.h:359
@ LANG_NO_CHAR_SELECTED
Definition Language.h:118
@ LANG_DONE
Definition Language.h:56
@ PLAYER_FIELD_KNOWN_TITLES
@ PLAYER_FIELD_PLAYER_TITLE
virtual std::string GetNameLink() const
Definition Chat.h:79
bool HasLowerSecurity(Player *target, uint64 guid, bool strong=false)
Definition Chat.cpp:78
Player * getSelectedPlayer()
Definition Chat.cpp:829
char * extractKeyFromLink(char *text, char const *linkType, char **something1=NULL)
Definition Chat.cpp:873
void PSendSysMessage(const char *format,...) ATTR_PRINTF(2
Definition Chat.cpp:221
void SetSentErrorMessage(bool val)
Definition Chat.h:114
virtual void SendSysMessage(const char *str)
Definition Chat.cpp:153
CommandScript(const char *name)
int32 GetInt32Value(uint16 index) const
Definition Object.cpp:319
void SetUInt32Value(uint16 index, uint32 value)
Definition Object.cpp:1038
void SetUInt64Value(uint16 index, uint64 value)
Definition Object.cpp:1079
bool HasTitle(uint32 bitIndex)
Definition Player.cpp:20424
void SetTitle(CharTitlesEntry const *title, bool lost=false)
Definition Player.cpp:20434
std::string const & GetName() const
Definition Object.h:664
std::vector< ChatCommand > GetCommands() const OVERRIDE
Definition cs_titles.cpp:24
static bool HandleTitlesRemoveCommand(ChatHandler *handler, char const *args)
static bool HandleTitlesAddCommand(ChatHandler *handler, char const *args)
Definition cs_titles.cpp:89
static bool HandleTitlesCurrentCommand(ChatHandler *handler, char const *args)
Definition cs_titles.cpp:44
static bool HandleTitlesSetMaskCommand(ChatHandler *handler, char const *args)
void AddSC_titles_commandscript()
@ RBAC_PERM_COMMAND_TITLES_CURRENT
Definition RBAC.h:651
@ RBAC_PERM_COMMAND_TITLES_ADD
Definition RBAC.h:650
@ RBAC_PERM_COMMAND_TITLES_SET_MASK
Definition RBAC.h:654
@ RBAC_PERM_COMMAND_TITLES_REMOVE
Definition RBAC.h:652
@ RBAC_PERM_COMMAND_TITLES
Definition RBAC.h:649
@ RBAC_PERM_COMMAND_TITLES_SET
Definition RBAC.h:653
uint32 bit_index
char * name