AdminEnchant.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  19. import net.sf.l2j.gameserver.model.Inventory;
  20. import net.sf.l2j.gameserver.model.L2ItemInstance;
  21. import net.sf.l2j.gameserver.model.L2Object;
  22. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  23. import net.sf.l2j.gameserver.network.SystemMessageId;
  24. import net.sf.l2j.gameserver.network.serverpackets.CharInfo;
  25. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  26. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  27. import net.sf.l2j.gameserver.network.serverpackets.UserInfo;
  28. /**
  29. * This class handles following admin commands:
  30. * - enchant_armor
  31. *
  32. * @version $Revision: 1.3.2.1.2.10 $ $Date: 2005/08/24 21:06:06 $
  33. */
  34. public class AdminEnchant implements IAdminCommandHandler
  35. {
  36. private static Logger _log = Logger.getLogger(AdminEnchant.class.getName());
  37. private static final String[] ADMIN_COMMANDS =
  38. {
  39. "admin_seteh",//6
  40. "admin_setec",//10
  41. "admin_seteg",//9
  42. "admin_setel",//11
  43. "admin_seteb",//12
  44. "admin_setew",//7
  45. "admin_setes",//8
  46. "admin_setle",//1
  47. "admin_setre",//2
  48. "admin_setlf",//4
  49. "admin_setrf",//5
  50. "admin_seten",//3
  51. "admin_setun",//0
  52. "admin_setba",//13
  53. "admin_enchant"
  54. };
  55. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  56. {
  57. if (command.equals("admin_enchant"))
  58. {
  59. showMainPage(activeChar);
  60. }
  61. else
  62. {
  63. int armorType = -1;
  64. if (command.startsWith("admin_seteh"))
  65. armorType = Inventory.PAPERDOLL_HEAD;
  66. else if (command.startsWith("admin_setec"))
  67. armorType = Inventory.PAPERDOLL_CHEST;
  68. else if (command.startsWith("admin_seteg"))
  69. armorType = Inventory.PAPERDOLL_GLOVES;
  70. else if (command.startsWith("admin_seteb"))
  71. armorType = Inventory.PAPERDOLL_FEET;
  72. else if (command.startsWith("admin_setel"))
  73. armorType = Inventory.PAPERDOLL_LEGS;
  74. else if (command.startsWith("admin_setew"))
  75. armorType = Inventory.PAPERDOLL_RHAND;
  76. else if (command.startsWith("admin_setes"))
  77. armorType = Inventory.PAPERDOLL_LHAND;
  78. else if (command.startsWith("admin_setle"))
  79. armorType = Inventory.PAPERDOLL_LEAR;
  80. else if (command.startsWith("admin_setre"))
  81. armorType = Inventory.PAPERDOLL_REAR;
  82. else if (command.startsWith("admin_setlf"))
  83. armorType = Inventory.PAPERDOLL_LFINGER;
  84. else if (command.startsWith("admin_setrf"))
  85. armorType = Inventory.PAPERDOLL_RFINGER;
  86. else if (command.startsWith("admin_seten"))
  87. armorType = Inventory.PAPERDOLL_NECK;
  88. else if (command.startsWith("admin_setun"))
  89. armorType = Inventory.PAPERDOLL_UNDER;
  90. else if (command.startsWith("admin_setba"))
  91. armorType = Inventory.PAPERDOLL_BACK;
  92. if (armorType != -1)
  93. {
  94. try
  95. {
  96. int ench = Integer.parseInt(command.substring(12));
  97. // check value
  98. if (ench < 0 || ench > 65535)
  99. activeChar.sendMessage("You must set the enchant level to be between 0-65535.");
  100. else
  101. setEnchant(activeChar, ench, armorType);
  102. }
  103. catch (StringIndexOutOfBoundsException e)
  104. {
  105. if (Config.DEVELOPER)
  106. _log.warning("Set enchant error: " + e);
  107. activeChar.sendMessage("Please specify a new enchant value.");
  108. }
  109. catch (NumberFormatException e)
  110. {
  111. if (Config.DEVELOPER)
  112. _log.warning("Set enchant error: " + e);
  113. activeChar.sendMessage("Please specify a valid new enchant value.");
  114. }
  115. }
  116. // show the enchant menu after an action
  117. showMainPage(activeChar);
  118. }
  119. return true;
  120. }
  121. private void setEnchant(L2PcInstance activeChar, int ench, int armorType)
  122. {
  123. // get the target
  124. L2Object target = activeChar.getTarget();
  125. if (target == null)
  126. target = activeChar;
  127. L2PcInstance player = null;
  128. if (target instanceof L2PcInstance)
  129. {
  130. player = (L2PcInstance) target;
  131. }
  132. else
  133. {
  134. activeChar.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));
  135. return;
  136. }
  137. // now we need to find the equipped weapon of the targeted character...
  138. int curEnchant = 0; // display purposes only
  139. L2ItemInstance itemInstance = null;
  140. // only attempt to enchant if there is a weapon equipped
  141. L2ItemInstance parmorInstance = player.getInventory().getPaperdollItem(armorType);
  142. if (parmorInstance != null && parmorInstance.getLocationSlot() == armorType)
  143. {
  144. itemInstance = parmorInstance;
  145. }
  146. else
  147. {
  148. // for bows/crossbows and double handed weapons
  149. parmorInstance = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LRHAND);
  150. if (parmorInstance != null && parmorInstance.getLocationSlot() == Inventory.PAPERDOLL_LRHAND)
  151. itemInstance = parmorInstance;
  152. }
  153. if (itemInstance != null)
  154. {
  155. curEnchant = itemInstance.getEnchantLevel();
  156. // set enchant value
  157. player.getInventory().unEquipItemInSlotAndRecord(armorType);
  158. itemInstance.setEnchantLevel(ench);
  159. player.getInventory().equipItemAndRecord(itemInstance);
  160. // send packets
  161. InventoryUpdate iu = new InventoryUpdate();
  162. iu.addModifiedItem(itemInstance);
  163. player.sendPacket(iu);
  164. player.broadcastPacket(new CharInfo(player));
  165. player.sendPacket(new UserInfo(player));
  166. // informations
  167. activeChar.sendMessage("Changed enchantment of " + player.getName() + "'s " + itemInstance.getItem().getName() + " from " + curEnchant + " to " + ench + ".");
  168. player.sendMessage("Admin has changed the enchantment of your " + itemInstance.getItem().getName() + " from " + curEnchant + " to " + ench + ".");
  169. }
  170. }
  171. private void showMainPage(L2PcInstance activeChar)
  172. {
  173. AdminHelpPage.showHelpPage(activeChar, "enchant.htm");
  174. }
  175. public String[] getAdminCommandList()
  176. {
  177. return ADMIN_COMMANDS;
  178. }
  179. }