BuffInfo.java 12 KB

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