AdminChangeAccessLevel.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 com.l2jserver.Config;
  20. import com.l2jserver.L2DatabaseFactory;
  21. import com.l2jserver.gameserver.datatables.AdminTable;
  22. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  23. import com.l2jserver.gameserver.model.L2World;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.network.SystemMessageId;
  26. /**
  27. * This class handles following admin commands:
  28. * - changelvl = change a character's access level
  29. * Can be used for character ban (as opposed to regular //ban that affects accounts)
  30. * or to grant mod/GM privileges ingame
  31. * @version $Revision: 1.1.2.2.2.3 $ $Date: 2005/04/11 10:06:00 $
  32. * con.close() change by Zoey76 24/02/2011
  33. */
  34. public class AdminChangeAccessLevel implements IAdminCommandHandler
  35. {
  36. private static final String[] ADMIN_COMMANDS =
  37. {
  38. "admin_changelvl"
  39. };
  40. @Override
  41. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  42. {
  43. handleChangeLevel(command, activeChar);
  44. return true;
  45. }
  46. @Override
  47. public String[] getAdminCommandList()
  48. {
  49. return ADMIN_COMMANDS;
  50. }
  51. /**
  52. * If no character name is specified, tries to change GM's target access
  53. * level. Else if a character name is provided, will try to reach it either
  54. * from L2World or from a database connection.
  55. *
  56. * @param command
  57. * @param activeChar
  58. */
  59. private void handleChangeLevel(String command, L2PcInstance activeChar)
  60. {
  61. String[] parts = command.split(" ");
  62. if (parts.length == 2)
  63. {
  64. try
  65. {
  66. int lvl = Integer.parseInt(parts[1]);
  67. if (activeChar.getTarget() instanceof L2PcInstance)
  68. onLineChange(activeChar, (L2PcInstance) activeChar.getTarget(), lvl);
  69. else
  70. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  71. }
  72. catch (Exception e)
  73. {
  74. activeChar.sendMessage("Usage: //changelvl <target_new_level> | <player_name> <new_level>");
  75. }
  76. }
  77. else if (parts.length == 3)
  78. {
  79. String name = parts[1];
  80. int lvl = Integer.parseInt(parts[2]);
  81. L2PcInstance player = L2World.getInstance().getPlayer(name);
  82. if (player != null)
  83. onLineChange(activeChar, player, lvl);
  84. else
  85. {
  86. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  87. {
  88. PreparedStatement statement = con.prepareStatement("UPDATE characters SET accesslevel=? WHERE char_name=?");
  89. statement.setInt(1, lvl);
  90. statement.setString(2, name);
  91. statement.execute();
  92. int count = statement.getUpdateCount();
  93. statement.close();
  94. if (count == 0)
  95. activeChar.sendMessage("Character not found or access level unaltered.");
  96. else
  97. activeChar.sendMessage("Character's access level is now set to " + lvl);
  98. }
  99. catch (SQLException se)
  100. {
  101. activeChar.sendMessage("SQLException while changing character's access level");
  102. if (Config.DEBUG)
  103. se.printStackTrace();
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * @param activeChar
  110. * @param player
  111. * @param lvl
  112. */
  113. private void onLineChange(L2PcInstance activeChar, L2PcInstance player, int lvl)
  114. {
  115. if (lvl >= 0)
  116. {
  117. if (AdminTable.getInstance().hasAccessLevel(lvl))
  118. {
  119. player.setAccessLevel(lvl);
  120. player.sendMessage("Your access level has been changed to " + lvl);
  121. activeChar.sendMessage("Character's access level is now set to " + lvl + ". Effects won't be noticeable until next session.");
  122. }
  123. else
  124. {
  125. activeChar.sendMessage("You are trying to set unexisting access level: " + lvl + " please try again with a valid one!");
  126. }
  127. }
  128. else
  129. {
  130. player.setAccessLevel(lvl);
  131. player.sendMessage("Your character has been banned. Bye.");
  132. player.logout();
  133. }
  134. }
  135. }