AdminTeleport.java 17 KB

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