AbstractEffect.java 9.1 KB

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