BuffInfo.java 12 KB

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