EffectTemplate.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.List;
  24. import java.util.logging.Level;
  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.skills.funcs.FuncTemplate;
  31. import com.l2jserver.gameserver.model.skills.funcs.Lambda;
  32. import com.l2jserver.gameserver.model.stats.Env;
  33. /**
  34. * Effect template class.
  35. * @author mkizub, Zoey76
  36. */
  37. public class EffectTemplate
  38. {
  39. private static final Logger _log = Logger.getLogger(EffectTemplate.class.getName());
  40. private final Class<?> _handler;
  41. private final Constructor<?> _constructor;
  42. private final Condition _attachCond;
  43. // private final Condition _applyCond; // TODO: Use or cleanup.
  44. private final Lambda _lambda;
  45. private final int _totalTickCount;
  46. /** Effect specific abnormal time. */
  47. private final int _abnormalTime;
  48. private final AbnormalEffect _abnormalEffect;
  49. private final AbnormalEffect[] _specialEffect;
  50. private final AbnormalEffect _eventEffect;
  51. private List<FuncTemplate> _funcTemplates;
  52. private final boolean _showIcon;
  53. private final String _name;
  54. private final double _effectPower; // to handle chance
  55. private final int _triggeredId;
  56. private final int _triggeredLevel;
  57. private final ChanceCondition _chanceCondition;
  58. private final StatsSet _parameters;
  59. public EffectTemplate(Condition attachCond, Condition applyCond, Lambda lambda, StatsSet set, StatsSet params)
  60. {
  61. _attachCond = attachCond;
  62. // _applyCond = applyCond;
  63. _lambda = lambda;
  64. _name = set.getString("name");
  65. _totalTickCount = set.getInteger("ticks", 1);
  66. _abnormalTime = set.getInteger("abnormalTime", 0);
  67. _abnormalEffect = AbnormalEffect.getByName(set.getString("abnormalVisualEffect", ""));
  68. final String[] specialEffects = set.getString("special", "").split(",");
  69. _specialEffect = new AbnormalEffect[specialEffects.length];
  70. for (int i = 0; i < specialEffects.length; i++)
  71. {
  72. _specialEffect[i] = AbnormalEffect.getByName(specialEffects[i]);
  73. }
  74. _eventEffect = AbnormalEffect.getByName(set.getString("event", ""));
  75. _showIcon = set.getInteger("noicon", 0) == 0;
  76. _effectPower = set.getDouble("effectPower", -1);
  77. _triggeredId = set.getInteger("triggeredId", 0);
  78. _triggeredLevel = set.getInteger("triggeredLevel", 1);
  79. _chanceCondition = ChanceCondition.parse(set.getString("chanceType", null), set.getInteger("activationChance", -1), set.getInteger("activationMinDamage", -1), set.getString("activationElements", null), set.getString("activationSkills", null), set.getBool("pvpChanceOnly", false));
  80. _parameters = params;
  81. _handler = EffectHandler.getInstance().getHandler(_name);
  82. if (_handler == null)
  83. {
  84. throw new RuntimeException(getClass().getSimpleName() + ": Requested unexistent effect handler: " + _name);
  85. }
  86. try
  87. {
  88. _constructor = _handler.getConstructor(Env.class, EffectTemplate.class);
  89. }
  90. catch (NoSuchMethodException e)
  91. {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. public L2Effect getEffect(Env env)
  96. {
  97. return getEffect(env, false);
  98. }
  99. public L2Effect getEffect(Env env, boolean ignoreTest)
  100. {
  101. if (!ignoreTest && ((_attachCond != null) && !_attachCond.test(env)))
  102. {
  103. return null;
  104. }
  105. try
  106. {
  107. return (L2Effect) _constructor.newInstance(env, this);
  108. }
  109. catch (IllegalAccessException | InstantiationException e)
  110. {
  111. _log.log(Level.WARNING, "", e);
  112. return null;
  113. }
  114. catch (InvocationTargetException e)
  115. {
  116. _log.log(Level.WARNING, "Error creating new instance of Class " + _handler + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  117. return null;
  118. }
  119. }
  120. /**
  121. * Creates an L2Effect instance from an existing one and an Env object.
  122. * @param env
  123. * @param stolen
  124. * @return the stolen effect
  125. */
  126. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  127. {
  128. Class<?> func = EffectHandler.getInstance().getHandler(_name);
  129. if (func == null)
  130. {
  131. throw new RuntimeException();
  132. }
  133. Constructor<?> stolenCons;
  134. try
  135. {
  136. stolenCons = func.getConstructor(Env.class, L2Effect.class);
  137. }
  138. catch (NoSuchMethodException e)
  139. {
  140. throw new RuntimeException(e);
  141. }
  142. try
  143. {
  144. final L2Effect effect = (L2Effect) stolenCons.newInstance(env, stolen);
  145. // if (_applyCond != null)
  146. // {
  147. // effect.setCondition(_applyCond);
  148. // }
  149. return effect;
  150. }
  151. catch (IllegalAccessException | InstantiationException e)
  152. {
  153. _log.log(Level.WARNING, "", e);
  154. return null;
  155. }
  156. catch (InvocationTargetException e)
  157. {
  158. _log.log(Level.WARNING, "Error creating new instance of Class " + func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  159. return null;
  160. }
  161. }
  162. public void attach(FuncTemplate f)
  163. {
  164. if (_funcTemplates == null)
  165. {
  166. _funcTemplates = new ArrayList<>(1);
  167. }
  168. _funcTemplates.add(f);
  169. }
  170. public Lambda getLambda()
  171. {
  172. return _lambda;
  173. }
  174. public int getTotalTickCount()
  175. {
  176. return _totalTickCount;
  177. }
  178. public int getAbnormalTime()
  179. {
  180. return _abnormalTime;
  181. }
  182. public AbnormalEffect getAbnormalEffect()
  183. {
  184. return _abnormalEffect;
  185. }
  186. public AbnormalEffect[] getSpecialEffect()
  187. {
  188. return _specialEffect;
  189. }
  190. public AbnormalEffect getEventEffect()
  191. {
  192. return _eventEffect;
  193. }
  194. public List<FuncTemplate> getFuncTemplates()
  195. {
  196. return _funcTemplates;
  197. }
  198. public boolean isIconDisplay()
  199. {
  200. return _showIcon;
  201. }
  202. public double getEffectPower()
  203. {
  204. return _effectPower;
  205. }
  206. public int getTriggeredId()
  207. {
  208. return _triggeredId;
  209. }
  210. public int getTriggeredLevel()
  211. {
  212. return _triggeredLevel;
  213. }
  214. public ChanceCondition getChanceCondition()
  215. {
  216. return _chanceCondition;
  217. }
  218. /**
  219. * Get the parameters.
  220. * @return the parameters of this effect template
  221. */
  222. public StatsSet getParameters()
  223. {
  224. return _parameters;
  225. }
  226. /**
  227. * Verify if this effect template has parameters.
  228. * @return {@code true} if this effect template has parameters, {@code false} otherwise
  229. */
  230. public boolean hasParameters()
  231. {
  232. return _parameters != null;
  233. }
  234. }