AdminBan.java 7.5 KB

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