AbstractEffect.java 7.7 KB

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