TrinityCore
Loading...
Searching...
No Matches
cs_tele.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: tele_commandscript
20%Complete: 100
21Comment: All tele related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "Chat.h"
27#include "DatabaseEnv.h"
28#include "DBCStores.h"
29#include "Group.h"
30#include "Language.h"
31#include "MapManager.h"
32#include "ObjectMgr.h"
33#include "Player.h"
34#include "RBAC.h"
35#include "WorldSession.h"
36
37using namespace Trinity::ChatCommands;
38
40{
41public:
42 tele_commandscript() : CommandScript("tele_commandscript") { }
43
45 {
46 static ChatCommandTable teleNameNpcCommandTable =
47 {
51 };
52 static ChatCommandTable teleNameCommandTable =
53 {
54 { "npc", teleNameNpcCommandTable },
56 };
57 static ChatCommandTable teleCommandTable =
58 {
61 { "name", teleNameCommandTable },
64 };
65 static ChatCommandTable commandTable =
66 {
67 { "tele", teleCommandTable },
68 };
69 return commandTable;
70 }
71
72 static bool HandleTeleAddCommand(ChatHandler* handler, std::string const& name)
73 {
74 Player* player = handler->GetSession()->GetPlayer();
75 if (!player)
76 return false;
77
78 if (sObjectMgr->GetGameTeleExactName(name))
79 {
81 handler->SetSentErrorMessage(true);
82 return false;
83 }
84
85 GameTele tele;
86 tele.position_x = player->GetPositionX();
87 tele.position_y = player->GetPositionY();
88 tele.position_z = player->GetPositionZ();
89 tele.orientation = player->GetOrientation();
90 tele.mapId = player->GetMapId();
91 tele.name = name;
92
93 if (sObjectMgr->AddGameTele(tele))
94 {
96 }
97 else
98 {
100 handler->SetSentErrorMessage(true);
101 return false;
102 }
103
104 return true;
105 }
106
107 static bool HandleTeleDelCommand(ChatHandler* handler, GameTele const* tele)
108 {
109 if (!tele)
110 {
112 handler->SetSentErrorMessage(true);
113 return false;
114 }
115 std::string name = tele->name;
116 sObjectMgr->DeleteGameTele(name);
118 return true;
119 }
120
121 static bool DoNameTeleport(ChatHandler* handler, PlayerIdentifier player, uint32 mapId, Position const& pos, std::string const& locationName)
122 {
123 if (!MapManager::IsValidMapCoord(mapId, pos) || sObjectMgr->IsTransportMap(mapId))
124 {
126 handler->SetSentErrorMessage(true);
127 return false;
128 }
129
130 if (Player* target = player.GetConnectedPlayer())
131 {
132 // check online security
133 if (handler->HasLowerSecurity(target, ObjectGuid::Empty))
134 return false;
135
136 std::string chrNameLink = handler->playerLink(target->GetName());
137
138 if (target->IsBeingTeleported() == true)
139 {
140 handler->PSendSysMessage(LANG_IS_TELEPORTED, chrNameLink.c_str());
141 handler->SetSentErrorMessage(true);
142 return false;
143 }
144
145 handler->PSendSysMessage(LANG_TELEPORTING_TO, chrNameLink.c_str(), "", locationName.c_str());
146 if (handler->needReportToTarget(target))
147 ChatHandler(target->GetSession()).PSendSysMessage(LANG_TELEPORTED_TO_BY, handler->GetNameLink().c_str());
148
149 // stop flight if need
150 if (target->IsInFlight())
151 target->FinishTaxiFlight();
152 else
153 target->SaveRecallPosition(); // save only in non-flight case
154
155 target->TeleportTo({ mapId, pos });
156 }
157 else
158 {
159 // check offline security
160 if (handler->HasLowerSecurity(nullptr, player.GetGUID()))
161 return false;
162
163 std::string nameLink = handler->playerLink(player.GetName());
164
165 handler->PSendSysMessage(LANG_TELEPORTING_TO, nameLink.c_str(), handler->GetTrinityString(LANG_OFFLINE), locationName.c_str());
166
167 Player::SavePositionInDB({ mapId, pos }, sMapMgr->GetZoneId(PHASEMASK_NORMAL, { mapId, pos }), player.GetGUID(), nullptr);
168 }
169
170 return true;
171 }
172
173 // teleport player to given game_tele.entry
174 static bool HandleTeleNameCommand(ChatHandler* handler, Optional<PlayerIdentifier> player, Variant<GameTele const*, EXACT_SEQUENCE("$home")> where)
175 {
176 if (!player)
177 player = PlayerIdentifier::FromTargetOrSelf(handler);
178 if (!player)
179 return false;
180
181 if (where.index() == 1) // References target's homebind
182 {
183 if (Player* target = player->GetConnectedPlayer())
184 target->TeleportTo(target->m_homebindMapId, target->m_homebindX, target->m_homebindY, target->m_homebindZ, target->GetOrientation());
185 else
186 {
188 stmt->setUInt32(0, player->GetGUID().GetCounter());
189 PreparedQueryResult resultDB = CharacterDatabase.Query(stmt);
190
191 if (resultDB)
192 {
193 Field* fieldsDB = resultDB->Fetch();
194 WorldLocation loc(fieldsDB[0].GetUInt16(), fieldsDB[2].GetFloat(), fieldsDB[3].GetFloat(), fieldsDB[4].GetFloat(), 0.0f);
195 uint32 zoneId = fieldsDB[1].GetUInt16();
196
197 Player::SavePositionInDB(loc, zoneId, player->GetGUID(), nullptr);
198 }
199 }
200
201 return true;
202 }
203
204 // id, or string, or [name] Shift-click form |color|Htele:id|h[name]|h|r
205 GameTele const* tele = where.get<GameTele const*>();
206 return DoNameTeleport(handler, *player, tele->mapId, { tele->position_x, tele->position_y, tele->position_z, tele->orientation }, tele->name);
207 }
208
209 //Teleport group to given game_tele.entry
210 static bool HandleTeleGroupCommand(ChatHandler* handler, GameTele const* tele)
211 {
212 if (!tele)
213 {
215 handler->SetSentErrorMessage(true);
216 return false;
217 }
218
219 Player* target = handler->getSelectedPlayer();
220 if (!target)
221 {
223 handler->SetSentErrorMessage(true);
224 return false;
225 }
226
227 // check online security
228 if (handler->HasLowerSecurity(target, ObjectGuid::Empty))
229 return false;
230
231 MapEntry const* map = sMapStore.LookupEntry(tele->mapId);
232 if (!map || map->IsBattlegroundOrArena())
233 {
235 handler->SetSentErrorMessage(true);
236 return false;
237 }
238
239 std::string nameLink = handler->GetNameLink(target);
240
241 Group* grp = target->GetGroup();
242 if (!grp)
243 {
244 handler->PSendSysMessage(LANG_NOT_IN_GROUP, nameLink.c_str());
245 handler->SetSentErrorMessage(true);
246 return false;
247 }
248
249 for (GroupReference* itr = grp->GetFirstMember(); itr != nullptr; itr = itr->next())
250 {
251 Player* player = itr->GetSource();
252
253 if (!player || !player->GetSession())
254 continue;
255
256 // check online security
257 if (handler->HasLowerSecurity(player, ObjectGuid::Empty))
258 return false;
259
260 std::string plNameLink = handler->GetNameLink(player);
261
262 if (player->IsBeingTeleported())
263 {
264 handler->PSendSysMessage(LANG_IS_TELEPORTED, plNameLink.c_str());
265 continue;
266 }
267
268 handler->PSendSysMessage(LANG_TELEPORTING_TO, plNameLink.c_str(), "", tele->name.c_str());
269 if (handler->needReportToTarget(player))
270 ChatHandler(player->GetSession()).PSendSysMessage(LANG_TELEPORTED_TO_BY, nameLink.c_str());
271
272 // stop flight if need
273 if (player->IsInFlight())
274 player->FinishTaxiFlight();
275 else
276 player->SaveRecallPosition(); // save only in non-flight case
277
278 player->TeleportTo(tele->mapId, tele->position_x, tele->position_y, tele->position_z, tele->orientation);
279 }
280
281 return true;
282 }
283
284 static bool HandleTeleCommand(ChatHandler* handler, GameTele const* tele)
285 {
286 if (!tele)
287 {
289 handler->SetSentErrorMessage(true);
290 return false;
291 }
292
293 Player* player = handler->GetSession()->GetPlayer();
295 {
297 handler->SetSentErrorMessage(true);
298 return false;
299 }
300
301 MapEntry const* map = sMapStore.LookupEntry(tele->mapId);
302 if (!map || (map->IsBattlegroundOrArena() && (player->GetMapId() != tele->mapId || !player->IsGameMaster())))
303 {
305 handler->SetSentErrorMessage(true);
306 return false;
307 }
308
309 // stop flight if need
310 if (player->IsInFlight())
311 player->FinishTaxiFlight();
312 else
313 player->SaveRecallPosition(); // save only in non-flight case
314
315 player->TeleportTo(tele->mapId, tele->position_x, tele->position_y, tele->position_z, tele->orientation);
316 return true;
317 }
318
320 {
321 CreatureData const* spawnpoint = nullptr;
322 for (auto const& pair : sObjectMgr->GetAllCreatureData())
323 {
324 if (pair.second.id != *creatureId)
325 continue;
326
327 if (!spawnpoint)
328 spawnpoint = &pair.second;
329 else
330 {
332 break;
333 }
334 }
335
336 if (!spawnpoint)
337 {
339 handler->SetSentErrorMessage(true);
340 return false;
341 }
342
343 CreatureTemplate const* creatureTemplate = ASSERT_NOTNULL(sObjectMgr->GetCreatureTemplate(*creatureId));
344
345 return DoNameTeleport(handler, player, spawnpoint->mapId, spawnpoint->spawnPoint, creatureTemplate->Name);
346 }
347
349 {
350 CreatureData const* spawnpoint = sObjectMgr->GetCreatureData(spawnId);
351 if (!spawnpoint)
352 {
354 handler->SetSentErrorMessage(true);
355 return false;
356 }
357
358 CreatureTemplate const* creatureTemplate = ASSERT_NOTNULL(sObjectMgr->GetCreatureTemplate(spawnpoint->id));
359
360 return DoNameTeleport(handler, player, spawnpoint->mapId, spawnpoint->spawnPoint, creatureTemplate->Name);
361 }
362
364 {
365 std::string normalizedName(name);
366 WorldDatabase.EscapeString(normalizedName);
367
368 QueryResult result = WorldDatabase.PQuery("SELECT c.position_x, c.position_y, c.position_z, c.orientation, c.map, ct.name FROM creature c INNER JOIN creature_template ct ON c.id = ct.entry WHERE ct.name LIKE '{}'", normalizedName);
369 if (!result)
370 {
372 handler->SetSentErrorMessage(true);
373 return false;
374 }
375
376 if (result->GetRowCount() > 1)
378
379 Field* fields = result->Fetch();
380 return DoNameTeleport(handler, player, fields[4].GetUInt16(), { fields[0].GetFloat(), fields[1].GetFloat(), fields[2].GetFloat(), fields[3].GetFloat() }, fields[5].GetString());
381 }
382};
383
@ CHAR_SEL_CHAR_HOMEBIND
#define EXACT_SEQUENCE(str)
DBCStorage< MapEntry > sMapStore(MapEntryfmt)
std::shared_ptr< ResultSet > QueryResult
std::shared_ptr< PreparedResultSet > PreparedQueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
uint32_t uint32
Definition Define.h:133
#define ASSERT_NOTNULL(pointer)
Definition Errors.h:84
@ LANG_COMMAND_TP_ALREADYEXIST
Definition Language.h:525
@ LANG_OFFLINE
Definition Language.h:69
@ LANG_COMMAND_TP_ADDEDERR
Definition Language.h:527
@ LANG_NOT_IN_GROUP
Definition Language.h:151
@ LANG_IS_TELEPORTED
Definition Language.h:136
@ LANG_COMMAND_GOCREATNOTFOUND
Definition Language.h:319
@ LANG_YOU_IN_COMBAT
Definition Language.h:55
@ LANG_COMMAND_TP_DELETED
Definition Language.h:528
@ LANG_NO_CHAR_SELECTED
Definition Language.h:150
@ LANG_INVALID_TARGET_COORD
Definition Language.h:314
@ LANG_COMMAND_GOCREATMULTIPLE
Definition Language.h:320
@ LANG_COMMAND_TP_ADDED
Definition Language.h:526
@ LANG_TELEPORTED_TO_BY
Definition Language.h:145
@ LANG_COMMAND_TELE_NOTFOUND
Definition Language.h:205
@ LANG_TELEPORTING_TO
Definition Language.h:144
@ LANG_CANNOT_TELE_TO_BG
Definition Language.h:726
#define sMapMgr
Definition MapManager.h:211
@ PHASEMASK_NORMAL
#define sObjectMgr
Definition ObjectMgr.h:1721
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
Role Based Access Control related classes definition.
std::string playerLink(std::string const &name) const
Definition Chat.h:127
Player * getSelectedPlayer()
Definition Chat.cpp:302
WorldSession * GetSession()
Definition Chat.h:46
virtual std::string GetNameLink() const
Definition Chat.cpp:46
bool HasLowerSecurity(Player *target, ObjectGuid guid, bool strong=false)
Definition Chat.cpp:51
void SetSentErrorMessage(bool val)
Definition Chat.h:134
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:101
virtual bool needReportToTarget(Player *chr) const
Definition Chat.cpp:686
virtual char const * GetTrinityString(uint32 entry) const
Definition Chat.cpp:36
Class used to access individual fields of database query result.
Definition Field.h:92
std::string GetString() const
Definition Field.cpp:125
uint16 GetUInt16() const
Definition Field.cpp:45
float GetFloat() const
Definition Field.cpp:93
Definition Group.h:165
GroupReference * GetFirstMember()
Definition Group.h:247
static bool IsValidMapCoord(uint32 mapid, float x, float y)
Definition MapManager.h:90
static ObjectGuid const Empty
Definition ObjectGuid.h:140
uint32 LowType
Definition ObjectGuid.h:142
static void SavePositionInDB(WorldLocation const &loc, uint16 zoneId, ObjectGuid guid, CharacterDatabaseTransaction trans)
Definition Player.cpp:20047
WorldSession * GetSession() const
Definition Player.h:1719
void SaveRecallPosition()
Definition Player.h:2078
void FinishTaxiFlight()
Definition Player.cpp:21202
Group * GetGroup()
Definition Player.h:2171
bool IsGameMaster() const
Definition Player.h:998
bool TeleportTo(uint32 mapid, float x, float y, float z, float orientation, uint32 options=0)
Definition Player.cpp:1524
bool IsBeingTeleported() const
Definition Player.h:1820
void setUInt32(uint8 index, uint32 value)
bool IsInFlight() const
Definition Unit.h:1119
bool IsInCombat() const
Definition Unit.h:1144
uint32 GetMapId() const
Definition Position.h:193
Player * GetPlayer() const
bool HasPermission(uint32 permissionId)
ChatCommandTable GetCommands() const override
Definition cs_tele.cpp:44
static bool HandleTeleNameNpcNameCommand(ChatHandler *handler, PlayerIdentifier player, Tail name)
Definition cs_tele.cpp:363
static bool HandleTeleNameNpcIdCommand(ChatHandler *handler, PlayerIdentifier player, Variant< Hyperlink< creature_entry >, uint32 > creatureId)
Definition cs_tele.cpp:319
static bool HandleTeleNameCommand(ChatHandler *handler, Optional< PlayerIdentifier > player, Variant< GameTele const *, EXACT_SEQUENCE("$home")> where)
Definition cs_tele.cpp:174
static bool HandleTeleNameNpcSpawnIdCommand(ChatHandler *handler, PlayerIdentifier player, Variant< Hyperlink< creature >, ObjectGuid::LowType > spawnId)
Definition cs_tele.cpp:348
static bool HandleTeleAddCommand(ChatHandler *handler, std::string const &name)
Definition cs_tele.cpp:72
static bool HandleTeleCommand(ChatHandler *handler, GameTele const *tele)
Definition cs_tele.cpp:284
static bool HandleTeleDelCommand(ChatHandler *handler, GameTele const *tele)
Definition cs_tele.cpp:107
static bool DoNameTeleport(ChatHandler *handler, PlayerIdentifier player, uint32 mapId, Position const &pos, std::string const &locationName)
Definition cs_tele.cpp:121
static bool HandleTeleGroupCommand(ChatHandler *handler, GameTele const *tele)
Definition cs_tele.cpp:210
void AddSC_tele_commandscript()
Definition cs_tele.cpp:384
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:50
@ RBAC_PERM_COMMAND_TELE_GROUP
Definition RBAC.h:611
@ RBAC_PERM_COMMAND_TELE_ADD
Definition RBAC.h:608
@ RBAC_PERM_COMMAND_TELE_NAME
Definition RBAC.h:610
@ RBAC_PERM_COMMAND_TELE
Definition RBAC.h:607
@ RBAC_PERM_COMMAND_TELE_DEL
Definition RBAC.h:609
std::string Name
float position_y
Definition ObjectMgr.h:162
float orientation
Definition ObjectMgr.h:164
float position_x
Definition ObjectMgr.h:161
uint32 mapId
Definition ObjectMgr.h:165
std::string name
Definition ObjectMgr.h:166
float position_z
Definition ObjectMgr.h:163
bool IsBattlegroundOrArena() const
float GetPositionZ() const
Definition Position.h:81
float GetOrientation() const
Definition Position.h:82
float GetPositionX() const
Definition Position.h:79
float GetPositionY() const
Definition Position.h:80
uint32 id
Definition SpawnData.h:96
Position spawnPoint
Definition SpawnData.h:97
uint32 mapId
Definition SpawnData.h:86
std::string const & GetName() const
static Optional< PlayerIdentifier > FromTargetOrSelf(ChatHandler *handler)