EffectTemplate.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.gameserver.model.ChanceCondition;
  21. import com.l2jserver.gameserver.model.L2Effect;
  22. import com.l2jserver.gameserver.skills.AbnormalEffect;
  23. import com.l2jserver.gameserver.skills.Env;
  24. import com.l2jserver.gameserver.skills.conditions.Condition;
  25. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  26. import com.l2jserver.gameserver.skills.funcs.Lambda;
  27. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  28. /**
  29. * @author mkizub
  30. *
  31. */
  32. public class EffectTemplate
  33. {
  34. static Logger _log = Logger.getLogger(EffectTemplate.class.getName());
  35. private final Class<?> _func;
  36. private final Constructor<?> _constructor;
  37. public final Condition attachCond;
  38. public final Condition applayCond;
  39. public final Lambda lambda;
  40. public final int counter;
  41. public final int period; // in seconds
  42. public final AbnormalEffect abnormalEffect;
  43. public final AbnormalEffect specialEffect;
  44. public final AbnormalEffect eventEffect;
  45. public FuncTemplate[] funcTemplates;
  46. public final String abnormalType;
  47. public final byte abnormalLvl;
  48. public final boolean icon;
  49. public final String funcName;
  50. public final double effectPower; // to thandle chance
  51. public final L2SkillType effectType; // to handle resistences etc...
  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,
  56. int pCounter, int pPeriod, AbnormalEffect pAbnormalEffect, AbnormalEffect pSpecialEffect,
  57. AbnormalEffect pEventEffect, String pAbnormalType, byte pAbnormalLvl, boolean showicon,
  58. double ePower, L2SkillType eType, int trigId, int trigLvl, ChanceCondition chanceCond)
  59. {
  60. attachCond = pAttachCond;
  61. applayCond = pApplayCond;
  62. lambda = pLambda;
  63. counter = pCounter;
  64. period = pPeriod;
  65. abnormalEffect = pAbnormalEffect;
  66. specialEffect = pSpecialEffect;
  67. eventEffect = pEventEffect;
  68. abnormalType = pAbnormalType;
  69. abnormalLvl = pAbnormalLvl;
  70. icon = showicon;
  71. funcName = func;
  72. effectPower = ePower;
  73. effectType = eType;
  74. triggeredId = trigId;
  75. triggeredLevel = trigLvl;
  76. chanceCondition = chanceCond;
  77. try
  78. {
  79. _func = Class.forName("com.l2jserver.gameserver.skills.effects.Effect" + func);
  80. }
  81. catch (ClassNotFoundException e)
  82. {
  83. throw new RuntimeException(e);
  84. }
  85. try
  86. {
  87. _constructor = _func.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. if (attachCond != null && !attachCond.test(env))
  97. return null;
  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. *
  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. _log.log(Level.WARNING, "", e);
  157. return null;
  158. }
  159. catch (InstantiationException e)
  160. {
  161. _log.log(Level.WARNING, "", e);
  162. return null;
  163. }
  164. catch (InvocationTargetException e)
  165. {
  166. _log.log(Level.WARNING, "Error creating new instance of Class " + func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  167. return null;
  168. }
  169. }
  170. public void attach(FuncTemplate f)
  171. {
  172. if (funcTemplates == null)
  173. {
  174. funcTemplates = new FuncTemplate[] { f };
  175. }
  176. else
  177. {
  178. int len = funcTemplates.length;
  179. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  180. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  181. tmp[len] = f;
  182. funcTemplates = tmp;
  183. }
  184. }
  185. }