EffectTemplate.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 net.sf.l2j.gameserver.skills.effects;
  16. import java.lang.reflect.Constructor;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.logging.Logger;
  19. import net.sf.l2j.gameserver.model.L2Effect;
  20. import net.sf.l2j.gameserver.skills.Env;
  21. import net.sf.l2j.gameserver.skills.conditions.Condition;
  22. import net.sf.l2j.gameserver.skills.funcs.FuncTemplate;
  23. import net.sf.l2j.gameserver.skills.funcs.Lambda;
  24. /**
  25. * @author mkizub
  26. *
  27. * TODO To change the template for this generated type comment go to
  28. * Window - Preferences - Java - Code Style - Code Templates
  29. */
  30. public final 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 period; // in seconds
  40. public final int abnormalEffect;
  41. public FuncTemplate[] funcTemplates;
  42. public final String stackType;
  43. public final float stackOrder;
  44. public final boolean icon;
  45. public EffectTemplate(Condition pAttachCond, Condition pApplayCond,
  46. String func, Lambda pLambda, int pCounter, int pPeriod,
  47. int pAbnormalEffect, String pStackType, float pStackOrder, boolean showicon)
  48. {
  49. attachCond = pAttachCond;
  50. applayCond = pApplayCond;
  51. lambda = pLambda;
  52. counter = pCounter;
  53. period = pPeriod;
  54. abnormalEffect = pAbnormalEffect;
  55. stackType = pStackType;
  56. stackOrder = pStackOrder;
  57. icon = showicon;
  58. try {
  59. _func = Class.forName("net.sf.l2j.gameserver.skills.effects.Effect"+func);
  60. } catch (ClassNotFoundException e) {
  61. throw new RuntimeException(e);
  62. }
  63. try {
  64. _constructor = _func.getConstructor(Env.class, EffectTemplate.class);
  65. } catch (NoSuchMethodException e) {
  66. throw new RuntimeException(e);
  67. }
  68. }
  69. public L2Effect getEffect(Env env)
  70. {
  71. if (attachCond != null && !attachCond.test(env))
  72. return null;
  73. try {
  74. L2Effect effect = (L2Effect)_constructor.newInstance(env, this);
  75. //if (_applayCond != null)
  76. // effect.setCondition(_applayCond);
  77. return effect;
  78. } catch (IllegalAccessException e) {
  79. e.printStackTrace();
  80. return null;
  81. } catch (InstantiationException e) {
  82. e.printStackTrace();
  83. return null;
  84. } catch (InvocationTargetException e) {
  85. _log.warning("Error creating new instance of Class "+_func+" Exception was:");
  86. e.getTargetException().printStackTrace();
  87. return null;
  88. }
  89. }
  90. public void attach(FuncTemplate f)
  91. {
  92. if (funcTemplates == null)
  93. {
  94. funcTemplates = new FuncTemplate[]{f};
  95. }
  96. else
  97. {
  98. int len = funcTemplates.length;
  99. FuncTemplate[] tmp = new FuncTemplate[len+1];
  100. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  101. tmp[len] = f;
  102. funcTemplates = tmp;
  103. }
  104. }
  105. }