EffectTemplate.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.handler.EffectHandler;
  21. import com.l2jserver.gameserver.model.ChanceCondition;
  22. import com.l2jserver.gameserver.model.L2Effect;
  23. import com.l2jserver.gameserver.skills.AbnormalEffect;
  24. import com.l2jserver.gameserver.skills.Env;
  25. import com.l2jserver.gameserver.skills.conditions.Condition;
  26. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  27. import com.l2jserver.gameserver.skills.funcs.Lambda;
  28. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  29. /**
  30. * @author mkizub
  31. *
  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 thandle chance
  52. public final L2SkillType effectType; // to handle resistences etc...
  53. public final int triggeredId;
  54. public final int triggeredLevel;
  55. public final ChanceCondition chanceCondition;
  56. public final boolean passiveEffect;
  57. public EffectTemplate(Condition pAttachCond, Condition pApplayCond, String func, Lambda pLambda,
  58. int pCounter, int pAbnormalTime, AbnormalEffect pAbnormalEffect, AbnormalEffect[] pSpecialEffect,
  59. AbnormalEffect pEventEffect, String pAbnormalType, byte pAbnormalLvl, boolean showicon,
  60. double ePower, L2SkillType eType, int trigId, int trigLvl, ChanceCondition chanceCond, boolean passiveEff)
  61. {
  62. attachCond = pAttachCond;
  63. applayCond = pApplayCond;
  64. lambda = pLambda;
  65. counter = pCounter;
  66. abnormalTime = pAbnormalTime;
  67. abnormalEffect = pAbnormalEffect;
  68. specialEffect = pSpecialEffect;
  69. eventEffect = pEventEffect;
  70. abnormalType = pAbnormalType;
  71. abnormalLvl = pAbnormalLvl;
  72. icon = showicon;
  73. funcName = func;
  74. effectPower = ePower;
  75. effectType = eType;
  76. triggeredId = trigId;
  77. triggeredLevel = trigLvl;
  78. chanceCondition = chanceCond;
  79. passiveEffect = passiveEff;
  80. _func = EffectHandler.getInstance().getHandler(func);
  81. if(func == null)
  82. {
  83. _log.warning("EffectTemplate: Requested Unexistent effect: "+func);
  84. throw new RuntimeException();
  85. }
  86. try
  87. {
  88. _constructor = _func.getConstructor(Env.class, EffectTemplate.class);
  89. }
  90. catch (NoSuchMethodException e)
  91. {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. public L2Effect getEffect(Env env)
  96. {
  97. if (attachCond != null && !attachCond.test(env))
  98. return null;
  99. try
  100. {
  101. L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
  102. return effect;
  103. }
  104. catch (IllegalAccessException e)
  105. {
  106. _log.log(Level.WARNING, "", e);
  107. return null;
  108. }
  109. catch (InstantiationException e)
  110. {
  111. _log.log(Level.WARNING, "", e);
  112. return null;
  113. }
  114. catch (InvocationTargetException e)
  115. {
  116. _log.log(Level.WARNING, "Error creating new instance of Class " + _func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  117. return null;
  118. }
  119. }
  120. /**
  121. * Creates an L2Effect instance from an existing one and an Env object.
  122. *
  123. * @param env
  124. * @param stolen
  125. * @return
  126. */
  127. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  128. {
  129. Class<?> func = EffectHandler.getInstance().getHandler(funcName);
  130. if(func == null)
  131. throw new RuntimeException();
  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[] { f };
  169. }
  170. else
  171. {
  172. int len = funcTemplates.length;
  173. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  174. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  175. tmp[len] = f;
  176. funcTemplates = tmp;
  177. }
  178. }
  179. }