AbstractEffect.java 8.7 KB

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