AdminEnchant.java 8.1 KB

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