BuffInfo.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.model.skills;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.concurrent.ScheduledFuture;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.GameTimeController;
  27. import com.l2jserver.gameserver.ThreadPoolManager;
  28. import com.l2jserver.gameserver.model.CharEffectList;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.L2Summon;
  31. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  32. import com.l2jserver.gameserver.model.effects.EffectTaskInfo;
  33. import com.l2jserver.gameserver.model.effects.EffectTickTask;
  34. import com.l2jserver.gameserver.model.stats.Formulas;
  35. import com.l2jserver.gameserver.network.SystemMessageId;
  36. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  37. /**
  38. * Buff Info.<br>
  39. * Complex DTO that holds all the information for a given buff (or debuff or dance/song) set of effects issued by an skill.
  40. * @author Zoey76
  41. */
  42. public final class BuffInfo
  43. {
  44. // Data
  45. /** Data. */
  46. private final L2Character _effector;
  47. private final L2Character _effected;
  48. private final Skill _skill;
  49. /** The effects. */
  50. private final List<AbstractEffect> _effects = new ArrayList<>(1);
  51. // Tasks
  52. /** Effect tasks for ticks. */
  53. private volatile Map<AbstractEffect, EffectTaskInfo> _tasks;
  54. /** Scheduled future. */
  55. private ScheduledFuture<?> _scheduledFutureTimeTask;
  56. // Time and ticks
  57. /** Abnormal time. */
  58. private int _abnormalTime;
  59. /** The game ticks at the start of this effect. */
  60. private final int _periodStartTicks;
  61. // Misc
  62. /** If {@code true} then this effect has been cancelled. */
  63. private boolean _isRemoved = false;
  64. /** If {@code true} then this effect is in use (or has been stop because an Herb took place). */
  65. private boolean _isInUse = true;
  66. /**
  67. * Buff Info constructor.
  68. * @param effector
  69. * @param effected
  70. * @param skill
  71. */
  72. public BuffInfo(L2Character effector, L2Character effected, Skill skill)
  73. {
  74. _effector = effector;
  75. _effected = effected;
  76. _skill = skill;
  77. _abnormalTime = Formulas.calcEffectAbnormalTime(effector, effected, skill);
  78. _periodStartTicks = GameTimeController.getInstance().getGameTicks();
  79. }
  80. /**
  81. * Gets the effects on this buff info.
  82. * @return the effects
  83. */
  84. public List<AbstractEffect> getEffects()
  85. {
  86. return _effects;
  87. }
  88. /**
  89. * Adds an effect to this buff info.
  90. * @param effect the effect to add
  91. */
  92. public void addEffect(AbstractEffect effect)
  93. {
  94. _effects.add(effect);
  95. }
  96. /**
  97. * Adds an effect task to this buff info.<br>
  98. * Uses double-checked locking to initialize the map if it's necessary.
  99. * @param effect the effect that owns the task
  100. * @param effectTaskInfo the task info
  101. */
  102. private void addTask(AbstractEffect effect, EffectTaskInfo effectTaskInfo)
  103. {
  104. if (_tasks == null)
  105. {
  106. synchronized (this)
  107. {
  108. if (_tasks == null)
  109. {
  110. _tasks = new ConcurrentHashMap<>();
  111. }
  112. }
  113. }
  114. _tasks.put(effect, effectTaskInfo);
  115. }
  116. /**
  117. * Gets the task for the given effect.
  118. * @param effect the effect
  119. * @return the task
  120. */
  121. private EffectTaskInfo getEffectTask(AbstractEffect effect)
  122. {
  123. return (_tasks == null) ? null : _tasks.get(effect);
  124. }
  125. /**
  126. * Gets the skill that created this buff info.
  127. * @return the skill
  128. */
  129. public Skill getSkill()
  130. {
  131. return _skill;
  132. }
  133. /**
  134. * Gets the calculated abnormal time.
  135. * @return the abnormal time
  136. */
  137. public int getAbnormalTime()
  138. {
  139. return _abnormalTime;
  140. }
  141. /**
  142. * Sets the abnormal time.
  143. * @param abnormalTime the abnormal time to set
  144. */
  145. public void setAbnormalTime(int abnormalTime)
  146. {
  147. _abnormalTime = abnormalTime;
  148. }
  149. /**
  150. * Gets the period start ticks.
  151. * @return the period start
  152. */
  153. public int getPeriodStartTicks()
  154. {
  155. return _periodStartTicks;
  156. }
  157. /**
  158. * Get the remaining time in seconds for this buff info.
  159. * @return the elapsed time
  160. */
  161. public int getTime()
  162. {
  163. return _abnormalTime - ((GameTimeController.getInstance().getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND);
  164. }
  165. /**
  166. * Verify if this buff info has been cancelled.
  167. * @return {@code true} if this buff info has been cancelled, {@code false} otherwise
  168. */
  169. public boolean isRemoved()
  170. {
  171. return _isRemoved;
  172. }
  173. /**
  174. * Set the buff info to removed.
  175. * @param val the value to set
  176. */
  177. public void setRemoved(boolean val)
  178. {
  179. _isRemoved = val;
  180. }
  181. /**
  182. * Verify if this buff info is in use.
  183. * @return {@code true} if this buff info is in use, {@code false} otherwise
  184. */
  185. public boolean isInUse()
  186. {
  187. return _isInUse;
  188. }
  189. /**
  190. * Set the buff info to in use.
  191. * @param val the value to set
  192. */
  193. public void setInUse(boolean val)
  194. {
  195. _isInUse = val;
  196. }
  197. /**
  198. * Gets the character that launched the buff.
  199. * @return the effector
  200. */
  201. public L2Character getEffector()
  202. {
  203. return _effector;
  204. }
  205. /**
  206. * Gets the target of the skill.
  207. * @return the effected
  208. */
  209. public L2Character getEffected()
  210. {
  211. return _effected;
  212. }
  213. /**
  214. * Stops all the effects for this buff info.<br>
  215. * Removes effects stats.<br>
  216. * <b>It will not remove the buff info from the effect list</b>.<br>
  217. * Instead call {@link CharEffectList#stopSkillEffects(boolean, Skill)}
  218. * @param removed if {@code true} the skill will be handled as removed
  219. */
  220. public void stopAllEffects(boolean removed)
  221. {
  222. setRemoved(removed);
  223. // Cancels the task that will end this buff info
  224. if ((_scheduledFutureTimeTask != null) && !_scheduledFutureTimeTask.isCancelled())
  225. {
  226. _scheduledFutureTimeTask.cancel(true);
  227. }
  228. finishEffects();
  229. }
  230. public void initializeEffects()
  231. {
  232. if ((_effected == null) || (_skill == null))
  233. {
  234. return;
  235. }
  236. // When effects are initialized, the successfully landed.
  237. if (_effected.isPlayer() && !_skill.isPassive())
  238. {
  239. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  240. sm.addSkillName(_skill);
  241. _effected.sendPacket(sm);
  242. }
  243. // Creates a task that will stop all the effects.
  244. if (_abnormalTime > 0)
  245. {
  246. _scheduledFutureTimeTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(new BuffTimeTask(this), 0, 1000L);
  247. }
  248. boolean update = false;
  249. for (AbstractEffect effect : _effects)
  250. {
  251. if (effect.isInstant() || (_effected.isDead() && !_skill.isPassive()))
  252. {
  253. continue;
  254. }
  255. // Call on start.
  256. effect.onStart(this);
  257. // If it's a continuous effect, if has ticks schedule a task with period, otherwise schedule a simple task to end it.
  258. if (effect.getTicks() > 0)
  259. {
  260. // The task for the effect ticks.
  261. final EffectTickTask effectTask = new EffectTickTask(this, effect);
  262. final ScheduledFuture<?> scheduledFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(effectTask, effect.getTicks() * Config.EFFECT_TICK_RATIO, effect.getTicks() * Config.EFFECT_TICK_RATIO);
  263. // Adds the task for ticking.
  264. addTask(effect, new EffectTaskInfo(effectTask, scheduledFuture));
  265. }
  266. // Add stats.
  267. _effected.addStatFuncs(effect.getStatFuncs(_effector, _effected, _skill));
  268. update = true;
  269. }
  270. if (update)
  271. {
  272. // Add abnormal visual effects.
  273. addAbnormalVisualEffects();
  274. }
  275. }
  276. /**
  277. * Called on each tick.<br>
  278. * Verify if the effect should end and the effect task should be cancelled.
  279. * @param effect the effect that is ticking
  280. * @param tickCount the tick count
  281. */
  282. public void onTick(AbstractEffect effect, int tickCount)
  283. {
  284. boolean continueForever = false;
  285. // If the effect is in use, allow it to affect the effected.
  286. if (_isInUse)
  287. {
  288. // Callback for on action time event.
  289. continueForever = effect.onActionTime(this);
  290. }
  291. if (!continueForever && _skill.isToggle())
  292. {
  293. final EffectTaskInfo task = getEffectTask(effect);
  294. if (task != null)
  295. {
  296. task.getScheduledFuture().cancel(true); // Don't allow to finish current run.
  297. _effected.getEffectList().stopSkillEffects(true, getSkill()); // Remove the buff from the effect list.
  298. }
  299. }
  300. }
  301. public void finishEffects()
  302. {
  303. // Cancels the ticking task.
  304. if (_tasks != null)
  305. {
  306. for (EffectTaskInfo effectTask : _tasks.values())
  307. {
  308. effectTask.getScheduledFuture().cancel(true); // Don't allow to finish current run.
  309. }
  310. }
  311. // Remove stats
  312. removeStats();
  313. // Notify on exit.
  314. for (AbstractEffect effect : _effects)
  315. {
  316. // Instant effects shouldn't call onExit(..).
  317. if ((effect != null) && !effect.isInstant())
  318. {
  319. effect.onExit(this);
  320. }
  321. }
  322. // Remove abnormal visual effects.
  323. removeAbnormalVisualEffects();
  324. // Set the proper system message.
  325. if (!(_effected.isSummon() && !((L2Summon) _effected).getOwner().hasSummon()))
  326. {
  327. SystemMessageId smId = null;
  328. if (_skill.isToggle())
  329. {
  330. smId = SystemMessageId.S1_HAS_BEEN_ABORTED;
  331. }
  332. else if (isRemoved())
  333. {
  334. smId = SystemMessageId.EFFECT_S1_HAS_BEEN_REMOVED;
  335. }
  336. else if (!_skill.isPassive())
  337. {
  338. smId = SystemMessageId.S1_HAS_WORN_OFF;
  339. }
  340. if (smId != null)
  341. {
  342. final SystemMessage sm = SystemMessage.getSystemMessage(smId);
  343. sm.addSkillName(_skill);
  344. _effected.sendPacket(sm);
  345. }
  346. }
  347. // Remove short buff.
  348. if (this == _effected.getEffectList().getShortBuff())
  349. {
  350. _effected.getEffectList().shortBuffStatusUpdate(null);
  351. }
  352. }
  353. /**
  354. * Applies all the abnormal visual effects to the effected.<br>
  355. * Prevents multiple updates.
  356. */
  357. private void addAbnormalVisualEffects()
  358. {
  359. if (_skill.hasAbnormalVisualEffects())
  360. {
  361. _effected.startAbnormalVisualEffect(false, _skill.getAbnormalVisualEffects());
  362. }
  363. if (_effected.isPlayer() && _skill.hasAbnormalVisualEffectsEvent())
  364. {
  365. _effected.startAbnormalVisualEffect(false, _skill.getAbnormalVisualEffectsEvent());
  366. }
  367. if (_skill.hasAbnormalVisualEffectsSpecial())
  368. {
  369. _effected.startAbnormalVisualEffect(false, _skill.getAbnormalVisualEffectsSpecial());
  370. }
  371. // Update abnormal visual effects.
  372. _effected.updateAbnormalEffect();
  373. }
  374. /**
  375. * Removes all the abnormal visual effects from the effected.<br>
  376. * Prevents multiple updates.
  377. */
  378. private void removeAbnormalVisualEffects()
  379. {
  380. if ((_effected == null) || (_skill == null))
  381. {
  382. return;
  383. }
  384. if (_skill.hasAbnormalVisualEffects())
  385. {
  386. _effected.stopAbnormalVisualEffect(false, _skill.getAbnormalVisualEffects());
  387. }
  388. if (_effected.isPlayer() && _skill.hasAbnormalVisualEffectsEvent())
  389. {
  390. _effected.stopAbnormalVisualEffect(false, _skill.getAbnormalVisualEffectsEvent());
  391. }
  392. if (_skill.hasAbnormalVisualEffectsSpecial())
  393. {
  394. _effected.stopAbnormalVisualEffect(false, _skill.getAbnormalVisualEffectsSpecial());
  395. }
  396. _effected.updateAbnormalEffect();
  397. }
  398. /**
  399. * Adds the buff stats.
  400. */
  401. public void addStats()
  402. {
  403. _effects.forEach(effect -> _effected.addStatFuncs(effect.getStatFuncs(_effector, _effected, _skill)));
  404. }
  405. /**
  406. * Removes the buff stats.
  407. */
  408. public void removeStats()
  409. {
  410. _effects.forEach(_effected::removeStatsOwner);
  411. _effected.removeStatsOwner(_skill);
  412. }
  413. /**
  414. * Gets the effect tick count.
  415. * @param effect the effect
  416. * @return the current tick count
  417. */
  418. public int getTickCount(AbstractEffect effect)
  419. {
  420. if (_tasks != null)
  421. {
  422. final EffectTaskInfo effectTaskInfo = _tasks.get(effect);
  423. if (effectTaskInfo != null)
  424. {
  425. return effectTaskInfo.getEffectTask().getTickCount();
  426. }
  427. }
  428. return 0;
  429. }
  430. }