AdminMenu.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 net.sf.l2j.gameserver.handler.admincommandhandlers;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.StringTokenizer;
  19. import java.util.logging.Logger;
  20. import net.sf.l2j.Config;
  21. import net.sf.l2j.L2DatabaseFactory;
  22. import net.sf.l2j.gameserver.LoginServerThread;
  23. import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  24. import net.sf.l2j.gameserver.model.L2Character;
  25. import net.sf.l2j.gameserver.model.L2Clan;
  26. import net.sf.l2j.gameserver.model.L2Object;
  27. import net.sf.l2j.gameserver.model.L2World;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  29. import net.sf.l2j.gameserver.network.SystemMessageId;
  30. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  31. /**
  32. * This class handles following admin commands:
  33. * - handles every admin menu command
  34. *
  35. * @version $Revision: 1.3.2.6.2.4 $ $Date: 2005/04/11 10:06:06 $
  36. */
  37. public class AdminMenu implements IAdminCommandHandler
  38. {
  39. private static final Logger _log = Logger.getLogger(AdminMenu.class.getName());
  40. private static final String[] ADMIN_COMMANDS =
  41. {
  42. "admin_char_manage",
  43. "admin_teleport_character_to_menu",
  44. "admin_recall_char_menu",
  45. "admin_recall_party_menu",
  46. "admin_recall_clan_menu",
  47. "admin_goto_char_menu",
  48. "admin_kick_menu",
  49. "admin_kill_menu",
  50. "admin_ban_menu",
  51. "admin_unban_menu"
  52. };
  53. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  54. {
  55. if (command.equals("admin_char_manage"))
  56. showMainPage(activeChar);
  57. else if (command.startsWith("admin_teleport_character_to_menu"))
  58. {
  59. String[] data = command.split(" ");
  60. if (data.length == 5)
  61. {
  62. String playerName = data[1];
  63. L2PcInstance player = L2World.getInstance().getPlayer(playerName);
  64. if (player != null)
  65. teleportCharacter(player, Integer.parseInt(data[2]), Integer.parseInt(data[3]), Integer.parseInt(data[4]), activeChar, "Admin is teleporting you.");
  66. }
  67. showMainPage(activeChar);
  68. }
  69. else if (command.startsWith("admin_recall_char_menu"))
  70. {
  71. try
  72. {
  73. String targetName = command.substring(23);
  74. L2PcInstance player = L2World.getInstance().getPlayer(targetName);
  75. teleportCharacter(player, activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar, "Admin is teleporting you.");
  76. }
  77. catch (StringIndexOutOfBoundsException e)
  78. {
  79. }
  80. }
  81. else if (command.startsWith("admin_recall_party_menu"))
  82. {
  83. int x = activeChar.getX(), y = activeChar.getY(), z = activeChar.getZ();
  84. try
  85. {
  86. String targetName = command.substring(24);
  87. L2PcInstance player = L2World.getInstance().getPlayer(targetName);
  88. if (player == null)
  89. {
  90. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  91. return true;
  92. }
  93. if (!player.isInParty())
  94. {
  95. activeChar.sendMessage("Player is not in party.");
  96. teleportCharacter(player, x, y, z, activeChar, "Admin is teleporting you.");
  97. return true;
  98. }
  99. for (L2PcInstance pm : player.getParty().getPartyMembers())
  100. teleportCharacter(pm, x, y, z, activeChar, "Your party is being teleported by an Admin.");
  101. }
  102. catch (Exception e)
  103. {
  104. }
  105. }
  106. else if (command.startsWith("admin_recall_clan_menu"))
  107. {
  108. int x = activeChar.getX(), y = activeChar.getY(), z = activeChar.getZ();
  109. try
  110. {
  111. String targetName = command.substring(23);
  112. L2PcInstance player = L2World.getInstance().getPlayer(targetName);
  113. if (player == null)
  114. {
  115. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  116. return true;
  117. }
  118. L2Clan clan = player.getClan();
  119. if (clan == null)
  120. {
  121. activeChar.sendMessage("Player is not in a clan.");
  122. teleportCharacter(player, x, y, z, activeChar, "Admin is teleporting you.");
  123. return true;
  124. }
  125. L2PcInstance[] members = clan.getOnlineMembers(0);
  126. for (int i = 0; i < members.length; i++)
  127. teleportCharacter(members[i], x, y, z, activeChar, "Your clan is being teleported by an Admin.");
  128. }
  129. catch (Exception e)
  130. {
  131. }
  132. }
  133. else if (command.startsWith("admin_goto_char_menu"))
  134. {
  135. try
  136. {
  137. String targetName = command.substring(21);
  138. L2PcInstance player = L2World.getInstance().getPlayer(targetName);
  139. teleportToCharacter(activeChar, player);
  140. }
  141. catch (StringIndexOutOfBoundsException e)
  142. {
  143. }
  144. }
  145. else if (command.equals("admin_kill_menu"))
  146. {
  147. handleKill(activeChar);
  148. }
  149. else if (command.startsWith("admin_kick_menu"))
  150. {
  151. StringTokenizer st = new StringTokenizer(command);
  152. if (st.countTokens() > 1)
  153. {
  154. st.nextToken();
  155. String player = st.nextToken();
  156. L2PcInstance plyr = L2World.getInstance().getPlayer(player);
  157. String text;
  158. if (plyr != null)
  159. {
  160. plyr.logout();
  161. text = "You kicked " + plyr.getName() + " from the game.";
  162. }
  163. else
  164. text = "Player " + player + " was not found in the game.";
  165. activeChar.sendMessage(text);
  166. }
  167. showMainPage(activeChar);
  168. }
  169. else if (command.startsWith("admin_ban_menu"))
  170. {
  171. StringTokenizer st = new StringTokenizer(command);
  172. if (st.countTokens() > 1)
  173. {
  174. st.nextToken();
  175. String player = st.nextToken();
  176. L2PcInstance plyr = L2World.getInstance().getPlayer(player);
  177. if (plyr != null)
  178. {
  179. plyr.logout();
  180. }
  181. setAccountAccessLevel(player, activeChar, -100);
  182. }
  183. showMainPage(activeChar);
  184. }
  185. else if (command.startsWith("admin_unban_menu"))
  186. {
  187. StringTokenizer st = new StringTokenizer(command);
  188. if (st.countTokens() > 1)
  189. {
  190. st.nextToken();
  191. String player = st.nextToken();
  192. setAccountAccessLevel(player, activeChar, 0);
  193. }
  194. showMainPage(activeChar);
  195. }
  196. return true;
  197. }
  198. public String[] getAdminCommandList()
  199. {
  200. return ADMIN_COMMANDS;
  201. }
  202. private void handleKill(L2PcInstance activeChar)
  203. {
  204. handleKill(activeChar, null);
  205. }
  206. private void handleKill(L2PcInstance activeChar, String player)
  207. {
  208. L2Object obj = activeChar.getTarget();
  209. L2Character target = (L2Character) obj;
  210. String filename = "main_menu.htm";
  211. if (player != null)
  212. {
  213. L2PcInstance plyr = L2World.getInstance().getPlayer(player);
  214. if (plyr != null)
  215. target = plyr;
  216. activeChar.sendMessage("You killed " + plyr.getName());
  217. }
  218. if (target != null)
  219. {
  220. if (target instanceof L2PcInstance)
  221. {
  222. target.reduceCurrentHp(target.getMaxHp() + target.getMaxCp() + 1, activeChar);
  223. filename = "charmanage.htm";
  224. }
  225. else if (Config.L2JMOD_CHAMPION_ENABLE && target.isChampion())
  226. target.reduceCurrentHp(target.getMaxHp() * Config.L2JMOD_CHAMPION_HP + 1, activeChar);
  227. else
  228. target.reduceCurrentHp(target.getMaxHp() + 1, activeChar);
  229. }
  230. else
  231. {
  232. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  233. }
  234. AdminHelpPage.showHelpPage(activeChar, filename);
  235. }
  236. private void teleportCharacter(L2PcInstance player, int x, int y, int z, L2PcInstance activeChar, String message)
  237. {
  238. if (player != null)
  239. {
  240. player.sendMessage(message);
  241. player.teleToLocation(x, y, z, true);
  242. }
  243. showMainPage(activeChar);
  244. }
  245. private void teleportToCharacter(L2PcInstance activeChar, L2Object target)
  246. {
  247. L2PcInstance player = null;
  248. if (target instanceof L2PcInstance)
  249. player = (L2PcInstance) target;
  250. else
  251. {
  252. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  253. return;
  254. }
  255. if (player.getObjectId() == activeChar.getObjectId())
  256. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_USE_ON_YOURSELF));
  257. else
  258. {
  259. activeChar.teleToLocation(player.getX(), player.getY(), player.getZ(), true);
  260. activeChar.sendMessage("You're teleporting yourself to character " + player.getName());
  261. }
  262. showMainPage(activeChar);
  263. }
  264. /**
  265. * @param activeChar
  266. */
  267. private void showMainPage(L2PcInstance activeChar)
  268. {
  269. AdminHelpPage.showHelpPage(activeChar, "charmanage.htm");
  270. }
  271. private void setAccountAccessLevel(String player, L2PcInstance activeChar, int banLevel)
  272. {
  273. java.sql.Connection con = null;
  274. try
  275. {
  276. con = L2DatabaseFactory.getInstance().getConnection();
  277. String stmt = "SELECT account_name FROM characters WHERE char_name = ?";
  278. PreparedStatement statement = con.prepareStatement(stmt);
  279. statement.setString(1, player);
  280. ResultSet result = statement.executeQuery();
  281. if (result.next())
  282. {
  283. String acc_name = result.getString(1);
  284. String text;
  285. if (acc_name.length() > 0)
  286. {
  287. LoginServerThread.getInstance().sendAccessLevel(acc_name, banLevel);
  288. text = "Account Access Level for " + player + " set to " + banLevel + ".";
  289. }
  290. else
  291. text = "Couldn't find player: " + player + ".";
  292. activeChar.sendMessage(text);
  293. }
  294. else
  295. activeChar.sendMessage("Specified player name didn't lead to a valid account.");
  296. statement.close();
  297. }
  298. catch (Exception e)
  299. {
  300. _log.warning("Could not set accessLevel:" + e);
  301. if (Config.DEBUG)
  302. e.printStackTrace();
  303. }
  304. finally
  305. {
  306. try
  307. {
  308. con.close();
  309. }
  310. catch (Exception e)
  311. {
  312. }
  313. }
  314. }
  315. }