ConvertItem.java 4.9 KB

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