AbstractEffect.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.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.gameserver.handler.EffectHandler;
  27. import com.l2jserver.gameserver.model.ChanceCondition;
  28. import com.l2jserver.gameserver.model.StatsSet;
  29. import com.l2jserver.gameserver.model.conditions.Condition;
  30. import com.l2jserver.gameserver.model.interfaces.IChanceSkillTrigger;
  31. import com.l2jserver.gameserver.model.skills.BuffInfo;
  32. import com.l2jserver.gameserver.model.skills.funcs.Func;
  33. import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
  34. import com.l2jserver.gameserver.model.stats.Env;
  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)}. Do not call super class methods {@link #onStart(BuffInfo)} nor {@link #onExit(BuffInfo)}.<br>
  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 implements IChanceSkillTrigger
  43. {
  44. protected static final Logger _log = Logger.getLogger(AbstractEffect.class.getName());
  45. // Conditions
  46. private final Condition _attachCond;
  47. // private final Condition _applyCond; // TODO: Use or cleanup.
  48. private List<FuncTemplate> _funcTemplates;
  49. private final String _name;
  50. private final double _val;
  51. private final boolean _isSelfEffect;
  52. /** Ticks. */
  53. private final int _ticks;
  54. private final int _triggeredId;
  55. private final int _triggeredLevel;
  56. private final ChanceCondition _chanceCondition;
  57. private final StatsSet _parameters;
  58. /**
  59. * Abstract effect constructor.
  60. * @param attachCond
  61. * @param applyCond
  62. * @param set
  63. * @param params
  64. */
  65. protected AbstractEffect(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  66. {
  67. _attachCond = attachCond;
  68. // _applyCond = applyCond;
  69. _name = set.getString("name");
  70. _val = set.getDouble("val", 0);
  71. _isSelfEffect = set.getInt("self", 0) == 1;
  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. _parameters = params;
  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 e1)
  93. {
  94. _log.warning(AbstractEffect.class.getSimpleName() + ": Requested unexistent constructor for effect handler: " + name);
  95. e1.printStackTrace();
  96. return null;
  97. }
  98. try
  99. {
  100. return (AbstractEffect) constructor.newInstance(attachCond, applyCond, set, params);
  101. }
  102. catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
  103. {
  104. e.printStackTrace();
  105. }
  106. return null;
  107. }
  108. /**
  109. * Tests the attach condition.
  110. * @param env the data
  111. * @return {@code true} if there isn't a condition to test or it's passed, {@code false} otherwise
  112. */
  113. public boolean testConditions(Env env)
  114. {
  115. return (_attachCond == null) || _attachCond.test(env);
  116. }
  117. /**
  118. * Attachs a function template.
  119. * @param f the function
  120. */
  121. public void attach(FuncTemplate f)
  122. {
  123. if (_funcTemplates == null)
  124. {
  125. _funcTemplates = new ArrayList<>(1);
  126. }
  127. _funcTemplates.add(f);
  128. }
  129. /**
  130. * Gets the effect name.
  131. * @return the name
  132. */
  133. public String getName()
  134. {
  135. return _name;
  136. }
  137. /**
  138. * Verify if this is a self-effect.
  139. * @return {@code true} if it is a self-effect, {@code false} otherwise
  140. */
  141. public boolean isSelfEffect()
  142. {
  143. return _isSelfEffect;
  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 List<FuncTemplate> getFuncTemplates()
  162. {
  163. return _funcTemplates;
  164. }
  165. @Override
  166. public int getTriggeredChanceId()
  167. {
  168. return _triggeredId;
  169. }
  170. @Override
  171. public int getTriggeredChanceLevel()
  172. {
  173. return _triggeredLevel;
  174. }
  175. @Override
  176. public ChanceCondition getTriggeredChanceCondition()
  177. {
  178. return _chanceCondition;
  179. }
  180. /**
  181. * Verify if this effect template has parameters.
  182. * @return {@code true} if this effect template has parameters, {@code false} otherwise
  183. */
  184. public boolean hasParameters()
  185. {
  186. return _parameters != null;
  187. }
  188. /**
  189. * Get the parameters.
  190. * @return the parameters of this effect template
  191. */
  192. public StatsSet getParameters()
  193. {
  194. return _parameters;
  195. }
  196. /**
  197. * Calculates whether this effects land or not.<br>
  198. * If it lands will be scheduled and added to the character effect list.<br>
  199. * Override in effect implementation to change behavior. <br>
  200. * <b>Warning:</b> Must be used only for instant effects continuous effects will not call this they have their success handled by activate_rate.
  201. * @param info the buff info
  202. * @return {@code true} if this effect land, {@code false} otherwise
  203. */
  204. public boolean calcSuccess(BuffInfo info)
  205. {
  206. return true;
  207. }
  208. /**
  209. * Get this effect's type.<br>
  210. * TODO: Remove.
  211. * @return the effect type
  212. */
  213. public L2EffectType getEffectType()
  214. {
  215. return L2EffectType.NONE;
  216. }
  217. /**
  218. * Verify if the buff can start.<br>
  219. * Used for continuous effects.
  220. * @param info the buff info
  221. * @return {@code true} if all the start conditions are meet, {@code false} otherwise
  222. */
  223. public boolean canStart(BuffInfo info)
  224. {
  225. return true;
  226. }
  227. /**
  228. * Called on effect start.
  229. * @param info the buff info
  230. */
  231. public void onStart(BuffInfo info)
  232. {
  233. }
  234. /**
  235. * Called on each tick.<br>
  236. * If the abnormal time is lesser than zero it will last forever.
  237. * @param info the buff info
  238. * @return if {@code true} this effect will continue forever, if {@code false} it will stop after abnormal time has passed
  239. */
  240. public boolean onActionTime(BuffInfo info)
  241. {
  242. return false;
  243. }
  244. /**
  245. * Called when the effect is exited.
  246. * @param info the buff info
  247. */
  248. public void onExit(BuffInfo info)
  249. {
  250. }
  251. /**
  252. * Get this effect's stats functions.
  253. * @param env the data
  254. * @return a list of stat functions.
  255. */
  256. public List<Func> getStatFuncs(Env env)
  257. {
  258. if (getFuncTemplates() == null)
  259. {
  260. return Collections.<Func> emptyList();
  261. }
  262. final List<Func> funcs = new ArrayList<>(getFuncTemplates().size());
  263. for (FuncTemplate t : getFuncTemplates())
  264. {
  265. final Func f = t.getFunc(env, this);
  266. if (f != null)
  267. {
  268. funcs.add(f);
  269. }
  270. }
  271. return funcs;
  272. }
  273. /**
  274. * Get the effect flags.
  275. * @return bit flag for current effect
  276. */
  277. public int getEffectFlags()
  278. {
  279. return EffectFlag.NONE.getMask();
  280. }
  281. @Override
  282. public String toString()
  283. {
  284. return "Effect " + _name;
  285. }
  286. public void decreaseForce()
  287. {
  288. }
  289. public void increaseEffect()
  290. {
  291. }
  292. public boolean checkCondition(Object obj)
  293. {
  294. return true;
  295. }
  296. @Override
  297. public boolean triggersChanceSkill()
  298. {
  299. return _triggeredId > 0;
  300. }
  301. /**
  302. * Verify if this effect is an instant effect.
  303. * @return {@code true} if this effect is instant, {@code false} otherwise
  304. */
  305. public boolean isInstant()
  306. {
  307. return false;
  308. }
  309. }