StealBuffs.java 6.2 KB

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