AbstractEffect.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.effects;
  20. import java.lang.reflect.Constructor;
  21. import java.lang.reflect.InvocationTargetException;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.handler.EffectHandler;
  28. import com.l2jserver.gameserver.model.StatsSet;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.conditions.Condition;
  31. import com.l2jserver.gameserver.model.skills.BuffInfo;
  32. import com.l2jserver.gameserver.model.skills.Skill;
  33. import com.l2jserver.gameserver.model.stats.functions.AbstractFunction;
  34. import com.l2jserver.gameserver.model.stats.functions.FuncTemplate;
  35. /**
  36. * Abstract effect implementation.<br>
  37. * Instant effects should not override {@link #onExit(BuffInfo)}.<br>
  38. * Instant effects should not override {@link #canStart(BuffInfo)}, all checks should be done {@link #onStart(BuffInfo)}.<br>
  39. * Do not call super class methods {@link #onStart(BuffInfo)} nor {@link #onExit(BuffInfo)}.
  40. * @since <a href="http://trac.l2jserver.com/changeset/6249">Changeset 6249</a> the "effect steal constructor" is deprecated.
  41. * @author Zoey76
  42. */
  43. public abstract class AbstractEffect
  44. {
  45. protected static final Logger _log = Logger.getLogger(AbstractEffect.class.getName());
  46. // Conditions
  47. /** Attach condition. */
  48. private final Condition _attachCond;
  49. // Apply condition
  50. // private final Condition _applyCond; // TODO: Use or cleanup.
  51. private List<FuncTemplate> _funcTemplates;
  52. /** Effect name. */
  53. private final String _name;
  54. /** Ticks. */
  55. private final int _ticks;
  56. /**
  57. * Abstract effect constructor.
  58. * @param attachCond the attach condition
  59. * @param applyCond the apply condition
  60. * @param set the attributes
  61. * @param params the parameters
  62. */
  63. protected AbstractEffect(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  64. {
  65. _attachCond = attachCond;
  66. // _applyCond = applyCond;
  67. _name = set.getString("name");
  68. _ticks = set.getInt("ticks", 0);
  69. }
  70. /**
  71. * Creates an effect given the parameters.
  72. * @param attachCond the attach condition
  73. * @param applyCond the apply condition
  74. * @param set the attributes
  75. * @param params the parameters
  76. * @return the new effect
  77. */
  78. public static final AbstractEffect createEffect(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  79. {
  80. final String name = set.getString("name");
  81. final Class<? extends AbstractEffect> handler = EffectHandler.getInstance().getHandler(name);
  82. if (handler == null)
  83. {
  84. _log.warning(AbstractEffect.class.getSimpleName() + ": Requested unexistent effect handler: " + name);
  85. return null;
  86. }
  87. final Constructor<?> constructor;
  88. try
  89. {
  90. constructor = handler.getConstructor(Condition.class, Condition.class, StatsSet.class, StatsSet.class);
  91. }
  92. catch (NoSuchMethodException | SecurityException e)
  93. {
  94. _log.warning(AbstractEffect.class.getSimpleName() + ": Requested unexistent constructor for effect handler: " + name + ": " + e.getMessage());
  95. return null;
  96. }
  97. try
  98. {
  99. return (AbstractEffect) constructor.newInstance(attachCond, applyCond, set, params);
  100. }
  101. catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
  102. {
  103. _log.warning(AbstractEffect.class.getSimpleName() + ": Unable to initialize effect handler: " + name + ": " + e.getMessage());
  104. }
  105. return null;
  106. }
  107. /**
  108. * Tests the attach condition.
  109. * @param caster the caster
  110. * @param target the target
  111. * @param skill the skill
  112. * @return {@code true} if there isn't a condition to test or it's passed, {@code false} otherwise
  113. */
  114. public boolean testConditions(L2Character caster, L2Character target, Skill skill)
  115. {
  116. return (_attachCond == null) || _attachCond.test(caster, target, skill);
  117. }
  118. /**
  119. * Attaches a function template.
  120. * @param f the function
  121. */
  122. public void attach(FuncTemplate f)
  123. {
  124. if (_funcTemplates == null)
  125. {
  126. _funcTemplates = new ArrayList<>(1);
  127. }
  128. _funcTemplates.add(f);
  129. }
  130. /**
  131. * Gets the effect name.
  132. * @return the name
  133. */
  134. public String getName()
  135. {
  136. return _name;
  137. }
  138. /**
  139. * Gets the effect ticks
  140. * @return the ticks
  141. */
  142. public int getTicks()
  143. {
  144. return _ticks;
  145. }
  146. public double getTicksMultiplier()
  147. {
  148. return (getTicks() * Config.EFFECT_TICK_RATIO) / 1000f;
  149. }
  150. public List<FuncTemplate> getFuncTemplates()
  151. {
  152. return _funcTemplates;
  153. }
  154. /**
  155. * Calculates whether this effects land or not.<br>
  156. * If it lands will be scheduled and added to the character effect list.<br>
  157. * Override in effect implementation to change behavior. <br>
  158. * <b>Warning:</b> Must be used only for instant effects continuous effects will not call this they have their success handled by activate_rate.
  159. * @param info the buff info
  160. * @return {@code true} if this effect land, {@code false} otherwise
  161. */
  162. public boolean calcSuccess(BuffInfo info)
  163. {
  164. return true;
  165. }
  166. /**
  167. * Get this effect's type.<br>
  168. * TODO: Remove.
  169. * @return the effect type
  170. */
  171. public L2EffectType getEffectType()
  172. {
  173. return L2EffectType.NONE;
  174. }
  175. /**
  176. * Verify if the buff can start.<br>
  177. * Used for continuous effects.
  178. * @param info the buff info
  179. * @return {@code true} if all the start conditions are meet, {@code false} otherwise
  180. */
  181. public boolean canStart(BuffInfo info)
  182. {
  183. return true;
  184. }
  185. /**
  186. * Called on effect start.
  187. * @param info the buff info
  188. */
  189. public void onStart(BuffInfo info)
  190. {
  191. }
  192. /**
  193. * Called on each tick.<br>
  194. * If the abnormal time is lesser than zero it will last forever.
  195. * @param info the buff info
  196. * @return if {@code true} this effect will continue forever, if {@code false} it will stop after abnormal time has passed
  197. */
  198. public boolean onActionTime(BuffInfo info)
  199. {
  200. return false;
  201. }
  202. /**
  203. * Called when the effect is exited.
  204. * @param info the buff info
  205. */
  206. public void onExit(BuffInfo info)
  207. {
  208. }
  209. /**
  210. * Get this effect's stats functions.
  211. * @param caster the caster
  212. * @param target the target
  213. * @param skill the skill
  214. * @return a list of stat functions.
  215. */
  216. public List<AbstractFunction> getStatFuncs(L2Character caster, L2Character target, Skill skill)
  217. {
  218. if (getFuncTemplates() == null)
  219. {
  220. return Collections.<AbstractFunction> emptyList();
  221. }
  222. final List<AbstractFunction> functions = new ArrayList<>(getFuncTemplates().size());
  223. for (FuncTemplate functionTemplate : getFuncTemplates())
  224. {
  225. final AbstractFunction function = functionTemplate.getFunc(caster, target, skill, this);
  226. if (function != null)
  227. {
  228. functions.add(function);
  229. }
  230. }
  231. return functions;
  232. }
  233. /**
  234. * Get the effect flags.
  235. * @return bit flag for current effect
  236. */
  237. public int getEffectFlags()
  238. {
  239. return EffectFlag.NONE.getMask();
  240. }
  241. @Override
  242. public String toString()
  243. {
  244. return "Effect " + _name;
  245. }
  246. public void decreaseForce()
  247. {
  248. }
  249. public void increaseEffect()
  250. {
  251. }
  252. public boolean checkCondition(Object obj)
  253. {
  254. return true;
  255. }
  256. /**
  257. * Verify if this effect is an instant effect.
  258. * @return {@code true} if this effect is instant, {@code false} otherwise
  259. */
  260. public boolean isInstant()
  261. {
  262. return false;
  263. }
  264. }