EffectTemplate.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 com.l2jserver.gameserver.templates.effects;
  16. import java.lang.reflect.Constructor;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.gameserver.model.ChanceCondition;
  20. import com.l2jserver.gameserver.model.L2Effect;
  21. import com.l2jserver.gameserver.skills.AbnormalEffect;
  22. import com.l2jserver.gameserver.skills.Env;
  23. import com.l2jserver.gameserver.skills.conditions.Condition;
  24. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  25. import com.l2jserver.gameserver.skills.funcs.Lambda;
  26. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  27. /**
  28. * @author mkizub
  29. *
  30. */
  31. public class EffectTemplate
  32. {
  33. static Logger _log = Logger.getLogger(EffectTemplate.class.getName());
  34. private final Class<?> _func;
  35. private final Constructor<?> _constructor;
  36. public final Condition attachCond;
  37. public final Condition applayCond;
  38. public final Lambda lambda;
  39. public final int counter;
  40. public final int period; // in seconds
  41. public final AbnormalEffect abnormalEffect;
  42. public final AbnormalEffect specialEffect;
  43. public final AbnormalEffect eventEffect;
  44. public FuncTemplate[] funcTemplates;
  45. public final String stackType;
  46. public final float stackOrder;
  47. public final boolean icon;
  48. public final String funcName;
  49. public final double effectPower; // to thandle chance
  50. public final L2SkillType effectType; // to handle resistences etc...
  51. public final int triggeredId;
  52. public final int triggeredLevel;
  53. public final ChanceCondition chanceCondition;
  54. public EffectTemplate(Condition pAttachCond, Condition pApplayCond, String func, Lambda pLambda,
  55. int pCounter, int pPeriod, AbnormalEffect pAbnormalEffect, AbnormalEffect pSpecialEffect,
  56. AbnormalEffect pEventEffect, String pStackType, float pStackOrder, boolean showicon,
  57. double ePower, L2SkillType eType, int trigId, int trigLvl, ChanceCondition chanceCond)
  58. {
  59. attachCond = pAttachCond;
  60. applayCond = pApplayCond;
  61. lambda = pLambda;
  62. counter = pCounter;
  63. period = pPeriod;
  64. abnormalEffect = pAbnormalEffect;
  65. specialEffect = pSpecialEffect;
  66. eventEffect = pEventEffect;
  67. stackType = pStackType;
  68. stackOrder = pStackOrder;
  69. icon = showicon;
  70. funcName = func;
  71. effectPower = ePower;
  72. effectType = eType;
  73. triggeredId = trigId;
  74. triggeredLevel = trigLvl;
  75. chanceCondition = chanceCond;
  76. try
  77. {
  78. _func = Class.forName("com.l2jserver.gameserver.skills.effects.Effect" + func);
  79. }
  80. catch (ClassNotFoundException e)
  81. {
  82. throw new RuntimeException(e);
  83. }
  84. try
  85. {
  86. _constructor = _func.getConstructor(Env.class, EffectTemplate.class);
  87. }
  88. catch (NoSuchMethodException e)
  89. {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. public L2Effect getEffect(Env env)
  94. {
  95. if (attachCond != null && !attachCond.test(env))
  96. return null;
  97. try
  98. {
  99. L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
  100. return effect;
  101. }
  102. catch (IllegalAccessException e)
  103. {
  104. e.printStackTrace();
  105. return null;
  106. }
  107. catch (InstantiationException e)
  108. {
  109. e.printStackTrace();
  110. return null;
  111. }
  112. catch (InvocationTargetException e)
  113. {
  114. _log.warning("Error creating new instance of Class " + _func + " Exception was:");
  115. e.getTargetException().printStackTrace();
  116. return null;
  117. }
  118. }
  119. /**
  120. * Creates an L2Effect instance from an existing one and an Env object.
  121. *
  122. * @param env
  123. * @param stolen
  124. * @return
  125. */
  126. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  127. {
  128. Class<?> func;
  129. Constructor<?> stolenCons;
  130. try
  131. {
  132. func = Class.forName("com.l2jserver.gameserver.skills.effects.Effect"
  133. + stolen.getEffectTemplate().funcName);
  134. }
  135. catch (ClassNotFoundException e)
  136. {
  137. throw new RuntimeException(e);
  138. }
  139. try
  140. {
  141. stolenCons = func.getConstructor(Env.class, L2Effect.class);
  142. }
  143. catch (NoSuchMethodException e)
  144. {
  145. throw new RuntimeException(e);
  146. }
  147. try
  148. {
  149. L2Effect effect = (L2Effect) stolenCons.newInstance(env, stolen);
  150. // if (_applayCond != null)
  151. // effect.setCondition(_applayCond);
  152. return effect;
  153. }
  154. catch (IllegalAccessException e)
  155. {
  156. e.printStackTrace();
  157. return null;
  158. }
  159. catch (InstantiationException e)
  160. {
  161. e.printStackTrace();
  162. return null;
  163. }
  164. catch (InvocationTargetException e)
  165. {
  166. _log.warning("Error creating new instance of Class " + func + " Exception was:");
  167. e.getTargetException().printStackTrace();
  168. return null;
  169. }
  170. }
  171. public void attach(FuncTemplate f)
  172. {
  173. if (funcTemplates == null)
  174. {
  175. funcTemplates = new FuncTemplate[] { f };
  176. }
  177. else
  178. {
  179. int len = funcTemplates.length;
  180. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  181. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  182. tmp[len] = f;
  183. funcTemplates = tmp;
  184. }
  185. }
  186. }