AdminMenu.java 9.6 KB

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