BuffInfo.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (C) 2004-2013 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.Map.Entry;
  24. import java.util.concurrent.ConcurrentHashMap;
  25. import java.util.concurrent.ScheduledFuture;
  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.effects.AbstractEffect;
  31. import com.l2jserver.gameserver.model.effects.EffectTaskInfo;
  32. import com.l2jserver.gameserver.model.effects.EffectTickTask;
  33. import com.l2jserver.gameserver.model.stats.Env;
  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 Env _env;
  47. /** The effects. */
  48. private final List<AbstractEffect> _effects = new ArrayList<>(1);
  49. // Tasks
  50. /** Effect tasks for ticks. */
  51. private Map<AbstractEffect, EffectTaskInfo> _tasks;
  52. /** Task for effect ending. */
  53. private BuffTimeTask _effectTimeTask;
  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 env the cast data
  69. */
  70. public BuffInfo(Env env)
  71. {
  72. _env = env;
  73. _abnormalTime = Formulas.calcEffectAbnormalTime(env);
  74. _periodStartTicks = GameTimeController.getInstance().getGameTicks();
  75. }
  76. /**
  77. * Gets the effects on this buff info.
  78. * @return the effects
  79. */
  80. public List<AbstractEffect> getEffects()
  81. {
  82. return _effects;
  83. }
  84. /**
  85. * Adds an effect to this buff info.
  86. * @param effect the effect to add
  87. */
  88. public void addEffect(AbstractEffect effect)
  89. {
  90. _effects.add(effect);
  91. }
  92. /**
  93. * Adds an effect task to this buff info.<br>
  94. * Uses double-checked locking to initialize the map if it's necessary.
  95. * @param effect the effect that owns the task
  96. * @param effectTaskInfo the task info
  97. */
  98. private void addTask(AbstractEffect effect, EffectTaskInfo effectTaskInfo)
  99. {
  100. if (_tasks == null)
  101. {
  102. synchronized (this)
  103. {
  104. if (_tasks == null)
  105. {
  106. _tasks = new ConcurrentHashMap<>();
  107. }
  108. }
  109. }
  110. _tasks.put(effect, effectTaskInfo);
  111. }
  112. /**
  113. * Gets the task for the given effect.
  114. * @param effect the effect
  115. * @return the task
  116. */
  117. private EffectTaskInfo getEffectTask(AbstractEffect effect)
  118. {
  119. return (_tasks == null) ? null : _tasks.get(effect);
  120. }
  121. /**
  122. * Gets the skill that created this buff info.
  123. * @return the skill
  124. */
  125. public L2Skill getSkill()
  126. {
  127. return _env.getSkill();
  128. }
  129. public Env getEnv()
  130. {
  131. return _env;
  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 _env.getCharacter();
  204. }
  205. /**
  206. * Gets the target of the skill.
  207. * @return the effected
  208. */
  209. public L2Character getEffected()
  210. {
  211. return _env.getTarget();
  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, L2Skill)}
  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(false);
  227. }
  228. // Remove stats
  229. removeStats();
  230. finishEffects();
  231. }
  232. public void initializeEffects()
  233. {
  234. // When effects are initialized, the successfully landed.
  235. if (_env.getTarget().isPlayer())
  236. {
  237. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  238. sm.addSkillName(_env.getSkill());
  239. _env.getTarget().sendPacket(sm);
  240. }
  241. // Creates a task that will stop all the effects.
  242. if (_abnormalTime > 0)
  243. {
  244. _effectTimeTask = new BuffTimeTask(this);
  245. _scheduledFutureTimeTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(_effectTimeTask, 0, 1000L);
  246. }
  247. applyAbnormalVisualEffects();
  248. for (AbstractEffect effect : _effects)
  249. {
  250. if (effect.isInstant())
  251. {
  252. continue;
  253. }
  254. // Call on start.
  255. effect.onStart(this);
  256. // If it's a continuous effect, if has ticks schedule a task with period, otherwise schedule a simple task to end it.
  257. if (effect.getTicks() > 0)
  258. {
  259. // The task for the effect ticks
  260. final EffectTickTask effectTask = new EffectTickTask(this, effect);
  261. final ScheduledFuture<?> scheduledFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(effectTask, effect.getTicks() * 1000L, effect.getTicks() * 1000L);
  262. // Adds the task for ticking
  263. addTask(effect, new EffectTaskInfo(effectTask, scheduledFuture));
  264. }
  265. // Add stats.
  266. _env.getTarget().addStatFuncs(effect.getStatFuncs(_env));
  267. }
  268. }
  269. /**
  270. * Called on each tick.<br>
  271. * Verify if the effect should end and the effect task should be cancelled.
  272. * @param effect the effect that is ticking
  273. * @param tickCount the tick count
  274. */
  275. public void onTick(AbstractEffect effect, int tickCount)
  276. {
  277. boolean continueForever = false;
  278. // If the effect is in use, allow it to affect the effected.
  279. if (_isInUse)
  280. {
  281. // Callback for on action time event.
  282. continueForever = effect.onActionTime(this);
  283. }
  284. // Skills without abnormal time should tick indefinitely, unless they fail on action time.
  285. if (_env.getSkill().getAbnormalTime() > 0)
  286. {
  287. // Checks made in seconds, although tasks run in milliseconds, it'll increase margin for effects that run indefinitely.
  288. final long elapsedTime = effect.getTicks() * tickCount;
  289. if (elapsedTime >= _env.getSkill().getAbnormalTime())
  290. {
  291. final EffectTaskInfo task = getEffectTask(effect);
  292. if (task != null)
  293. {
  294. task.getScheduledFuture().cancel(true); // Allow to finish current run.
  295. _env.getTarget().getEffectList().remove(this); // Remove the buff from the effect list.
  296. }
  297. }
  298. }
  299. else if (!continueForever)
  300. {
  301. final EffectTaskInfo task = getEffectTask(effect);
  302. if (task != null)
  303. {
  304. task.getScheduledFuture().cancel(true); // Allow to finish current run.
  305. _env.getTarget().getEffectList().remove(this); // Remove the buff from the effect list.
  306. }
  307. }
  308. }
  309. public void finishEffects()
  310. {
  311. removeAbnormalVisualEffects();
  312. for (AbstractEffect effect : _effects)
  313. {
  314. // Instant effects shouldn't call onExit(..).
  315. if ((effect != null) && !effect.isInstant())
  316. {
  317. effect.onExit(this);
  318. }
  319. }
  320. // Cancels the ticking task.
  321. if (_tasks != null)
  322. {
  323. for (Entry<AbstractEffect, EffectTaskInfo> entry : _tasks.entrySet())
  324. {
  325. entry.getValue().getScheduledFuture().cancel(true);
  326. }
  327. }
  328. // Sends the proper system message.
  329. SystemMessageId smId = null;
  330. if (_env.getSkill().isToggle())
  331. {
  332. smId = SystemMessageId.S1_HAS_BEEN_ABORTED;
  333. }
  334. else if (isRemoved())
  335. {
  336. smId = SystemMessageId.EFFECT_S1_HAS_BEEN_REMOVED;
  337. }
  338. else if (!_env.getSkill().isPassive())
  339. {
  340. smId = SystemMessageId.S1_HAS_WORN_OFF;
  341. }
  342. if (smId != null)
  343. {
  344. final SystemMessage sm = SystemMessage.getSystemMessage(smId);
  345. sm.addSkillName(_env.getSkill());
  346. _env.getTarget().sendPacket(sm);
  347. }
  348. if (this == _env.getTarget().getEffectList().getShortBuff())
  349. {
  350. _env.getTarget().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 applyAbnormalVisualEffects()
  358. {
  359. if ((_env.getTarget() == null) || (_env.getSkill() == null))
  360. {
  361. return;
  362. }
  363. if (_env.getSkill().hasAbnormalVisualEffects())
  364. {
  365. _env.getTarget().startAbnormalVisualEffect(false, _env.getSkill().getAbnormalVisualEffects());
  366. }
  367. if (_env.getTarget().isPlayer() && _env.getSkill().hasAbnormalVisualEffectsEvent())
  368. {
  369. _env.getTarget().startAbnormalVisualEffect(false, _env.getSkill().getAbnormalVisualEffectsEvent());
  370. }
  371. if (_env.getSkill().hasAbnormalVisualEffectsSpecial())
  372. {
  373. _env.getTarget().startAbnormalVisualEffect(false, _env.getSkill().getAbnormalVisualEffectsSpecial());
  374. }
  375. _env.getTarget().updateAbnormalEffect();
  376. }
  377. /**
  378. * Removes all the abnormal visual effects from the effected.<br>
  379. * Prevents multiple updates.
  380. */
  381. private void removeAbnormalVisualEffects()
  382. {
  383. if ((_env.getTarget() == null) || (_env.getSkill() == null))
  384. {
  385. return;
  386. }
  387. if (_env.getSkill().hasAbnormalVisualEffects())
  388. {
  389. _env.getTarget().stopAbnormalVisualEffect(false, _env.getSkill().getAbnormalVisualEffects());
  390. }
  391. if (_env.getTarget().isPlayer() && _env.getSkill().hasAbnormalVisualEffectsEvent())
  392. {
  393. _env.getTarget().stopAbnormalVisualEffect(false, _env.getSkill().getAbnormalVisualEffectsEvent());
  394. }
  395. if (_env.getSkill().hasAbnormalVisualEffectsSpecial())
  396. {
  397. _env.getTarget().stopAbnormalVisualEffect(false, _env.getSkill().getAbnormalVisualEffectsSpecial());
  398. }
  399. _env.getTarget().updateAbnormalEffect();
  400. }
  401. /**
  402. * Adds the buff stats.
  403. */
  404. public void addStats()
  405. {
  406. for (AbstractEffect effect : _effects)
  407. {
  408. _env.getTarget().addStatFuncs(effect.getStatFuncs(_env));
  409. }
  410. }
  411. /**
  412. * Removes the buff stats.
  413. */
  414. public void removeStats()
  415. {
  416. for (AbstractEffect effect : _effects)
  417. {
  418. _env.getTarget().removeStatsOwner(effect);
  419. }
  420. _env.getTarget().removeStatsOwner(_env.getSkill());
  421. }
  422. /**
  423. * Gets the effect tick count.
  424. * @param effect the effect
  425. * @return the current tick count
  426. */
  427. public int getTickCount(AbstractEffect effect)
  428. {
  429. if (_tasks != null)
  430. {
  431. final EffectTaskInfo effectTaskInfo = _tasks.get(effect);
  432. if (effectTaskInfo != null)
  433. {
  434. return effectTaskInfo.getEffectTask().getTickCount();
  435. }
  436. }
  437. return 0;
  438. }
  439. }