AdminTeleport.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * Copyright (C) 2004-2014 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.admincommandhandlers;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.SQLException;
  23. import java.util.NoSuchElementException;
  24. import java.util.StringTokenizer;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.ai.CtrlIntention;
  29. import com.l2jserver.gameserver.datatables.NpcData;
  30. import com.l2jserver.gameserver.datatables.SpawnTable;
  31. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  32. import com.l2jserver.gameserver.instancemanager.MapRegionManager;
  33. import com.l2jserver.gameserver.instancemanager.RaidBossSpawnManager;
  34. import com.l2jserver.gameserver.model.L2Object;
  35. import com.l2jserver.gameserver.model.L2Spawn;
  36. import com.l2jserver.gameserver.model.L2World;
  37. import com.l2jserver.gameserver.model.Location;
  38. import com.l2jserver.gameserver.model.actor.L2Npc;
  39. import com.l2jserver.gameserver.model.actor.instance.L2GrandBossInstance;
  40. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  41. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  42. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  43. import com.l2jserver.gameserver.network.SystemMessageId;
  44. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  45. import com.l2jserver.util.StringUtil;
  46. /**
  47. * This class handles following admin commands: - show_moves - show_teleport - teleport_to_character - move_to - teleport_character
  48. * @version $Revision: 1.3.2.6.2.4 $ $Date: 2005/04/11 10:06:06 $ con.close() change and small typo fix by Zoey76 24/02/2011
  49. */
  50. public class AdminTeleport implements IAdminCommandHandler
  51. {
  52. private static final Logger _log = Logger.getLogger(AdminTeleport.class.getName());
  53. private static final String[] ADMIN_COMMANDS =
  54. {
  55. "admin_show_moves",
  56. "admin_show_moves_other",
  57. "admin_show_teleport",
  58. "admin_teleport_to_character",
  59. "admin_teleportto",
  60. "admin_move_to",
  61. "admin_teleport_character",
  62. "admin_recall",
  63. "admin_walk",
  64. "teleportto",
  65. "recall",
  66. "admin_recall_npc",
  67. "admin_gonorth",
  68. "admin_gosouth",
  69. "admin_goeast",
  70. "admin_gowest",
  71. "admin_goup",
  72. "admin_godown",
  73. "admin_tele",
  74. "admin_teleto",
  75. "admin_instant_move",
  76. "admin_sendhome"
  77. };
  78. @Override
  79. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  80. {
  81. if (command.equals("admin_teleto"))
  82. {
  83. activeChar.setTeleMode(1);
  84. }
  85. if (command.equals("admin_instant_move"))
  86. {
  87. activeChar.sendMessage("Instant move ready. Click where you want to go.");
  88. activeChar.setTeleMode(1);
  89. }
  90. if (command.equals("admin_teleto r"))
  91. {
  92. activeChar.setTeleMode(2);
  93. }
  94. if (command.equals("admin_teleto end"))
  95. {
  96. activeChar.setTeleMode(0);
  97. }
  98. if (command.equals("admin_show_moves"))
  99. {
  100. AdminHtml.showAdminHtml(activeChar, "teleports.htm");
  101. }
  102. if (command.equals("admin_show_moves_other"))
  103. {
  104. AdminHtml.showAdminHtml(activeChar, "tele/other.html");
  105. }
  106. else if (command.equals("admin_show_teleport"))
  107. {
  108. showTeleportCharWindow(activeChar);
  109. }
  110. else if (command.equals("admin_recall_npc"))
  111. {
  112. recallNPC(activeChar);
  113. }
  114. else if (command.equals("admin_teleport_to_character"))
  115. {
  116. teleportToCharacter(activeChar, activeChar.getTarget());
  117. }
  118. else if (command.startsWith("admin_walk"))
  119. {
  120. try
  121. {
  122. String val = command.substring(11);
  123. StringTokenizer st = new StringTokenizer(val);
  124. int x = Integer.parseInt(st.nextToken());
  125. int y = Integer.parseInt(st.nextToken());
  126. int z = Integer.parseInt(st.nextToken());
  127. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new Location(x, y, z, 0));
  128. }
  129. catch (Exception e)
  130. {
  131. if (Config.DEBUG)
  132. {
  133. _log.info("admin_walk: " + e);
  134. }
  135. }
  136. }
  137. else if (command.startsWith("admin_move_to"))
  138. {
  139. try
  140. {
  141. String val = command.substring(14);
  142. teleportTo(activeChar, val);
  143. }
  144. catch (StringIndexOutOfBoundsException e)
  145. {
  146. // Case of empty or missing coordinates
  147. AdminHtml.showAdminHtml(activeChar, "teleports.htm");
  148. }
  149. catch (NumberFormatException nfe)
  150. {
  151. activeChar.sendMessage("Usage: //move_to <x> <y> <z>");
  152. AdminHtml.showAdminHtml(activeChar, "teleports.htm");
  153. }
  154. }
  155. else if (command.startsWith("admin_teleport_character"))
  156. {
  157. try
  158. {
  159. String val = command.substring(25);
  160. teleportCharacter(activeChar, val);
  161. }
  162. catch (StringIndexOutOfBoundsException e)
  163. {
  164. // Case of empty coordinates
  165. activeChar.sendMessage("Wrong or no Coordinates given.");
  166. showTeleportCharWindow(activeChar); // back to character teleport
  167. }
  168. }
  169. else if (command.startsWith("admin_teleportto "))
  170. {
  171. try
  172. {
  173. String targetName = command.substring(17);
  174. L2PcInstance player = L2World.getInstance().getPlayer(targetName);
  175. teleportToCharacter(activeChar, player);
  176. }
  177. catch (StringIndexOutOfBoundsException e)
  178. {
  179. }
  180. }
  181. else if (command.startsWith("admin_recall "))
  182. {
  183. try
  184. {
  185. String[] param = command.split(" ");
  186. if (param.length != 2)
  187. {
  188. activeChar.sendMessage("Usage: //recall <playername>");
  189. return false;
  190. }
  191. String targetName = param[1];
  192. L2PcInstance player = L2World.getInstance().getPlayer(targetName);
  193. if (player != null)
  194. {
  195. teleportCharacter(player, activeChar.getLocation(), activeChar);
  196. }
  197. else
  198. {
  199. changeCharacterPosition(activeChar, targetName);
  200. }
  201. }
  202. catch (StringIndexOutOfBoundsException e)
  203. {
  204. }
  205. }
  206. else if (command.equals("admin_tele"))
  207. {
  208. showTeleportWindow(activeChar);
  209. }
  210. else if (command.startsWith("admin_go"))
  211. {
  212. int intVal = 150;
  213. int x = activeChar.getX(), y = activeChar.getY(), z = activeChar.getZ();
  214. try
  215. {
  216. String val = command.substring(8);
  217. StringTokenizer st = new StringTokenizer(val);
  218. String dir = st.nextToken();
  219. if (st.hasMoreTokens())
  220. {
  221. intVal = Integer.parseInt(st.nextToken());
  222. }
  223. if (dir.equals("east"))
  224. {
  225. x += intVal;
  226. }
  227. else if (dir.equals("west"))
  228. {
  229. x -= intVal;
  230. }
  231. else if (dir.equals("north"))
  232. {
  233. y -= intVal;
  234. }
  235. else if (dir.equals("south"))
  236. {
  237. y += intVal;
  238. }
  239. else if (dir.equals("up"))
  240. {
  241. z += intVal;
  242. }
  243. else if (dir.equals("down"))
  244. {
  245. z -= intVal;
  246. }
  247. activeChar.teleToLocation(new Location(x, y, z));
  248. showTeleportWindow(activeChar);
  249. }
  250. catch (Exception e)
  251. {
  252. activeChar.sendMessage("Usage: //go<north|south|east|west|up|down> [offset] (default 150)");
  253. }
  254. }
  255. else if (command.startsWith("admin_sendhome"))
  256. {
  257. StringTokenizer st = new StringTokenizer(command, " ");
  258. st.nextToken(); // Skip command.
  259. if (st.countTokens() > 1)
  260. {
  261. activeChar.sendMessage("Usage: //sendhome <playername>");
  262. }
  263. else if (st.countTokens() == 1)
  264. {
  265. final String name = st.nextToken();
  266. final L2PcInstance player = L2World.getInstance().getPlayer(name);
  267. if (player == null)
  268. {
  269. activeChar.sendPacket(SystemMessageId.TARGET_IS_NOT_FOUND_IN_THE_GAME);
  270. return false;
  271. }
  272. teleportHome(player);
  273. }
  274. else
  275. {
  276. final L2Object target = activeChar.getTarget();
  277. if (target instanceof L2PcInstance)
  278. {
  279. teleportHome(target.getActingPlayer());
  280. }
  281. else
  282. {
  283. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  284. }
  285. }
  286. }
  287. return true;
  288. }
  289. @Override
  290. public String[] getAdminCommandList()
  291. {
  292. return ADMIN_COMMANDS;
  293. }
  294. /**
  295. * This method sends a player to it's home town.
  296. * @param player the player to teleport.
  297. */
  298. private void teleportHome(L2PcInstance player)
  299. {
  300. String regionName;
  301. switch (player.getRace())
  302. {
  303. case ELF:
  304. regionName = "elf_town";
  305. break;
  306. case DARK_ELF:
  307. regionName = "darkelf_town";
  308. break;
  309. case ORC:
  310. regionName = "orc_town";
  311. break;
  312. case DWARF:
  313. regionName = "dwarf_town";
  314. break;
  315. case KAMAEL:
  316. regionName = "kamael_town";
  317. break;
  318. case HUMAN:
  319. default:
  320. regionName = "talking_island_town";
  321. }
  322. player.teleToLocation(MapRegionManager.getInstance().getMapRegionByName(regionName).getSpawnLoc(), true);
  323. player.setInstanceId(0);
  324. player.setIsIn7sDungeon(false);
  325. }
  326. private void teleportTo(L2PcInstance activeChar, String Coords)
  327. {
  328. try
  329. {
  330. StringTokenizer st = new StringTokenizer(Coords);
  331. String x1 = st.nextToken();
  332. int x = Integer.parseInt(x1);
  333. String y1 = st.nextToken();
  334. int y = Integer.parseInt(y1);
  335. String z1 = st.nextToken();
  336. int z = Integer.parseInt(z1);
  337. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  338. activeChar.teleToLocation(x, y, z);
  339. activeChar.sendMessage("You have been teleported to " + Coords);
  340. }
  341. catch (NoSuchElementException nsee)
  342. {
  343. activeChar.sendMessage("Wrong or no Coordinates given.");
  344. }
  345. }
  346. private void showTeleportWindow(L2PcInstance activeChar)
  347. {
  348. AdminHtml.showAdminHtml(activeChar, "move.htm");
  349. }
  350. private void showTeleportCharWindow(L2PcInstance activeChar)
  351. {
  352. L2Object target = activeChar.getTarget();
  353. L2PcInstance player = null;
  354. if (target instanceof L2PcInstance)
  355. {
  356. player = (L2PcInstance) target;
  357. }
  358. else
  359. {
  360. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  361. return;
  362. }
  363. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  364. final String replyMSG = StringUtil.concat("<html><title>Teleport Character</title>" + "<body>" + "The character you will teleport is ", player.getName(), "." + "<br>" + "Co-ordinate x" + "<edit var=\"char_cord_x\" width=110>" + "Co-ordinate y" + "<edit var=\"char_cord_y\" width=110>" + "Co-ordinate z" + "<edit var=\"char_cord_z\" width=110>" + "<button value=\"Teleport\" action=\"bypass -h admin_teleport_character $char_cord_x $char_cord_y $char_cord_z\" width=60 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">" + "<button value=\"Teleport near you\" action=\"bypass -h admin_teleport_character ", String.valueOf(activeChar.getX()), " ", String.valueOf(activeChar.getY()), " ", String.valueOf(activeChar.getZ()), "\" width=115 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">" + "<center><button value=\"Back\" action=\"bypass -h admin_current_player\" width=40 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></center>" + "</body></html>");
  365. adminReply.setHtml(replyMSG);
  366. activeChar.sendPacket(adminReply);
  367. }
  368. private void teleportCharacter(L2PcInstance activeChar, String Cords)
  369. {
  370. L2Object target = activeChar.getTarget();
  371. L2PcInstance player = null;
  372. if (target instanceof L2PcInstance)
  373. {
  374. player = (L2PcInstance) target;
  375. }
  376. else
  377. {
  378. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  379. return;
  380. }
  381. if (player.getObjectId() == activeChar.getObjectId())
  382. {
  383. player.sendPacket(SystemMessageId.CANNOT_USE_ON_YOURSELF);
  384. }
  385. else
  386. {
  387. try
  388. {
  389. StringTokenizer st = new StringTokenizer(Cords);
  390. String x1 = st.nextToken();
  391. int x = Integer.parseInt(x1);
  392. String y1 = st.nextToken();
  393. int y = Integer.parseInt(y1);
  394. String z1 = st.nextToken();
  395. int z = Integer.parseInt(z1);
  396. teleportCharacter(player, new Location(x, y, z), null);
  397. }
  398. catch (NoSuchElementException nsee)
  399. {
  400. }
  401. }
  402. }
  403. /**
  404. * @param player
  405. * @param loc
  406. * @param activeChar
  407. */
  408. private void teleportCharacter(L2PcInstance player, Location loc, L2PcInstance activeChar)
  409. {
  410. if (player != null)
  411. {
  412. // Check for jail
  413. if (player.isJailed())
  414. {
  415. activeChar.sendMessage("Sorry, player " + player.getName() + " is in Jail.");
  416. }
  417. else
  418. {
  419. // Set player to same instance as GM teleporting.
  420. if ((activeChar != null) && (activeChar.getInstanceId() >= 0))
  421. {
  422. player.setInstanceId(activeChar.getInstanceId());
  423. activeChar.sendMessage("You have recalled " + player.getName());
  424. }
  425. else
  426. {
  427. player.setInstanceId(0);
  428. }
  429. player.sendMessage("Admin is teleporting you.");
  430. player.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  431. player.teleToLocation(loc, true);
  432. }
  433. }
  434. }
  435. private void teleportToCharacter(L2PcInstance activeChar, L2Object target)
  436. {
  437. if (target == null)
  438. {
  439. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  440. return;
  441. }
  442. L2PcInstance player = null;
  443. if (target instanceof L2PcInstance)
  444. {
  445. player = (L2PcInstance) target;
  446. }
  447. else
  448. {
  449. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  450. return;
  451. }
  452. if (player.getObjectId() == activeChar.getObjectId())
  453. {
  454. player.sendPacket(SystemMessageId.CANNOT_USE_ON_YOURSELF);
  455. }
  456. else
  457. {
  458. // move to targets instance
  459. activeChar.setInstanceId(target.getInstanceId());
  460. int x = player.getX();
  461. int y = player.getY();
  462. int z = player.getZ();
  463. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  464. activeChar.teleToLocation(new Location(x, y, z), true);
  465. activeChar.sendMessage("You have teleported to character " + player.getName() + ".");
  466. }
  467. }
  468. private void changeCharacterPosition(L2PcInstance activeChar, String name)
  469. {
  470. final int x = activeChar.getX();
  471. final int y = activeChar.getY();
  472. final int z = activeChar.getZ();
  473. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  474. {
  475. PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=? WHERE char_name=?");
  476. statement.setInt(1, x);
  477. statement.setInt(2, y);
  478. statement.setInt(3, z);
  479. statement.setString(4, name);
  480. statement.execute();
  481. int count = statement.getUpdateCount();
  482. statement.close();
  483. if (count == 0)
  484. {
  485. activeChar.sendMessage("Character not found or position unaltered.");
  486. }
  487. else
  488. {
  489. activeChar.sendMessage("Player's [" + name + "] position is now set to (" + x + "," + y + "," + z + ").");
  490. }
  491. }
  492. catch (SQLException se)
  493. {
  494. activeChar.sendMessage("SQLException while changing offline character's position");
  495. }
  496. }
  497. private void recallNPC(L2PcInstance activeChar)
  498. {
  499. L2Object obj = activeChar.getTarget();
  500. if ((obj instanceof L2Npc) && !((L2Npc) obj).isMinion() && !(obj instanceof L2RaidBossInstance) && !(obj instanceof L2GrandBossInstance))
  501. {
  502. L2Npc target = (L2Npc) obj;
  503. int monsterTemplate = target.getTemplate().getId();
  504. L2NpcTemplate template1 = NpcData.getInstance().getTemplate(monsterTemplate);
  505. if (template1 == null)
  506. {
  507. activeChar.sendMessage("Incorrect monster template.");
  508. _log.warning("ERROR: NPC " + target.getObjectId() + " has a 'null' template.");
  509. return;
  510. }
  511. L2Spawn spawn = target.getSpawn();
  512. if (spawn == null)
  513. {
  514. activeChar.sendMessage("Incorrect monster spawn.");
  515. _log.warning("ERROR: NPC " + target.getObjectId() + " has a 'null' spawn.");
  516. return;
  517. }
  518. int respawnTime = spawn.getRespawnDelay() / 1000;
  519. target.deleteMe();
  520. spawn.stopRespawn();
  521. SpawnTable.getInstance().deleteSpawn(spawn, true);
  522. try
  523. {
  524. // L2MonsterInstance mob = new L2MonsterInstance(monsterTemplate, template1);
  525. spawn = new L2Spawn(template1);
  526. if (Config.SAVE_GMSPAWN_ON_CUSTOM)
  527. {
  528. spawn.setCustom(true);
  529. }
  530. spawn.setX(activeChar.getX());
  531. spawn.setY(activeChar.getY());
  532. spawn.setZ(activeChar.getZ());
  533. spawn.setAmount(1);
  534. spawn.setHeading(activeChar.getHeading());
  535. spawn.setRespawnDelay(respawnTime);
  536. if (activeChar.getInstanceId() >= 0)
  537. {
  538. spawn.setInstanceId(activeChar.getInstanceId());
  539. }
  540. else
  541. {
  542. spawn.setInstanceId(0);
  543. }
  544. SpawnTable.getInstance().addNewSpawn(spawn, true);
  545. spawn.init();
  546. activeChar.sendMessage("Created " + template1.getName() + " on " + target.getObjectId() + ".");
  547. if (Config.DEBUG)
  548. {
  549. _log.fine("Spawn at X=" + spawn.getX() + " Y=" + spawn.getY() + " Z=" + spawn.getZ());
  550. _log.warning("GM: " + activeChar.getName() + "(" + activeChar.getObjectId() + ") moved NPC " + target.getObjectId());
  551. }
  552. }
  553. catch (Exception e)
  554. {
  555. activeChar.sendMessage("Target is not in game.");
  556. }
  557. }
  558. else if (obj instanceof L2RaidBossInstance)
  559. {
  560. L2RaidBossInstance target = (L2RaidBossInstance) obj;
  561. L2Spawn spawn = target.getSpawn();
  562. double curHP = target.getCurrentHp();
  563. double curMP = target.getCurrentMp();
  564. if (spawn == null)
  565. {
  566. activeChar.sendMessage("Incorrect raid spawn.");
  567. _log.warning("ERROR: NPC Id" + target.getId() + " has a 'null' spawn.");
  568. return;
  569. }
  570. RaidBossSpawnManager.getInstance().deleteSpawn(spawn, true);
  571. try
  572. {
  573. L2NpcTemplate template = NpcData.getInstance().getTemplate(target.getId());
  574. L2Spawn spawnDat = new L2Spawn(template);
  575. if (Config.SAVE_GMSPAWN_ON_CUSTOM)
  576. {
  577. spawn.setCustom(true);
  578. }
  579. spawnDat.setX(activeChar.getX());
  580. spawnDat.setY(activeChar.getY());
  581. spawnDat.setZ(activeChar.getZ());
  582. spawnDat.setAmount(1);
  583. spawnDat.setHeading(activeChar.getHeading());
  584. spawnDat.setRespawnMinDelay(43200);
  585. spawnDat.setRespawnMaxDelay(129600);
  586. RaidBossSpawnManager.getInstance().addNewSpawn(spawnDat, 0, curHP, curMP, true);
  587. }
  588. catch (Exception e)
  589. {
  590. activeChar.sendPacket(SystemMessageId.TARGET_CANT_FOUND);
  591. }
  592. }
  593. else
  594. {
  595. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  596. }
  597. }
  598. }