AbstractEffect.java 9.3 KB

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