AdminBan.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.SQLException;
  19. import java.util.NoSuchElementException;
  20. import java.util.StringTokenizer;
  21. import net.sf.l2j.Config;
  22. import net.sf.l2j.L2DatabaseFactory;
  23. import net.sf.l2j.gameserver.LoginServerThread;
  24. import net.sf.l2j.gameserver.communitybbs.Manager.RegionBBSManager;
  25. import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  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. * - ban account_name = changes account access level to -100 and logs him off. If no account is specified, target's account is used.
  34. * - unban account_name = changes account access level to 0.
  35. * - jail charname [penalty_time] = jails character. Time specified in minutes. For ever if no time is specified.
  36. * - unjail charname = Unjails player, teleport him to Floran.
  37. *
  38. * @version $Revision: 1.1.6.3 $ $Date: 2005/04/11 10:06:06 $
  39. */
  40. public class AdminBan implements IAdminCommandHandler {
  41. private static final String[] ADMIN_COMMANDS = {"admin_ban", "admin_unban","admin_jail","admin_unjail"};
  42. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  43. {
  44. StringTokenizer st = new StringTokenizer(command);
  45. st.nextToken();
  46. String account_name = "";
  47. String player = "";
  48. L2PcInstance plyr = null;
  49. if (command.startsWith("admin_ban"))
  50. {
  51. try
  52. {
  53. player = st.nextToken();
  54. plyr = L2World.getInstance().getPlayer(player);
  55. }
  56. catch(Exception e)
  57. {
  58. L2Object target = activeChar.getTarget();
  59. if (target instanceof L2PcInstance)
  60. plyr = (L2PcInstance)target;
  61. else
  62. activeChar.sendMessage("Usage: //ban [account_name] (if none, target char's account gets banned)");
  63. }
  64. if (plyr != null && plyr.equals(activeChar))
  65. plyr.sendPacket(new SystemMessage(SystemMessageId.CANNOT_USE_ON_YOURSELF));
  66. else if (plyr==null)
  67. {
  68. account_name=player;
  69. LoginServerThread.getInstance().sendAccessLevel(account_name, 0);
  70. activeChar.sendMessage("Ban request sent for account "+account_name+". If you need a playername based commmand, see //ban_menu");
  71. }
  72. else
  73. {
  74. plyr.setAccountAccesslevel(-100);
  75. account_name = plyr.getAccountName();
  76. RegionBBSManager.getInstance().changeCommunityBoard();
  77. plyr.logout();
  78. activeChar.sendMessage("Account "+account_name+" banned.");
  79. }
  80. }
  81. else if (command.startsWith("admin_unban"))
  82. {
  83. try
  84. {
  85. account_name = st.nextToken();
  86. LoginServerThread.getInstance().sendAccessLevel(account_name, 0);
  87. activeChar.sendMessage("Unban request sent for account "+account_name+". If you need a playername based commmand, see //unban_menu");
  88. }
  89. catch(Exception e)
  90. {
  91. activeChar.sendMessage("Usage: //unban <account_name>");
  92. if (Config.DEBUG)
  93. e.printStackTrace();
  94. }
  95. }
  96. else if (command.startsWith("admin_jail"))
  97. {
  98. try
  99. {
  100. player = st.nextToken();
  101. int delay = 0;
  102. try
  103. {
  104. delay = Integer.parseInt(st.nextToken());
  105. }
  106. catch (NumberFormatException nfe)
  107. {
  108. activeChar.sendMessage("Usage: //jail <charname> [penalty_minutes]");
  109. }
  110. catch (NoSuchElementException nsee) {}
  111. L2PcInstance playerObj = L2World.getInstance().getPlayer(player);
  112. if (playerObj != null)
  113. {
  114. playerObj.setInJail(true, delay);
  115. activeChar.sendMessage("Character "+player+" jailed for "+(delay>0 ? delay+" minutes." : "ever!"));
  116. }
  117. else
  118. jailOfflinePlayer(activeChar, player, delay);
  119. }
  120. catch (NoSuchElementException nsee)
  121. {
  122. activeChar.sendMessage("Usage: //jail <charname> [penalty_minutes]");
  123. }
  124. catch(Exception e)
  125. {
  126. if (Config.DEBUG)
  127. e.printStackTrace();
  128. }
  129. }
  130. else if (command.startsWith("admin_unjail"))
  131. {
  132. try
  133. {
  134. player = st.nextToken();
  135. L2PcInstance playerObj = L2World.getInstance().getPlayer(player);
  136. if (playerObj != null)
  137. {
  138. playerObj.setInJail(false, 0);
  139. activeChar.sendMessage("Character "+player+" removed from jail");
  140. }
  141. else
  142. unjailOfflinePlayer(activeChar, player);
  143. }
  144. catch (NoSuchElementException nsee)
  145. {
  146. activeChar.sendMessage("Specify a character name.");
  147. }
  148. catch(Exception e)
  149. {
  150. if (Config.DEBUG)
  151. e.printStackTrace();
  152. }
  153. }
  154. return true;
  155. }
  156. private void jailOfflinePlayer(L2PcInstance activeChar, String name, int delay)
  157. {
  158. Connection con = null;
  159. try
  160. {
  161. con = L2DatabaseFactory.getInstance().getConnection();
  162. PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, in_jail=?, jail_timer=? WHERE char_name=?");
  163. statement.setInt(1, -114356);
  164. statement.setInt(2, -249645);
  165. statement.setInt(3, -2984);
  166. statement.setInt(4, 1);
  167. statement.setLong(5, delay * 60000L);
  168. statement.setString(6, name);
  169. statement.execute();
  170. int count = statement.getUpdateCount();
  171. statement.close();
  172. if (count == 0)
  173. activeChar.sendMessage("Character not found!");
  174. else
  175. activeChar.sendMessage("Character "+name+" jailed for "+(delay>0 ? delay+" minutes." : "ever!"));
  176. } catch (SQLException se)
  177. {
  178. activeChar.sendMessage("SQLException while jailing player");
  179. if (Config.DEBUG) se.printStackTrace();
  180. } finally
  181. {
  182. try { con.close(); } catch (Exception e)
  183. {
  184. if (Config.DEBUG)
  185. e.printStackTrace();
  186. }
  187. }
  188. }
  189. private void unjailOfflinePlayer(L2PcInstance activeChar, String name)
  190. {
  191. Connection con = null;
  192. try
  193. {
  194. con = L2DatabaseFactory.getInstance().getConnection();
  195. PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, in_jail=?, jail_timer=? WHERE char_name=?");
  196. statement.setInt(1, 17836);
  197. statement.setInt(2, 170178);
  198. statement.setInt(3, -3507);
  199. statement.setInt(4, 0);
  200. statement.setLong(5, 0);
  201. statement.setString(6, name);
  202. statement.execute();
  203. int count = statement.getUpdateCount();
  204. statement.close();
  205. if (count == 0)
  206. activeChar.sendMessage("Character not found!");
  207. else
  208. activeChar.sendMessage("Character "+name+" removed from jail");
  209. } catch (SQLException se)
  210. {
  211. activeChar.sendMessage("SQLException while jailing player");
  212. if (Config.DEBUG) se.printStackTrace();
  213. } finally
  214. {
  215. try
  216. {
  217. con.close();
  218. }
  219. catch (Exception e)
  220. {
  221. if (Config.DEBUG)
  222. e.printStackTrace();
  223. }
  224. }
  225. }
  226. public String[] getAdminCommandList() {
  227. return ADMIN_COMMANDS;
  228. }
  229. }