ConvertItem.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2004-2013 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.effecthandlers;
  20. import com.l2jserver.gameserver.model.Elementals;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import com.l2jserver.gameserver.model.effects.EffectTemplate;
  23. import com.l2jserver.gameserver.model.effects.L2Effect;
  24. import com.l2jserver.gameserver.model.effects.L2EffectType;
  25. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  26. import com.l2jserver.gameserver.model.items.L2Weapon;
  27. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  28. import com.l2jserver.gameserver.model.stats.Env;
  29. import com.l2jserver.gameserver.network.SystemMessageId;
  30. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  31. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  32. /**
  33. * Convert Item effect.
  34. * @author Zoey76
  35. */
  36. public class ConvertItem extends L2Effect
  37. {
  38. public ConvertItem(Env env, EffectTemplate template)
  39. {
  40. super(env, template);
  41. }
  42. @Override
  43. public boolean onStart()
  44. {
  45. if ((getEffector() == null) || (getEffected() == null) || getEffected().isAlikeDead() || !getEffected().isPlayer())
  46. {
  47. return false;
  48. }
  49. final L2PcInstance player = getEffected().getActingPlayer();
  50. if (player.isEnchanting())
  51. {
  52. return false;
  53. }
  54. final L2Weapon weaponItem = player.getActiveWeaponItem();
  55. if (weaponItem == null)
  56. {
  57. return false;
  58. }
  59. L2ItemInstance wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_RHAND);
  60. if (wpn == null)
  61. {
  62. wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  63. }
  64. if ((wpn == null) || wpn.isAugmented() || (weaponItem.getChangeWeaponId() == 0))
  65. {
  66. return false;
  67. }
  68. final int newItemId = weaponItem.getChangeWeaponId();
  69. if (newItemId == -1)
  70. {
  71. return false;
  72. }
  73. final int enchantLevel = wpn.getEnchantLevel();
  74. final Elementals elementals = wpn.getElementals() == null ? null : wpn.getElementals()[0];
  75. final L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(wpn.getItem().getBodyPart());
  76. final InventoryUpdate iu = new InventoryUpdate();
  77. for (L2ItemInstance item : unequiped)
  78. {
  79. iu.addModifiedItem(item);
  80. }
  81. player.sendPacket(iu);
  82. if (unequiped.length <= 0)
  83. {
  84. return false;
  85. }
  86. byte count = 0;
  87. for (L2ItemInstance item : unequiped)
  88. {
  89. if (!(item.getItem() instanceof L2Weapon))
  90. {
  91. count++;
  92. continue;
  93. }
  94. final SystemMessage sm;
  95. if (item.getEnchantLevel() > 0)
  96. {
  97. sm = SystemMessage.getSystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  98. sm.addNumber(item.getEnchantLevel());
  99. sm.addItemName(item);
  100. }
  101. else
  102. {
  103. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISARMED);
  104. sm.addItemName(item);
  105. }
  106. player.sendPacket(sm);
  107. }
  108. if (count == unequiped.length)
  109. {
  110. return false;
  111. }
  112. final L2ItemInstance destroyItem = player.getInventory().destroyItem("ChangeWeapon", wpn, player, null);
  113. if (destroyItem == null)
  114. {
  115. return false;
  116. }
  117. final L2ItemInstance newItem = player.getInventory().addItem("ChangeWeapon", newItemId, 1, player, destroyItem);
  118. if (newItem == null)
  119. {
  120. return false;
  121. }
  122. if ((elementals != null) && (elementals.getElement() != -1) && (elementals.getValue() != -1))
  123. {
  124. newItem.setElementAttr(elementals.getElement(), elementals.getValue());
  125. }
  126. newItem.setEnchantLevel(enchantLevel);
  127. player.getInventory().equipItem(newItem);
  128. final SystemMessage msg;
  129. if (newItem.getEnchantLevel() > 0)
  130. {
  131. msg = SystemMessage.getSystemMessage(SystemMessageId.S1_S2_EQUIPPED);
  132. msg.addNumber(newItem.getEnchantLevel());
  133. msg.addItemName(newItem);
  134. }
  135. else
  136. {
  137. msg = SystemMessage.getSystemMessage(SystemMessageId.S1_EQUIPPED);
  138. msg.addItemName(newItem);
  139. }
  140. player.sendPacket(msg);
  141. final InventoryUpdate u = new InventoryUpdate();
  142. u.addRemovedItem(destroyItem);
  143. u.addItem(newItem);
  144. player.sendPacket(u);
  145. player.broadcastUserInfo();
  146. return true;
  147. }
  148. @Override
  149. public boolean onActionTime()
  150. {
  151. return false;
  152. }
  153. @Override
  154. public L2EffectType getEffectType()
  155. {
  156. return L2EffectType.BUFF;
  157. }
  158. }