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. Constructor<?> stolenCons;
  129. try
  130. {
  131. stolenCons = _handler.getConstructor(Env.class, L2Effect.class);
  132. }
  133. catch (NoSuchMethodException e)
  134. {
  135. throw new RuntimeException(e);
  136. }
  137. try
  138. {
  139. final L2Effect effect = (L2Effect) stolenCons.newInstance(env, stolen);
  140. // if (_applyCond != null)
  141. // {
  142. // effect.setCondition(_applyCond);
  143. // }
  144. return effect;
  145. }
  146. catch (IllegalAccessException | InstantiationException e)
  147. {
  148. _log.log(Level.WARNING, "", e);
  149. return null;
  150. }
  151. catch (InvocationTargetException e)
  152. {
  153. _log.log(Level.WARNING, "Error creating new instance of Class " + _handler + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  154. return null;
  155. }
  156. }
  157. public void attach(FuncTemplate f)
  158. {
  159. if (_funcTemplates == null)
  160. {
  161. _funcTemplates = new ArrayList<>(1);
  162. }
  163. _funcTemplates.add(f);
  164. }
  165. public Lambda getLambda()
  166. {
  167. return _lambda;
  168. }
  169. public int getTotalTickCount()
  170. {
  171. return _totalTickCount;
  172. }
  173. public int getAbnormalTime()
  174. {
  175. return _abnormalTime;
  176. }
  177. public AbnormalEffect getAbnormalEffect()
  178. {
  179. return _abnormalEffect;
  180. }
  181. public AbnormalEffect[] getSpecialEffect()
  182. {
  183. return _specialEffect;
  184. }
  185. public AbnormalEffect getEventEffect()
  186. {
  187. return _eventEffect;
  188. }
  189. public List<FuncTemplate> getFuncTemplates()
  190. {
  191. return _funcTemplates;
  192. }
  193. public boolean isIconDisplay()
  194. {
  195. return _showIcon;
  196. }
  197. public double getEffectPower()
  198. {
  199. return _effectPower;
  200. }
  201. public int getTriggeredId()
  202. {
  203. return _triggeredId;
  204. }
  205. public int getTriggeredLevel()
  206. {
  207. return _triggeredLevel;
  208. }
  209. public ChanceCondition getChanceCondition()
  210. {
  211. return _chanceCondition;
  212. }
  213. /**
  214. * Get the parameters.
  215. * @return the parameters of this effect template
  216. */
  217. public StatsSet getParameters()
  218. {
  219. return _parameters;
  220. }
  221. /**
  222. * Verify if this effect template has parameters.
  223. * @return {@code true} if this effect template has parameters, {@code false} otherwise
  224. */
  225. public boolean hasParameters()
  226. {
  227. return _parameters != null;
  228. }
  229. @Override
  230. public String toString()
  231. {
  232. return "Effect template[" + _handler + "]";
  233. }
  234. }