AdminTeleport.java 18 KB

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