AdminChangeAccessLevel.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2004-2014 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.L2AccessLevel;
  28. import com.l2jserver.gameserver.model.L2World;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. /**
  32. * Change access level command handler.
  33. */
  34. public final 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. String[] parts = command.split(" ");
  44. if (parts.length == 2)
  45. {
  46. try
  47. {
  48. int lvl = Integer.parseInt(parts[1]);
  49. if (activeChar.getTarget() instanceof L2PcInstance)
  50. {
  51. onlineChange(activeChar, (L2PcInstance) activeChar.getTarget(), lvl);
  52. }
  53. else
  54. {
  55. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  56. }
  57. }
  58. catch (Exception e)
  59. {
  60. activeChar.sendMessage("Usage: //changelvl <target_new_level> | <player_name> <new_level>");
  61. }
  62. }
  63. else if (parts.length == 3)
  64. {
  65. String name = parts[1];
  66. int lvl = Integer.parseInt(parts[2]);
  67. L2PcInstance player = L2World.getInstance().getPlayer(name);
  68. if (player != null)
  69. {
  70. onlineChange(activeChar, player, lvl);
  71. }
  72. else
  73. {
  74. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  75. {
  76. PreparedStatement statement = con.prepareStatement("UPDATE characters SET accesslevel=? WHERE char_name=?");
  77. statement.setInt(1, lvl);
  78. statement.setString(2, name);
  79. statement.execute();
  80. int count = statement.getUpdateCount();
  81. statement.close();
  82. if (count == 0)
  83. {
  84. activeChar.sendMessage("Character not found or access level unaltered.");
  85. }
  86. else
  87. {
  88. activeChar.sendMessage("Character's access level is now set to " + lvl);
  89. }
  90. }
  91. catch (SQLException se)
  92. {
  93. activeChar.sendMessage("SQLException while changing character's access level");
  94. if (Config.DEBUG)
  95. {
  96. se.printStackTrace();
  97. }
  98. }
  99. }
  100. }
  101. return true;
  102. }
  103. @Override
  104. public String[] getAdminCommandList()
  105. {
  106. return ADMIN_COMMANDS;
  107. }
  108. /**
  109. * @param activeChar the active GM
  110. * @param player the online target
  111. * @param lvl the access level
  112. */
  113. private static void onlineChange(L2PcInstance activeChar, L2PcInstance player, int lvl)
  114. {
  115. if (lvl >= 0)
  116. {
  117. if (AdminTable.getInstance().hasAccessLevel(lvl))
  118. {
  119. final L2AccessLevel acccessLevel = AdminTable.getInstance().getAccessLevel(lvl);
  120. player.setAccessLevel(lvl);
  121. player.sendMessage("Your access level has been changed to " + acccessLevel.getName() + " (" + acccessLevel.getLevel() + ").");
  122. activeChar.sendMessage(player.getName() + "'s access level has been changed to " + acccessLevel.getName() + " (" + acccessLevel.getLevel() + ").");
  123. }
  124. else
  125. {
  126. activeChar.sendMessage("You are trying to set unexisting access level: " + lvl + " please try again with a valid one!");
  127. }
  128. }
  129. else
  130. {
  131. player.setAccessLevel(lvl);
  132. player.sendMessage("Your character has been banned. Bye.");
  133. player.logout();
  134. }
  135. }
  136. }