AbstractEffect.java 8.9 KB

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