CharEffectList.java 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 com.l2jserver.gameserver.model;
  16. import java.util.concurrent.LinkedBlockingQueue;
  17. import java.util.concurrent.atomic.AtomicBoolean;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.actor.L2Playable;
  24. import com.l2jserver.gameserver.model.actor.L2Summon;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  27. import com.l2jserver.gameserver.network.SystemMessageId;
  28. import com.l2jserver.gameserver.network.serverpackets.AbnormalStatusUpdate;
  29. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadSpelledInfo;
  30. import com.l2jserver.gameserver.network.serverpackets.PartySpelled;
  31. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  32. import com.l2jserver.gameserver.templates.skills.L2EffectType;
  33. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  34. import javolution.util.FastList;
  35. import javolution.util.FastMap;
  36. public class CharEffectList
  37. {
  38. private static final L2Effect[] EMPTY_EFFECTS = new L2Effect[0];
  39. private FastList<L2Effect> _buffs;
  40. private FastList<L2Effect> _debuffs;
  41. // The table containing the List of all stacked effect in progress for each Stack group Identifier
  42. private Map<String, List<L2Effect>> _stackedEffects;
  43. private volatile boolean _hasEffectsRemovedOnAnyAction = false;
  44. private boolean _queuesInitialized = false;
  45. private LinkedBlockingQueue<L2Effect> _addQueue;
  46. private LinkedBlockingQueue<L2Effect> _removeQueue;
  47. private AtomicBoolean queueLock = new AtomicBoolean();
  48. // only party icons need to be updated
  49. private boolean _partyOnly = false;
  50. // Owner of this list
  51. private L2Character _owner;
  52. public CharEffectList(L2Character owner)
  53. {
  54. _owner = owner;
  55. }
  56. /**
  57. * Returns all effects affecting stored in this CharEffectList
  58. * @return
  59. */
  60. public final L2Effect[] getAllEffects()
  61. {
  62. // If no effect is active, return EMPTY_EFFECTS
  63. if ( (_buffs == null || _buffs.isEmpty()) && (_debuffs == null || _debuffs.isEmpty()) )
  64. {
  65. return EMPTY_EFFECTS;
  66. }
  67. // Create a copy of the effects
  68. FastList<L2Effect> temp = FastList.newInstance();
  69. // Add all buffs and all debuffs
  70. if (_buffs != null)
  71. {
  72. synchronized (_buffs)
  73. {
  74. if (!_buffs.isEmpty())
  75. temp.addAll(_buffs);
  76. }
  77. }
  78. if (_debuffs != null)
  79. {
  80. synchronized (_debuffs)
  81. {
  82. if (!_debuffs.isEmpty())
  83. temp.addAll(_debuffs);
  84. }
  85. }
  86. // Return all effects in an array
  87. L2Effect[] tempArray = new L2Effect[temp.size()];
  88. temp.toArray(tempArray);
  89. FastList.recycle(temp);
  90. return tempArray;
  91. }
  92. /**
  93. * Returns the first effect matching the given EffectType
  94. * @param tp
  95. * @return
  96. */
  97. public final L2Effect getFirstEffect(L2EffectType tp)
  98. {
  99. L2Effect effectNotInUse = null;
  100. if (_buffs != null)
  101. {
  102. synchronized (_buffs)
  103. {
  104. if (!_buffs.isEmpty())
  105. {
  106. for (L2Effect e: _buffs)
  107. {
  108. if (e == null)
  109. continue;
  110. if (e.getEffectType() == tp)
  111. {
  112. if (e.getInUse())
  113. return e;
  114. else
  115. effectNotInUse = e;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. if (effectNotInUse == null && _debuffs != null)
  122. {
  123. synchronized (_debuffs)
  124. {
  125. if (!_debuffs.isEmpty())
  126. {
  127. for (L2Effect e: _debuffs)
  128. {
  129. if (e == null)
  130. continue;
  131. if (e.getEffectType() == tp)
  132. {
  133. if (e.getInUse())
  134. return e;
  135. else
  136. effectNotInUse = e;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. return effectNotInUse;
  143. }
  144. /**
  145. * Returns the first effect matching the given L2Skill
  146. * @param skill
  147. * @return
  148. */
  149. public final L2Effect getFirstEffect(L2Skill skill)
  150. {
  151. L2Effect effectNotInUse = null;
  152. if (skill.isDebuff())
  153. {
  154. if (_debuffs == null)
  155. return null;
  156. synchronized (_debuffs)
  157. {
  158. if (_debuffs.isEmpty())
  159. return null;
  160. for (L2Effect e: _debuffs)
  161. {
  162. if (e == null)
  163. continue;
  164. if (e.getSkill() == skill)
  165. {
  166. if (e.getInUse())
  167. return e;
  168. else
  169. effectNotInUse = e;
  170. }
  171. }
  172. }
  173. return effectNotInUse;
  174. }
  175. else
  176. {
  177. if (_buffs == null)
  178. return null;
  179. synchronized (_buffs)
  180. {
  181. if (_buffs.isEmpty())
  182. return null;
  183. for (L2Effect e: _buffs)
  184. {
  185. if (e == null)
  186. continue;
  187. if (e.getSkill() == skill)
  188. {
  189. if (e.getInUse())
  190. return e;
  191. else
  192. effectNotInUse = e;
  193. }
  194. }
  195. }
  196. return effectNotInUse;
  197. }
  198. }
  199. /**
  200. * Returns the first effect matching the given skillId
  201. * @param index
  202. * @return
  203. */
  204. public final L2Effect getFirstEffect(int skillId)
  205. {
  206. L2Effect effectNotInUse = null;
  207. if (_buffs != null)
  208. {
  209. synchronized (_buffs)
  210. {
  211. if (!_buffs.isEmpty())
  212. {
  213. for (L2Effect e: _buffs)
  214. {
  215. if (e == null)
  216. continue;
  217. if (e.getSkill().getId() == skillId)
  218. {
  219. if (e.getInUse())
  220. return e;
  221. else
  222. effectNotInUse = e;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. if (effectNotInUse == null && _debuffs != null)
  229. {
  230. synchronized (_debuffs)
  231. {
  232. if (!_debuffs.isEmpty())
  233. {
  234. for (L2Effect e: _debuffs)
  235. {
  236. if (e == null)
  237. continue;
  238. if (e.getSkill().getId() == skillId)
  239. {
  240. if (e.getInUse())
  241. return e;
  242. else
  243. effectNotInUse = e;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. return effectNotInUse;
  250. }
  251. /**
  252. * Checks if the given skill stacks with an existing one.
  253. *
  254. * @param checkSkill the skill to be checked
  255. *
  256. * @return Returns whether or not this skill will stack
  257. */
  258. private boolean doesStack(L2Skill checkSkill)
  259. {
  260. if ( (_buffs == null || _buffs.isEmpty()) ||
  261. checkSkill._effectTemplates == null ||
  262. checkSkill._effectTemplates.length < 1 ||
  263. checkSkill._effectTemplates[0].stackType == null ||
  264. "none".equals(checkSkill._effectTemplates[0].stackType))
  265. {
  266. return false;
  267. }
  268. String stackType = checkSkill._effectTemplates[0].stackType;
  269. for (L2Effect e : _buffs)
  270. {
  271. if (e.getStackType() != null && e.getStackType().equals(stackType))
  272. {
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. /**
  279. * Return the number of buffs in this CharEffectList not counting Songs/Dances
  280. * @return
  281. */
  282. public int getBuffCount()
  283. {
  284. if (_buffs == null) return 0;
  285. int buffCount=0;
  286. synchronized(_buffs)
  287. {
  288. if (_buffs.isEmpty())
  289. return 0;
  290. for (L2Effect e : _buffs)
  291. {
  292. if (e != null && e.getShowIcon() && !e.getSkill().isDance() && !e.getSkill().is7Signs())
  293. {
  294. switch (e.getSkill().getSkillType())
  295. {
  296. case BUFF:
  297. case REFLECT:
  298. case HEAL_PERCENT:
  299. case MANAHEAL_PERCENT:
  300. buffCount++;
  301. }
  302. }
  303. }
  304. }
  305. return buffCount;
  306. }
  307. /**
  308. * Return the number of Songs/Dances in this CharEffectList
  309. * @return
  310. */
  311. public int getDanceCount()
  312. {
  313. if (_buffs == null) return 0;
  314. int danceCount = 0;
  315. synchronized(_buffs)
  316. {
  317. if (_buffs.isEmpty())
  318. return 0;
  319. for (L2Effect e : _buffs)
  320. {
  321. if (e != null && e.getSkill().isDance() && e.getInUse())
  322. danceCount++;
  323. }
  324. }
  325. return danceCount;
  326. }
  327. /**
  328. * Exits all effects in this CharEffectList
  329. */
  330. public final void stopAllEffects()
  331. {
  332. // Get all active skills effects from this list
  333. L2Effect[] effects = getAllEffects();
  334. // Exit them
  335. for (L2Effect e : effects)
  336. {
  337. if (e != null)
  338. e.exit(true);
  339. }
  340. }
  341. /**
  342. * Exits all effects in this CharEffectList
  343. */
  344. public final void stopAllEffectsExceptThoseThatLastThroughDeath()
  345. {
  346. // Get all active skills effects from this list
  347. L2Effect[] effects = getAllEffects();
  348. // Exit them
  349. for (L2Effect e : effects)
  350. {
  351. if (e != null && !e.getSkill().isStayAfterDeath())
  352. e.exit(true);
  353. }
  354. }
  355. /**
  356. * Exit all toggle-type effects
  357. */
  358. public void stopAllToggles()
  359. {
  360. if (_buffs != null)
  361. {
  362. synchronized (_buffs)
  363. {
  364. if (!_buffs.isEmpty())
  365. {
  366. for (L2Effect e : _buffs)
  367. if (e != null && e.getSkill().isToggle())
  368. e.exit();
  369. }
  370. }
  371. }
  372. }
  373. /**
  374. * Exit all effects having a specified type
  375. * @param type
  376. */
  377. public final void stopEffects(L2EffectType type)
  378. {
  379. // Go through all active skills effects
  380. FastList<L2Effect> temp = FastList.newInstance();
  381. if (_buffs != null)
  382. {
  383. synchronized (_buffs)
  384. {
  385. if (!_buffs.isEmpty())
  386. {
  387. for (L2Effect e : _buffs)
  388. // Get active skills effects of the selected type
  389. if (e != null && e.getEffectType() == type)
  390. temp.add(e);
  391. }
  392. }
  393. }
  394. if (_debuffs != null)
  395. {
  396. synchronized (_debuffs)
  397. {
  398. if (!_debuffs.isEmpty())
  399. {
  400. for (L2Effect e : _debuffs)
  401. // Get active skills effects of the selected type
  402. if (e != null && e.getEffectType() == type)
  403. temp.add(e);
  404. }
  405. }
  406. }
  407. if (!temp.isEmpty())
  408. {
  409. for (L2Effect e : temp)
  410. if (e != null)
  411. e.exit();
  412. }
  413. FastList.recycle(temp);
  414. }
  415. /**
  416. * Exits all effects created by a specific skillId
  417. * @param skillId
  418. */
  419. public final void stopSkillEffects(int skillId)
  420. {
  421. // Go through all active skills effects
  422. FastList<L2Effect> temp = FastList.newInstance();
  423. if (_buffs != null)
  424. {
  425. synchronized (_buffs)
  426. {
  427. if (!_buffs.isEmpty())
  428. {
  429. for (L2Effect e : _buffs)
  430. if (e != null && e.getSkill().getId() == skillId)
  431. temp.add(e);
  432. }
  433. }
  434. }
  435. if (_debuffs != null)
  436. {
  437. synchronized (_debuffs)
  438. {
  439. if (!_debuffs.isEmpty())
  440. {
  441. for (L2Effect e : _debuffs)
  442. if (e != null && e.getSkill().getId() == skillId)
  443. temp.add(e);
  444. }
  445. }
  446. }
  447. if (!temp.isEmpty())
  448. {
  449. for (L2Effect e : temp)
  450. if (e != null)
  451. e.exit();
  452. }
  453. FastList.recycle(temp);
  454. }
  455. /**
  456. * Exits all effects created by a specific skill type
  457. * @param skillType skill type
  458. */
  459. public final void stopSkillEffects(L2SkillType skillType, int negateLvl)
  460. {
  461. // Go through all active skills effects
  462. FastList<L2Effect> temp = FastList.newInstance();
  463. if (_buffs != null)
  464. {
  465. synchronized (_buffs)
  466. {
  467. if (!_buffs.isEmpty())
  468. {
  469. for (L2Effect e : _buffs)
  470. {
  471. if (e != null && (e.getSkill().getSkillType() == skillType || (e.getSkill().getEffectType() != null && e.getSkill().getEffectType() == skillType))
  472. && (negateLvl == -1 || (e.getSkill().getEffectType() != null && e.getSkill().getEffectAbnormalLvl() >= 0 && e.getSkill().getEffectAbnormalLvl() <= negateLvl) || (e.getSkill().getAbnormalLvl() >= 0 && e.getSkill().getAbnormalLvl() <= negateLvl)))
  473. temp.add(e);
  474. }
  475. }
  476. }
  477. }
  478. if (_debuffs != null)
  479. {
  480. synchronized (_debuffs)
  481. {
  482. if (!_debuffs.isEmpty())
  483. {
  484. for (L2Effect e : _debuffs)
  485. {
  486. if (e != null && (e.getSkill().getSkillType() == skillType || (e.getSkill().getEffectType() != null && e.getSkill().getEffectType() == skillType))
  487. && (negateLvl == -1 || (e.getSkill().getEffectType() != null && e.getSkill().getEffectAbnormalLvl() >= 0 && e.getSkill().getEffectAbnormalLvl() <= negateLvl) || (e.getSkill().getAbnormalLvl() >= 0 && e.getSkill().getAbnormalLvl() <= negateLvl)))
  488. temp.add(e);
  489. }
  490. }
  491. }
  492. }
  493. if (!temp.isEmpty())
  494. {
  495. for (L2Effect e : temp)
  496. if (e != null)
  497. e.exit();
  498. }
  499. FastList.recycle(temp);
  500. }
  501. /**
  502. * Exits all buffs effects of the skills with "removedOnAnyAction" set.
  503. * Called on any action except movement (attack, cast).
  504. */
  505. public void stopEffectsOnAction()
  506. {
  507. if (_hasEffectsRemovedOnAnyAction)
  508. {
  509. if (_buffs != null)
  510. {
  511. synchronized (_buffs)
  512. {
  513. if (!_buffs.isEmpty())
  514. {
  515. for (L2Effect e : _buffs)
  516. if (e != null && e.getSkill().isRemovedOnAnyActionExceptMove())
  517. e.exit(true);
  518. }
  519. }
  520. }
  521. }
  522. }
  523. public void updateEffectIcons(boolean partyOnly)
  524. {
  525. if (_buffs == null && _debuffs == null)
  526. return;
  527. if (partyOnly)
  528. _partyOnly = true;
  529. queueRunner();
  530. }
  531. public void queueEffect(L2Effect effect, boolean remove)
  532. {
  533. if (effect == null) return;
  534. if (!_queuesInitialized)
  535. init();
  536. if (remove)
  537. _removeQueue.offer(effect);
  538. else
  539. _addQueue.offer(effect);
  540. queueRunner();
  541. }
  542. synchronized private void init()
  543. {
  544. _addQueue = new LinkedBlockingQueue<L2Effect>();
  545. _removeQueue = new LinkedBlockingQueue<L2Effect>();
  546. _queuesInitialized = true;
  547. }
  548. private void queueRunner()
  549. {
  550. if (!queueLock.compareAndSet(false, true))
  551. return;
  552. try
  553. {
  554. L2Effect effect;
  555. do
  556. {
  557. // remove has more priority than add
  558. // so removing all effects from queue first
  559. while ((effect = _removeQueue.poll()) != null)
  560. {
  561. removeEffectFromQueue(effect);
  562. _partyOnly = false;
  563. }
  564. if ((effect = _addQueue.poll()) != null)
  565. {
  566. addEffectFromQueue(effect);
  567. _partyOnly = false;
  568. }
  569. }
  570. while (!_addQueue.isEmpty() || !_removeQueue.isEmpty());
  571. updateEffectIcons();
  572. }
  573. finally
  574. {
  575. queueLock.set(false);
  576. }
  577. }
  578. protected void removeEffectFromQueue(L2Effect effect)
  579. {
  580. if (effect == null) return;
  581. FastList<L2Effect> effectList;
  582. if (effect.getSkill().isDebuff())
  583. {
  584. if (_debuffs == null)
  585. return;
  586. effectList = _debuffs;
  587. }
  588. else
  589. {
  590. if (_buffs == null)
  591. return;
  592. effectList = _buffs;
  593. }
  594. if ("none".equals(effect.getStackType()))
  595. {
  596. // Remove Func added by this effect from the L2Character Calculator
  597. _owner.removeStatsOwner(effect);
  598. }
  599. else
  600. {
  601. if(_stackedEffects == null) return;
  602. // Get the list of all stacked effects corresponding to the stack type of the L2Effect to add
  603. List<L2Effect> stackQueue = _stackedEffects.get(effect.getStackType());
  604. if (stackQueue == null || stackQueue.isEmpty()) return;
  605. int index = stackQueue.indexOf(effect);
  606. // Remove the effect from the stack group
  607. if (index >= 0)
  608. {
  609. stackQueue.remove(effect);
  610. // Check if the first stacked effect was the effect to remove
  611. if (index == 0)
  612. {
  613. // Remove all its Func objects from the L2Character calculator set
  614. _owner.removeStatsOwner(effect);
  615. // Check if there's another effect in the Stack Group
  616. if (!stackQueue.isEmpty())
  617. {
  618. L2Effect newStackedEffect = listsContains(stackQueue.get(0));
  619. if (newStackedEffect != null)
  620. {
  621. // Set the effect to In Use
  622. if (newStackedEffect.setInUse(true))
  623. // Add its list of Funcs to the Calculator set of the L2Character
  624. _owner.addStatFuncs(newStackedEffect.getStatFuncs());
  625. }
  626. }
  627. }
  628. if (stackQueue.isEmpty())
  629. _stackedEffects.remove(effect.getStackType());
  630. else
  631. // Update the Stack Group table _stackedEffects of the L2Character
  632. _stackedEffects.put(effect.getStackType(), stackQueue);
  633. }
  634. }
  635. // Remove the active skill L2effect from _effects of the L2Character
  636. if (effectList.remove(effect) && _owner instanceof L2PcInstance && effect.getShowIcon())
  637. {
  638. SystemMessage sm;
  639. if (effect.getSkill().isToggle())
  640. {
  641. sm = new SystemMessage(SystemMessageId.S1_HAS_BEEN_ABORTED);
  642. }
  643. else
  644. {
  645. sm = new SystemMessage(SystemMessageId.EFFECT_S1_DISAPPEARED);
  646. }
  647. sm.addSkillName(effect);
  648. _owner.sendPacket(sm);
  649. }
  650. }
  651. protected void addEffectFromQueue(L2Effect newEffect)
  652. {
  653. if (newEffect == null) return;
  654. L2Skill newSkill = newEffect.getSkill();
  655. if (newSkill.isDebuff())
  656. {
  657. if (_debuffs == null) _debuffs = new FastList<L2Effect>().shared();
  658. for (L2Effect e : _debuffs)
  659. {
  660. if (e != null
  661. && e.getSkill().getId() == newEffect.getSkill().getId()
  662. && e.getEffectType() == newEffect.getEffectType()
  663. && e.getStackOrder() == newEffect.getStackOrder()
  664. && e.getStackType().equals(newEffect.getStackType()))
  665. {
  666. // Started scheduled timer needs to be canceled.
  667. newEffect.stopEffectTask();
  668. return;
  669. }
  670. }
  671. _debuffs.addLast(newEffect);
  672. }
  673. else
  674. {
  675. if (_buffs == null) _buffs = new FastList<L2Effect>().shared();
  676. for (L2Effect e : _buffs)
  677. {
  678. if (e != null
  679. && e.getSkill().getId() == newEffect.getSkill().getId()
  680. && e.getEffectType() == newEffect.getEffectType()
  681. && e.getStackOrder() == newEffect.getStackOrder()
  682. && e.getStackType().equals(newEffect.getStackType()))
  683. {
  684. e.exit(); // exit this
  685. }
  686. }
  687. // if max buffs, no herb effects are used, even if they would replace one old
  688. if (newEffect.isHerbEffect() && getBuffCount() >= _owner.getMaxBuffCount())
  689. {
  690. newEffect.stopEffectTask();
  691. return;
  692. }
  693. // Remove first buff when buff list is full
  694. if (!doesStack(newSkill) && !newSkill.is7Signs())
  695. {
  696. int effectsToRemove;
  697. if (newSkill.isDance())
  698. {
  699. effectsToRemove = getDanceCount() - Config.DANCES_MAX_AMOUNT;
  700. if (effectsToRemove >= 0)
  701. {
  702. for (L2Effect e : _buffs)
  703. {
  704. if (e == null || !e.getSkill().isDance())
  705. continue;
  706. // get first dance
  707. e.exit();
  708. effectsToRemove--;
  709. if (effectsToRemove < 0)
  710. break;
  711. }
  712. }
  713. }
  714. else
  715. {
  716. effectsToRemove = getBuffCount() - _owner.getMaxBuffCount();
  717. if (effectsToRemove >= 0)
  718. {
  719. switch (newSkill.getSkillType())
  720. {
  721. case BUFF:
  722. case REFLECT:
  723. case HEAL_PERCENT:
  724. case MANAHEAL_PERCENT:
  725. for (L2Effect e : _buffs)
  726. {
  727. if (e == null || e.getSkill().isDance())
  728. continue;
  729. switch (e.getSkill().getSkillType())
  730. {
  731. case BUFF:
  732. case REFLECT:
  733. case HEAL_PERCENT:
  734. case MANAHEAL_PERCENT:
  735. e.exit();
  736. effectsToRemove--;
  737. break; // break switch()
  738. default:
  739. continue; // continue for()
  740. }
  741. if (effectsToRemove < 0)
  742. break; // break for()
  743. }
  744. }
  745. }
  746. }
  747. }
  748. // Icons order: buffs, 7s, toggles, dances
  749. if (newSkill.isDance())
  750. _buffs.addLast(newEffect);
  751. else
  752. {
  753. int pos=0;
  754. if (newSkill.isToggle())
  755. {
  756. // toggle skill - before all dances
  757. for (L2Effect e : _buffs)
  758. {
  759. if (e == null)
  760. continue;
  761. if (e.getSkill().isDance())
  762. break;
  763. pos++;
  764. }
  765. }
  766. else
  767. {
  768. // normal buff - before toggles and 7s and dances
  769. for (L2Effect e : _buffs)
  770. {
  771. if (e == null)
  772. continue;
  773. if (e.getSkill().isToggle() || e.getSkill().is7Signs()
  774. || e.getSkill().isDance())
  775. break;
  776. pos++;
  777. }
  778. }
  779. _buffs.add(pos, newEffect);
  780. }
  781. }
  782. // Check if a stack group is defined for this effect
  783. if ("none".equals(newEffect.getStackType()))
  784. {
  785. // Set this L2Effect to In Use
  786. if (newEffect.setInUse(true))
  787. // Add Funcs of this effect to the Calculator set of the L2Character
  788. _owner.addStatFuncs(newEffect.getStatFuncs());
  789. return;
  790. }
  791. List<L2Effect> stackQueue;
  792. L2Effect effectToAdd = null;
  793. L2Effect effectToRemove = null;
  794. if (_stackedEffects == null) _stackedEffects = new FastMap<String, List<L2Effect>>();
  795. // Get the list of all stacked effects corresponding to the stack type of the L2Effect to add
  796. stackQueue = _stackedEffects.get(newEffect.getStackType());
  797. if (stackQueue != null)
  798. {
  799. int pos = 0;
  800. if (!stackQueue.isEmpty())
  801. {
  802. // Get the first stacked effect of the Stack group selected
  803. effectToRemove = listsContains(stackQueue.get(0));
  804. // Create an Iterator to go through the list of stacked effects in progress on the L2Character
  805. Iterator<L2Effect> queueIterator = stackQueue.iterator();
  806. while (queueIterator.hasNext())
  807. {
  808. if (newEffect.getStackOrder() < queueIterator.next().getStackOrder())
  809. pos++;
  810. else break;
  811. }
  812. // Add the new effect to the Stack list in function of its position in the Stack group
  813. stackQueue.add(pos, newEffect);
  814. // skill.exit() could be used, if the users don't wish to see "effect
  815. // removed" always when a timer goes off, even if the buff isn't active
  816. // any more (has been replaced). but then check e.g. npc hold and raid petrification.
  817. if (Config.EFFECT_CANCELING && !newEffect.isHerbEffect() && stackQueue.size() > 1)
  818. {
  819. if (newSkill.isDebuff())
  820. {
  821. _debuffs.remove(stackQueue.remove(1));
  822. }
  823. else
  824. {
  825. _buffs.remove(stackQueue.remove(1));
  826. }
  827. }
  828. }
  829. else
  830. stackQueue.add(0, newEffect);
  831. }
  832. else
  833. {
  834. stackQueue = new FastList<L2Effect>();
  835. stackQueue.add(0, newEffect);
  836. }
  837. // Update the Stack Group table _stackedEffects of the L2Character
  838. _stackedEffects.put(newEffect.getStackType(), stackQueue);
  839. // Get the first stacked effect of the Stack group selected
  840. if (!stackQueue.isEmpty())
  841. {
  842. effectToAdd = listsContains(stackQueue.get(0));
  843. }
  844. if (effectToRemove != effectToAdd)
  845. {
  846. if (effectToRemove != null)
  847. {
  848. // Remove all Func objects corresponding to this stacked effect from the Calculator set of the L2Character
  849. _owner.removeStatsOwner(effectToRemove);
  850. // Set the L2Effect to Not In Use
  851. effectToRemove.setInUse(false);
  852. }
  853. if (effectToAdd != null)
  854. {
  855. // Set this L2Effect to In Use
  856. if (effectToAdd.setInUse(true))
  857. // Add all Func objects corresponding to this stacked effect to the Calculator set of the L2Character
  858. _owner.addStatFuncs(effectToAdd.getStatFuncs());
  859. }
  860. }
  861. }
  862. protected void updateEffectIcons()
  863. {
  864. if (_owner == null || !(_owner instanceof L2Playable))
  865. return;
  866. AbnormalStatusUpdate mi = null;
  867. PartySpelled ps = null;
  868. ExOlympiadSpelledInfo os = null;
  869. if (_owner instanceof L2PcInstance)
  870. {
  871. if (_partyOnly)
  872. _partyOnly = false;
  873. else
  874. mi = new AbnormalStatusUpdate();
  875. if (_owner.isInParty())
  876. ps = new PartySpelled(_owner);
  877. if (((L2PcInstance)_owner).isInOlympiadMode() && ((L2PcInstance)_owner).isOlympiadStart())
  878. os = new ExOlympiadSpelledInfo((L2PcInstance)_owner);
  879. }
  880. else
  881. if (_owner instanceof L2Summon)
  882. ps = new PartySpelled(_owner);
  883. boolean found = false;
  884. if (_buffs != null && !_buffs.isEmpty())
  885. {
  886. synchronized (_buffs)
  887. {
  888. for (L2Effect e : _buffs)
  889. {
  890. if (e == null)
  891. continue;
  892. if (e.getSkill().isRemovedOnAnyActionExceptMove())
  893. found = true;
  894. if (!e.getShowIcon())
  895. continue;
  896. switch (e.getEffectType())
  897. {
  898. case CHARGE: // handled by EtcStatusUpdate
  899. case SIGNET_GROUND:
  900. continue;
  901. }
  902. if (e.getInUse())
  903. {
  904. if (mi != null)
  905. e.addIcon(mi);
  906. if (ps != null)
  907. e.addPartySpelledIcon(ps);
  908. if (os != null)
  909. e.addOlympiadSpelledIcon(os);
  910. }
  911. }
  912. }
  913. }
  914. if (_debuffs != null && !_debuffs.isEmpty())
  915. {
  916. synchronized (_debuffs)
  917. {
  918. for (L2Effect e : _debuffs)
  919. {
  920. if (e == null)
  921. continue;
  922. if (e.getSkill().isRemovedOnAnyActionExceptMove())
  923. found = true;
  924. if (!e.getShowIcon())
  925. continue;
  926. switch (e.getEffectType())
  927. {
  928. case SIGNET_GROUND:
  929. continue;
  930. }
  931. if (e.getInUse())
  932. {
  933. if (mi != null)
  934. e.addIcon(mi);
  935. if (ps != null)
  936. e.addPartySpelledIcon(ps);
  937. if (os != null)
  938. e.addOlympiadSpelledIcon(os);
  939. }
  940. }
  941. }
  942. }
  943. _hasEffectsRemovedOnAnyAction = found;
  944. if (mi != null)
  945. _owner.sendPacket(mi);
  946. if (ps != null)
  947. {
  948. if (_owner instanceof L2Summon)
  949. {
  950. L2PcInstance summonOwner = ((L2Summon)_owner).getOwner();
  951. if (summonOwner != null)
  952. {
  953. if (summonOwner.isInParty())
  954. summonOwner.getParty().broadcastToPartyMembers(ps);
  955. else
  956. summonOwner.sendPacket(ps);
  957. }
  958. }
  959. else
  960. if (_owner instanceof L2PcInstance && _owner.isInParty())
  961. _owner.getParty().broadcastToPartyMembers(ps);
  962. }
  963. if (os != null)
  964. {
  965. final List<L2PcInstance> specs = Olympiad.getInstance().getSpectators(((L2PcInstance)_owner).getOlympiadGameId());
  966. if (specs != null && !specs.isEmpty())
  967. {
  968. for (L2PcInstance spec : specs)
  969. {
  970. if (spec != null)
  971. spec.sendPacket(os);
  972. }
  973. }
  974. }
  975. }
  976. /**
  977. * Returns effect if contains in _buffs or _debuffs and null if not found
  978. * @param effect
  979. * @return
  980. */
  981. private L2Effect listsContains(L2Effect effect)
  982. {
  983. if (_buffs != null && !_buffs.isEmpty()&& _buffs.contains(effect))
  984. return effect;
  985. if (_debuffs != null && !_debuffs.isEmpty() && _debuffs.contains(effect))
  986. return effect;
  987. return null;
  988. }
  989. /**
  990. * Clear and null all queues and lists
  991. * Use only during delete character from the world.
  992. */
  993. public void clear()
  994. {
  995. try
  996. {
  997. if (_addQueue != null)
  998. {
  999. _addQueue.clear();
  1000. _addQueue = null;
  1001. }
  1002. if (_removeQueue != null)
  1003. {
  1004. _removeQueue.clear();
  1005. _removeQueue = null;
  1006. }
  1007. _queuesInitialized = false;
  1008. if (_buffs != null)
  1009. {
  1010. _buffs.clear();
  1011. _buffs = null;
  1012. }
  1013. if (_debuffs != null)
  1014. {
  1015. _debuffs.clear();
  1016. _debuffs = null;
  1017. }
  1018. if (_stackedEffects != null)
  1019. {
  1020. _stackedEffects.clear();
  1021. _stackedEffects = null;
  1022. }
  1023. }
  1024. catch (Exception e)
  1025. {
  1026. e.printStackTrace();
  1027. }
  1028. }
  1029. }