2
0

EffectTemplate.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. 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 abnormalTime; // in seconds
  40. public final AbnormalEffect abnormalEffect;
  41. public final AbnormalEffect[] specialEffect;
  42. public final AbnormalEffect eventEffect;
  43. public FuncTemplate[] funcTemplates;
  44. public final String abnormalType;
  45. public final byte abnormalLvl;
  46. public final boolean icon;
  47. public final String funcName;
  48. public final double effectPower; // to thandle chance
  49. public final L2SkillType effectType; // to handle resistences etc...
  50. public final int triggeredId;
  51. public final int triggeredLevel;
  52. public final ChanceCondition chanceCondition;
  53. public EffectTemplate(Condition pAttachCond, Condition pApplayCond, String func, Lambda pLambda, int pCounter, int pAbnormalTime, AbnormalEffect pAbnormalEffect, AbnormalEffect[] pSpecialEffect, AbnormalEffect pEventEffect, String pAbnormalType, byte pAbnormalLvl, boolean showicon, double ePower, L2SkillType eType, int trigId, int trigLvl, ChanceCondition chanceCond)
  54. {
  55. attachCond = pAttachCond;
  56. applayCond = pApplayCond;
  57. lambda = pLambda;
  58. counter = pCounter;
  59. abnormalTime = pAbnormalTime;
  60. abnormalEffect = pAbnormalEffect;
  61. specialEffect = pSpecialEffect;
  62. eventEffect = pEventEffect;
  63. abnormalType = pAbnormalType;
  64. abnormalLvl = pAbnormalLvl;
  65. icon = showicon;
  66. funcName = func;
  67. effectPower = ePower;
  68. effectType = eType;
  69. triggeredId = trigId;
  70. triggeredLevel = trigLvl;
  71. chanceCondition = chanceCond;
  72. _func = EffectHandler.getInstance().getHandler(func);
  73. if (func == null)
  74. {
  75. _log.warning("EffectTemplate: Requested Unexistent effect: " + func);
  76. throw new RuntimeException();
  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. {
  91. return null;
  92. }
  93. try
  94. {
  95. L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
  96. return effect;
  97. }
  98. catch (IllegalAccessException e)
  99. {
  100. _log.log(Level.WARNING, "", e);
  101. return null;
  102. }
  103. catch (InstantiationException e)
  104. {
  105. _log.log(Level.WARNING, "", e);
  106. return null;
  107. }
  108. catch (InvocationTargetException e)
  109. {
  110. _log.log(Level.WARNING, "Error creating new instance of Class " + _func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
  111. return null;
  112. }
  113. }
  114. /**
  115. * Creates an L2Effect instance from an existing one and an Env object.
  116. * @param env
  117. * @param stolen
  118. * @return the stolent effect
  119. */
  120. public L2Effect getStolenEffect(Env env, L2Effect stolen)
  121. {
  122. Class<?> func = EffectHandler.getInstance().getHandler(funcName);
  123. if (func == null)
  124. {
  125. throw new RuntimeException();
  126. }
  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[]
  164. {
  165. f
  166. };
  167. }
  168. else
  169. {
  170. int len = funcTemplates.length;
  171. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  172. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  173. tmp[len] = f;
  174. funcTemplates = tmp;
  175. }
  176. }
  177. }