StealBuffs.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 handlers.skillhandlers;
  16. import java.util.ArrayList;
  17. import java.util.logging.Level;
  18. import com.l2jserver.gameserver.handler.ISkillHandler;
  19. import com.l2jserver.gameserver.model.L2Effect;
  20. import com.l2jserver.gameserver.model.L2Object;
  21. import com.l2jserver.gameserver.model.L2Skill;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.actor.L2Npc;
  24. import com.l2jserver.gameserver.model.actor.L2Summon;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
  27. import com.l2jserver.gameserver.network.SystemMessageId;
  28. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  29. import com.l2jserver.gameserver.skills.Env;
  30. import com.l2jserver.gameserver.skills.Formulas;
  31. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  32. public class StealBuffs implements ISkillHandler
  33. {
  34. private static final L2SkillType[] SKILL_IDS =
  35. {
  36. L2SkillType.STEAL_BUFF
  37. };
  38. /**
  39. *
  40. * @see com.l2jserver.gameserver.handler.ISkillHandler#useSkill(com.l2jserver.gameserver.model.actor.L2Character, com.l2jserver.gameserver.model.L2Skill, com.l2jserver.gameserver.model.L2Object[])
  41. */
  42. @Override
  43. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  44. {
  45. // discharge shots
  46. final L2ItemInstance weaponInst = activeChar.getActiveWeaponInstance();
  47. if (weaponInst != null)
  48. {
  49. if (skill.isMagic())
  50. {
  51. if (weaponInst.getChargedSpiritshot() == L2ItemInstance.CHARGED_BLESSED_SPIRITSHOT)
  52. weaponInst.setChargedSpiritshot(L2ItemInstance.CHARGED_NONE);
  53. else if (weaponInst.getChargedSpiritshot() == L2ItemInstance.CHARGED_SPIRITSHOT)
  54. weaponInst.setChargedSpiritshot(L2ItemInstance.CHARGED_NONE);
  55. }
  56. }
  57. else if (activeChar instanceof L2Summon)
  58. {
  59. final L2Summon activeSummon = (L2Summon) activeChar;
  60. if (skill.isMagic())
  61. {
  62. if (activeSummon.getChargedSpiritShot() == L2ItemInstance.CHARGED_BLESSED_SPIRITSHOT)
  63. activeSummon.setChargedSpiritShot(L2ItemInstance.CHARGED_NONE);
  64. else if (activeSummon.getChargedSpiritShot() == L2ItemInstance.CHARGED_SPIRITSHOT)
  65. activeSummon.setChargedSpiritShot(L2ItemInstance.CHARGED_NONE);
  66. }
  67. }
  68. else if (activeChar instanceof L2Npc)
  69. ((L2Npc) activeChar)._spiritshotcharged = false;
  70. L2Character target;
  71. L2Effect effect;
  72. int count = (int)skill.getPower();
  73. for (L2Object obj: targets)
  74. {
  75. if (!(obj instanceof L2Character))
  76. continue;
  77. target = (L2Character)obj;
  78. if (target.isDead())
  79. continue;
  80. if (!(target instanceof L2PcInstance))
  81. continue;
  82. Env env;
  83. int lastSkillId = 0;
  84. final L2Effect[] effects = target.getAllEffects();
  85. final ArrayList<L2Effect> toSteal = new ArrayList<L2Effect>(count);
  86. for (int i = effects.length; --i >= 0;) // reverse order
  87. {
  88. effect = effects[i];
  89. if (effect == null)
  90. continue;
  91. if (!effect.canBeStolen()) // remove effect if can't be stolen
  92. {
  93. effects[i] = null;
  94. continue;
  95. }
  96. // if eff time is smaller than 5 sec, will not be stolen, just to save CPU,
  97. // avoid synchronization(?) problems and NPEs
  98. if (effect.getAbnormalTime() - effect.getTime() < 5)
  99. {
  100. effects[i] = null;
  101. continue;
  102. }
  103. // first pass - only dances/songs
  104. if (!effect.getSkill().isDance())
  105. continue;
  106. if (effect.getSkill().getId() != lastSkillId)
  107. {
  108. lastSkillId = effect.getSkill().getId();
  109. count--;
  110. }
  111. toSteal.add(effect);
  112. if (count == 0)
  113. break;
  114. }
  115. if (count > 0) // second pass
  116. {
  117. lastSkillId = 0;
  118. for (int i = effects.length; --i >= 0;)
  119. {
  120. effect = effects[i];
  121. if (effect == null)
  122. continue;
  123. // second pass - all except dances/songs
  124. if (effect.getSkill().isDance())
  125. continue;
  126. if (effect.getSkill().getId() != lastSkillId)
  127. {
  128. lastSkillId = effect.getSkill().getId();
  129. count--;
  130. }
  131. toSteal.add(effect);
  132. if (count == 0)
  133. break;
  134. }
  135. }
  136. if (toSteal.size() == 0)
  137. continue;
  138. // stealing effects
  139. for (L2Effect eff : toSteal)
  140. {
  141. env = new Env();
  142. env.player = target;
  143. env.target = activeChar;
  144. env.skill = eff.getSkill();
  145. try
  146. {
  147. effect = eff.getEffectTemplate().getStolenEffect(env, eff);
  148. if (effect != null)
  149. {
  150. effect.scheduleEffect();
  151. if (effect.getShowIcon() && activeChar instanceof L2PcInstance)
  152. {
  153. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  154. sm.addSkillName(effect);
  155. activeChar.sendPacket(sm);
  156. }
  157. }
  158. // Finishing stolen effect
  159. eff.exit();
  160. }
  161. catch (RuntimeException e)
  162. {
  163. _log.log(Level.WARNING, "Cannot steal effect: " + eff + " Stealer: " + activeChar + " Stolen: " + target, e);
  164. }
  165. }
  166. //Possibility of a lethal strike
  167. Formulas.calcLethalHit(activeChar, target, skill);
  168. }
  169. if (skill.hasSelfEffects())
  170. {
  171. // Applying self-effects
  172. effect = activeChar.getFirstEffect(skill.getId());
  173. if (effect != null && effect.isSelfEffect())
  174. {
  175. //Replace old effect with new one.
  176. effect.exit();
  177. }
  178. skill.getEffectsSelf(activeChar);
  179. }
  180. }
  181. /**
  182. *
  183. * @see com.l2jserver.gameserver.handler.ISkillHandler#getSkillIds()
  184. */
  185. @Override
  186. public L2SkillType[] getSkillIds()
  187. {
  188. return SKILL_IDS;
  189. }
  190. }