StealBuffs.java 6.1 KB

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