AdminChangeAccessLevel.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2004-2013 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.admincommandhandlers;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.SQLException;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.datatables.AdminTable;
  26. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.network.SystemMessageId;
  30. /**
  31. * This class handles following admin commands: - changelvl = change a character's access level Can be used for character ban (as opposed to regular //ban that affects accounts) or to grant mod/GM privileges ingame
  32. * @version $Revision: 1.1.2.2.2.3 $ $Date: 2005/04/11 10:06:00 $ 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 level. Else if a character name is provided, will try to reach it either from L2World or from a database connection.
  53. * @param command
  54. * @param activeChar
  55. */
  56. private void handleChangeLevel(String command, L2PcInstance activeChar)
  57. {
  58. String[] parts = command.split(" ");
  59. if (parts.length == 2)
  60. {
  61. try
  62. {
  63. int lvl = Integer.parseInt(parts[1]);
  64. if (activeChar.getTarget() instanceof L2PcInstance)
  65. {
  66. onLineChange(activeChar, (L2PcInstance) activeChar.getTarget(), lvl);
  67. }
  68. else
  69. {
  70. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. activeChar.sendMessage("Usage: //changelvl <target_new_level> | <player_name> <new_level>");
  76. }
  77. }
  78. else if (parts.length == 3)
  79. {
  80. String name = parts[1];
  81. int lvl = Integer.parseInt(parts[2]);
  82. L2PcInstance player = L2World.getInstance().getPlayer(name);
  83. if (player != null)
  84. {
  85. onLineChange(activeChar, player, lvl);
  86. }
  87. else
  88. {
  89. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  90. {
  91. PreparedStatement statement = con.prepareStatement("UPDATE characters SET accesslevel=? WHERE char_name=?");
  92. statement.setInt(1, lvl);
  93. statement.setString(2, name);
  94. statement.execute();
  95. int count = statement.getUpdateCount();
  96. statement.close();
  97. if (count == 0)
  98. {
  99. activeChar.sendMessage("Character not found or access level unaltered.");
  100. }
  101. else
  102. {
  103. activeChar.sendMessage("Character's access level is now set to " + lvl);
  104. }
  105. }
  106. catch (SQLException se)
  107. {
  108. activeChar.sendMessage("SQLException while changing character's access level");
  109. if (Config.DEBUG)
  110. {
  111. se.printStackTrace();
  112. }
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * @param activeChar
  119. * @param player
  120. * @param lvl
  121. */
  122. private void onLineChange(L2PcInstance activeChar, L2PcInstance player, int lvl)
  123. {
  124. if (lvl >= 0)
  125. {
  126. if (AdminTable.getInstance().hasAccessLevel(lvl))
  127. {
  128. player.setAccessLevel(lvl);
  129. player.sendMessage("Your access level has been changed to " + lvl);
  130. activeChar.sendMessage("Character's access level is now set to " + lvl + ". Effects won't be noticeable until next session.");
  131. }
  132. else
  133. {
  134. activeChar.sendMessage("You are trying to set unexisting access level: " + lvl + " please try again with a valid one!");
  135. }
  136. }
  137. else
  138. {
  139. player.setAccessLevel(lvl);
  140. player.sendMessage("Your character has been banned. Bye.");
  141. player.logout();
  142. }
  143. }
  144. }