EffectTemplate.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /** Effect's total tick count. */
  46. private final int _totalTickCount;
  47. /** Effect specific abnormal time. */
  48. private final int _abnormalTime;
  49. private final AbnormalEffect _abnormalEffect;
  50. private final AbnormalEffect[] _specialEffect;
  51. private final AbnormalEffect _eventEffect;
  52. private List<FuncTemplate> _funcTemplates;
  53. private final boolean _showIcon;
  54. private final String _name;
  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.getInt("ticks", 0);
  66. _abnormalTime = set.getInt("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.getInt("noicon", 0) == 0;
  76. _triggeredId = set.getInt("triggeredId", 0);
  77. _triggeredLevel = set.getInt("triggeredLevel", 1);
  78. _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));
  79. _parameters = params;
  80. _handler = EffectHandler.getInstance().getHandler(_name);
  81. if (_handler == null)
  82. {
  83. throw new RuntimeException(getClass().getSimpleName() + ": Requested unexistent effect handler: " + _name);
  84. }
  85. try
  86. {
  87. _constructor = _handler.getConstructor(Env.class, EffectTemplate.class);
  88. }
  89. catch (NoSuchMethodException e)
  90. {
  91. throw new RuntimeException(e);
  92. }
  93. }
  94. public L2Effect getEffect(Env env)
  95. {
  96. return getEffect(env, false);
  97. }
  98. public L2Effect getEffect(Env env, boolean ignoreTest)
  99. {
  100. if (!ignoreTest && ((_attachCond != null) && !_attachCond.test(env)))
  101. {
  102. return null;
  103. }
  104. try
  105. {
  106. return (L2Effect) _constructor.newInstance(env, this);
  107. }
  108. catch (IllegalAccessException | InstantiationException e)
  109. {
  110. _log.log(Level.WARNING, "", e);
  111. return null;
  112. }
  113. catch (InvocationTargetException e)
  114. {
  115. _log.log(Level.WARNING, "Error creating new instance of Class " + _handler + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  116. return null;
  117. }
  118. }
  119. /**
  120. * Creates an L2Effect instance from an existing one and an Env object.
  121. * @param env
  122. * @param stolen
  123. * @return the stolen effect
  124. */
  125. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  126. {
  127. Constructor<?> stolenCons;
  128. try
  129. {
  130. stolenCons = _handler.getConstructor(Env.class, L2Effect.class);
  131. }
  132. catch (NoSuchMethodException e)
  133. {
  134. throw new RuntimeException(e);
  135. }
  136. try
  137. {
  138. final L2Effect effect = (L2Effect) stolenCons.newInstance(env, stolen);
  139. // if (_applyCond != null)
  140. // {
  141. // effect.setCondition(_applyCond);
  142. // }
  143. return effect;
  144. }
  145. catch (IllegalAccessException | InstantiationException e)
  146. {
  147. _log.log(Level.WARNING, "", e);
  148. return null;
  149. }
  150. catch (InvocationTargetException e)
  151. {
  152. _log.log(Level.WARNING, "Error creating new instance of Class " + _handler + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  153. return null;
  154. }
  155. }
  156. public void attach(FuncTemplate f)
  157. {
  158. if (_funcTemplates == null)
  159. {
  160. _funcTemplates = new ArrayList<>(1);
  161. }
  162. _funcTemplates.add(f);
  163. }
  164. public Lambda getLambda()
  165. {
  166. return _lambda;
  167. }
  168. public int getTotalTickCount()
  169. {
  170. return _totalTickCount;
  171. }
  172. public int getAbnormalTime()
  173. {
  174. return _abnormalTime;
  175. }
  176. public String getName()
  177. {
  178. return _name;
  179. }
  180. public AbnormalEffect getAbnormalEffect()
  181. {
  182. return _abnormalEffect;
  183. }
  184. public AbnormalEffect[] getSpecialEffect()
  185. {
  186. return _specialEffect;
  187. }
  188. public AbnormalEffect getEventEffect()
  189. {
  190. return _eventEffect;
  191. }
  192. public List<FuncTemplate> getFuncTemplates()
  193. {
  194. return _funcTemplates;
  195. }
  196. public boolean isIconDisplay()
  197. {
  198. return _showIcon;
  199. }
  200. public int getTriggeredId()
  201. {
  202. return _triggeredId;
  203. }
  204. public int getTriggeredLevel()
  205. {
  206. return _triggeredLevel;
  207. }
  208. public ChanceCondition getChanceCondition()
  209. {
  210. return _chanceCondition;
  211. }
  212. /**
  213. * Get the parameters.
  214. * @return the parameters of this effect template
  215. */
  216. public StatsSet getParameters()
  217. {
  218. return _parameters;
  219. }
  220. /**
  221. * Verify if this effect template has parameters.
  222. * @return {@code true} if this effect template has parameters, {@code false} otherwise
  223. */
  224. public boolean hasParameters()
  225. {
  226. return _parameters != null;
  227. }
  228. @Override
  229. public String toString()
  230. {
  231. return "Effect template[" + _handler + "]";
  232. }
  233. }