EffectTemplate.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.templates.effects;
  16. import java.lang.reflect.Constructor;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.logging.Logger;
  19. import net.sf.l2j.gameserver.model.ChanceCondition;
  20. import net.sf.l2j.gameserver.model.L2Effect;
  21. import net.sf.l2j.gameserver.skills.Env;
  22. import net.sf.l2j.gameserver.skills.conditions.Condition;
  23. import net.sf.l2j.gameserver.skills.funcs.FuncTemplate;
  24. import net.sf.l2j.gameserver.skills.funcs.Lambda;
  25. import net.sf.l2j.gameserver.templates.skills.L2SkillType;
  26. /**
  27. * @author mkizub
  28. *
  29. */
  30. public class EffectTemplate
  31. {
  32. static Logger _log = Logger.getLogger(EffectTemplate.class.getName());
  33. private final Class<?> _func;
  34. private final Constructor<?> _constructor;
  35. public final Condition attachCond;
  36. public final Condition applayCond;
  37. public final Lambda lambda;
  38. public final int counter;
  39. public final int period; // in seconds
  40. public final int abnormalEffect;
  41. public FuncTemplate[] funcTemplates;
  42. public final String stackType;
  43. public final float stackOrder;
  44. public final boolean icon;
  45. public final String funcName;
  46. public final double effectPower; // to thandle chance
  47. public final L2SkillType effectType; // to handle resistences etc...
  48. public final int triggeredId;
  49. public final int triggeredLevel;
  50. public final ChanceCondition chanceCondition;
  51. public EffectTemplate(Condition pAttachCond, Condition pApplayCond, String func, Lambda pLambda,
  52. int pCounter, int pPeriod, int pAbnormalEffect, String pStackType, float pStackOrder, boolean showicon,
  53. double ePower, L2SkillType eType, int trigId, int trigLvl, ChanceCondition chanceCond)
  54. {
  55. attachCond = pAttachCond;
  56. applayCond = pApplayCond;
  57. lambda = pLambda;
  58. counter = pCounter;
  59. period = pPeriod;
  60. abnormalEffect = pAbnormalEffect;
  61. stackType = pStackType;
  62. stackOrder = pStackOrder;
  63. icon = showicon;
  64. funcName = func;
  65. effectPower = ePower;
  66. effectType = eType;
  67. triggeredId = trigId;
  68. triggeredLevel = trigLvl;
  69. chanceCondition = chanceCond;
  70. try
  71. {
  72. _func = Class.forName("net.sf.l2j.gameserver.skills.effects.Effect" + func);
  73. }
  74. catch (ClassNotFoundException e)
  75. {
  76. throw new RuntimeException(e);
  77. }
  78. try
  79. {
  80. _constructor = _func.getConstructor(Env.class, EffectTemplate.class);
  81. }
  82. catch (NoSuchMethodException e)
  83. {
  84. throw new RuntimeException(e);
  85. }
  86. }
  87. public L2Effect getEffect(Env env)
  88. {
  89. if (attachCond != null && !attachCond.test(env))
  90. return null;
  91. try
  92. {
  93. L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
  94. return effect;
  95. }
  96. catch (IllegalAccessException e)
  97. {
  98. e.printStackTrace();
  99. return null;
  100. }
  101. catch (InstantiationException e)
  102. {
  103. e.printStackTrace();
  104. return null;
  105. }
  106. catch (InvocationTargetException e)
  107. {
  108. _log.warning("Error creating new instance of Class " + _func + " Exception was:");
  109. e.getTargetException().printStackTrace();
  110. return null;
  111. }
  112. }
  113. /**
  114. * Creates an L2Effect instance from an existing one and an Env object.
  115. *
  116. * @param env
  117. * @param stolen
  118. * @return
  119. */
  120. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  121. {
  122. Class<?> func;
  123. Constructor<?> stolenCons;
  124. try
  125. {
  126. func = Class.forName("net.sf.l2j.gameserver.skills.effects.Effect"
  127. + stolen.getEffectTemplate().funcName);
  128. }
  129. catch (ClassNotFoundException e)
  130. {
  131. throw new RuntimeException(e);
  132. }
  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. e.printStackTrace();
  151. return null;
  152. }
  153. catch (InstantiationException e)
  154. {
  155. e.printStackTrace();
  156. return null;
  157. }
  158. catch (InvocationTargetException e)
  159. {
  160. _log.warning("Error creating new instance of Class " + func + " Exception was:");
  161. e.getTargetException().printStackTrace();
  162. return null;
  163. }
  164. }
  165. public void attach(FuncTemplate f)
  166. {
  167. if (funcTemplates == null)
  168. {
  169. funcTemplates = new FuncTemplate[] { f };
  170. }
  171. else
  172. {
  173. int len = funcTemplates.length;
  174. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  175. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  176. tmp[len] = f;
  177. funcTemplates = tmp;
  178. }
  179. }
  180. }