EffectTemplate.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.model.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.conditions.Condition;
  23. import com.l2jserver.gameserver.model.skills.L2SkillType;
  24. import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
  25. import com.l2jserver.gameserver.model.skills.funcs.Lambda;
  26. import com.l2jserver.gameserver.model.stats.Env;
  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 abnormalTime; // 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 abnormalType;
  46. public final byte abnormalLvl;
  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 pAbnormalTime, AbnormalEffect pAbnormalEffect, AbnormalEffect[] pSpecialEffect,
  56. AbnormalEffect pEventEffect, String pAbnormalType, byte pAbnormalLvl, 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. abnormalTime = pAbnormalTime;
  64. abnormalEffect = pAbnormalEffect;
  65. specialEffect = pSpecialEffect;
  66. eventEffect = pEventEffect;
  67. abnormalType = pAbnormalType;
  68. abnormalLvl = pAbnormalLvl;
  69. icon = showicon;
  70. funcName = func;
  71. effectPower = ePower;
  72. effectType = eType;
  73. triggeredId = trigId;
  74. triggeredLevel = trigLvl;
  75. chanceCondition = chanceCond;
  76. _func = EffectHandler.getInstance().getHandler(func);
  77. if(func == null)
  78. {
  79. _log.warning("EffectTemplate: Requested Unexistent effect: "+func);
  80. throw new RuntimeException();
  81. }
  82. try
  83. {
  84. _constructor = _func.getConstructor(Env.class, EffectTemplate.class);
  85. }
  86. catch (NoSuchMethodException e)
  87. {
  88. throw new RuntimeException(e);
  89. }
  90. }
  91. public L2Effect getEffect(Env env)
  92. {
  93. if (attachCond != null && !attachCond.test(env))
  94. return null;
  95. try
  96. {
  97. L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
  98. return effect;
  99. }
  100. catch (IllegalAccessException e)
  101. {
  102. _log.log(Level.WARNING, "", e);
  103. return null;
  104. }
  105. catch (InstantiationException e)
  106. {
  107. _log.log(Level.WARNING, "", e);
  108. return null;
  109. }
  110. catch (InvocationTargetException e)
  111. {
  112. _log.log(Level.WARNING, "Error creating new instance of Class " + _func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  113. return null;
  114. }
  115. }
  116. /**
  117. * Creates an L2Effect instance from an existing one and an Env object.
  118. * @param env
  119. * @param stolen
  120. * @return the stolent effect
  121. */
  122. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  123. {
  124. Class<?> func = EffectHandler.getInstance().getHandler(funcName);
  125. if(func == null)
  126. throw new RuntimeException();
  127. Constructor<?> stolenCons;
  128. try
  129. {
  130. stolenCons = func.getConstructor(Env.class, L2Effect.class);
  131. }
  132. catch (NoSuchMethodException e)
  133. {
  134. throw new RuntimeException(e);
  135. }
  136. try
  137. {
  138. L2Effect effect = (L2Effect) stolenCons.newInstance(env, stolen);
  139. // if (_applayCond != null)
  140. // effect.setCondition(_applayCond);
  141. return effect;
  142. }
  143. catch (IllegalAccessException e)
  144. {
  145. _log.log(Level.WARNING, "", e);
  146. return null;
  147. }
  148. catch (InstantiationException e)
  149. {
  150. _log.log(Level.WARNING, "", e);
  151. return null;
  152. }
  153. catch (InvocationTargetException e)
  154. {
  155. _log.log(Level.WARNING, "Error creating new instance of Class " + func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  156. return null;
  157. }
  158. }
  159. public void attach(FuncTemplate f)
  160. {
  161. if (funcTemplates == null)
  162. {
  163. funcTemplates = new FuncTemplate[] { f };
  164. }
  165. else
  166. {
  167. int len = funcTemplates.length;
  168. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  169. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  170. tmp[len] = f;
  171. funcTemplates = tmp;
  172. }
  173. }
  174. }