2010-11-13 22:29:46 +01:00
/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2010-11-13 22:29:46 +01:00
*
* 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/>.
*/
/* ScriptData
Name: tele_commandscript
%Complete: 100
Comment: All tele related commands
Category: commandscripts
EndScriptData */
2017-05-20 00:09:37 +02:00
#include "ScriptMgr.h"
2010-11-13 22:29:46 +01:00
#include "Chat.h"
2022-02-11 21:57:45 +01:00
#include "ChatCommand.h"
2017-05-18 23:52:58 +02:00
#include "DatabaseEnv.h"
2017-06-04 01:00:45 +02:00
#include "DB2Stores.h"
2010-11-29 07:50:31 +01:00
#include "Group.h"
2012-11-20 12:30:30 +01:00
#include "Language.h"
#include "MapManager.h"
#include "ObjectMgr.h"
2018-02-27 23:57:54 +01:00
#include "PhasingHandler.h"
2012-11-17 05:18:37 +01:00
#include "Player.h"
2017-05-20 00:09:37 +02:00
#include "RBAC.h"
2022-07-23 19:13:33 +02:00
#include "TerrainMgr.h"
2017-06-04 01:00:45 +02:00
#include "WorldSession.h"
2010-11-13 22:29:46 +01:00
2018-09-07 20:31:04 +02:00
using namespace Trinity :: ChatCommands ;
2010-11-13 22:29:46 +01:00
class tele_commandscript : public CommandScript
{
2010-11-19 14:04:21 +01:00
public :
tele_commandscript () : CommandScript ( "tele_commandscript" ) { }
2010-11-13 22:29:46 +01:00
2020-09-20 02:50:38 +02:00
ChatCommandTable GetCommands () const override
2010-11-19 14:04:21 +01:00
{
2021-01-28 21:32:36 +01:00
static ChatCommandTable teleNameNpcCommandTable =
{
{ "id" , HandleTeleNameNpcIdCommand , rbac :: RBAC_PERM_COMMAND_TELE_NAME , Console :: Yes },
{ "guid" , HandleTeleNameNpcSpawnIdCommand , rbac :: RBAC_PERM_COMMAND_TELE_NAME , Console :: Yes },
{ "name" , HandleTeleNameNpcNameCommand , rbac :: RBAC_PERM_COMMAND_TELE_NAME , Console :: Yes },
};
static ChatCommandTable teleNameCommandTable =
{
{ "npc" , teleNameNpcCommandTable },
{ "" , HandleTeleNameCommand , rbac :: RBAC_PERM_COMMAND_TELE_NAME , Console :: Yes },
};
2020-09-20 02:50:38 +02:00
static ChatCommandTable teleCommandTable =
2010-11-13 22:29:46 +01:00
{
2020-09-20 02:50:38 +02:00
{ "add" , HandleTeleAddCommand , rbac :: RBAC_PERM_COMMAND_TELE_ADD , Console :: No },
{ "del" , HandleTeleDelCommand , rbac :: RBAC_PERM_COMMAND_TELE_DEL , Console :: Yes },
2021-01-28 21:32:36 +01:00
{ "name" , teleNameCommandTable },
2020-09-20 02:50:38 +02:00
{ "group" , HandleTeleGroupCommand , rbac :: RBAC_PERM_COMMAND_TELE_GROUP , Console :: No },
{ "" , HandleTeleCommand , rbac :: RBAC_PERM_COMMAND_TELE , Console :: No },
2010-11-19 14:04:21 +01:00
};
2020-09-20 02:50:38 +02:00
static ChatCommandTable commandTable =
2010-11-13 22:29:46 +01:00
{
2020-09-20 02:50:38 +02:00
{ "tele" , teleCommandTable },
2010-11-19 14:04:21 +01:00
};
return commandTable ;
}
2010-11-13 22:29:46 +01:00
2018-09-07 20:31:04 +02:00
static bool HandleTeleAddCommand ( ChatHandler * handler , std :: string const & name )
2010-11-19 14:04:21 +01:00
{
2011-06-12 01:47:45 +02:00
Player * player = handler -> GetSession () -> GetPlayer ();
2010-11-19 14:04:21 +01:00
if ( ! player )
return false ;
2010-11-13 22:29:46 +01:00
2013-10-25 21:50:52 +02:00
if ( sObjectMgr -> GetGameTeleExactName ( name ))
2010-11-19 14:04:21 +01:00
{
handler -> SendSysMessage ( LANG_COMMAND_TP_ALREADYEXIST );
handler -> SetSentErrorMessage ( true );
return false ;
}
2010-11-13 22:29:46 +01:00
2010-11-19 14:04:21 +01:00
GameTele tele ;
tele . position_x = player -> GetPositionX ();
tele . position_y = player -> GetPositionY ();
tele . position_z = player -> GetPositionZ ();
tele . orientation = player -> GetOrientation ();
tele . mapId = player -> GetMapId ();
tele . name = name ;
2010-11-13 22:29:46 +01:00
2010-12-22 21:25:23 +01:00
if ( sObjectMgr -> AddGameTele ( tele ))
2010-11-19 14:04:21 +01:00
{
handler -> SendSysMessage ( LANG_COMMAND_TP_ADDED );
2010-11-13 22:29:46 +01:00
}
2010-11-19 14:04:21 +01:00
else
2010-11-13 22:29:46 +01:00
{
2010-11-19 14:04:21 +01:00
handler -> SendSysMessage ( LANG_COMMAND_TP_ADDEDERR );
handler -> SetSentErrorMessage ( true );
return false ;
}
2010-11-13 22:29:46 +01:00
2010-11-19 14:04:21 +01:00
return true ;
}
2010-11-13 22:29:46 +01:00
2018-09-07 20:31:04 +02:00
static bool HandleTeleDelCommand ( ChatHandler * handler , GameTele const * tele )
2010-11-19 14:04:21 +01:00
{
2012-06-18 05:16:00 +03:00
if ( ! tele )
{
handler -> SendSysMessage ( LANG_COMMAND_TELE_NOTFOUND );
2010-11-19 14:04:21 +01:00
handler -> SetSentErrorMessage ( true );
return false ;
2012-06-18 05:16:00 +03:00
}
2012-06-18 04:54:50 +03:00
std :: string name = tele -> name ;
2012-06-18 05:16:00 +03:00
sObjectMgr -> DeleteGameTele ( name );
2010-11-19 14:04:21 +01:00
handler -> SendSysMessage ( LANG_COMMAND_TP_DELETED );
return true ;
}
2021-01-28 21:32:36 +01:00
static bool DoNameTeleport ( ChatHandler * handler , PlayerIdentifier player , uint32 mapId , Position const & pos , std :: string const & locationName )
2010-11-19 14:04:21 +01:00
{
2021-01-28 21:32:36 +01:00
if ( ! MapManager :: IsValidMapCoord ( mapId , pos ) || sObjectMgr -> IsTransportMap ( mapId ))
2011-11-23 19:17:33 +01:00
{
2021-01-28 21:32:36 +01:00
handler -> PSendSysMessage ( LANG_INVALID_TARGET_COORD , pos . GetPositionX (), pos . GetPositionY (), mapId );
handler -> SetSentErrorMessage ( true );
return false ;
2011-11-20 13:43:58 +01:00
}
2021-01-28 21:32:36 +01:00
if ( Player * target = player . GetConnectedPlayer ())
2010-11-19 14:04:21 +01:00
{
// check online security
2014-09-14 16:54:21 +02:00
if ( handler -> HasLowerSecurity ( target , ObjectGuid :: Empty ))
2010-11-13 22:29:46 +01:00
return false ;
2020-09-09 18:33:18 +02:00
std :: string chrNameLink = handler -> playerLink ( target -> GetName ());
2010-11-19 14:04:21 +01:00
if ( target -> IsBeingTeleported () == true )
2010-11-13 22:29:46 +01:00
{
2010-11-19 14:04:21 +01:00
handler -> PSendSysMessage ( LANG_IS_TELEPORTED , chrNameLink . c_str ());
2010-11-13 22:29:46 +01:00
handler -> SetSentErrorMessage ( true );
return false ;
}
2021-01-28 21:32:36 +01:00
handler -> PSendSysMessage ( LANG_TELEPORTING_TO , chrNameLink . c_str (), "" , locationName . c_str ());
2010-11-19 14:04:21 +01:00
if ( handler -> needReportToTarget ( target ))
2012-11-17 05:18:37 +01:00
ChatHandler ( target -> GetSession ()). PSendSysMessage ( LANG_TELEPORTED_TO_BY , handler -> GetNameLink (). c_str ());
2010-11-19 14:04:21 +01:00
// stop flight if need
2013-06-11 19:54:27 -02:30
if ( target -> IsInFlight ())
2018-06-03 10:06:57 -07:00
target -> FinishTaxiFlight ();
2010-11-13 22:29:46 +01:00
else
2018-06-03 10:06:57 -07:00
target -> SaveRecallPosition (); // save only in non-flight case
2010-11-13 22:29:46 +01:00
2021-01-28 21:32:36 +01:00
target -> TeleportTo ({ mapId , pos });
2010-11-13 22:29:46 +01:00
}
2010-11-19 14:04:21 +01:00
else
2010-11-13 22:29:46 +01:00
{
2010-11-19 14:04:21 +01:00
// check offline security
2021-01-28 21:32:36 +01:00
if ( handler -> HasLowerSecurity ( nullptr , player . GetGUID ()))
2010-11-13 22:29:46 +01:00
return false ;
2021-01-28 21:32:36 +01:00
std :: string nameLink = handler -> playerLink ( player . GetName ());
2010-11-13 22:29:46 +01:00
2021-01-28 21:32:36 +01:00
handler -> PSendSysMessage ( LANG_TELEPORTING_TO , nameLink . c_str (), handler -> GetTrinityString ( LANG_OFFLINE ), locationName . c_str ());
2014-10-19 15:12:20 +02:00
2022-07-23 19:13:33 +02:00
Player :: SavePositionInDB ({ mapId , pos }, sTerrainMgr . GetZoneId ( PhasingHandler :: GetEmptyPhaseShift (), { mapId , pos }), player . GetGUID (), nullptr );
2010-11-19 14:04:21 +01:00
}
2010-11-13 22:29:46 +01:00
2010-11-19 14:04:21 +01:00
return true ;
}
2010-11-13 22:29:46 +01:00
2021-01-28 21:32:36 +01:00
// teleport player to given game_tele.entry
static bool HandleTeleNameCommand ( ChatHandler * handler , Optional < PlayerIdentifier > player , Variant < GameTele const * , EXACT_SEQUENCE ( "$home" ) > where )
{
if ( ! player )
player = PlayerIdentifier :: FromTargetOrSelf ( handler );
if ( ! player )
return false ;
if ( where . index () == 1 ) // References target's homebind
{
if ( Player * target = player -> GetConnectedPlayer ())
target -> TeleportTo ( target -> m_homebind );
else
{
CharacterDatabasePreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_CHAR_HOMEBIND );
stmt -> setUInt64 ( 0 , player -> GetGUID (). GetCounter ());
PreparedQueryResult resultDB = CharacterDatabase . Query ( stmt );
if ( resultDB )
{
Field * fieldsDB = resultDB -> Fetch ();
WorldLocation loc ( fieldsDB [ 0 ]. GetUInt16 (), fieldsDB [ 2 ]. GetFloat (), fieldsDB [ 3 ]. GetFloat (), fieldsDB [ 4 ]. GetFloat (), 0.0f );
uint32 zoneId = fieldsDB [ 1 ]. GetUInt16 ();
Player :: SavePositionInDB ( loc , zoneId , player -> GetGUID (), nullptr );
}
}
return true ;
}
// id, or string, or [name] Shift-click form |color|Htele:id|h[name]|h|r
GameTele const * tele = where . get < GameTele const *> ();
return DoNameTeleport ( handler , * player , tele -> mapId , { tele -> position_x , tele -> position_y , tele -> position_z , tele -> orientation }, tele -> name );
}
2010-11-19 14:04:21 +01:00
//Teleport group to given game_tele.entry
2018-09-07 20:31:04 +02:00
static bool HandleTeleGroupCommand ( ChatHandler * handler , GameTele const * tele )
2010-11-19 14:04:21 +01:00
{
2018-09-07 20:31:04 +02:00
if ( ! tele )
{
handler -> SendSysMessage ( LANG_COMMAND_TELE_NOTFOUND );
handler -> SetSentErrorMessage ( true );
2010-11-19 14:04:21 +01:00
return false ;
2018-09-07 20:31:04 +02:00
}
2010-11-13 22:29:46 +01:00
2011-11-23 19:17:33 +01:00
Player * target = handler -> getSelectedPlayer ();
if ( ! target )
2010-11-19 14:04:21 +01:00
{
handler -> SendSysMessage ( LANG_NO_CHAR_SELECTED );
handler -> SetSentErrorMessage ( true );
return false ;
}
2010-11-13 22:29:46 +01:00
2010-11-19 14:04:21 +01:00
// check online security
2014-09-14 16:54:21 +02:00
if ( handler -> HasLowerSecurity ( target , ObjectGuid :: Empty ))
2010-11-19 14:04:21 +01:00
return false ;
2010-11-13 22:29:46 +01:00
2011-06-11 22:35:29 +02:00
MapEntry const * map = sMapStore . LookupEntry ( tele -> mapId );
2010-11-19 14:04:21 +01:00
if ( ! map || map -> IsBattlegroundOrArena ())
{
handler -> SendSysMessage ( LANG_CANNOT_TELE_TO_BG );
handler -> SetSentErrorMessage ( true );
return false ;
2010-11-13 22:29:46 +01:00
}
2011-11-23 19:17:33 +01:00
std :: string nameLink = handler -> GetNameLink ( target );
2010-11-19 14:04:21 +01:00
2011-11-23 19:17:33 +01:00
Group * grp = target -> GetGroup ();
2010-11-19 14:04:21 +01:00
if ( ! grp )
2010-11-13 22:29:46 +01:00
{
2011-04-29 20:47:02 +02:00
handler -> PSendSysMessage ( LANG_NOT_IN_GROUP , nameLink . c_str ());
2010-11-19 14:04:21 +01:00
handler -> SetSentErrorMessage ( true );
return false ;
}
2010-11-13 22:29:46 +01:00
2020-08-14 17:06:03 +02:00
for ( GroupReference * itr = grp -> GetFirstMember (); itr != nullptr ; itr = itr -> next ())
2010-11-19 14:04:21 +01:00
{
2013-06-11 21:25:12 -02:30
Player * player = itr -> GetSource ();
2010-11-13 22:29:46 +01:00
2011-11-07 11:18:00 -06:00
if ( ! player || ! player -> GetSession ())
2010-11-19 14:04:21 +01:00
continue ;
2010-11-13 22:29:46 +01:00
2010-11-19 14:04:21 +01:00
// check online security
2014-09-14 16:54:21 +02:00
if ( handler -> HasLowerSecurity ( player , ObjectGuid :: Empty ))
2010-11-13 22:29:46 +01:00
return false ;
2011-11-07 11:18:00 -06:00
std :: string plNameLink = handler -> GetNameLink ( player );
2010-11-13 22:29:46 +01:00
2011-11-07 11:18:00 -06:00
if ( player -> IsBeingTeleported ())
2010-11-13 22:29:46 +01:00
{
2010-11-19 14:04:21 +01:00
handler -> PSendSysMessage ( LANG_IS_TELEPORTED , plNameLink . c_str ());
continue ;
2010-11-13 22:29:46 +01:00
}
2011-04-29 20:47:02 +02:00
handler -> PSendSysMessage ( LANG_TELEPORTING_TO , plNameLink . c_str (), "" , tele -> name . c_str ());
2011-11-07 11:18:00 -06:00
if ( handler -> needReportToTarget ( player ))
2012-11-17 05:18:37 +01:00
ChatHandler ( player -> GetSession ()). PSendSysMessage ( LANG_TELEPORTED_TO_BY , nameLink . c_str ());
2010-11-19 14:04:21 +01:00
2010-11-13 22:29:46 +01:00
// stop flight if need
2013-06-11 19:54:27 -02:30
if ( player -> IsInFlight ())
2018-06-03 10:06:57 -07:00
player -> FinishTaxiFlight ();
2010-11-13 22:29:46 +01:00
else
2018-06-03 10:06:57 -07:00
player -> SaveRecallPosition (); // save only in non-flight case
2010-11-13 22:29:46 +01:00
2011-11-07 11:18:00 -06:00
player -> TeleportTo ( tele -> mapId , tele -> position_x , tele -> position_y , tele -> position_z , tele -> orientation );
2010-11-13 22:29:46 +01:00
}
2010-11-19 14:04:21 +01:00
return true ;
}
2018-09-07 20:31:04 +02:00
static bool HandleTeleCommand ( ChatHandler * handler , GameTele const * tele )
2010-11-19 14:04:21 +01:00
{
if ( ! tele )
{
handler -> SendSysMessage ( LANG_COMMAND_TELE_NOTFOUND );
handler -> SetSentErrorMessage ( true );
return false ;
}
2018-09-07 20:31:04 +02:00
Player * player = handler -> GetSession () -> GetPlayer ();
2018-06-03 10:06:57 -07:00
if ( player -> IsInCombat () && ! handler -> GetSession () -> HasPermission ( rbac :: RBAC_PERM_COMMAND_TELE_NAME ))
2010-11-19 14:04:21 +01:00
{
handler -> SendSysMessage ( LANG_YOU_IN_COMBAT );
handler -> SetSentErrorMessage ( true );
return false ;
}
2011-06-11 22:35:29 +02:00
MapEntry const * map = sMapStore . LookupEntry ( tele -> mapId );
2018-06-03 10:06:57 -07:00
if ( ! map || ( map -> IsBattlegroundOrArena () && ( player -> GetMapId () != tele -> mapId || ! player -> IsGameMaster ())))
2010-11-19 14:04:21 +01:00
{
handler -> SendSysMessage ( LANG_CANNOT_TELE_TO_BG );
handler -> SetSentErrorMessage ( true );
return false ;
}
// stop flight if need
2018-06-03 10:06:57 -07:00
if ( player -> IsInFlight ())
player -> FinishTaxiFlight ();
2010-11-19 14:04:21 +01:00
else
2018-06-03 10:06:57 -07:00
player -> SaveRecallPosition (); // save only in non-flight case
2010-11-19 14:04:21 +01:00
2018-06-03 10:06:57 -07:00
player -> TeleportTo ( tele -> mapId , tele -> position_x , tele -> position_y , tele -> position_z , tele -> orientation );
2010-11-19 14:04:21 +01:00
return true ;
}
2021-01-28 21:32:36 +01:00
static bool HandleTeleNameNpcIdCommand ( ChatHandler * handler , PlayerIdentifier player , Variant < Hyperlink < creature_entry > , uint32 > creatureId )
{
CreatureData const * spawnpoint = nullptr ;
for ( auto const & pair : sObjectMgr -> GetAllCreatureData ())
{
if ( pair . second . id != * creatureId )
continue ;
if ( ! spawnpoint )
spawnpoint = & pair . second ;
else
{
handler -> SendSysMessage ( LANG_COMMAND_GOCREATMULTIPLE );
break ;
}
}
if ( ! spawnpoint )
{
handler -> SendSysMessage ( LANG_COMMAND_GOCREATNOTFOUND );
handler -> SetSentErrorMessage ( true );
return false ;
}
CreatureTemplate const * creatureTemplate = ASSERT_NOTNULL ( sObjectMgr -> GetCreatureTemplate ( * creatureId ));
return DoNameTeleport ( handler , player , spawnpoint -> mapId , spawnpoint -> spawnPoint , creatureTemplate -> Name );
}
static bool HandleTeleNameNpcSpawnIdCommand ( ChatHandler * handler , PlayerIdentifier player , Variant < Hyperlink < creature > , ObjectGuid :: LowType > spawnId )
{
CreatureData const * spawnpoint = sObjectMgr -> GetCreatureData ( spawnId );
if ( ! spawnpoint )
{
handler -> SendSysMessage ( LANG_COMMAND_GOCREATNOTFOUND );
handler -> SetSentErrorMessage ( true );
return false ;
}
CreatureTemplate const * creatureTemplate = ASSERT_NOTNULL ( sObjectMgr -> GetCreatureTemplate ( spawnpoint -> id ));
return DoNameTeleport ( handler , player , spawnpoint -> mapId , spawnpoint -> spawnPoint , creatureTemplate -> Name );
}
static bool HandleTeleNameNpcNameCommand ( ChatHandler * handler , PlayerIdentifier player , Tail name )
{
std :: string normalizedName ( name );
WorldDatabase . EscapeString ( normalizedName );
2023-01-08 21:16:53 +01:00
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 );
2021-01-28 21:32:36 +01:00
if ( ! result )
{
handler -> SendSysMessage ( LANG_COMMAND_GOCREATNOTFOUND );
handler -> SetSentErrorMessage ( true );
return false ;
}
if ( result -> GetRowCount () > 1 )
handler -> SendSysMessage ( LANG_COMMAND_GOCREATMULTIPLE );
Field * fields = result -> Fetch ();
return DoNameTeleport ( handler , player , fields [ 4 ]. GetUInt16 (), { fields [ 0 ]. GetFloat (), fields [ 1 ]. GetFloat (), fields [ 2 ]. GetFloat (), fields [ 3 ]. GetFloat () }, fields [ 5 ]. GetString ());
}
2010-11-13 22:29:46 +01:00
};
void AddSC_tele_commandscript ()
{
new tele_commandscript ();
}