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