AdminChangeAccessLevel.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2004-2015 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.commons.database.pool.impl.ConnectionFactory;
  25. import com.l2jserver.gameserver.data.xml.impl.AdminData;
  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 = ConnectionFactory.getInstance().getConnection();
  75. PreparedStatement ps = con.prepareStatement("UPDATE characters SET accesslevel=? WHERE char_name=?"))
  76. {
  77. ps.setInt(1, lvl);
  78. ps.setString(2, name);
  79. ps.execute();
  80. if (ps.getUpdateCount() == 0)
  81. {
  82. activeChar.sendMessage("Character not found or access level unaltered.");
  83. }
  84. else
  85. {
  86. activeChar.sendMessage("Character's access level is now set to " + lvl);
  87. }
  88. }
  89. catch (SQLException se)
  90. {
  91. activeChar.sendMessage("SQLException while changing character's access level");
  92. if (Config.DEBUG)
  93. {
  94. se.printStackTrace();
  95. }
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. @Override
  102. public String[] getAdminCommandList()
  103. {
  104. return ADMIN_COMMANDS;
  105. }
  106. /**
  107. * @param activeChar the active GM
  108. * @param player the online target
  109. * @param lvl the access level
  110. */
  111. private static void onlineChange(L2PcInstance activeChar, L2PcInstance player, int lvl)
  112. {
  113. if (lvl >= 0)
  114. {
  115. if (AdminData.getInstance().hasAccessLevel(lvl))
  116. {
  117. final L2AccessLevel acccessLevel = AdminData.getInstance().getAccessLevel(lvl);
  118. player.setAccessLevel(lvl);
  119. player.sendMessage("Your access level has been changed to " + acccessLevel.getName() + " (" + acccessLevel.getLevel() + ").");
  120. activeChar.sendMessage(player.getName() + "'s access level has been changed to " + acccessLevel.getName() + " (" + acccessLevel.getLevel() + ").");
  121. }
  122. else
  123. {
  124. activeChar.sendMessage("You are trying to set unexisting access level: " + lvl + " please try again with a valid one!");
  125. }
  126. }
  127. else
  128. {
  129. player.setAccessLevel(lvl);
  130. player.sendMessage("Your character has been banned. Bye.");
  131. player.logout();
  132. }
  133. }
  134. }