AdminExpSp.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.util.StringTokenizer;
  17. import java.util.logging.Logger;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  20. import net.sf.l2j.gameserver.model.L2Object;
  21. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  22. import net.sf.l2j.gameserver.network.SystemMessageId;
  23. import net.sf.l2j.gameserver.serverpackets.NpcHtmlMessage;
  24. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  25. /**
  26. * This class handles following admin commands:
  27. * <li> add_exp_sp_to_character <i>shows menu for add or remove</i>
  28. * <li> add_exp_sp exp sp <i>Adds exp & sp to target, displays menu if a parameter is missing</i>
  29. * <li> remove_exp_sp exp sp <i>Removes exp & sp from target, displays menu if a parameter is missing</i>
  30. * @version $Revision: 1.2.4.6 $ $Date: 2005/04/11 10:06:06 $
  31. */
  32. public class AdminExpSp implements IAdminCommandHandler {
  33. private static Logger _log = Logger.getLogger(AdminExpSp.class.getName());
  34. private static final String[] ADMIN_COMMANDS = {"admin_add_exp_sp_to_character","admin_add_exp_sp","admin_remove_exp_sp"};
  35. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  36. {
  37. if (command.startsWith("admin_add_exp_sp"))
  38. {
  39. try
  40. {
  41. String val = command.substring(16);
  42. if (!adminAddExpSp(activeChar, val))
  43. activeChar.sendMessage("Usage: //add_exp_sp exp sp");
  44. }
  45. catch (StringIndexOutOfBoundsException e)
  46. { //Case of missing parameter
  47. activeChar.sendMessage("Usage: //add_exp_sp exp sp");
  48. }
  49. }
  50. else if(command.startsWith("admin_remove_exp_sp"))
  51. {
  52. try
  53. {
  54. String val = command.substring(19);
  55. if (!adminRemoveExpSP(activeChar, val))
  56. activeChar.sendMessage("Usage: //remove_exp_sp exp sp");
  57. }
  58. catch (StringIndexOutOfBoundsException e)
  59. { //Case of missing parameter
  60. activeChar.sendMessage("Usage: //remove_exp_sp exp sp");
  61. }
  62. }
  63. addExpSp(activeChar);
  64. return true;
  65. }
  66. public String[] getAdminCommandList() {
  67. return ADMIN_COMMANDS;
  68. }
  69. private void addExpSp(L2PcInstance activeChar)
  70. {
  71. L2Object target = activeChar.getTarget();
  72. L2PcInstance player = null;
  73. if (target instanceof L2PcInstance)
  74. player = (L2PcInstance)target;
  75. else
  76. {
  77. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  78. return;
  79. }
  80. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  81. adminReply.setFile("data/html/admin/expsp.htm");
  82. adminReply.replace("%name%", player.getName());
  83. adminReply.replace("%level%", String.valueOf(player.getLevel()));
  84. adminReply.replace("%xp%", String.valueOf(player.getExp()));
  85. adminReply.replace("%sp%", String.valueOf(player.getSp()));
  86. adminReply.replace("%class%", player.getTemplate().className);
  87. activeChar.sendPacket(adminReply);
  88. }
  89. private boolean adminAddExpSp(L2PcInstance activeChar, String ExpSp)
  90. {
  91. L2Object target = activeChar.getTarget();
  92. L2PcInstance player = null;
  93. if (target instanceof L2PcInstance)
  94. {
  95. player = (L2PcInstance)target;
  96. }
  97. else
  98. {
  99. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  100. return false;
  101. }
  102. StringTokenizer st = new StringTokenizer(ExpSp);
  103. if (st.countTokens()!=2)
  104. {
  105. return false;
  106. }
  107. else
  108. {
  109. String exp = st.nextToken();
  110. String sp = st.nextToken();
  111. long expval = 0;
  112. int spval = 0;
  113. try
  114. {
  115. expval = Long.parseLong(exp);
  116. spval = Integer.parseInt(sp);
  117. }
  118. catch(Exception e)
  119. {
  120. return false;
  121. }
  122. if(expval != 0 || spval != 0)
  123. {
  124. //Common character information
  125. player.sendMessage("Admin is adding you "+expval+" xp and "+spval+" sp.");
  126. player.addExpAndSp(expval,spval);
  127. //Admin information
  128. activeChar.sendMessage("Added "+expval+" xp and "+spval+" sp to "+player.getName()+".");
  129. if (Config.DEBUG)
  130. _log.fine("GM: "+activeChar.getName()+"("+activeChar.getObjectId()+") added "+expval+
  131. " xp and "+spval+" sp to "+player.getObjectId()+".");
  132. }
  133. }
  134. return true;
  135. }
  136. private boolean adminRemoveExpSP(L2PcInstance activeChar, String ExpSp)
  137. {
  138. L2Object target = activeChar.getTarget();
  139. L2PcInstance player = null;
  140. if (target instanceof L2PcInstance)
  141. {
  142. player = (L2PcInstance)target;
  143. }
  144. else
  145. {
  146. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  147. return false;
  148. }
  149. StringTokenizer st = new StringTokenizer(ExpSp);
  150. if (st.countTokens()!=2)
  151. return false;
  152. else
  153. {
  154. String exp = st.nextToken();
  155. String sp = st.nextToken();
  156. long expval = 0;
  157. int spval = 0;
  158. try
  159. {
  160. expval = Long.parseLong(exp);
  161. spval = Integer.parseInt(sp);
  162. }
  163. catch (Exception e)
  164. {
  165. return false;
  166. }
  167. if(expval != 0 || spval != 0)
  168. {
  169. //Common character information
  170. player.sendMessage("Admin is removing you "+expval+" xp and "+spval+" sp.");
  171. player.removeExpAndSp(expval,spval);
  172. //Admin information
  173. activeChar.sendMessage("Removed "+expval+" xp and "+spval+" sp from "+player.getName()+".");
  174. if (Config.DEBUG)
  175. _log.fine("GM: "+activeChar.getName()+"("+activeChar.getObjectId()+") removed "+expval+
  176. " xp and "+spval+" sp from "+player.getObjectId()+".");
  177. }
  178. }
  179. return true;
  180. }
  181. }