EffectTemplate.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.gameserver.handler.EffectHandler;
  25. import com.l2jserver.gameserver.model.ChanceCondition;
  26. import com.l2jserver.gameserver.model.conditions.Condition;
  27. import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
  28. import com.l2jserver.gameserver.model.skills.funcs.Lambda;
  29. import com.l2jserver.gameserver.model.stats.Env;
  30. /**
  31. * @author mkizub
  32. */
  33. public class EffectTemplate
  34. {
  35. static Logger _log = Logger.getLogger(EffectTemplate.class.getName());
  36. private final Class<?> _func;
  37. private final Constructor<?> _constructor;
  38. public final Condition attachCond;
  39. public final Condition applayCond;
  40. public final Lambda lambda;
  41. public final int counter;
  42. public final int abnormalTime; // in seconds
  43. public final AbnormalEffect abnormalEffect;
  44. public final AbnormalEffect[] specialEffect;
  45. public final AbnormalEffect eventEffect;
  46. public FuncTemplate[] funcTemplates;
  47. public final String abnormalType;
  48. public final byte abnormalLvl;
  49. public final boolean icon;
  50. public final String funcName;
  51. public final double effectPower; // to handle chance
  52. public final int triggeredId;
  53. public final int triggeredLevel;
  54. public final ChanceCondition chanceCondition;
  55. public EffectTemplate(Condition pAttachCond, Condition pApplayCond, String func, Lambda pLambda, int pCounter, int pAbnormalTime, AbnormalEffect pAbnormalEffect, AbnormalEffect[] pSpecialEffect, AbnormalEffect pEventEffect, String pAbnormalType, byte pAbnormalLvl, boolean showicon, double ePower, int trigId, int trigLvl, ChanceCondition chanceCond)
  56. {
  57. attachCond = pAttachCond;
  58. applayCond = pApplayCond;
  59. lambda = pLambda;
  60. counter = pCounter;
  61. abnormalTime = pAbnormalTime;
  62. abnormalEffect = pAbnormalEffect;
  63. specialEffect = pSpecialEffect;
  64. eventEffect = pEventEffect;
  65. abnormalType = pAbnormalType;
  66. abnormalLvl = pAbnormalLvl;
  67. icon = showicon;
  68. funcName = func;
  69. effectPower = ePower;
  70. triggeredId = trigId;
  71. triggeredLevel = trigLvl;
  72. chanceCondition = chanceCond;
  73. _func = EffectHandler.getInstance().getHandler(func);
  74. if (_func == null)
  75. {
  76. _log.warning("EffectTemplate: Requested Unexistent effect: " + func);
  77. throw new RuntimeException();
  78. }
  79. try
  80. {
  81. _constructor = _func.getConstructor(Env.class, EffectTemplate.class);
  82. }
  83. catch (NoSuchMethodException e)
  84. {
  85. throw new RuntimeException(e);
  86. }
  87. }
  88. public L2Effect getEffect(Env env)
  89. {
  90. return getEffect(env, false);
  91. }
  92. public L2Effect getEffect(Env env, boolean ignoreTest)
  93. {
  94. if (!ignoreTest && ((attachCond != null) && !attachCond.test(env)))
  95. {
  96. return null;
  97. }
  98. try
  99. {
  100. L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
  101. return effect;
  102. }
  103. catch (IllegalAccessException e)
  104. {
  105. _log.log(Level.WARNING, "", e);
  106. return null;
  107. }
  108. catch (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 " + _func + " 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 stolent effect
  124. */
  125. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  126. {
  127. Class<?> func = EffectHandler.getInstance().getHandler(funcName);
  128. if (func == null)
  129. {
  130. throw new RuntimeException();
  131. }
  132. Constructor<?> stolenCons;
  133. try
  134. {
  135. stolenCons = func.getConstructor(Env.class, L2Effect.class);
  136. }
  137. catch (NoSuchMethodException e)
  138. {
  139. throw new RuntimeException(e);
  140. }
  141. try
  142. {
  143. L2Effect effect = (L2Effect) stolenCons.newInstance(env, stolen);
  144. // if (_applayCond != null)
  145. // effect.setCondition(_applayCond);
  146. return effect;
  147. }
  148. catch (IllegalAccessException e)
  149. {
  150. _log.log(Level.WARNING, "", e);
  151. return null;
  152. }
  153. catch (InstantiationException e)
  154. {
  155. _log.log(Level.WARNING, "", e);
  156. return null;
  157. }
  158. catch (InvocationTargetException e)
  159. {
  160. _log.log(Level.WARNING, "Error creating new instance of Class " + func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  161. return null;
  162. }
  163. }
  164. public void attach(FuncTemplate f)
  165. {
  166. if (funcTemplates == null)
  167. {
  168. funcTemplates = new FuncTemplate[]
  169. {
  170. f
  171. };
  172. }
  173. else
  174. {
  175. int len = funcTemplates.length;
  176. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  177. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  178. tmp[len] = f;
  179. funcTemplates = tmp;
  180. }
  181. }
  182. }